Wyszukiwanie w witrynie

typescript - Zwracanie dokumentów źródłowych przy użyciu agenta langchain w javascript


Nie mogę znaleźć sposobu, jak przekazać source_documents do agenta, widzę, że otrzymuję zwrócone dokumenty źródłowe z retrievalChain. Ale nigdy nie przekazuje się to dalej, czy ktoś tutaj znalazł sposób na otrzymanie dokumentów źródłowych w wyniku wykonawcy?

Mam już otwarty problem na githubie langchainjs, ale nie udało mi się znaleźć działającego rozwiązania: https://github.com/langchain-ai/langchainjs/issues/3404

Zasugerowano mi stworzenie niestandardowego narzędzia, ale nie mogę go stworzyć bez napotkania problemów z niezgodnością typów.

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)
        }
      }
    ]
  }
)