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 NameRequiredTypeExample ValueDescription
Content-TypeYesStringapplication/jsonFormat of the request body.
AcceptYesStringapplication/jsonExpected response format.
AuthorizationNoStringBearer {{YOUR_API_KEY}}API Key used for authentication.

Request Body

The request body is a JSON object containing the following fields:

Parameter NameTypeRequiredDefaultDescription
modelStringYes-The ID of the model to use (e.g., gpt-4o-image-vip).
messagesArrayYes-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.
toolsArrayYes-A list of tools the model may call. Currently only functions are supported.
tool_choiceObjectYesnoneControls which tool is called by the model. none means no call, auto means automatic selection.
temperatureNumberNo1Sampling temperature (0-2). Higher values are more random, lower values are more focused.
top_pNumberNo1Nucleus sampling. The model considers the results of the tokens with top_p probability mass.
nIntegerNo1Number of chat completion choices to generate for each input.
streamBooleanNofalseWhether to stream responses. This specific endpoint is defined as non-streaming.
max_tokensIntegerNoinfThe maximum number of tokens to generate.
stopString/ArrayNonullStop sequences. The API will stop generating further tokens if these are encountered.
presence_penaltyNumberNo0Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.
frequency_penaltyNumberNo0Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text.
response_formatObjectNo-Specifies the output format, e.g., { "type": "json_object" } to enable JSON mode.
userStringNo-A unique identifier representing your end-user, used to monitor abuse.
seedIntegerNo-(Beta) If specified, the system will make a best effort to sample deterministically.

Response Parameters

Parameter NameTypeDescription
idStringA unique identifier for the request.
objectStringThe object type, always chat.completion.
createdIntegerThe Unix timestamp (in seconds) of when the completion was created.
choicesArrayA list of chat completion choices.
└ indexIntegerThe index of the choice.
└ messageObjectA message object containing role and content.
└ finish_reasonStringThe reason the model stopped generating (e.g., stop, length).
usageObjectUsage statistics for the completion request.
└ prompt_tokensIntegerNumber of tokens in the prompt.
└ completion_tokensIntegerNumber of tokens in the generated completion.
└ total_tokensIntegerTotal 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".