|
| 1 | +import { ConversationalRetrievalQAChain } from "langchain/chains"; |
| 2 | +import { getVectorStore } from "./vector-store"; |
| 3 | +import { getPineconeClient } from "./pinecone-client"; |
| 4 | +import { |
| 5 | + StreamingTextResponse, |
| 6 | + experimental_StreamData, |
| 7 | + LangChainStream, |
| 8 | +} from "ai-stream-experimental"; |
| 9 | +import { streamingModel, nonStreamingModel } from "./llm"; |
| 10 | +import { STANDALONE_QUESTION_TEMPLATE, QA_TEMPLATE } from "./prompt-templates"; |
| 11 | + |
| 12 | +type callChainArgs = { |
| 13 | + question: string; |
| 14 | + chatHistory: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export async function callChain({ question, chatHistory }: callChainArgs) { |
| 18 | + try { |
| 19 | + // Open AI recommendation |
| 20 | + const sanitizedQuestion = question.trim().replaceAll("\n", " "); |
| 21 | + const pineconeClient = await getPineconeClient(); |
| 22 | + const vectorStore = await getVectorStore(pineconeClient); |
| 23 | + const retriever = vectorStore.asRetriever({ |
| 24 | + searchKwargs: { k: 5 }, // Increase from default 4 to 5 |
| 25 | + searchType: "mmr", // Use Maximum Marginal Relevance for diverse results |
| 26 | + filter: { type: "code" } // Add a filter if you've categorized your embeddings |
| 27 | + }); |
| 28 | + const { stream, handlers } = LangChainStream({ |
| 29 | + experimental_streamData: true, |
| 30 | + }); |
| 31 | + const data = new experimental_StreamData(); |
| 32 | + |
| 33 | + const chain = ConversationalRetrievalQAChain.fromLLM( |
| 34 | + streamingModel, |
| 35 | + vectorStore.asRetriever(), |
| 36 | + { |
| 37 | + qaTemplate: QA_TEMPLATE, |
| 38 | + questionGeneratorTemplate: STANDALONE_QUESTION_TEMPLATE, |
| 39 | + returnSourceDocuments: true, //default 4 |
| 40 | + questionGeneratorChainOptions: { |
| 41 | + llm: nonStreamingModel, |
| 42 | + }, |
| 43 | + } |
| 44 | + ); |
| 45 | + |
| 46 | + // Question using chat-history |
| 47 | + // Reference https://js.langchain.com/docs/modules/chains/popular/chat_vector_db#externally-managed-memory |
| 48 | + chain |
| 49 | + .call( |
| 50 | + { |
| 51 | + question: sanitizedQuestion, |
| 52 | + chat_history: chatHistory, |
| 53 | + }, |
| 54 | + [handlers] |
| 55 | + ) |
| 56 | + .then(async (res) => { |
| 57 | + const sourceDocuments = res?.sourceDocuments; |
| 58 | + const firstTwoDocuments = sourceDocuments.slice(0, 2); |
| 59 | + const pageContents = firstTwoDocuments.map( |
| 60 | + ({ pageContent }: { pageContent: string }) => pageContent |
| 61 | + ); |
| 62 | + console.log("already appended ", data); |
| 63 | + data.append({ |
| 64 | + sources: pageContents, |
| 65 | + }); |
| 66 | + data.close(); |
| 67 | + }); |
| 68 | + |
| 69 | + // Return the readable stream |
| 70 | + return new StreamingTextResponse(stream, {}, data); |
| 71 | + } catch (e) { |
| 72 | + console.error(e); |
| 73 | + throw new Error("Call chain method failed to execute successfully!!"); |
| 74 | + } |
| 75 | +} |
0 commit comments