-
Notifications
You must be signed in to change notification settings - Fork 54
/
vectordbqa.ts
68 lines (60 loc) · 2.12 KB
/
vectordbqa.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
67
68
import { PineconeStore } from "langchain/vectorstores/pinecone"
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { PineconeClient } from "@pinecone-database/pinecone";
import { VectorDBQAChain } from "langchain/chains";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { CallbackManager } from "langchain/callbacks";
import type { NextApiRequest, NextApiResponse } from "next";
import { NextRequest, NextResponse } from "next/server";
export const config = {
runtime: "edge"
};
export default async function handler(req: NextRequest)
{
// Inputs
const { prompt } = (await req.json()) as { prompt: string; };
// Vector DB
const pinecone = new PineconeClient();
await pinecone.init({
environment: "us-east1-gcp",
apiKey: process.env.PINECONE_API_KEY ?? "",
});
const index = pinecone.Index("lex-gpt");
const vectorStore = await PineconeStore.fromExistingIndex(
new OpenAIEmbeddings(), {pineconeIndex: index},
);
// Call LLM and stream output
const encoder = new TextEncoder();
const stream = new TransformStream();
const writer = stream.writable.getWriter();
const llm = new ChatOpenAI({
temperature: 0.0,
streaming: true,
callbackManager: CallbackManager.fromHandlers({
handleLLMNewToken: async (token) => {
await writer.ready;
await writer.write(encoder.encode(`data: ${token.replace(/["'\n\r]/g,'')}\n\n`));
},
handleLLMEnd: async () => {
await writer.ready;
await writer.close();
},
handleLLMError: async (e) => {
await writer.ready;
await writer.abort(e);
},
}),
});
const chain = VectorDBQAChain.fromLLM(llm, vectorStore);
chain.returnSourceDocuments=false;
chain.k=4;
chain.call({
query: prompt,
}).catch(console.error);
return new NextResponse(stream.readable, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
},
});
}