跳过内容

并行工具

并行函数调用是一项功能,允许您在单个请求中调用多个函数。

实验性功能

目前只有Google和OpenAI支持并行函数调用。请确保为您的客户端使用等效的并行工具mode

理解并行函数调用

并行函数调用帮助您显著减少应用程序的延迟,而无需构建一个父模式作为这些工具调用的包装器。

from __future__ import annotations

import openai
import instructor

from typing import Iterable, Literal
from pydantic import BaseModel


class Weather(BaseModel):
    location: str
    units: Literal["imperial", "metric"]


class GoogleSearch(BaseModel):
    query: str


client = instructor.from_openai(
    openai.OpenAI(), mode=instructor.Mode.PARALLEL_TOOLS
)

function_calls = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You must always use tools"},
        {
            "role": "user",
            "content": "What is the weather in toronto and dallas and who won the super bowl?",
        },
    ],
    response_model=Iterable[Weather | GoogleSearch],
)

for fc in function_calls:
    print(fc)
    #> location='Toronto' units='metric'
    #> location='Dallas' units='imperial'
    #> query='who won the super bowl'
import instructor
import vertexai
from vertexai.generative_models import GenerativeModel
from typing import Iterable, Literal
from pydantic import BaseModel

vertexai.init()

class Weather(BaseModel):
    location: str
    units: Literal["imperial", "metric"]


class GoogleSearch(BaseModel):
    query: str


client = instructor.from_vertexai(
    GenerativeModel("gemini-1.5-pro-preview-0409"),
    mode=instructor.Mode.VERTEXAI_PARALLEL_TOOLS
)

function_calls = client.create(
    messages=[
        {
            "role": "user",
            "content": "What is the weather in toronto and dallas and who won the super bowl?",
        },
    ],
    response_model=Iterable[Weather | GoogleSearch],
)

for fc in function_calls:
    print(fc)
    #> location='Toronto' units='metric'
    #> location='Dallas' units='imperial'
    #> query='who won the super bowl'

我们需要将响应模型设置为Iterable[Weather | GoogleSearch],以表明响应将是WeatherGoogleSearch对象的列表。

这是必要的,因为响应将是一个对象列表,我们需要指定列表中对象的类型。这将返回一个可迭代对象,然后您可以对其进行迭代。