Мне удалось запустить LangChain с помощью инструментов, украшенных тегом @tool. Этот инструмент предполагает, что у вас есть строка в качестве входных данных (ввод действия) и выходных данных. Разбирая эту строку, я могу запустить инструмент, вернуть ответ LLM и получить что-то, что более или менее работает.
Моя проблема - «более или менее». Если я хочу продвигать код в рабочей среде, мне нужно, чтобы код работал в 99,9999% случаев, а не в 50%.
По этой причине я решил перейти на StructuredTool со схемой Pydantic, насколько это возможно. см. в примере.
Однако, когда я запустил его с LLama 3.1 (на моем Mac) с помощью интерфейса командной строки Ollama, я получил эту ошибку:
Код: Выделить всё
Traceback (most recent call last):
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/experiments/multiply.py", line 59, in
agent_executor.invoke({"input": "Can you multiply 3 by 2 and give me the result?"})
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain/chains/base.py", line 164, in invoke
raise e
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain/chains/base.py", line 154, in invoke
self._call(inputs, run_manager=run_manager)
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain/agents/agent.py", line 1625, in _call
next_step_output = self._take_next_step(
^^^^^^^^^^^^^^^^^^^^^
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain/agents/agent.py", line 1331, in _take_next_step
[
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain/agents/agent.py", line 1416, in _iter_next_step
yield self._perform_agent_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain/agents/agent.py", line 1438, in _perform_agent_action
observation = tool.run(
^^^^^^^^^
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain_core/tools/base.py", line 586, in run
raise error_to_raise
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain_core/tools/base.py", line 549, in run
tool_args, tool_kwargs = self._to_args_and_kwargs(tool_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain_core/tools/base.py", line 472, in _to_args_and_kwargs
tool_input = self._parse_input(tool_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/langchain_core/tools/base.py", line 420, in _parse_input
input_args.validate({key_: tool_input})
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/pydantic/main.py", line 1361, in validate
return cls.model_validate(value)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sasadangelo/github.com/sasadangelo/chatterpy/venv/lib/python3.12/site-packages/pydantic/main.py", line 568, in model_validate
return cls.__pydantic_validator__.validate_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 2 validation errors for MultiplyInput
a
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='{"a": 3, "b": 2', input_type=str]
For further information visit https://errors.pydantic.dev/2.8/v/int_parsing
b
Field required [type=missing, input_value={'a': '{"a": 3, "b": 2'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.8/v/missing
Код: Выделить всё
Human:
Answer the following questions as best you can. You have access to the following tools:
multiply(a: int, b: int) -> int - This function multiplies two integers and returns an integer.
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 [multiply].
Action Input: the input to the action (in dictionary format).
Observation: always analyze the result of the action.
... (this Thought/Action/Action Input/Observation could repeat N times until you don't find the final answer)
Thought: I now know the final answer.
Final Answer: the final answer to the original input question
Begin!
Question: Can you multiply 3 by 2 and give me the result?
Thought:
Код: Выделить всё
Question: Can you multiply 3 by 2 and give me the result?
Thought: I need to perform a multiplication operation on two integers.
Action: multiply
Action Input: {"a": 3, "b": 2
Может ли кто-нибудь помочь мне понять, что не так с моим кодом? Как я могу сделать его функциональным и стабильным?
Подробнее здесь: https://stackoverflow.com/questions/790 ... -signature