Create Chat Completion with Image (Non-streaming)
This endpoint is used to send a prompt (containing text or images) to the model, which then returns one or more generated responses. It is specifically designed for models supporting multimodal input (such as image processing, enhancement, and vision-based creation), such as gpt-4o-image-vip.
Endpoint Information
- URL:
https://api.codingplanx.ai/v1/chat/completions - Method:
POST - Content-Type:
application/json - Response Type:
application/json
Request Headers
| Parameter Name | Required | Type | Example Value | Description |
|---|---|---|---|---|
| Content-Type | Yes | String | application/json | Format of the request body. |
| Accept | Yes | String | application/json | Expected response format. |
| Authorization | No | String | Bearer {{YOUR_API_KEY}} | API Key used for authentication. |
Request Body
The request body is a JSON object containing the following fields:
| Parameter Name | Type | Required | Default | Description |
|---|---|---|---|---|
| model | String | Yes | - | The ID of the model to use (e.g., gpt-4o-image-vip). |
| messages | Array | Yes | - | A list of messages comprising the conversation. Each object contains role (user/assistant/system) and content. content can be a string or an array of objects containing types and image URLs. |
| tools | Array | Yes | - | A list of tools the model may call. Currently only functions are supported. |
| tool_choice | Object | Yes | none | Controls which tool is called by the model. none means no call, auto means automatic selection. |
| temperature | Number | No | 1 | Sampling temperature (0-2). Higher values are more random, lower values are more focused. |
| top_p | Number | No | 1 | Nucleus sampling. The model considers the results of the tokens with top_p probability mass. |
| n | Integer | No | 1 | Number of chat completion choices to generate for each input. |
| stream | Boolean | No | false | Whether to stream responses. This specific endpoint is defined as non-streaming. |
| max_tokens | Integer | No | inf | The maximum number of tokens to generate. |
| stop | String/Array | No | null | Stop sequences. The API will stop generating further tokens if these are encountered. |
| presence_penalty | Number | No | 0 | Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far. |
| frequency_penalty | Number | No | 0 | Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text. |
| response_format | Object | No | - | Specifies the output format, e.g., { "type": "json_object" } to enable JSON mode. |
| user | String | No | - | A unique identifier representing your end-user, used to monitor abuse. |
| seed | Integer | No | - | (Beta) If specified, the system will make a best effort to sample deterministically. |
Response Parameters
| Parameter Name | Type | Description |
|---|---|---|
| id | String | A unique identifier for the request. |
| object | String | The object type, always chat.completion. |
| created | Integer | The Unix timestamp (in seconds) of when the completion was created. |
| choices | Array | A list of chat completion choices. |
| └ index | Integer | The index of the choice. |
| └ message | Object | A message object containing role and content. |
| └ finish_reason | String | The reason the model stopped generating (e.g., stop, length). |
| usage | Object | Usage statistics for the completion request. |
| └ prompt_tokens | Integer | Number of tokens in the prompt. |
| └ completion_tokens | Integer | Number of tokens in the generated completion. |
| └ total_tokens | Integer | Total number of tokens used in the request (prompt + completion). |
Examples
Request Example (Image Enhancement/Creation)
{
"model": "gpt-4o-image-vip",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Beautify this image, add the text 'I Love China', and set the aspect ratio to [4:3]"},
{
"type": "image_url",
"image_url": {
"url": "https://lsky.zhongzhuan.chat/i/2024/10/17/6711068a14527.png"
}
}
]
}
],
"tools": [],
"tool_choice": "none"
}
Response Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Sure, I have processed the image for you. You can view the generated result here: [Image Link/Description]"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
FAQs
Q: Why does the request fail when using response_format: { "type": "json_object" }?
A: When using JSON mode, you must explicitly instruct the model to generate JSON in your messages (e.g., by including "Please respond in JSON format" in the system prompt). Otherwise, the model may produce non-JSON content, leading to errors or infinite loops.
Q: How can I ensure consistent results across requests?
A: While AI models are inherently probabilistic, you can try setting the seed parameter and keeping temperature at 0. Additionally, monitor the system_fingerprint field in the response to track if any backend changes have occurred.
Q: What is the recommended value for max_tokens?
A: This depends on your use case. For short image descriptions, 100-300 tokens are usually sufficient. For detailed analysis or creative writing, we recommend 1000 or more. Note that the sum of input tokens (including images) and output tokens cannot exceed the model's context limit.
Q: How do I handle image inputs?
A: Use type: "image_url" within the content array of the messages. We currently recommend using publicly accessible HTTPS image links.
Q: Are tools and tool_choice mandatory?
A: According to this interface definition, these two fields are marked as required. If you do not need to call specific functions, please set tools to an empty array [] and tool_choice to "none".