-
Notifications
You must be signed in to change notification settings - Fork 324
/
QuestionGenerator.ts
66 lines (58 loc) · 1.76 KB
/
QuestionGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { SubQuestionOutputParser } from "./OutputParser.js";
import type { SubQuestionPrompt } from "./Prompt.js";
import { buildToolsText, defaultSubQuestionPrompt } from "./Prompt.js";
import type {
BaseQuestionGenerator,
SubQuestion,
} from "./engines/query/types.js";
import { OpenAI } from "./llm/openai.js";
import type { LLM } from "./llm/types.js";
import { PromptMixin } from "./prompts/index.js";
import type {
BaseOutputParser,
StructuredOutput,
ToolMetadata,
} from "./types.js";
/**
* LLMQuestionGenerator uses the LLM to generate new questions for the LLM using tools and a user query.
*/
export class LLMQuestionGenerator
extends PromptMixin
implements BaseQuestionGenerator
{
llm: LLM;
prompt: SubQuestionPrompt;
outputParser: BaseOutputParser<StructuredOutput<SubQuestion[]>>;
constructor(init?: Partial<LLMQuestionGenerator>) {
super();
this.llm = init?.llm ?? new OpenAI();
this.prompt = init?.prompt ?? defaultSubQuestionPrompt;
this.outputParser = init?.outputParser ?? new SubQuestionOutputParser();
}
protected _getPrompts(): { [x: string]: SubQuestionPrompt } {
return {
subQuestion: this.prompt,
};
}
protected _updatePrompts(promptsDict: {
subQuestion: SubQuestionPrompt;
}): void {
if ("subQuestion" in promptsDict) {
this.prompt = promptsDict.subQuestion;
}
}
async generate(tools: ToolMetadata[], query: string): Promise<SubQuestion[]> {
const toolsStr = buildToolsText(tools);
const queryStr = query;
const prediction = (
await this.llm.complete({
prompt: this.prompt({
toolsStr,
queryStr,
}),
})
).text;
const structuredOutput = this.outputParser.parse(prediction);
return structuredOutput.parsedOutput;
}
}