Skip to content

OpenAI 对话格式(Chat Completions)

简介

给定一组包含对话的消息列表,模型将返回一个响应。相关指南可参阅OpenAI官网:Chat Completions

基础文本对话

js
curl https://api.nextaicore.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {
        "role": "system",
        "content": "你是一个有帮助的助手。"
      },
      {
        "role": "user",
        "content": "你好!"
      }
    ],
    "stream": true
  }'

响应示例:

js
{
  "id": "chatcmpl-C7cFr7HRoSbH1LDsyc1DFlnu2bBem",
  "object": "chat.completion",
  "created": 1755931383,
  "model": "gpt-4.1-2025-04-14",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好!有什么可以帮您的吗?",
        "refusal": null,
        "annotations": []
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 21,
    "completion_tokens": 9,
    "total_tokens": 30,
    "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_c79ab13e31"
}

流式响应

js
curl https://api.nextaicore.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {
        "role": "system",
        "content": "你是一个有帮助的助手。"
      },
      {
        "role": "user",
        "content": "你好!"
      }
    ],
    "stream": true
  }'

响应示例:

js
data: {"id":"","object":"","created":0,"model":"gpt-4.1-2025-04-14","choices":[]}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"你好"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"!"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"有什么"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"我"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"可以"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"帮"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"你"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"的吗"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"?"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{"content":"😊"},"logprobs":null,"finish_reason":null}],"usage":{}}

data: {"id":"chatcmpl-C7cMJVJjklJYcUDc0filozhZjekT6","object":"chat.completion.chunk","created":1755931783,"model":"gpt-4.1-2025-04-14","system_fingerprint":"fp_c79ab13e31","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}],"usage":{}}

data: [DONE]

图像分析对话

js
curl https://api.nextaicore.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "这张图片里有什么?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
            }
          }
        ]
      }
    ],
    "max_tokens": 300
  }'

响应示例:

js
{
  "id": "chatcmpl-C7cUbTqDLw9ICl6imC6RBEfMdI4Os",
  "object": "chat.completion",
  "created": 1755932297,
  "model": "gpt-4.1-2025-04-14",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "这张图片里有一条木头铺成的小路,延伸到远方。小路两旁是大片绿色的草地和一些灌木,背景中有一些树木。天空很蓝,有一些薄薄的白云,整个画面显得很宁静和清新,有田园或湿地的感觉。",
        "refusal": null,
        "annotations": []
      },
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 921,
    "completion_tokens": 75,
    "total_tokens": 996,
    "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_c79ab13e31"
}