Reputation: 11
i have build a chatbot on custom data using langchain, but the chatbot can not remember my previous questions, I found it difficult with the ConversationalRetrievalChain.
My code is as below:
`class MyBot(ActivityHandler):
def __init__(self, conversation_state: ConversationState):
self.conversation_state = conversation_state
self.session_accessor = self.conversation_state.create_property("Session")
# Data loader
loader = CSVLoader(file_path="data.csv", encoding="utf-8", csv_args={'delimiter': ','})
data = loader.load()
# OpenAI API key
openai_api_key = os.environ.get('OPENAI_API_KEY')
# Initialize OpenAIEmbeddings
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
# Initialize FAISS for the vector database
vectors = FAISS.from_documents(data, embeddings)
# Initialize the ConversationalRetrievalChain
self.chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(temperature=0.5,request_timeout=100, model_name='gpt-4o', max_tokens= 2048, openai_api_key=openai_api_key),
retriever=vectors.as_retriever(), combine_docs_chain_kwargs={"prompt": QA_PROMPT}
)
def run_chain(self, chat_history, question):
return self.chain.run({'chat_history': chat_history, 'question': question})
async def get_response(self, user_message, turn_context: TurnContext):
# Retrieve the session for the current user
session = await self.session_accessor.get(turn_context, lambda: {"messages": [{"role": "system", "content": "You work for and M company answer any questions related to the company."}]})
# Add user message to the session
session["messages"].append({"role": "user", "content": user_message})
response = self.run_chain("", user_message)
ai_response = response # Use response directly
# Add AI response to the session
session["messages"].append({"role": "assistant", "content": ai_response})
# Save the updated session
await self.conversation_state.save_changes(turn_context)
return ai_response`
I just want my chatbot to remember my previous question like chatgpt.
thanks in advance!
integrate the memory within the chatbot
Upvotes: 1
Views: 77