diff --git a/packages/core/src/Prompt.ts b/packages/core/src/Prompt.ts index baa2f3f0a7d0fb9d52830b06f534c8b944132e31..e9ce13964957023db532c3534f7e3721db877adf 100644 --- a/packages/core/src/Prompt.ts +++ b/packages/core/src/Prompt.ts @@ -80,3 +80,39 @@ ${context} ------------ Given the new context, refine the original answer to better answer the question. If the context isn't useful, return the original answer.`; }; + +export const defaultChoiceSelectPrompt: SimplePrompt = (input) => { + const { context = "", query = "" } = input; + + return ` + A list of documents is shown below. Each document has a number next to it along + with a summary of the document. A question is also provided. + Respond with the numbers of the documents + you should consult to answer the question, in order of relevance, as well + as the relevance score. The relevance score is a number from 1-10 based on + how relevant you think the document is to the question. + Do not include any documents that are not relevant to the question. + Example format: + Document 1: + <summary of document 1> + + Document 2: + <summary of document 2> + + ... + + Document 10:\n<summary of document 10> + + Question: <question> + Answer: + Doc: 9, Relevance: 7 + Doc: 3, Relevance: 4 + Doc: 7, Relevance: 3 + + Let's try this now: + + ${context} + Question: ${query} + Answer: +`.trim(); +}; diff --git a/packages/core/src/ListIndex.ts b/packages/core/src/index/list/ListIndex.ts similarity index 87% rename from packages/core/src/ListIndex.ts rename to packages/core/src/index/list/ListIndex.ts index ebfd73834e32b41964273a6ff1666f146c430bda..44496db2bca2ca2f73e5c543d1f3c73a19b0dd94 100644 --- a/packages/core/src/ListIndex.ts +++ b/packages/core/src/index/list/ListIndex.ts @@ -1,9 +1,9 @@ -import { BaseNode } from "./Node"; -import { BaseIndex, BaseIndexInit, IndexList } from "./BaseIndex"; -import { BaseRetriever } from "./Retriever"; +import { BaseNode } from "../../Node"; +import { BaseIndex, BaseIndexInit, IndexList } from "../../BaseIndex"; +import { BaseRetriever } from "../../Retriever"; import { ListIndexRetriever } from "./ListIndexRetriever"; -import { ServiceContext } from "./ServiceContext"; -import { RefDocInfo } from "./storage/docStore/types"; +import { ServiceContext } from "../../ServiceContext"; +import { RefDocInfo } from "../../storage/docStore/types"; import _ from "lodash"; export enum ListRetrieverMode { diff --git a/packages/core/src/ListIndexRetriever.ts b/packages/core/src/index/list/ListIndexRetriever.ts similarity index 84% rename from packages/core/src/ListIndexRetriever.ts rename to packages/core/src/index/list/ListIndexRetriever.ts index 744c11d48faf32701f4b81c79ff3bd2f6e6360fd..59760f2110e68083ec11570af22e0beb8551bfd7 100644 --- a/packages/core/src/ListIndexRetriever.ts +++ b/packages/core/src/index/list/ListIndexRetriever.ts @@ -1,15 +1,12 @@ -import { BaseRetriever } from "./Retriever"; -import { NodeWithScore } from "./Node"; +import { BaseRetriever } from "../../Retriever"; +import { NodeWithScore } from "../../Node"; import { ListIndex } from "./ListIndex"; -import { ServiceContext } from "./ServiceContext"; -import { - ChoiceSelectPrompt, - DEFAULT_CHOICE_SELECT_PROMPT, -} from "./ChoiceSelectPrompt"; +import { ServiceContext } from "../../ServiceContext"; import { defaultFormatNodeBatchFn, defaultParseChoiceSelectAnswerFn, -} from "./Utils"; +} from "./utils"; +import { SimplePrompt, defaultChoiceSelectPrompt } from "../../Prompt"; /** * Simple retriever for ListIndex that returns all nodes @@ -35,7 +32,7 @@ export class ListIndexRetriever implements BaseRetriever { */ export class ListIndexLLMRetriever implements BaseRetriever { index: ListIndex; - choiceSelectPrompt: ChoiceSelectPrompt; + choiceSelectPrompt: SimplePrompt; choiceBatchSize: number; formatNodeBatchFn: Function; parseChoiceSelectAnswerFn: Function; @@ -43,15 +40,14 @@ export class ListIndexLLMRetriever implements BaseRetriever { constructor( index: ListIndex, - choiceSelectPrompt?: ChoiceSelectPrompt, + choiceSelectPrompt?: SimplePrompt, choiceBatchSize: number = 10, formatNodeBatchFn?: Function, parseChoiceSelectAnswerFn?: Function, serviceContext?: ServiceContext ) { this.index = index; - this.choiceSelectPrompt = - choiceSelectPrompt || DEFAULT_CHOICE_SELECT_PROMPT; + this.choiceSelectPrompt = choiceSelectPrompt || defaultChoiceSelectPrompt; this.choiceBatchSize = choiceBatchSize; this.formatNodeBatchFn = formatNodeBatchFn || defaultFormatNodeBatchFn; this.parseChoiceSelectAnswerFn = @@ -68,10 +64,10 @@ export class ListIndexLLMRetriever implements BaseRetriever { const nodesBatch = await this.index.docStore.getNodes(nodeIdsBatch); const fmtBatchStr = this.formatNodeBatchFn(nodesBatch); + const input = { context: fmtBatchStr, query: query }; const rawResponse = await this.serviceContext.llmPredictor.apredict( this.choiceSelectPrompt, - fmtBatchStr, - query + input ); const [rawChoices, relevances] = this.parseChoiceSelectAnswerFn( diff --git a/packages/core/src/index/list/utils.ts b/packages/core/src/index/list/utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..33a9a3ce50ab2d8d0b92a146e000ef006f411676 --- /dev/null +++ b/packages/core/src/index/list/utils.ts @@ -0,0 +1,3 @@ +export function defaultFormatNodeBatchFn() {} + +export function defaultParseChoiceSelectAnswerFn() {}