From 0210160684b9ec6a58400078f7f7bb7948e60342 Mon Sep 17 00:00:00 2001 From: anpigon Date: Sat, 6 Jul 2024 11:46:57 +0900 Subject: [PATCH] refactor: improve generateSummaryFromContent function and add command to generate summary from selected text --- .../ai/generate/generateSummaryFromContent.ts | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/libs/ai/generate/generateSummaryFromContent.ts b/src/libs/ai/generate/generateSummaryFromContent.ts index 761ccc1..709a957 100644 --- a/src/libs/ai/generate/generateSummaryFromContent.ts +++ b/src/libs/ai/generate/generateSummaryFromContent.ts @@ -15,9 +15,10 @@ export default async function generateSummaryFromContent(settings: MAXSettings, const {provider, model} = getSelectedAIProviderAndModel(settings); const llm = createChatModelInstance(provider, model, settings); // TODO: model의 performance와 max context length에 따라서 prompt를 수정해야 함 - const prompt = [ - new SystemMessage( - `You will generate increasingly concise, entity-dense summaries of the above article. + try { + const prompt = [ + new SystemMessage( + `You will generate increasingly concise, entity-dense summaries of the above article. Repeat the following 2 steps 5 times: @@ -42,12 +43,34 @@ Guidelines: Ensure that each summary has exactly the same number of words. Respond in JSON format. The JSON should be a list (length 5) of dictionaries with keys "Missing_Entities" and "Denser_Summary". !IMPORTANT: Answer language is ${globalThis.moment().locale()}` - ), - new HumanMessage(`#Article:\n\n${fileContent}`), - ]; - Logger.debug(prompt); - const chain = llm.pipe(new JsonOutputParser()); - const response = await chain.invoke(prompt); - Logger.debug(response); - return response.last()?.Denser_Summary; + ), + new HumanMessage(`#Article:\n\n${fileContent}`), + ]; + Logger.debug(prompt); + const chain = llm.pipe(new JsonOutputParser()); + const response = await chain.invoke(prompt); + Logger.debug(response); + return response.last()?.Denser_Summary; + } catch (e) { + Logger.error(e); + const prompt = [ + new SystemMessage( + `Please summarize the following notes in a concise and clear manner: + +1. Include only the main concepts and ideas. +2. Exclude unnecessary details, focusing on the core content. +3. Maintain the original structure and logical flow. +4. Preserve technical terms or important keywords. +5. Ensure the summary is approximately 20% of the original length. +6. Use bullet points to list the key points. +7. Answer language is ${globalThis.moment().locale()}` + ), + new HumanMessage(`#Notes content:\n\n${fileContent}`), + ]; + Logger.debug(prompt); + const chain = llm.pipe(new StringOutputParser()); + const response = await chain.invoke(prompt); + Logger.debug(response); + return response; + } }