Skip to content

Chat Completions

The most commonly used endpoint — for chat, Q&A, code generation, tool calling, and more.

Create a chat completion

POST /v1/chat/completions

Parameters

ParameterTypeRequiredDescription
modelstringYesModel identifier, e.g. gpt-4o-mini
messagesarrayYesConversation messages (see below)
streambooleanNoStream the response, default false
temperaturenumberNoSampling temperature, 02, default 1
top_pnumberNoNucleus sampling, default 1
max_tokensintegerNoMax tokens to generate
stopstring | arrayNoStop sequences
toolsarrayNoCallable tool (function) definitions
tool_choicestring | objectNoTool selection strategy
response_formatobjectNoe.g. { "type": "json_object" }

messages

Each message has a role and content:

  • role: system / user / assistant / tool
  • content: a string, or a multimodal array (text + image)

Basic request

bash
curl https://ai.youqi.tech/v1/chat/completions \
  -H "Authorization: Bearer sk-YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      { "role": "system", "content": "You are a helpful assistant." },
      { "role": "user", "content": "Describe Hangzhou in one sentence." }
    ]
  }'

Response

json
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1715367049,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hangzhou is a city famed for West Lake, blending history with a vibrant digital economy."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 26,
    "completion_tokens": 22,
    "total_tokens": 48
  }
}

Streaming (SSE)

Add "stream": true to receive incremental chunks via SSE, ending with data: [DONE].

bash
curl https://ai.youqi.tech/v1/chat/completions \
  -H "Authorization: Bearer sk-YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "stream": true,
    "messages": [{ "role": "user", "content": "Write a short poem about autumn." }]
  }'

Each chunk looks like:

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Autumn"},"finish_reason":null}]}

Multimodal input (images)

Vision-capable models accept mixed text and images in content:

json
{
  "model": "gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": [
        { "type": "text", "text": "What is in this image?" },
        {
          "type": "image_url",
          "image_url": { "url": "https://example.com/photo.jpg" }
        }
      ]
    }
  ]
}

Tool calling (function calling)

json
{
  "model": "gpt-4o-mini",
  "messages": [{ "role": "user", "content": "What's the weather in Beijing today?" }],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "Get the weather for a city",
        "parameters": {
          "type": "object",
          "properties": { "city": { "type": "string" } },
          "required": ["city"]
        }
      }
    }
  ]
}

When the model decides to call a tool, finish_reason is tool_calls and the call arguments are returned in message.tool_calls. Execute the tool and send the result back as a role: "tool" message to continue the conversation.

TIP

Support for tools, response_format, and multimodal input varies by model. If a model doesn't support a parameter, the gateway passes through the upstream behavior or returns an appropriate error.