Skip to content
Snippets Groups Projects
Commit 8d618a6b authored by Sourabh Desai's avatar Sourabh Desai
Browse files

better structuring + adding missing functionality

parent 8d8bee52
No related branches found
No related tags found
No related merge requests found
...@@ -80,3 +80,39 @@ ${context} ...@@ -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.`; 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();
};
import { BaseNode } from "./Node"; import { BaseNode } from "../../Node";
import { BaseIndex, BaseIndexInit, IndexList } from "./BaseIndex"; import { BaseIndex, BaseIndexInit, IndexList } from "../../BaseIndex";
import { BaseRetriever } from "./Retriever"; import { BaseRetriever } from "../../Retriever";
import { ListIndexRetriever } from "./ListIndexRetriever"; import { ListIndexRetriever } from "./ListIndexRetriever";
import { ServiceContext } from "./ServiceContext"; import { ServiceContext } from "../../ServiceContext";
import { RefDocInfo } from "./storage/docStore/types"; import { RefDocInfo } from "../../storage/docStore/types";
import _ from "lodash"; import _ from "lodash";
export enum ListRetrieverMode { export enum ListRetrieverMode {
......
import { BaseRetriever } from "./Retriever"; import { BaseRetriever } from "../../Retriever";
import { NodeWithScore } from "./Node"; import { NodeWithScore } from "../../Node";
import { ListIndex } from "./ListIndex"; import { ListIndex } from "./ListIndex";
import { ServiceContext } from "./ServiceContext"; import { ServiceContext } from "../../ServiceContext";
import {
ChoiceSelectPrompt,
DEFAULT_CHOICE_SELECT_PROMPT,
} from "./ChoiceSelectPrompt";
import { import {
defaultFormatNodeBatchFn, defaultFormatNodeBatchFn,
defaultParseChoiceSelectAnswerFn, defaultParseChoiceSelectAnswerFn,
} from "./Utils"; } from "./utils";
import { SimplePrompt, defaultChoiceSelectPrompt } from "../../Prompt";
/** /**
* Simple retriever for ListIndex that returns all nodes * Simple retriever for ListIndex that returns all nodes
...@@ -35,7 +32,7 @@ export class ListIndexRetriever implements BaseRetriever { ...@@ -35,7 +32,7 @@ export class ListIndexRetriever implements BaseRetriever {
*/ */
export class ListIndexLLMRetriever implements BaseRetriever { export class ListIndexLLMRetriever implements BaseRetriever {
index: ListIndex; index: ListIndex;
choiceSelectPrompt: ChoiceSelectPrompt; choiceSelectPrompt: SimplePrompt;
choiceBatchSize: number; choiceBatchSize: number;
formatNodeBatchFn: Function; formatNodeBatchFn: Function;
parseChoiceSelectAnswerFn: Function; parseChoiceSelectAnswerFn: Function;
...@@ -43,15 +40,14 @@ export class ListIndexLLMRetriever implements BaseRetriever { ...@@ -43,15 +40,14 @@ export class ListIndexLLMRetriever implements BaseRetriever {
constructor( constructor(
index: ListIndex, index: ListIndex,
choiceSelectPrompt?: ChoiceSelectPrompt, choiceSelectPrompt?: SimplePrompt,
choiceBatchSize: number = 10, choiceBatchSize: number = 10,
formatNodeBatchFn?: Function, formatNodeBatchFn?: Function,
parseChoiceSelectAnswerFn?: Function, parseChoiceSelectAnswerFn?: Function,
serviceContext?: ServiceContext serviceContext?: ServiceContext
) { ) {
this.index = index; this.index = index;
this.choiceSelectPrompt = this.choiceSelectPrompt = choiceSelectPrompt || defaultChoiceSelectPrompt;
choiceSelectPrompt || DEFAULT_CHOICE_SELECT_PROMPT;
this.choiceBatchSize = choiceBatchSize; this.choiceBatchSize = choiceBatchSize;
this.formatNodeBatchFn = formatNodeBatchFn || defaultFormatNodeBatchFn; this.formatNodeBatchFn = formatNodeBatchFn || defaultFormatNodeBatchFn;
this.parseChoiceSelectAnswerFn = this.parseChoiceSelectAnswerFn =
...@@ -68,10 +64,10 @@ export class ListIndexLLMRetriever implements BaseRetriever { ...@@ -68,10 +64,10 @@ export class ListIndexLLMRetriever implements BaseRetriever {
const nodesBatch = await this.index.docStore.getNodes(nodeIdsBatch); const nodesBatch = await this.index.docStore.getNodes(nodeIdsBatch);
const fmtBatchStr = this.formatNodeBatchFn(nodesBatch); const fmtBatchStr = this.formatNodeBatchFn(nodesBatch);
const input = { context: fmtBatchStr, query: query };
const rawResponse = await this.serviceContext.llmPredictor.apredict( const rawResponse = await this.serviceContext.llmPredictor.apredict(
this.choiceSelectPrompt, this.choiceSelectPrompt,
fmtBatchStr, input
query
); );
const [rawChoices, relevances] = this.parseChoiceSelectAnswerFn( const [rawChoices, relevances] = this.parseChoiceSelectAnswerFn(
......
export function defaultFormatNodeBatchFn() {}
export function defaultParseChoiceSelectAnswerFn() {}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment