我在 JavaScript 中使用 langchain 创建了管道,当我使用 chain.invoke() 方法时,它工作得很好,但是当我尝试通过 chain.stream() 方法调用链时,我却遇到了问题在控制台上打印。有没有人遇到同样的问题?
const prompt = ChatPromptTemplate.fromMessages([
["system", systemPrompt],
new MessagesPlaceholder("chat_history"),
["user", "{input}"],
["user", userPrompt],
]);
// console.log("Prompt is : ");
// console.log(prompt);
// Chain creation using prompt and model
const chain = await createStuffDocumentsChain({
llm: modelInstance,
prompt,
});
// Initialize retriever with the vector store
const startTimeInner = Date.now();
const retriever = vectorStore.asRetriever({
search_type: "mmr",
search_kwargs: { k: 3 },
});
// console.log(vectorStore);
const endTimeInner = Date.now();
console.log(
"Total time taken by retriever to fetch data from db: ",
(endTimeInner - startTimeInner) / 1000
);
// Create a retrieval chain with the retriever and document chain
const retrievalChain = await createRetrievalChain({
retriever,
combineDocsChain: chain,
});
调用流响应
const response = await chain.stream({
chat_history: chatHistory,
input: question,
});
let firstResponse = false;
for await (const chunk of response) {
console.log(chunk);
yield chunk.answer;
}
尝试使用 -
chain.astream
,因为您正在使用 await