tmc / langchaingo

LangChain for Go, the easiest way to write LLM-based programs in Go
https://tmc.github.io/langchaingo/
MIT License
4.39k stars 597 forks source link

Error with NewConversationalRetrievalQA implementation related to memory buffer #837

Open victorguidi opened 4 months ago

victorguidi commented 4 months ago

Hey everyone,

A few months ago I implemented a Rag in one of our applications that used the NewRetrievalQA and it was working wonders! This week I am in charge of implementating the Rag but with access to memory in the chat so I decided to change to the NewConversationalRetrievalQA.

Steps to Reproduce:

  1. Implement NewConversationalRetrievalQA using memory access in the chat.
  2. Use the provided implementation code.
  3. Attempt to run the application.

Expected Behavior: The NewConversationalRetrievalQA should work without errors, utilizing the memory access in the chat.

Actual Behavior: Encountered the following error: missing memory key in input values: memory key is of wrong type

Code used and additional information:

Langchain Version: v0.1.8 Golang Version: 1.22.0

    mem := memory.NewConversationBuffer(memory.WithChatHistory(memory.NewChatMessageHistory(
        memory.WithPreviousMessages([]schema.ChatMessage{
            schema.HumanChatMessage{Content: "Hello"},
            schema.AIChatMessage{Content: "Hello, How Are your?"},
        }),
    )), memory.WithMemoryKey("chat_history"))

    var s Schemas
    llmchain := chains.NewLLMChain(llm, s.FormatPrompt())
    chain := chains.NewStuffDocuments(llmchain)
    // rchain := chains.NewRetrievalQA(chain, qdrant)
    condeseChain := chains.LoadCondenseQuestionGenerator(llm)
    rchain := chains.NewConversationalRetrievalQA(chain, condeseChain, qdrant, mem)
    log.Println(rchain.GetMemory(), rchain.GetInputKeys())
    rchain.ReturnSourceDocuments = true
    rchain.InputKey = "question"

s.FormatPrompt() Function:

func (s *Schemas) FormatPrompt() prompts.PromptTemplate {
    prompt := prompts.PromptTemplate{
        Template: "
      PROMPT HERE....

      Document: {{.input_documents}}

      History: {{.chat_history}}

      Question: {{.question}}
    ",
        InputVariables: []string{"input_documents", "chat_history", "question"},
        TemplateFormat: prompts.TemplateFormatGoTemplate,
    }
    return prompt
}

I have also tried with the default history variable and also adding the memory as inputKey but nothing seemed to work... Is that a bug or am I missing something here?

Thanks a lot