diff --git a/packages/core/src/Prompt.ts b/packages/core/src/Prompt.ts index 0a1c6c0b02b6be8dd4bd51c35faa9a658114f4d6..d6ccde84ec4703629773c20b58cf391c193b1f0b 100644 --- a/packages/core/src/Prompt.ts +++ b/packages/core/src/Prompt.ts @@ -11,12 +11,14 @@ export type SimplePrompt = (input: Record<string, string>) => string; /* DEFAULT_TEXT_QA_PROMPT_TMPL = ( - "Context information is below. \n" + "Context information is below.\n" + "---------------------\n" + "{context_str}\n" "---------------------\n" - "{context_str}" - "\n---------------------\n" "Given the context information and not prior knowledge, " - "answer the question: {query_str}\n" + "answer the query.\n" + "Query: {query_str}\n" + "Answer: " ) */ @@ -27,8 +29,9 @@ export const defaultTextQaPrompt: SimplePrompt = (input) => { --------------------- ${context} --------------------- -Given the context information and not prior knowledge, answer the question: ${query} -`; +Given the context information and not prior knowledge, answer the query. +Query: ${query} +Answer:`; }; /* @@ -60,7 +63,7 @@ SUMMARY:""" /* DEFAULT_REFINE_PROMPT_TMPL = ( - "The original question is as follows: {query_str}\n" + "The original query is as follows: {query_str}\n" "We have provided an existing answer: {existing_answer}\n" "We have the opportunity to refine the existing answer " "(only if needed) with some more context below.\n" @@ -68,21 +71,48 @@ DEFAULT_REFINE_PROMPT_TMPL = ( "{context_msg}\n" "------------\n" "Given the new context, refine the original answer to better " - "answer the question. " - "If the context isn't useful, return the original answer." + "answer the query. " + "If the context isn't useful, return the original answer.\n" + "Refined Answer: " ) */ export const defaultRefinePrompt: SimplePrompt = (input) => { const { query = "", existingAnswer = "", context = "" } = input; - return `The original question is as follows: ${query} + return `The original query is as follows: ${query} We have provided an existing answer: ${existingAnswer} We have the opportunity to refine the existing answer (only if needed) with some more context below. ------------ ${context} ------------ -Given the new context, refine the original answer to better answer the question. If the context isn't useful, return the original answer.`; +Given the new context, refine the original answer to better answer the query. If the context isn't useful, return the original answer. +Refined Answer:`; +}; + +/* +DEFAULT_TREE_SUMMARIZE_TMPL = ( + "Context information from multiple sources is below.\n" + "---------------------\n" + "{context_str}\n" + "---------------------\n" + "Given the information from multiple sources and not prior knowledge, " + "answer the query.\n" + "Query: {query_str}\n" + "Answer: " +) +*/ + +export const defaultTreeSummarizePrompt: SimplePrompt = (input) => { + const { context = "", query = "" } = input; + + return `Context information from multiple sources is below. +--------------------- +${context} +--------------------- +Given the information from multiple sources and not prior knowledge, answer the query. +Query: ${query} +Answer:`; }; export const defaultChoiceSelectPrompt: SimplePrompt = (input) => { diff --git a/packages/core/src/ResponseSynthesizer.ts b/packages/core/src/ResponseSynthesizer.ts index 8aa60d9bd35e0ad3061b555b44c8acd08a907e69..d43d68ed5462b1a640455ecf14533fd33807d360 100644 --- a/packages/core/src/ResponseSynthesizer.ts +++ b/packages/core/src/ResponseSynthesizer.ts @@ -3,6 +3,7 @@ import { SimplePrompt, defaultRefinePrompt, defaultTextQaPrompt, + defaultTreeSummarizePrompt, } from "./Prompt"; import { getBiggestPrompt } from "./PromptHelper"; import { Response } from "./Response"; @@ -35,7 +36,7 @@ interface BaseResponseBuilder { query: string, textChunks: string[], parentEvent?: Event, - prevResponse?: string + prevResponse?: string, ): Promise<string>; } @@ -54,7 +55,7 @@ export class SimpleResponseBuilder implements BaseResponseBuilder { async getResponse( query: string, textChunks: string[], - parentEvent?: Event + parentEvent?: Event, ): Promise<string> { const input = { query, @@ -78,7 +79,7 @@ export class Refine implements BaseResponseBuilder { constructor( serviceContext: ServiceContext, textQATemplate?: SimplePrompt, - refineTemplate?: SimplePrompt + refineTemplate?: SimplePrompt, ) { this.serviceContext = serviceContext; this.textQATemplate = textQATemplate ?? defaultTextQaPrompt; @@ -89,7 +90,7 @@ export class Refine implements BaseResponseBuilder { query: string, textChunks: string[], parentEvent?: Event, - prevResponse?: string + prevResponse?: string, ): Promise<string> { let response: string | undefined = undefined; @@ -101,7 +102,7 @@ export class Refine implements BaseResponseBuilder { prevResponse, query, chunk, - parentEvent + parentEvent, ); } prevResponse = response; @@ -113,7 +114,7 @@ export class Refine implements BaseResponseBuilder { private async giveResponseSingle( queryStr: string, textChunk: string, - parentEvent?: Event + parentEvent?: Event, ): Promise<string> { const textQATemplate: SimplePrompt = (input) => this.textQATemplate({ ...input, query: queryStr }); @@ -130,7 +131,7 @@ export class Refine implements BaseResponseBuilder { textQATemplate({ context: chunk, }), - parentEvent + parentEvent, ) ).message.content; } else { @@ -138,7 +139,7 @@ export class Refine implements BaseResponseBuilder { response, queryStr, chunk, - parentEvent + parentEvent, ); } } @@ -150,7 +151,7 @@ export class Refine implements BaseResponseBuilder { response: string, queryStr: string, textChunk: string, - parentEvent?: Event + parentEvent?: Event, ) { const refineTemplate: SimplePrompt = (input) => this.refineTemplate({ ...input, query: queryStr }); @@ -166,7 +167,7 @@ export class Refine implements BaseResponseBuilder { context: chunk, existingAnswer: response, }), - parentEvent + parentEvent, ) ).message.content; } @@ -182,7 +183,7 @@ export class CompactAndRefine extends Refine { query: string, textChunks: string[], parentEvent?: Event, - prevResponse?: string + prevResponse?: string, ): Promise<string> { const textQATemplate: SimplePrompt = (input) => this.textQATemplate({ ...input, query: query }); @@ -192,13 +193,13 @@ export class CompactAndRefine extends Refine { const maxPrompt = getBiggestPrompt([textQATemplate, refineTemplate]); const newTexts = this.serviceContext.promptHelper.repack( maxPrompt, - textChunks + textChunks, ); const response = super.getResponse( query, newTexts, parentEvent, - prevResponse + prevResponse, ); return response; } @@ -216,10 +217,9 @@ export class TreeSummarize implements BaseResponseBuilder { async getResponse( query: string, textChunks: string[], - parentEvent?: Event + parentEvent?: Event, ): Promise<string> { - const summaryTemplate: SimplePrompt = (input) => - defaultTextQaPrompt({ ...input, query: query }); + const summaryTemplate: SimplePrompt = defaultTreeSummarizePrompt; if (!textChunks || textChunks.length === 0) { throw new Error("Must have at least one text chunk"); @@ -227,7 +227,7 @@ export class TreeSummarize implements BaseResponseBuilder { const packedTextChunks = this.serviceContext.promptHelper.repack( summaryTemplate, - textChunks + textChunks, ); if (packedTextChunks.length === 1) { @@ -236,7 +236,7 @@ export class TreeSummarize implements BaseResponseBuilder { summaryTemplate({ context: packedTextChunks[0], }), - parentEvent + parentEvent, ) ).message.content; } else { @@ -246,14 +246,14 @@ export class TreeSummarize implements BaseResponseBuilder { summaryTemplate({ context: chunk, }), - parentEvent - ) - ) + parentEvent, + ), + ), ); return this.getResponse( query, - summaries.map((s) => s.message.content) + summaries.map((s) => s.message.content), ); } } @@ -261,7 +261,7 @@ export class TreeSummarize implements BaseResponseBuilder { export function getResponseBuilder( serviceContext: ServiceContext, - responseMode?: ResponseMode + responseMode?: ResponseMode, ): BaseResponseBuilder { switch (responseMode) { case ResponseMode.SIMPLE: @@ -296,16 +296,16 @@ export class ResponseSynthesizer { async synthesize(query: string, nodes: NodeWithScore[], parentEvent?: Event) { let textChunks: string[] = nodes.map((node) => - node.node.getContent(MetadataMode.NONE) + node.node.getContent(MetadataMode.NONE), ); const response = await this.responseBuilder.getResponse( query, textChunks, - parentEvent + parentEvent, ); return new Response( response, - nodes.map((node) => node.node) + nodes.map((node) => node.node), ); } }