Chat API (Function Calls)
Short description: Chat API supporting model-generated function (tool) calls. Models can return suggested function calls during a conversation; the platform or caller may execute those functions.
Overview
- Method:
POST - Path:
/v1/chat/completions - Content-Type:
application/json
Authentication
Supports common header-based authentication:
- OpenAI:
Authorization: Bearer sk-xxx - Anthropic:
x-api-key: sk-xxx(may also requireanthropic-version) - Google:
x-goog-api-key: sk-xxx
Request example
Body parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| model | string | yes | Model name, e.g. gpt-4o |
| messages | array<object> | yes | Chat message list, typical item `{ role: 'user' |
| tools | array<object> | yes | List of tools (functions) the model may call; each item is type function,and includes name, description and parameters JSON Schema |
Tool item (example structure):
json
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string", "description": "City and country e.g. Bogotá, Colombia" }
},
"required": ["location"],
"additionalProperties": false
},
"strict": true
}
}After receiving the tools list, the model may return tool_calls in its response, indicating a suggested function call with arguments serialized as a JSON string.
curl example
bash
curl -X POST "https://api.gpt.ge/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxx" \
-d '{
"model": "gpt-4o",
"messages": [{"role":"user","content":"What is the weather like in Paris today?"}],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": { "type": "object", "properties": { "location": { "type": "string" } }, "required": ["location"], "additionalProperties": false },
"strict": true
}
}
]
}'JavaScript (fetch) example
javascript
fetch('https://api.gpt.ge/v1/chat/completions', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer sk-xxxx' },
body: JSON.stringify({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'What is the weather like in Paris today?' }],
tools: [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get current temperature for a given location.',
parameters: { type: 'object', properties: { location: { type: 'string' } }, required: ['location'], additionalProperties: false },
strict: true
}
}
]
})
}).then(r => r.json()).then(console.log)Python (requests) example
python
import requests
payload = {
'model': 'gpt-4o',
'messages': [{'role': 'user', 'content': 'What is the weather like in Paris today?'}],
'tools': [
{
'type': 'function',
'function': {
'name': 'get_weather',
'description': 'Get current temperature for a given location.',
'parameters': { 'type': 'object', 'properties': { 'location': { 'type': 'string' } }, 'required': ['location'], 'additionalProperties': False },
'strict': True
}
}
]
}
resp = requests.post('https://api.gpt.ge/v1/chat/completions', headers={'Content-Type':'application/json','Authorization':'Bearer sk-xxxx'}, json=payload)
print(resp.json())Response example (200)
json
{
"id": "chatcmpl-Ax2bU1RFE8P0Y9uqKcErQNLcx4dDe",
"object": "chat.completion",
"created": 1738634724,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_FezxjoWuDV1CL3dITVFOjUzK",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\":\"Paris, France\"}"
}
}
],
"refusal": null
},
"logprobs": null,
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 65,
"completion_tokens": 16,
"total_tokens": 81,
"prompt_tokens_details": { "cached_tokens": 0, "audio_tokens": 0 },
"completion_tokens_details": { "reasoning_tokens": 0, "audio_tokens": 0, "accepted_prediction_tokens": 0, "rejected_prediction_tokens": 0 }
},
"system_fingerprint": "fp_f3927aa00d"
}