AI - gtp-image-2使用(openrouter)
AI - gtp-image-2使用(openrouter)
gtp-image-2使用(openrouter)
参考文档:https://openrouter.ai/docs/client-sdks/go/api-reference/videogeneration
The Go SDK and docs are currently in beta. Report issues on GitHub.
https://openrouter.ai/models?q=gpt&output_modalities=image
因为sdk是beta测试版,干脆按照openai原协议进行对接生图。
两种方案例子:
- https://openrouter.ai/api/v1/chat/completions
- https://openrouter.ai/api/v1/responses
对话模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
POST https://openrouter.ai/api/v1/chat/completions
Authorization: Bearer
Content-Type: application/json
{
"model": "openai/gpt-5.4-image-2",
"messages": [
{
"role": "user",
"content": [
{
"type":"text",
"text":"a b c d e fg "
},
{
"type":"image_url",
"image_url":"https://www.360club.net/pic/124/jpg/1.jpg"
}
]
}
],
"modalities": [
"image",
"text"
],
"image_config": {
"quality": "high",
"aspect_ratio": "16:9",
"image_size": "2K" // [可选] 控制图像分辨率,如 "1K", "2K", "4K"
}
}
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{
"id": "gen-1778815970-WfDVKNLvEAn8BEWfkHIA",
"object": "chat.completion",
"created": 1778815970,
"model": "openai/gpt-5.4-image-2-20260421",
"provider": "OpenAI",
"system_fingerprint": null,
"service_tier": "default",
"choices": [{
"index": 0,
"logprobs": null,
"finish_reason": "stop",
"native_finish_reason": "completed",
"message": {
"role": "assistant",
"content": null,
"refusal": null,
"reasoning": null,
"images": [{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,"
}
}]
}
}],
"usage": {
"prompt_tokens": 1635,
"completion_tokens": 7067,
"total_tokens": 8702,
"cost": 0.224445,
"is_byok": false,
"prompt_tokens_details": {
"cached_tokens": 0,
"cache_write_tokens": 0,
"audio_tokens": 0,
"video_tokens": 0
},
"cost_details": {
"upstream_inference_cost": 0.224445,
"upstream_inference_prompt_cost": 0.01308,
"upstream_inference_completions_cost": 0.211365
},
"completion_tokens_details": {
"reasoning_tokens": 0,
"image_tokens": 7024,
"audio_tokens": 0
}
}
}
续写模式
https://developers.openai.com/api/reference/resources/responses/methods/create
参考文档的一些枚举值,确保可以请求通过。
模型名映射关系:
- openai/gpt-5.4-image-2 => openai/gpt-5.4-image-2-20260421
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
POST https://openrouter.ai/api/v1/responses
Authorization: Bearer
Content-Type: application/json
{
"model" : "gpt-5.4",
"input" : [
{
"role" : "user",
"content" : [{
"type" : "input_text",
"text" : "Theme and Style: Create a set of high-quality 3D cute cartoon avatars (personal portraits) based on characters in reference images. The aesthetic style is :Memoji style in Apple's iOS system.\n\nCharacters and Expressions: Highly accurate representation of the core features of the characters. The final product should include 9 different versions of the avatars, each displaying a unique, vibrant pose and distinct facial expression, presenting a lively and adorable image. Some avatars will carry props that match their emotions.\n\nComposition and Layout: Use a simple 3x3 grid layout, containing 9 independent avatars. All scenes are placed on a seamless pure white background. The key is to use only white space to separate the characters; there should be no visible grid lines, borders, frames, or boxes between characters.\n\nDetails and Styling: The 9 virtual characters will be randomly paired with some small accessories.\n\nImportant Visual Constraints: Grid lines, black lines, cell borders, borders, distorted facial features, and text are prohibited.\n And 1k resolution."
}, {
"type" : "input_image",
"image_url" : "https://www.360club.net/pic/124/jpg/1.jpg"
} ]
} ],
"tools" : [ {
"type" : "image_generation",
"action" : "auto",
"size" : "1024x1024",
"output_format" : "png"
} ]
}
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
"id": "gen-1778938920-zXWbSt7ffeet8ywZH4Vx",
"object": "response",
"created_at": 1778938920,
"model": "openai/gpt-5.4-20260305",
"status": "completed",
"completed_at": 1778939034,
"output": [{
"type": "openrouter:image_generation",
"id": "st_tmp_5bru8cqyzcw",
"status": "completed",
"result": "data:image/png;base64,=="
}, {
"id": "msg_tmp_ehp6pv4yq9",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [{
"type": "output_text",
"text": "Done.",
"annotations": [],
"logprobs": []
}]
}],
"error": null,
"incomplete_details": null,
"tools": [{
"type": "image_generation"
}],
"tool_choice": "auto",
"parallel_tool_calls": true,
"max_output_tokens": null,
"temperature": 1,
"top_p": 1,
"presence_penalty": 0,
"frequency_penalty": 0,
"top_logprobs": 0,
"max_tool_calls": null,
"metadata": {},
"background": false,
"previous_response_id": null,
"service_tier": "auto",
"truncation": "disabled",
"store": false,
"instructions": null,
"text": {
"format": {
"type": "text"
}
},
"reasoning": null,
"safety_identifier": null,
"prompt_cache_key": null,
"usage": {
"input_tokens": 3932,
"input_tokens_details": {
"cached_tokens": 0
},
"output_tokens": 253,
"output_tokens_details": {
"reasoning_tokens": 0
},
"total_tokens": 4185,
"cost": 0.013625,
"cost_details": {
"upstream_inference_cost": 0.013625,
"upstream_inference_input_cost": 0.00983,
"upstream_inference_output_cost": 0.0037949999999999998
}
}
}
结论
使用对话模式时,需要使用返回的image字段,如果使用golang的openai的库:github.com/openai/openai-go/v3时,是没有这个参数接收的(也就是内部接收数据不全,需要自己实现)– openrouter官方建议对话模式。
使用续写模式时,参数的model却不能跟openrouter一致,必须用openai的,这样程序上就需要兼容或者使用两套来对比效果。
本文由作者按照 CC BY 4.0 进行授权