How to pass chat history to the arun method? #16725
Replies: 3 comments 8 replies
-
I found a similar discussion on how to add chat memory in a custom chat engine, which might be helpful for your situation [1]. Regarding your code, the error "Output dict must have exactly one key" suggests that the In your code, you are passing
Here's a possible modification: async def achat(
self, message: str, chat_history: Optional[List[ChatMessage]] = None
):
try:
# Retrieve chat history
chat_history = self._memory.get()
# Add the new message to the chat history
chat_history.append(ChatMessage(role="user", content=message))
# Prepare inputs as a dictionary
inputs = {"query": message, "chat_history": chat_history}
# Process the message with the query pipeline
chat_response = await self.query_pipeline.arun(**inputs)
# Add the response to the chat history
chat_history.append(ChatMessage(role="assistant", content=chat_response))
# Update the memory with the new chat history
self._memory.chat_store.set_messages(
self._memory.chat_store_key, chat_history
)
print('chat history', chat_history)
return chat_response
except Exception as e:
return "An error occurred while processing your query: {}".format(e) Ensure that the |
Beta Was this translation helpful? Give feedback.
-
@dosu can you fix this code to use the chat memory while running a query?
|
Beta Was this translation helpful? Give feedback.
-
@dosu why I am getting this error?
Retrieve chat history
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following code where I want to pass the chat memory to a custom chat pipeline. But it's giving the error:
Output dict must have exactly one key.
below is the code? is it the correct way of passing the chat history to the arun method?
```
async def achat(
self, message: str, chat_history: Optional[List[ChatMessage]] = None
):
try:
# Retrieve chat history
chat_history = self._memory.get()
# Add the new message to the chat history
chat_history.append(ChatMessage(role="user", content=message))
# prepare inputs
chat_history_str = "\n".join([str(x) for x in chat_history])
# Process the message with the query pipeline
chat_response = await self.query_pipeline.arun(query=message, chat_history=chat_history_str)
# Add the response to the chat history
chat_history.append(ChatMessage(role="assistant", content=chat_response))
# Update the memory with the new chat history
self._memory.chat_store.set_messages(
self._memory.chat_store_key, chat_history
)
print('chat history', chat_history)
return chat_response
except Exception as e:
return "An error occurred while processing your query: {}".format(e)
Beta Was this translation helpful? Give feedback.
All reactions