-
Notifications
You must be signed in to change notification settings - Fork 3
/
answer.ts
34 lines (31 loc) · 1.04 KB
/
answer.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
import { type Prompt, Msg } from '@dexaai/dexter/prompt';
import { chatModel, chunkDatastore } from './tools.js';
const system = Msg.system(`
You are a helpful and accurate Q&A bot.
You answer the user's QUESTION, based only on the PROVIDED_CONTEXT.
Your answer should be at most 100 words long.
You use Markdown footnotes to cite the chunkId of the PROVIDED_CONTEXT that
you used for each part of your answer. (Eg: [^1])
`);
function userMsg(query: string, chunks: unknown[]) {
return Msg.user(`
QUESTION: ${query}
PROVIDED_CONTEXT:
\`\`\`json
${JSON.stringify(chunks, null, 2)}
\`\`\`
`);
}
export async function generateAnswer(
query: string,
history: Prompt.Msg[]
): Promise<Prompt.Msg> {
const results = await chunkDatastore.query({ query, topK: 5 });
const chunks = results.docs.map((doc) => doc.metadata);
const messages = [system, ...history.slice(-5), userMsg(query, chunks)];
const { message } = await chatModel.run({
messages,
handleUpdate: (c) => process.stdout.write(c),
});
return message;
}