Langchain工具调用不返回任何内容

问题描述 投票:0回答:1

我正在尝试让 Langchain 与工具调用一起工作。我一直在关注他们网站上的教程,并得到了一些意想不到的行为。

这是教程:https://python.langchain.com/docs/how_to/tool_results_pass_to_model/

这是我的代码。

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langchain_core.tools import tool


llm = ChatOpenAI(
    base_url="https://api.together.xyz/v1",
    api_key="<api_key>",
    model="mistralai/Mixtral-8x7B-Instruct-v0.1",
)


@tool
def add(a: int, b: int) -> int:
    """Add two integers.

    Args:
        a: First integer
        b: Second integer
    """
    return a + b


@tool
def multiply(a: int, b: int) -> int:
    """Multiply two integers.

    Args:
        a: First integer
        b: Second integer
    """
    return a * b


tools = [add, multiply]

llm = llm.bind_tools(tools)

messages = [HumanMessage("What is 3 * 12? Also, what is 11 + 49?")]

response = llm.invoke(messages)

print(response)
print("-----------------------------------")

if response.tool_calls:
    messages.append(response)
    for tool_call in response.tool_calls:
        selected_tool = {"add": add, "multiply": multiply}[tool_call["name"].lower()]
        tool_msg = selected_tool.invoke(tool_call)
        messages.append(tool_msg)

    print(messages)
    print("-----------------------------------")

    second_response = llm.invoke(messages)
    print(second_response)

这是我的打印语句的结果:

content='' additional_kwargs={'tool_calls': [{'id': 'call_thdcqzbn85b4nrl63n3jqj1y', 'function': {'arguments': '{"a":3,"b":12}', 'name': 'multiply'}, 'type': 'function'}, {'id': 'call_7qhd09fknz6t74z5b5zuetvb', 'function': {'arguments': '{"a":11,"b":49}', 'name': 'add'}, 'type': 'function'}], 'refusal': None} response_metadata={'token_usage': {'completion_tokens': 84, 'prompt_tokens': 342, 'total_tokens': 426, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'mistralai/Mixtral-8x7B-Instruct-v0.1', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None} id='run-74d1414c-f12e-4a98-a1f5-eeff6039b4e5-0' tool_calls=[{'name': 'multiply', 'args': {'a': 3, 'b': 12}, 'id': 'call_thdcqzbn85b4nrl63n3jqj1y', 'type': 'tool_call'}, {'name': 'add', 'args': {'a': 11, 'b': 49}, 'id': 'call_7qhd09fknz6t74z5b5zuetvb', 'type': 'tool_call'}] usage_metadata={'input_tokens': 342, 'output_tokens': 84, 'total_tokens': 426, 'input_token_details': {}, 'output_token_details': {}}
-----------------------------------
[HumanMessage(content='What is 3 * 12? Also, what is 11 + 49?', additional_kwargs={}, response_metadata={}), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_thdcqzbn85b4nrl63n3jqj1y', 'function': {'arguments': '{"a":3,"b":12}', 'name': 'multiply'}, 'type': 'function'}, {'id': 'call_7qhd09fknz6t74z5b5zuetvb', 'function': {'arguments': '{"a":11,"b":49}', 'name': 'add'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 84, 'prompt_tokens': 342, 'total_tokens': 426, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'mistralai/Mixtral-8x7B-Instruct-v0.1', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-74d1414c-f12e-4a98-a1f5-eeff6039b4e5-0', tool_calls=[{'name': 'multiply', 'args': {'a': 3, 'b': 12}, 'id': 'call_thdcqzbn85b4nrl63n3jqj1y', 'type': 'tool_call'}, {'name': 'add', 'args': {'a': 11, 'b': 49}, 'id': 'call_7qhd09fknz6t74z5b5zuetvb', 'type': 'tool_call'}], usage_metadata={'input_tokens': 342, 'output_tokens': 84, 'total_tokens': 426, 'input_token_details': {}, 'output_token_details': {}}), ToolMessage(content='36', name='multiply', tool_call_id='call_thdcqzbn85b4nrl63n3jqj1y'), ToolMessage(content='60', name='add', tool_call_id='call_7qhd09fknz6t74z5b5zuetvb')]
-----------------------------------
content='' additional_kwargs={'refusal': None} response_metadata={'token_usage': {'completion_tokens': 1, 'prompt_tokens': 477, 'total_tokens': 478, 'completion_tokens_details': None, 'prompt_tokens_details': None}, 'model_name': 'mistralai/Mixtral-8x7B-Instruct-v0.1', 'system_fingerprint': None, 'finish_reason': 'eos', 'logprobs': None} id='run-4195c304-6c37-45bf-b745-cb370dba452f-0' usage_metadata={'input_tokens': 477, 'output_tokens': 1, 'total_tokens': 478, 'input_token_details': {}, 'output_token_details': {}}

我的期望是,

content
中的
second_response
应该填充人工智能对我的问题生成的回答。类似于
The result of 3 times 12 is 36, and the result of 11 + 49 is 60.

我是不是做错了什么?任何帮助我获得预期行为的帮助都将非常感激。

谢谢

python langchain py-langchain langchain-agents langchain-together
1个回答
0
投票

事实证明问题出在我使用的模型上。尽管它据称支持工具,但它并没有完成工作。我切换到 4o-mini,现在一切都按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.