"Agent responds to the previous question rather than addressing the current one." #25653
Replies: 2 comments
-
The issue where the agent responds to the previous question rather than addressing the current one is likely due to how the chat history is being managed and incorporated into the prompt. The Here are a few steps to troubleshoot and potentially resolve the issue:
Here is an example of how you can ensure the chat history is correctly managed and incorporated into the prompt: from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder, SystemMessage, HumanMessage
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
from langchain_community.chat_message_histories import RedisChatMessageHistory
from langchain.agents import AgentExecutor, create_openai_tools_agent
# Define the prompt template
prompt_rec = ChatPromptTemplate.from_messages(
[
SystemMessage(content=self.system_prompt),
MessagesPlaceholder(variable_name="chat_history"),
HumanMessage(content="{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
# Create the chain with history
agent_with_chat_history = RunnableWithMessageHistory(
agent_executor,
get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
)
# Invoke the chain with history
result = agent_with_chat_history.invoke({'input': input}, config={"configurable": {"session_id": "222"}})
response = result['output'] Ensure that the def get_session_history(session_id):
return RedisChatMessageHistory(session_id=session_id, url="redis://localhost:6379") By ensuring that the chat history is correctly managed and incorporated into the prompt, the agent should be able to respond to the current question rather than the previous one [1][2][3][4]. |
Beta Was this translation helpful? Give feedback.
-
its already been followed properly,Still its an error |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
"Agent responds to the previous question rather than addressing the current one."
Example behavoir
User :How are you
AI: i am good thanku for asking
User: Can you tell me about mesi
AI: Thanku for asking i am good
User: can you tell me abour ronaldo
AI: Messi is an international football player
System Info
langchain==0.2.6
langchain-anthropic==0.1.19
langchain-aws==0.1.9
langchain-cohere==0.1.8
langchain-community==0.2.6
langchain-core==0.2.11
langchain-fireworks==0.1.4
langchain-google-genai==1.0.7
langchain-google-vertexai==1.0.6
langchain-groq==0.1.6
langchain-huggingface==0.0.3
langchain-mistralai==0.1.9
langchain-openai==0.1.14
langchain-text-splitters==0.2.2
langchain-together==0.1.3
langchainhub==0.1.20
Beta Was this translation helpful? Give feedback.
All reactions