Chris Vorster
Chris Vorster

Reputation: 51

langchain with context and memory

I'm attempting to modify an existing Colab example to combine langchain memory and also context document loading. In two separate tests, each instance works perfectly. Now I'd like to combine the two (training context loading and conversation memory) into one - so I can load previously trained data and also have conversation history in my chat bot. The issue is that I do not know how to achieve this with using 'ConversationChain' which expects only a single parameter, namely 'input'.

When I use 'ConversationChain', I'm able to pass the following: query = "What is the title of the document?" docs = docsearch.similarity_search(query) chain.run(input_documents=docs, question=query)

Could anyone point me in the right direction?

I'm using the memory example from here: https://www.pinecone.io/learn/langchain-conversational-memory/

My knowledge of Python and langchain is limited.

I tried:

    with open('/content/gdrive/My Drive/ai-data/docsearch.pkl', 'rb') as f:
        docsearch = pickle.load(f)
  
    model_kwargs = {"model": "text-davinci-003", "temperature": 0.7, "max_tokens": -1, "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0.5, "n": 1, "best_of": 1}

    llm = OpenAI(model_kwargs=model_kwargs)
    
    def count_tokens(chain, query):
    with get_openai_callback() as cb:
        docs = docsearch.similarity_search(query)
        # working older version: result = chain.run(query)
        result = chain.run(input_documents=docs, question=query)
        print(f'Spent a total of {cb.total_tokens} tokens')

    return result
    
    conversation_bufw = ConversationChain(
        llm=llm, 
        memory=ConversationBufferWindowMemory(k=5)
    )
    
    count_tokens(
        conversation_bufw, 
        "Good morning AI!"
    )

Upvotes: 5

Views: 22294

Answers (1)

andrew_reece
andrew_reece

Reputation: 21274

I think you want a ConversationalRetrievalChain. This kind of chain allows for conversation memory and pulls information from input documents.

Here is an example with a toy document set (using ephemeral Chroma DB vector store):

Example dataset using Pandas and DataFrameLoader:

import pandas as pd

from langchain.document_loaders import DataFrameLoader
from langchain.llms import OpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory

data = {
    'index': ['001', '002', '003'], 
    'text': [
        'title: cat friend\ni like cats and the color blue.', 
        'title: dog friend\ni like dogs and the smell of rain.', 
        'title: bird friend\ni like birds and the feel of sunshine.'
    ]
}

df = pd.DataFrame(data)
loader = DataFrameLoader(df, page_content_column="text")
docs = loader.load()

Now get embeddings and store in Chroma (note: you need an OpenAI API token to run this code)

embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(docs, embeddings)

Now create the memory buffer and initialize the chain:

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

qa = ConversationalRetrievalChain.from_llm(
    OpenAI(temperature=0.8), 
    vectorstore.as_retriever(search_kwargs={"k": 3}),
    memory=memory
)

And now you can begin your chat:

q_1 = "What are all of the document titles?"
result = qa({"question": q_1})

result
{'question': 'What are all of the document titles?',
 'chat_history': [HumanMessage(content='What are all of the document titles?', additional_kwargs={}),
  AIMessage(content=' The document titles are "bird friend", "cat friend", and "dog friend".', additional_kwargs={})],
 'answer': ' The document titles are "bird friend", "cat friend", and "dog friend".'}
q_2 = ("Do any documents mention a color?")
result = qa({"question": q_2})

result
{'question': 'Do any documents mention a color?',
 'chat_history': [HumanMessage(content='What are all of the document titles?', additional_kwargs={}),
  AIMessage(content=' The document titles are "bird friend", "cat friend", and "dog friend".', additional_kwargs={}),
  HumanMessage(content='Do any documents mention a color?', additional_kwargs={}),
  AIMessage(content=' Yes, the document titled "cat friend" mentions the color blue.', additional_kwargs={})],
 'answer': ' Yes, the document titled "cat friend" mentions the color blue.'}

Upvotes: 2

Related Questions