5

I want to use StuffDocumentsChain but with behaviour of ConversationChain the suggested example in the documentation doesn't work as I want:

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

The output:

{ 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."
}

The desire output:

{ 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?"
}

How to fix it to get desire output ?

Arsenius
  • 4,972
  • 4
  • 26
  • 39

0 Answers0