Closed junruxiong closed 7 months ago
I am a 🤖_
Hello,
Thank you for providing detailed information about the issue you're experiencing. It seems like the problem is related to the output parser not being able to parse the output from the language model.
Based on the context provided, there have been similar issues in the past. For instance, issue #1371 was about agents not being able to parse OpenAIChat output. This issue was addressed in pull request #1345 where an acceptable LLM output pattern was added.
Another similar issue was #7116, where the agent run output parser caused an error when running a simple quick start. The suggested solution for this issue was to refer to the article on how to handle parsing errors in LangChain: https://python.langchain.com/docs/modules/agents/how_to/handle_parsing_errors
Given these previous issues and solutions, it seems like the problem you're experiencing might be due to the output parser not recognizing the output format from the ChatOpenAI 3.5 and Claude models.
You might want to check if the output format from these models matches the expected format that the output parser can handle. If the output format is different, you might need to modify the output parser to handle the new format.
Here's a simple example of how you can modify the output parser:
class MyOutputParser(OutputParser):
def parse(self, text):
# Add your parsing logic here
pass
agent.output_parser = MyOutputParser()
Please let me know if this helps or if you need further assistance.
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
@junruxiong looking into this common issue. This is an issue that many people are experiencing. Hopefully, will be able to come up with a good fix.
After AgentExecutor receives the initial observation from your tool's results, it proceeds to invoke the chain with this revised input. Gven the formatting instruction the response obtained is 'Do I need to use a tool? No.' However, this triggers a parsing error since the default parser anticipates 'Action' and 'Action Input' elements. To mitigate this, the returned output should be prefixed with 'ai' and should encompass the ultimate response in the format 'AI: [final response]'. To successfully achieve this, employing the provided custom format guidelines in tandem with the custom parser yields the desired outcome (I have personally tested this approach).
from langchain.memory import ConversationBufferMemory
from langchain.agents import initialize_agent,AgentType,AgentOutputParser
from langchain.schema import AgentAction,AgentFinish
from langchain.memory import ConversationBufferMemory
from typing import Union
import re
CUSTOM_FORMAT_INSTRUCTIONS = """To use a tool, please use the following format:
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
{ai_prefix}: [your response here]
"""
class CustomConvoOutputParser(AgentOutputParser):
"""Output parser for the conversational agent."""
ai_prefix: str = "AI"
"""Prefix to use before AI output."""
def get_format_instructions(self) -> str:
return CUSTOM_FORMAT_INSTRUCTIONS
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
if f"{self.ai_prefix}:" in text:
return AgentFinish(
{"output": text.split(f"{self.ai_prefix}:")[-1].strip()}, text
)
regex = r"Action: (.*?)[\n]*Action Input: (.*)"
match = re.search(regex, text)
if not match:
return AgentFinish(
{"output": text.split(f"{self.ai_prefix}:")[-1].strip()}, text
)
action = match.group(1)
action_input = match.group(2)
return AgentAction(action.strip(), action_input.strip(" ").strip('"'), text)
@property
def _type(self) -> str:
return "conversational"
agent = initialize_agent(
tools=tools, # You did not provide the tools you were using. I tested this with the Wikipedia tool
agent = AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
llm=models['llm_chat_gpt3'], ## I assumed you have a dict with gets the model. I tested with following: ChatOpenAI(model_name="gpt-3.5-turbo-16k")
verbose=True,
memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True),
handle_parsing_errors = False,
agent_kwargs={'format_instructions':CUSTOM_FORMAT_INSTRUCTIONS,'output_parser':CustomConvoOutputParser}
)
while True:
user_input = input("Human: ")
print(agent.run(input()))
print()
print('-'*50)
Please use the above parser and custom-formatted instructions. It works. TBH, I know the issue but solving this issue in the main code base will take time. However, this will suffice to make your conversational agent work.
I hope this works. If yes , please close the issue.
@keenborder786 when trying to run your code I get this error:
ValidationError: 1 validation error for ConversationalAgent
output_parser
value is not a valid dict (type=type_error.dict)
Using Python 3.10.12 and langchain 0.0.281
Hi @dentro-innovation ,
try this when passing kwargs
agent_kwargs={'format_instructions':CUSTOM_FORMAT_INSTRUCTIONS,'output_parser':CustomConvoOutputParser()}
Hi, @junruxiong,
I'm helping the LangChain team manage their backlog and am marking this issue as stale. From what I understand, the issue you reported regarding the ConversationalAgent/CHAT_CONVERSATIONAL_REACT_DESCRIPTION not working well with ChatOpenAI 3.5 and Claude models has been resolved. I provided a detailed response, keenborder786 suggested a custom format and parser, and axen2u offered a modification to the code, resulting in a successful resolution.
Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, kindly let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you!
System Info
Python 3.10
Who can help?
@hw
Information
Related Components
Reproduction
I tried with 3.5 and Claude models, but it rarely generates a reply to the prompt preset (like below) when I use the conversation agent. GPT 4 works fine.
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
This is my code:
Errors:
Expected behavior
Should have no parser error and generate the same format response as the prompt.