Принудительный тип функции с вызовом функции OpenAI при ожидании нескольких вызовов функций? ⇐ Python

Программы на Python
Anonymous
Принудительный тип функции с вызовом функции OpenAI при ожидании нескольких вызовов функций?

Сообщение Anonymous »

Параметр tool_choice заставляет API завершения чата возвращать только один вызов инструмента, даже если требуется несколько вызовов. Есть ли способ заставить API использовать определенный тип функции и разрешить несколько типов возврата?
import os

import openai
from dotenv import load_dotenv
from openai import NOT_GIVEN, AzureOpenAI

load_dotenv()

azure_endpoint = os.environ["OAI-GPT4O-mini-18072024-ENDPOINT"]
api_key = os.environ["OAI-GPT4O-mini-18072024-API-KEY"]
model = "m-gpt-4o-mini-18072024"
api_version = os.getenv("OAI-GPT4O-API-VERSION", "2024-08-01-preview")

client = AzureOpenAI(
azure_endpoint=azure_endpoint,
api_key=api_key,
api_version=api_version,
)

tools = [
{
"type": "function",
"function": {
"name": "GetWeather",
"parameters": {
"$defs": {
"TemperatureUnit": {
"enum": ["celsius", "fahrenheit"],
"title": "TemperatureUnit",
"type": "string",
}
},
"properties": {
"location": {
"description": "The location to get the weather for",
"title": "Location",
"type": "string",
},
"unit": {
"description": "Temperature Unit",
"enum": ["celsius", "fahrenheit"],
"title": "TemperatureUnit",
"type": "string",
},
},
"required": ["location", "unit"],
"title": "GetWeather",
"type": "object",
"additionalProperties": False,
},
},
}
]

tool_choice = {"type": "function", "function": {"name": "GetWeather"}}

messages = [
{"role": "system", "content": "Extract the weather information."},
{"role": "user", "content": "What's the weather like in London and New York??"},
]
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
temperature=0,
tool_choice=tool_choice,
)

response.choices[0].message.tool_calls
# length 1, only 1 tool call
# [ChatCompletionMessageToolCall(id='call_CmmBX8Oe1MutDUBLv6Xr59wW', function=Function(arguments='{"location":"London","unit":"celsius"}', name='GetWeather'), type='function')]

response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
temperature=0,
)
response.choices[0].message.tool_calls
# length 2
# [ChatCompletionMessageToolCall(id='call_DusKoDRUHJexQ03igKk5q2nw', function=Function(arguments='{"location": "London", "unit": "celsius"}', name='GetWeather'), type='function'),
# ChatCompletionMessageToolCall(id='call_bdeoRaWtsuDZkN1g1Vrj2KXj', function=Function(arguments='{"location": "New York", "unit": "fahrenheit"}', name='GetWeather'), type='function')]


Подробнее здесь: https://stackoverflow.com/questions/790 ... iple-funct

Вернуться в «Python»