#Langchain help

25 messages · Page 1 of 1 (latest)

round idol
#

@vestal fulcrum Hi Tom - I finished your langchain course (It was quite impressive, thank you for making it). I went along with it on the scrimba application but now that Im trying to build a similar thing on VS code on my REACT app. I keep on running into issues. I'm stuck on the part of setting up my vector base:

function App() {
  const fetchBase = async () => {
    try {
      const baseResponse = await fetch(document);
      const baseData = await baseResponse.text();
      const splitter = new RecursiveCharacterTextSplitter({
        chunkSize: 500,
        chunkOverlap: 50,
      });

      const output = await splitter.createDocuments([baseData]);


      const sbApiKey = process.env.sbKey;
      const sbUrl = process.env.sbURL;
      const openAIApiKey = process.env.OPENAI_API_KEY;

      const client = createClient(sbUrl, sbApiKey);

      await SupabaseVectorStore.fromDocuments(
        output,
        new OpenAIEmbeddings({ openAIApiKey }),
        {
          client,
          tableName: "documents",
        }
      );
    } catch (err) {
      console.log(err);
    }

    fetchBase();

    return <></>;
  };
}

This exact same code, when putting it on the scrimba app works smoothly and my vector store gets created. Please can you help me resolve this issue - its driving me insane I've been on this step since last night. So I can create my vector base using scrimba but not on vs code

I think the error is to do with the 'process.env' because I get an error saying 'openAI key is not found' And when I put my key directly without the process.env the code works on vs code. So begs the questions what is happening exactly.

Would greatly appreciate the help!

solar radish
#

Did you install dotenv and add this to your code:

import dotenv from "dotenv";
dotenv.config();

Or at least try to log out process.env.OPENAI_API_KEY and test that it's coming in

--

Also, it seems like it's not a problem for you but it seems like you're missing query name, for example:

const vectorStore = new SupabaseVectorStore(embeddings, {
client,
tableName: "documents", // name of the table in the database
queryName: "match_documents", // name of the query function we created in supabase
});

round idol
round idol
#

So I moved on with the test app and got to the final stages and chaining.

@solar radish However, I have ran into some other issues and I would greatly appreciate it if you could help me out here.

I get these 2 error messages:

  1. Multiple GoTrueClient instances detected in the same browser context. It is not an error, but this should be avoided as it may produce undefined behavior when used concurrently under the same storage key.

this occurs at this line of code const client = createClient(sbUrl, sbApiKey)

  1. My bot will answer some questions and other times it'll reply with ' I don't know'

Below is my full code

#
  const combineDocuments = (docs) => {
    return docs.map((doc) => doc.pageContent).join("\n");
  };

  const llm = new ChatOpenAI({ openAIApiKey });

  const embeddings = new OpenAIEmbeddings({ openAIApiKey });

  const vectorStore = new SupabaseVectorStore(embeddings, {
    client,
    tableName: "documents",
    queryName: "match_documents",
  });

  const retriever = vectorStore.asRetriever();

  const questionTemplate = `Given some conversation history (if any) and a question, convert the question to a standalone question.
  conversationHistory: {convHistory}
  question: {question}
  standalone question:`;

  const questionPrompt = PromptTemplate.fromTemplate(questionTemplate);

  const questionChain = questionPrompt.pipe(llm).pipe(new StringOutputParser());

  const answerTemplate = `You are a helpful and enthusiastic support bot who can answer a given question about based on the context provided and the conversation history. Try to find the answer in the context. If the answer is not given in the context, find the answer in the conversation history if possible. If you really don't know the answer, say "I'm sorry, I don't know the answer to that." And direct the questioner to email [email protected]. Don't try to make up an answer. Always speak as if you were chatting to a friend.
  context: {context}
  conversationHistory: {convHistory}
  question: {question}
  answer: `;

  const answerPrompt = PromptTemplate.fromTemplate(answerTemplate);

  const answerChain = answerPrompt.pipe(llm).pipe(new StringOutputParser());

  const retrieverChain = RunnableSequence.from([
    (prevResult) => prevResult.standaloneQuestion,
    retriever,
    combineDocuments,
  ]);
  console.log(retrieverChain);


#
    e.preventDefault();
    progressConversation();
    setInputValue("");
  };

  async function progressConversation() {
    const response = await mainChain.invoke({
      question: inputValue,
      convHistory: convHistory,
    });

    convHistory.push(inputValue);
    convHistory.push(response);
  }

  return (
    <main>
      <section className="chatbot-container">
        <div className="chatbot-header">
          <p className="sub-heading">Knowledge Bank</p>
        </div>
        <div className="chatbot-conversation-container">
          {convHistory[0]
            ? convHistory.map((msg, index) => (
                <div
                  key={index}
                  className={`speech ${
                    index % 2 === 0 ? "speech-human" : "speech-ai"
                  }`}
                >
                  {msg}
                </div>
              ))
            : null}
        </div>
        <form className="chatbot-input-container" onSubmit={handleSubmit}>
          <input
            name="user-input"
            type="text"
            value={inputValue}
            onChange={(e) => setInputValue(e.target.value)}
            required
          />
          <button className="submit-btn" type="submit">
            CLICK CLAK
          </button>
        </form>
      </section>
    </main>```
round idol
#

@vestal fulcrum Would really appreciate your take on this - because quite honestly its driving me insane

vestal fulcrum
#

Hi
So just to be clear, has the 'openAI key is not found' error now been solved?

solar radish
#

I just copied this code into my own vs code project and only change i made was i used my own documents table in my supabase and it all works great so I wonder if something is wrong with your table? It replying "i dont know" can be normal. Check what is returned in the retrieved docs. Maybe it needs to return more documents or the prompt needs to be adjusted so that it can better be sure that it can answer the question

round idol
#

Thanks for the help guys! Although im using the scrimba template - I just wanted to see if i can understand langchain and reformat it into a react app. Ngl langchain does take some getting used to - its a real headache

#

I have one last issue and im not sure if you've ran into this too. Basically I get this error: App.jsx:134
POST https://api.openai.com/v1/chat/completions 429 (Too Many Requests) for this line of code: const response = await mainChain.invoke()

I find it strange because, the user can only invoke the main chain if they submit the form. So I'll submit a question, get this error message and then after 10secs I get my chatbot response.
This delay in response makes the UX very poor.... how is it possible that i'm making too many requests if Im only submitting the form once?

is that an openAI api rate limit or something?

solar radish
#

https://help.openai.com/en/articles/5955604-how-can-i-solve-429-too-many-requests-errors Are you sending a huge amount of documents through? I havent had that experience. Maybe your account is super new? But you're sending too many tokens per minute, thats what the error is. Also if you want the answer to come faster for the user you can stream it by using stream instead of invoke

For example:
// Invoke the chain
const response = await chain.stream({
question:
" your question ",
});

for await (const chunk of response) {
process.stdout.write(chunk);
}

round idol
# solar radish https://help.openai.com/en/articles/5955604-how-can-i-solve-429-too-many-request...

Yeah I had a good read on it last night. its mainly due to rate limit. I really didn't know about tokens. The free tier (which Im using) allows 3 request per minute but I guess the questions I send take up too many tokens.

also just to clear confusion - does tokens take into account the standalone question im embeddding to retrieve the data from the vector store? - if so then I need to upgrade my OpenAI to allow more token usage

round idol
round idol
#

also @solar radish and @vestal fulcrum I'm really sorry to disturb but you're pretty much the only ones helping me out with langchain stuff.

I just wanted to ask - have you tried playing around with large sets of text as your knowledge base? Because I tried to use a file thats 4000 lines of text to split it into chunks and store it in my vector store however When I look at my vector store - it only saved half of my text file. I checked the supabase docs but theres nothing about vector store limitations

solar radish
solar radish
# round idol Cheers for that tip! Will test it out on my upcoming project. is the necessary ...

//This part, instead of "invoke" we use "stream" which will bring in the data as chunks instead of delivering it all at once.
// Invoke the chain
const response = await chain.stream({
question:
" your question ",
});

// here is how we can print nicely to the terminal. Console.log adds a \n new line to each chunk so you can use process.stdout.write to avoid that. To get this to stream to the browser though we'd need something different. I havent fully implemented but it's nice to have streaming while you're working on the code

for await (const chunk of response) {
process.stdout.write(chunk);
}

All my langchain knowledge comes from this course and the docs from lanchain. Yeah it's been pretty hard to find information anywhere. When i have more time over the next few weeks I can make some videos when i finish implementing some of these things

solar radish
round idol
round idol
round idol
#

Thanks for the clarification- I’ll need to check my code again because there’s definitely something wrong here.

#

Thanks Daniel bro, you’re a legend! Can’t thank you enough!