跳轉到主要內容
POST
/
v1
/
chat
/
completions
Create chat completion
curl --request POST \
  --url https://api.dgrid.ai/v1/chat/completions \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "openai/gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ]
}
'
{
  "id": "<string>",
  "object": "<string>",
  "created": 123,
  "model": "<string>",
  "choices": [
    {
      "index": 123,
      "message": {
        "role": "<string>",
        "content": "<string>",
        "reasoning_content": "<string>",
        "tool_calls": [
          {}
        ]
      },
      "finish_reason": "<string>"
    }
  ],
  "usage": {
    "prompt_tokens": 123,
    "completion_tokens": 123,
    "total_tokens": 123,
    "prompt_tokens_details": {
      "cached_tokens": 123,
      "text_tokens": 123,
      "audio_tokens": 123,
      "image_tokens": 123
    },
    "completion_tokens_details": {
      "text_tokens": 123,
      "audio_tokens": 123,
      "reasoning_tokens": 123
    }
  },
  "system_fingerprint": "<string>"
}
本頁使用與 建立對話補全 相同的 chat/completions 操作,上方 playground 已預先填入通用對話結構。以下說明如何從相容 OpenAI 的用戶端,透過這個端點請求由 Gemini 驅動的圖像生成。
請在 model 中指定圖像生成模型(例如 gemini-2.0-flash-preview-image-generation),依需求設定 stream,並透過 messages 提供 prompt。你也可以選擇加入 contents,搭配 messages 提供額外的 Gemini 風格多模態上下文。

供應商特定說明

欄位類型必填說明
modelstring圖像生成模型識別碼,例如 gemini-2.0-flash-preview-image-generation
messages[].contentstring描述目標圖像的 prompt 文字。
contentsarray額外多模態上下文的 Gemini 風格內容陣列,並非標準 OpenAI 對話結構的一部分。
contents 欄位是 DGrid 疊加在標準 chat completions 結構上的延伸。當目標模型支援時,它可讓你在 OpenAI 風格的 messages 陣列之外,再傳送 Gemini 原生的 parts(例如 inlineData)。

範例:請求圖像

{
  "model": "gemini-2.0-flash-preview-image-generation",
  "stream": false,
  "messages": [
    {
      "role": "user",
      "content": "Generate an image of a futuristic city skyline at sunset."
    }
  ]
}

回應欄位

回應遵循標準 chat completions 結構。生成圖像會直接嵌入在 assistant 訊息內容中:
choices
array
回傳的 choice 條目。
usage
object
Token 使用摘要。

回應範例

200
{
  "id": "chatcmpl-abc123",
  "model": "gemini-2.0-flash-preview-image-generation",
  "object": "chat.completion",
  "created": 1719859200,
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "![generated image](data:image/png;base64,<base64-encoded-image-bytes>)"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 0,
    "total_tokens": 12
  }
}

授權

Authorization
string
header
必填

Your DGrid API key. All endpoints use Authorization: Bearer <DGRID_API_KEY>.

主體

application/json
model
string
必填

Target model ID in provider/model-name format.

範例:

"openai/gpt-4o"

messages
object[]
必填

Conversation message list.

temperature
number
預設值:1

Sampling temperature.

top_p
number
預設值:1

Nucleus sampling value.

n
integer
預設值:1

Number of choices to generate.

stream
boolean
預設值:false

Enable SSE streaming.

max_tokens
integer

Maximum token count.

max_completion_tokens
integer

Max completion-only tokens.

presence_penalty
number
預設值:0

Presence penalty.

frequency_penalty
number
預設值:0

Frequency penalty.

logit_bias
object

Token bias configuration.

stop

Stop sequence string or array.

tools
object[]

Tool definitions.

tool_choice

Tool selection behavior. Defaults to auto.

response_format
object

Response schema or JSON mode config.

seed
integer

Deterministic seed.

user
string

End-user identifier.

回應

Chat completion result.

id
string

Completion identifier.

object
string

Always chat.completion.

created
integer

Creation timestamp.

model
string

Model that served the request.

choices
object[]

Returned choices.

usage
object

Token usage breakdown.

system_fingerprint
string