Parallel tool use | Документация для разработчиков

Parallel tool use

Обновлено 24 мая 2024

In the Chains with multiple tools guide we saw how to build function-calling chains that select between multiple tools. Some models, like the OpenAI models released in Fall 2023, also support parallel function calling, which allows you to invoke multiple functions (or the same function multiple times) in a single model call. Our previous chain from the multiple tools guides actually already supports this, we just need to use an OpenAI model capable of parallel function calling.


We'll need to install the following packages for this guide:

%pip install --upgrade --quiet langchain langchain-openai

And set these environment variables:

import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()

# If you'd like to use LangSmith, uncomment the below
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()


from langchain_core.tools import tool

def multiply(first_int: int, second_int: int) -> int:
"""Multiply two integers together."""
return first_int * second_int

def add(first_int: int, second_int: int) -> int:
"Add two integers."
return first_int + second_int

def exponentiate(base: int, exponent: int) -> int:
"Exponentiate the base to the exponent power."
return base**exponent


Обновлено 24 мая 2024

Notice we use an -1106 model, which as of this writing is the only kind that supports parallel function calling:

from operator import itemgetter
from typing import Union

from langchain.output_parsers import JsonOutputToolsParser
from langchain_core.runnables import (
from langchain_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-3.5-turbo-1106")
tools = [multiply, exponentiate, add]
model_with_tools = model.bind_tools(tools)
tool_map = {tool.name: tool for tool in tools}

def call_tool(tool_invocation: dict) -> Union[str, Runnable]:
"""Function for dynamically constructing the end of the chain based on the model-selected tool."""
tool = tool_map[tool_invocation["type"]]
return RunnablePassthrough.assign(output=itemgetter("args") | tool)

# .map() allows us to apply a function to a list of inputs.
call_tool_list = RunnableLambda(call_tool).map()
chain = model_with_tools | JsonOutputToolsParser() | call_tool_list
"What's 23 times 7, and what's five times 18 and add a million plus a billion and cube thirty-seven"
    [{'type': 'multiply',
'args': {'first_int': 23, 'second_int': 7},
'output': 161},
{'type': 'add', 'args': {'first_int': 5, 'second_int': 18}, 'output': 23},
{'type': 'add',
'args': {'first_int': 1000000, 'second_int': 1000000000},
'output': 1001000000},
{'type': 'exponentiate',
'args': {'base': 37, 'exponent': 3},
'output': 50653}]
ПАО Сбербанк использует cookie для персонализации сервисов и удобства пользователей.
Вы можете запретить сохранение cookie в настройках своего браузера.