Я пытаюсь создать простого агента React, использующего Langchain, который использует инструмент для получения погодного условия местоположения < /p>
Я попытался сделать свой пользовательский шаблон быстрого приглашения и использовал «create_react_agent 'и' AgentExecutor 'из Langchain < /p>
, как я исправил. Не останавливаясь и продолжает регистрировать ошибку, что «действие» отсутствует после «мысли» < /p>
from langchain_ollama import ChatOllama
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain.tools import Tool
from langchain.agents import create_react_agent, AgentExecutor
from langchain_core.prompts import PromptTemplate
llm = ChatOllama(
model="llama3.2:1b",
temperature=0.1,
)
react_template = """
Use the following set of rules and instructions to give the best possible answer for the user's questions, based on the tools provided to you
Tools: {tools}
Follow these steps of reasoning and action strictly to find out the correct answer for the user's question
[Question]: The input question asked by the user
[Thought]: Understanding and thinking about the question asked by the user
[Action]: The appropriate action to be taken based on the question asked by the user, use one of the following tools [{tool_names}]
[Observation]: The result of the action
[Thought]: Is this the answer to the question asked by the user ? If yes then give the final answer, else repeat the process (Thought->Action->Observation)
[Final Answer]: I have the final answer to the user's question
Example 1:
[Question]: What is the weather in france ?
[Thought]: I should first search for the weather in france
[Action]: I should use the get_weather tool to get the weather in france
[Observation]: The weather returned from get_weather was cloudy
[Thought]: This was the answer returned from the get_weather tool
[Final Answer]: cloudy
Critical mistakes to avoid:
1. Do not make up answers. The model should only give the correct answer to the user's question returned by the tools provided to it
2. Do not provide justifications to answer produced
3. Keep the final answers short and to the point
Begin !
[Question]: {question}
{agent_scratchpad}
"""
react_prompt = PromptTemplate(
template=react_template,
input_variables=["question", "tools", "tool_names"],
)
def get_weather_func(location: str) -> str:
location = location.lower().strip()
if location == "france":
return "cloudy"
elif location == "italy":
return "sunny"
else:
return "unknown weather"
weather_tool = Tool(
name= "get_weather",
func=get_weather_func,
description="Useful for getting the weather of a location"
)
tools = [weather_tool]
agent = create_react_agent(
llm=llm,
tools=tools,
prompt=react_prompt
)
agent_exexcutor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
handle_parsing_errors=True
)
if __name__ == "__main__":
response = agent_exexcutor.invoke(
input={
"question": "What is the weather in france ?",
"tools": "\n".join([t.name for t in tools]),
"tool_names": "\n".join([t.name + " : " + t.description for t in tools]),
"agent_scratchpad": "",
}
)
print(response["output"])
< /code>
Это ошибки, которые я получаю на своем терминале < /p>
[Question]: What is the weather in france ?
< /code>
The result of the action is cloudy
< /code>
Invalid Format: Missing 'Action:' after 'Thought:'
< /code>
[Question]: What is the weather in france ?
< /code>
The result of the action is cloudy
< /code>
Invalid Format: Missing 'Action:' after 'Thought:'
< /code>
The result of the action is cloudy
< /code>
Invalid Format: Missing 'Action:' after 'Thought:'
< /code>
Thought: I should first search for the weather in franceInvalid Format: Missing 'Action Input:' after 'Action:'
< /code>
Thought: I should first search for the weather in franceInvalid Format: Missing 'Action Input:' after 'Action:'
< /code>
> Finished chain.
< /code>
> Finished chain.
< /code>
Agent stopped due to iteration limit or time limit.
Подробнее здесь: https://stackoverflow.com/questions/794 ... on-after-t
Infinite Loop в пользовательском агенте React с использованием Langchain, [отсутствует »действие:« После «мысли:»] ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение