Tool calling
Tool & function calling
Mô tả các hàm của bạn và một mô hình hỗ trợ công cụ sẽ yêu cầu bạn chạy một hàm. Oxyy chuyển đổi khai báo thành định dạng mà mỗi nhà cung cấp yêu cầu, do đó cùng một tools mảng hoạt động trên OpenAI, Anthropic, Google, xAI và các bên khác.
| Tham số | Loại | Bắt buộc | Mô tả |
|---|---|---|---|
| tools | array | Không bắt buộc | Khai báo hàm: {type: "function", function: {name, description, parameters}}, trong đó parameters là một đối tượng JSON Schema. |
| tool_choice | string|object | Không bắt buộc | Mức độ tự do của model khi gọi tool. Một trong:noneautorequired{type:"function",function:{name}} Mặc định auto |
| parallel_tool_calls | boolean | Không bắt buộc | Cho phép nhiều hơn một lệnh gọi công cụ trong mỗi lượt. Mặc định true |
import json, os from openai import OpenAI client = OpenAI( api_key=os.environ["OXYY_API_KEY"], base_url="https://api.oxyy.ai/v1" ) tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Current weather for a city.", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, }, }] messages = [{"role": "user", "content": "Weather in Dhaka?"}] response = client.chat.completions.create( model="gpt-5.5", messages=messages, tools=tools, tool_choice="auto" ) call = response.choices[0].message.tool_calls[0] args = json.loads(call.function.arguments) # Run the tool yourself, then send the result back as a tool message. messages.append(response.choices[0].message) messages.append({ "role": "tool", "tool_call_id": call.id, "content": json.dumps({"temp_c": 31, "sky": "humid"}), }) final = client.chat.completions.create(model="gpt-5.5", messages=messages, tools=tools) print(final.choices[0].message.content)
import OpenAI from 'openai'; const client = new OpenAI({ apiKey: process.env.OXYY_API_KEY, baseURL: 'https://api.oxyy.ai/v1' }); const tools = [{ type: 'function', function: { name: 'get_weather', description: 'Current weather for a city.', parameters: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'], }, }, }]; const messages = [{ role: 'user', content: 'Weather in Dhaka?' }]; const res = await client.chat.completions.create({ model: 'gpt-5.5', messages, tools, tool_choice: 'auto', }); const call = res.choices[0].message.tool_calls[0]; const args = JSON.parse(call.function.arguments); messages.push(res.choices[0].message, { role: 'tool', tool_call_id: call.id, content: JSON.stringify({ temp_c: 31, sky: 'humid' }), }); const final = await client.chat.completions.create({ model: 'gpt-5.5', messages, tools }); console.log(final.choices[0].message.content);
Lượt gọi tool
Phản hồi
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1700000000,
"model": "gpt-5.5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"Dhaka\"}"
}
}
],
"refusal": null
},
"logprobs": null,
"finish_reason": "tool_calls"
}
],
"usage": { "prompt_tokens": 61, "completion_tokens": 18, "total_tokens": 79, "cost": 0.00029 }
}Vòng lặp. Thêm tin nhắn trợ lý như khi nó trả về, sau đó một
{role:"tool", tool_call_id, content}tin nhắn mỗi lần gọi và gửi lại toàn bộ cuộc trò chuyện. finish_reason là tool_callsbất cứ khi nào có các cuộc gọi, ngay cả trên các nhà cung cấp báo cáo stop. Trên các mô hình suy luận, hãy gửi lại bất kỳ reasoning_details không sửa đổi để quá trình suy nghĩ của mô hình được giữ nguyên vẹn qua vòng lặp.Chỉ các tool function. Oxyy không chạy các công cụ lưu trữ riêng — không có tìm kiếm web, trình thông dịch mã hoặc tìm kiếm tệp để bật. Hãy khai báo một function và chạy nó trong ứng dụng của bạn. Các công cụ máy chủ của Anthropic là ngoại lệ: chúng được chuyển tiếp nguyên bản đến nhà cung cấp qua Anthropic SDK.
