Hello guys. Im trying to create an react agent with memory, using langchain.agents create_agent
Here is my code
model = init_chat_model(
model = configs.MODEL_LLM,
api_key = configs.LLM_API_KEY,
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
)
agent = create_agent(
model=model,
tools=\[cypher_query_tool, vector_retriever_tool, weather_tool\],
\# middleware=\[
\# SummarizationMiddleware(
\# model=model,
\# max_tokens_before_summary=1000,
\# messages_to_keep=2,
\# summary_prompt = prompts.load_prompt("summarize_middleware_prompt.md")
\# )
\# \],
)
prompt_chain = ChatPromptTemplate.from_messages(
\[
("system", "You are an assistant."),
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}"),
\]
)
def get_session_history(session_id: str):
return MongoDBChatMessageHistory(
session_id=session_id,
connection_string="Example",
database_name="my_db",
collection_name="chat_histories",
history_size=4,
)
agent_with_memory = RunnableWithMessageHistory(
prompt_chain | agent ,# | parser,
get_session_history,
input_messages="input",
history_messages_key="chat_history",
)
agent_with_memory_stream = RunnableWithMessageHistory(
prompt_chain | agent | extract_message_stream,
get_session_history,
input_messages="input",
history_messages_key="chat_history",
)
The thing is this bug only appear when i using stream
result = agent_with_memory.stream({“input”: query}, config={“configurable”: { “session_id”: session_id, “user_id”:" ok"}})
for chunk in result:
print(chunk)
when i use stream mode, it’s not write into DB.
Error in RootListenersTracer.on_chain_end callback: ValueError("Expected str, BaseMessage, list[BaseMessage], or tuple[BaseMessage]. Got {‘messages’: [AIMessage
Anyone got solution or alternative method ?