-
Notifications
You must be signed in to change notification settings - Fork 5
/
06_usestore.js
42 lines (32 loc) · 1.12 KB
/
06_usestore.js
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
import { config } from "dotenv";
config();
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { FaissStore } from "@langchain/community/vectorstores/faiss";
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";
const embeddings = new OpenAIEmbeddings();
const vectorStore = await FaissStore.load("./", embeddings);
const retriever = vectorStore.asRetriever();
const model = new ChatOpenAI({ temperature: 0 });
const prompt =
PromptTemplate.fromTemplate(`Answer the question based only on the following context:
{context}
Question: {question}`);
const chain = RunnableSequence.from([
{
context: retriever.pipe(formatDocumentsAsString),
question: new RunnablePassthrough(),
},
prompt,
model,
new StringOutputParser(),
]);
let res = await chain.invoke("What is the Sorcerer's Stone?");
console.log(res);
res = await chain.invoke("Who is Norbert?");
console.log(res);