From 125354ea13ab720e5ea8466f7d27bd288d831730 Mon Sep 17 00:00:00 2001 From: rpidanny Date: Thu, 4 Jul 2024 17:54:39 +0200 Subject: [PATCH] feat: update map reduce promots --- .gitignore | 3 ++- src/services/llm/llm.service.ts | 12 +++++++----- .../prompt-templates/map-reduce.template.ts | 18 +++++++++--------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index da57923..91e09cd 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ coverage *.html *.csv -data/ \ No newline at end of file +data/ +darwin-data/ diff --git a/src/services/llm/llm.service.ts b/src/services/llm/llm.service.ts index 17cceeb..0e87b9e 100644 --- a/src/services/llm/llm.service.ts +++ b/src/services/llm/llm.service.ts @@ -49,7 +49,7 @@ export class LLMService { public async summarize(inputText: string) { const bar = new SingleBar( { - clearOnComplete: false, + clearOnComplete: true, hideCursor: true, format: `${chalk.magenta('Summarizing')} [{bar}] {percentage}% | ETA: {eta}s | {value}/{total}`, }, @@ -62,7 +62,7 @@ export class LLMService { const docChunks = await this.textSplitter.splitDocuments([document]) this.logger?.info( - `Summarizing ${inputText.length} char (${docChunks.length} chunks) document...`, + `Summarizing document with ${inputText.length} chars (${docChunks.length} chunks)`, ) bar.start(docChunks.length, 0) @@ -93,9 +93,9 @@ export class LLMService { public async ask(inputText: string, question: string): Promise { const bar = new SingleBar( { - clearOnComplete: false, + clearOnComplete: true, hideCursor: true, - format: `${chalk.magenta('Processing')} [{bar}] {percentage}% | ETA: {eta}s | {value}/{total}`, + format: `${chalk.magenta('Querying')} [{bar}] {percentage}% | ETA: {eta}s | {value}/{total}`, }, Presets.shades_classic, ) @@ -105,7 +105,9 @@ export class LLMService { }) const docChunks = await this.textSplitter.splitDocuments([document]) - this.logger?.info(`QA ${inputText.length} char (${docChunks.length} chunks) document...`) + this.logger?.info( + `Querying "${question}" on document with ${inputText.length} chars (${docChunks.length} chunks)`, + ) // n map + 1 reduce bar.start(docChunks.length + 1, 0) diff --git a/src/services/llm/prompt-templates/map-reduce.template.ts b/src/services/llm/prompt-templates/map-reduce.template.ts index 8c94bff..3d74c4c 100644 --- a/src/services/llm/prompt-templates/map-reduce.template.ts +++ b/src/services/llm/prompt-templates/map-reduce.template.ts @@ -1,11 +1,11 @@ import { PromptTemplate } from '@langchain/core/prompts' export const MAP_TEMPLATE = ` -Use the following portion of a long document to see if any of the text is relevant to answer the question. +Examine the following document excerpt to identify any text that directly answers the question. -Return any relevant text verbatim. If no relevant text found, return nothing. +Return the relevant text verbatim. If no relevant text is found, return nothing. -Document Content: +Document: \`\`\`txt {context} \`\`\` @@ -15,20 +15,18 @@ QUESTION: {question} RELEVANT TEXT:` export const REDUCE_TEMPLATE = ` -Given the following extracted parts of a long document and a question, create a final answer. - -If you don't know the answer, just say that you don't know. Don't try to make up an answer. +Given the extracted text from a document and a question, provide a final answer. If you don't know the answer, state that you don't know. Do not fabricate information. EXAMPLES: \`\`\`txt QUESTION: Which state/country's law governs the interpretation of the contract? ========= -Content: This Agreement is governed by English law and the parties submit to the exclusive jurisdiction of the English courts in relation to any dispute (contractual or non-contractual) concerning this Agreement save that either party may apply to any court for an injunction or other relief to protect its Intellectual Property Rights. +Content: This Agreement is governed by English law and the parties submit to the exclusive jurisdiction of the English courts in relation to any dispute (contractual or non-contractual) concerning this Agreement save that either party may apply to any court for an injunction or other relief to protect its Intellectual Property Rights. -Content: No Waiver. Failure or delay in exercising any right or remedy under this Agreement shall not constitute a waiver of such (or any other) right or remedy.\n\n11.7 Severability. The invalidity, illegality or unenforceability of any term (or part of a term) of this Agreement shall not affect the continuation in force of the remainder of the term (if any) and this Agreement.\n\n11.8 No Agency. Except as expressly stated otherwise, nothing in this Agreement shall create an agency, partnership or joint venture of any kind between the parties.\n\n11.9 No Third-Party Beneficiaries. +Content: No Waiver. Failure or delay in exercising any right or remedy under this Agreement shall not constitute a waiver of such (or any other) right or remedy.\n\n11.7 Severability. The invalidity, illegality or unenforceability of any term (or part of a term) of this Agreement shall not affect the continuation in force of the remainder of the term (if any) and this Agreement.\n\n11.8 No Agency. Except as expressly stated otherwise, nothing in this Agreement shall create an agency, partnership or joint venture of any kind between the parties.\n\n11.9 No Third-Party Beneficiaries. -Content: (b) if Google believes, in good faith, that the Distributor has violated or caused Google to violate any Anti-Bribery Laws (as defined in Clause 8.5) or that such a violation is reasonably likely to occur, +Content: (b) if Google believes, in good faith, that the Distributor has violated or caused Google to violate any Anti-Bribery Laws (as defined in Clause 8.5) or that such a violation is reasonably likely to occur, ========= FINAL ANSWER: This Agreement is governed by English law. @@ -45,6 +43,8 @@ Content: More support for patients and families. \n\nTo get there, I call on Con FINAL ANSWER: The president did not mention Michael Jackson. \`\`\` +Use the extracted content below to formulate your answer. + QUESTION: {question} CONTENT: