并行工具¶
并行函数调用是一项功能,它允许您在单个请求中调用多个函数。
实验性功能
目前并行函数调用仅由 Google 和 OpenAI 支持。请确保为您的客户端使用等效的并行工具 mode
。
理解并行函数调用¶
并行函数调用可以帮助您显著减少应用程序的延迟,而无需构建一个父级 Schema 作为这些工具调用的包装器。
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='metric'
#> query='who won the super bowl 2023'
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]
,以表明响应将是 Weather
和 GoogleSearch
对象的列表。
这是必需的,因为响应将是一个对象列表,我们需要指定列表中对象的类型。这会返回一个可迭代对象,您可以对其进行迭代。