-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
66 lines (58 loc) · 2.07 KB
/
index.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 chalk from "chalk";
import { ChatOpenAI } from "@langchain/openai";
import { DriaRetriever, type DriaRetrieverArgs } from "@langchain/community/retrievers/dria";
import { formatDocumentsAsString } from "langchain/util/document";
import { PromptTemplate } from "@langchain/core/prompts";
import { RunnableSequence, RunnablePassthrough } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
// to test out your query on a number of knowledges, you can set them here
const knowledges: { id: string; desc: string }[] = [
{
id: "-B64DjhUtCwBdXSpsRytlRQCu-bie-vSTvTIT8Ap3g0",
desc: "TypeScript Handbook v4.9",
},
{
id: "7EZMw0vAAFaKVMNOmu2rFgFCFjRD2C2F0kI_N5Cv6QQ",
desc: "Rust Programming Language",
},
{
id: "WeSeU5t5WTshAtaInt6P-GKxmO0Pvre3Us6ptO84wvg",
desc: "Ethereum Whitepaper",
},
];
// our prompt template, telling the agent to only answer from the context
// and say "I dont know" if no confident answer is found
const prompt = PromptTemplate.fromTemplate(`
Answer the question below based only on the following context.
If an answer is not found within the context, say "I don't know.".
Do not mention the context within your answer.
{context}
Question:
{question}
`);
const query =
process.argv.length > 2
? process.argv
.slice(2)
.map((s) => s.trim())
.join(" ")
: "What does the `infer` keyword do?";
console.log(chalk.green("Query\n") + query);
for (const { id, desc } of knowledges) {
// dria arguments, hover over the type to see docs
const args: DriaRetrieverArgs = { contractId: id, level: 3, topK: 15 };
const retriever = new DriaRetriever(args);
const chain = RunnableSequence.from([
{
context: retriever.pipe(formatDocumentsAsString),
question: new RunnablePassthrough(),
},
prompt,
new ChatOpenAI({}),
new StringOutputParser(),
]);
const answer = await chain.invoke(query);
const url = chalk.gray(`https://dria.co/knowledge/${id}`);
const title = chalk.yellow(`${desc}`);
console.log(`\n${url}\n${title}\n${answer}`);
}