typescript - Vrátit zdrojové dokumenty pomocí agenta langchain v JavaScriptu
Nemohu přijít na to, jak předat source_documents agentovi, vidím, že mi retrievalChain vrací zdrojové dokumenty. Ale nikdy se odtud nepřenáší dál, našel zde někdo způsob, jak obdržet source documents ve výsledku exekutora?
Už mám otevřený problém na GitHubu langchainjs, ale nedařilo se mi najít funkční řešení: https://github.com/langchain-ai/langchainjs/issues/3404
Bylo mi navrženo vytvořit vlastní nástroj, ale zdá se, že nemohu vytvořit vlastní nástroj, aniž bych narazil na nesoulad typů.
const vectorStore = await Chroma.fromExistingCollection(
new OpenAIEmbeddings({
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION,
azureOpenAIApiDeploymentName:
process.env.AZURE_OPENAI_API_DEPLOYMENT_EMBEDDING_NAME,
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME
}),
{
collectionName: chatbotID,
url: 'url'
}
)
const retriever = vectorStore.asRetriever()
const model = new ChatOpenAI({
temperature: 0,
azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION,
azureOpenAIApiDeploymentName:
process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
streaming: true
})
const retrievalChain = new RetrievalQAChain({
combineDocumentsChain: loadQAStuffChain(model),
retriever,
returnSourceDocuments: true
})
const chatHistory = new ChatMessageHistory(previousMessages)
const memory = new OpenAIAgentTokenBufferMemory({
llm: model,
memoryKey: 'chat_history',
outputKey: 'output',
inputKey: 'input',
chatHistory
})
const qaTool = new ChainTool({
name: `state-of-${company}-qa`,
description: `State of the ${company} QA - useful for when you need to ask questions about ${company}.`,
chain: retrievalChain,
returnDirect: true,
verbose: true
})
const tools = [qaTool, shippingTool]
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: 'openai-functions',
memory,
returnIntermediateSteps: true,
agentArgs: {
prefix:
'insert prompt here'
}
})
await executor.call(
{
input: input
},
{
callbacks: [
{
handleLLMNewToken(token: string) {
res.write(token)
}
}
]
}
)