创建聊天补全
/v1/chat/completions为给定对话创建聊天补全响应。
身份验证
Authorization Bearer
在 Authorization header 中使用 API key 作为 Bearer token。
Request Body
messagesarrayrequiredConversation messages.
messages.rolestringrequiredMessage role, such as system, user, assistant, or tool.
messages.contentstring | object[]requiredMessage content. Multimodal models may accept structured content parts.
messages.namestringOptional participant name.
messages.tool_call_idstringTool call ID for tool messages.
modelstringrequiredModel ID.
temperaturenumberControls randomness in generated output. Higher values make responses more varied; lower values make responses more deterministic.
top_pnumberControls nucleus sampling by limiting token choices to a cumulative probability mass. Lower values make output more focused; higher values allow more variety.
max_completion_tokensintegerMaximum number of tokens the model may generate in the response.
top_kintegerTop-k sampling cutoff.
frequency_penaltynumberFrequency-based repetition penalty.
presence_penaltynumberPresence-based novelty penalty.
context_lengthintegerContext window length.
streambooleanStreaming response flag.
stream_optionsobjectStreaming response configuration.
stream_options.include_usagebooleanInclude usage information in the streaming response.
toolsarrayTool definitions.
tools.typestringTool type, usually function.
tools.functionobjectFunction definition.
tool_choiceobjectTool selection policy.
tool_choice.typestringTool choice type, such as function.
tool_choice.functionobjectFunction selection payload.
parallel_tool_callsbooleanParallel tool-call flag.
previous_response_idstringPrevious response ID.
conversationobjectConversation state object or ID.
prompt_cache_keystringPrompt cache key.
storebooleanResponse storage flag.
truncationstringInput truncation strategy.
includearrayAdditional response fields to include.
metadataobjectApplication metadata.
extra_bodyobjectAdditional request body fields.
provider_optionsobjectUpstream configuration.
schema_paramsobjectStructured output schema parameters.
network_searchbooleanNetwork search flag.
reasoningobjectCanonical reasoning/thinking control configuration.
reasoning.enabledbooleanEnable reasoning when the selected model supports it.
reasoning.effortstringReasoning effort. Supports xhigh, high, medium, low, minimal, and none.
reasoning.max_tokensintegerMaximum reasoning tokens. Used for execution when supplied with effort.
reasoning.excludebooleanReason internally without returning reasoning text.
response_formatobjectRequested response format for structured model output.
logprobsbooleanWhether to return log probabilities for generated output tokens.
top_logprobsintegerNumber of most likely tokens to return at each generated token position; requires logprobs=true.
stoparrayStop sequences that terminate generation; the verified contract uses an array of up to four strings.
verbositystringControls the level of detail in the model response.
seedintegerBest-effort sampling seed for reproducible outputs.
nintegerNumber of response choices to generate.
logit_biasobjectAdjusts the likelihood of specified token IDs.
响应格式
response_format 用于控制助手的输出格式:
{"type":"text"}返回普通文本。{"type":"json_object"}返回有效的 JSON 对象。{"type":"json_schema","json_schema":{...}}返回符合所提供 schema 的 JSON。json_schema对象包含name、可选的strict和schema字段。
如果结构化响应被拒绝,choices[].message.refusal 将包含拒绝信息,而 choices[].message.content 可能为 null。
Response
idstring唯一补全 ID。
objectstring非流式响应中为 chat.completion。
createdintegerUnix 时间戳。
modelstring本次请求使用的模型。
choicesobject[]生成结果。
choices.indexinteger结果下标。
choices.messageobjectassistant 消息。
choices.logprobsobject | null生成 token 的对数概率信息。未请求对数概率时为 null。
choices.finish_reasonstringstop、length 或 tool_calls。
usageobjectToken 用量和费用信息。
usage.prompt_tokensintegerInput token count when returned.
usage.completion_tokensintegerOutput token count when returned.
usage.total_tokensintegerTotal token count when returned.
usage.input_tokensintegerInput token count for providers that use input/output names.
usage.output_tokensintegerOutput token count for providers that use input/output names.
usage.generated_imagesintegerGenerated image count when returned.
usage.audio_duration_secondsnumberAudio duration used for billing when returned.
usage.video_duration_secondsnumberVideo duration used for billing when returned.
usage.provider_request_idstringProvider request ID used for upstream correlation.