[Python] Langchain tool calling not returning any content

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get Langchain working with tool calling. I've been following the tutorial on their website and am getting some unexpected behaviour.

    This is the tutorial: https://python.langchain.com/docs/how_to/tool_results_pass_to_model/

    Here is my code.

    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)



    Here are the results of my print statements:

    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': {}}


    My expectation is that content in second_response should be populated with the an AI-generated response to my question. Something like The result of 3 times 12 is 36, and the result of 11 + 49 is 60..

    Is there something I am doing wrong? Any help to get my expected behaviour would be very appreciated.

    Thanks

    Continue reading...

Compartilhe esta Página