-
Checked other resources
Commit to Help
Example Code@tool
def get_financial_info(co: str, fundamental: str, yr: str) -> float:
"""useful for getting information on a fundamental for a given company and year"""
<code for returning relevant financial information>
#get_financial_info.invoke({"co":"Tesla", "fundamental":"sales", "yr":"FY22"}) - WORKS OK
tools = [get_financial_info]
template = """Answer the following questions as best you can. You have access to the following tools:\
{tools}\
Use the following format:\
Question: the input question you must answer\
Thought: you should always think about what to do\
Action: the action to take, should be one of [{tool_names}]\
Action Input: the input to the action\
If there's an error, re run the tool with corrected input\
Observation: the result of the action\
... (this Thought/Action/Action Input/Observation can repeat N times)\
Thought: I now know the final answer\
Final Answer: the final answer to the original input question\
Begin!\
Question: {input}\
Thought:{agent_scratchpad}
"""
prompt_template = PromptTemplate.from_template(template)
react_agent = create_react_agent(llm, tools=tools, prompt=prompt_template)
# executes the logical steps we created
react_agent_executor = AgentExecutor(
agent=react_agent, tools=tools, verbose=True, handle_parsing_errors=True
)
query = "What is the sales for Tesla in FY22?"
react_agent_executor.invoke({"input": query})
> Entering new AgentExecutor chain...
I need to use the get_financial_info tool to get the sales information for Tesla in FY22.
Action: get_financial_info
Action Input: {"co": "Tesla", "fundamental": "sales", "yr": "FY22"}
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
<ipython-input-36-38d1c873087b> in <cell line: 2>()
1 query = "What is the sales for Tesla in FY22?"
----> 2 react_agent_executor.invoke({"input": query})
11 frames
/usr/local/lib/python3.10/dist-packages/pydantic/v1/main.py in __init__(__pydantic_self__, **data)
339 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
340 if validation_error:
--> 341 raise validation_error
342 try:
343 object_setattr(__pydantic_self__, '__dict__', values)
ValidationError: 2 validation errors for get_financial_infoSchema
fundamental
field required (type=value_error.missing)
yr
field required (type=value_error.missing) DescriptionHi folks: I am using a function to get financial information for a company and yr and coupling it with a react agent. The Action Input is generating the correct input: {"co": "Tesla", "fundamental": "sales", "yr": "FY22"} but the agent still gives the Validation error for inputs "fundamental" and "yr". When I run get_financial_info.invoke({"co":"Tesla"}) it gives the same error which means the other 2 arguments are not getting passed correctly. Looks like an output parser issue. Can someone please help? Am a newbie to Langchain Agents. Appreciate it. System InfoGoogle Colab |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
🤖 Hey @hepbc, great to see you diving into LangChain Agents! Always here to help. From the error message you provided, it seems like the In LangChain, the ReAct Agent uses the Here is the relevant code snippet from the class ReActSingleInputOutputParser(Runnable):
def run(self, text: str) -> dict:
lines = text.split("\n")
action = lines[0].split(": ")[1]
action_input = json.loads(lines[1].split(": ")[1])
observation = lines[2].split(": ")[1]
return {"action": action, "action_input": action_input, "observation": observation} Source: libs/langchain/langchain/agents/react/parsers.py From your code, it seems like the action input is being generated correctly: One possible solution could be to modify the @tool
def get_financial_info(inputs: dict) -> float:
"""useful for getting information on a fundamental for a given company and year"""
co = inputs["co"]
fundamental = inputs["fundamental"]
yr = inputs["yr"]
# <code for returning relevant financial information> Then, you would need to modify the action input in the ReAct Agent to pass a single dictionary input to the tool: Action Input: {"inputs": {"co": "Tesla", "fundamental": "sales", "yr": "FY22"}} Please try this solution and let me know if it resolves the issue. If not, we can explore other potential solutions. Sources
|
Beta Was this translation helpful? Give feedback.
-
The agent type you're using doesn't currently support multi-input tools. See more alternatives here: https://python.langchain.com/docs/modules/agents/agent_types/ |
Beta Was this translation helpful? Give feedback.
The agent type you're using doesn't currently support multi-input tools. See more alternatives here: https://python.langchain.com/docs/modules/agents/agent_types/