meta-llama / llama-models

Utilities intended for use with Llama models.
Other
3.78k stars 676 forks source link

Error running Llama3.1 with Langchain ReAct agent #113

Open rpm-arch opened 3 weeks ago

rpm-arch commented 3 weeks ago

I am attempting to use Llama3.1 with the following ReAct agent template from Langchain:

https://python.langchain.com/v0.1/docs/modules/agents/agent_types/react/

Here is the code:

from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent, load_tools
from langchain_ollama import ChatOllama

llm = ChatOllama(
    model="llama3.1",
    temperature=0
)

tools = load_tools(["ddg-search"])
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

print(agent_executor.invoke({"input": "Tell me a joke"}))

This results in looping use of the tool and finally an output parsing error:

ValueError: An output parsing error occurred. In order to pass this error back to the agent and have it try again, pass `handle_parsing_errors=True` to the AgentExecutor. This is the error: Could not parse LLM output:

With handle_parsing_errors=True:

Invalid Format: Missing 'Action:' after 'Thought:

> Finished chain.
{'input': 'Tell me a joke', 'output': 'Agent stopped due to iteration limit or time limit.'}

Process finished with exit code 0

The same code works if I use other models such as mistral-nemo or qwen2.

Should this work, or can you share an example ReAct prompt that Llama3.1 is able to use?

Here is the standard prompt I am trying:

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

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}

Thanks

ashwinb commented 3 weeks ago

I don't think we can provide support for this at a general level. Does the same snippet of code work with llama3 and fails with a loop for llama3.1? Do you have a full trace of the outputs? What other things or changes have you tried?