typescript - Hogyan használjuk a StuffDocumentsChain-et a ConversationChain-nel a LangChain-ben?
Azt szeretném, ha a StuffDocumentsChain-et használnám, de a ConversationChain viselkedésével, a dokumentációban javasolt példa azonban nem úgy működik, ahogy szeretném:
import fs from 'fs';
import path from 'path';
import { OpenAI } from "langchain/llms/openai";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { ConversationalRetrievalQAChain } from "langchain/chains";
const model = new OpenAI({openAIApiKey: 'sk-...', modelName: 'gpt-3.5-turbo'});
const text = fs.readFileSync(path.resolve(__dirname, './data.txt'), 'utf-8');
const textSplitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000 });
const docs = await textSplitter.createDocuments([text]);
const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings(
{openAIApiKey: 'sk-...', modelName: 'text-embedding-ada-002'}
));
const chain = ConversationalRetrievalQAChain.fromLLM(
model,
vectorStore.asRetriever()
);
const question = 'Hello my name is Archie';
const res =[]
res.push(await chain.call({
question,
chat_history: []
}))
console.log(res[0])
res.push(await chain.call({
question: "What is my name?",
chat_history: [question, res[0].text]
}))
console.log(res[1]);
A kimenet:
{ text: 'That is not a question, but nice to meet you Archie!' }
{
text: "I'm sorry, but I cannot provide you with an answer as the given context does not contain any relevant information to your question."
}
A kívánt kimenet:
{ text: 'Hi Archie, nice to meet you! How can I assist you today?' }
{
text: "Your name is Archie, as you mentioned earlier. Is there anything else you would like to know or discuss?"
}
Hogyan lehet kijavítani, hogy megkapjuk a kívánt eredményt?