++
This commit is contained in:
@@ -19,7 +19,7 @@ description: Generate images via AI text-to-image API (GPT Image / Grok). Use wh
|
||||
|------|------------|------------|------|
|
||||
| GPT / Grok 绘图 | `agent-image-t2i.py` | `POST /images/generations` | `gpt-image-2-1K` / `gpt-image-2-2K` / `gpt-image-2-4K` / `grok-imagine-image-lite` |
|
||||
| 健康检查 | `agent-image-t2i.py` | `GET /health` | — |
|
||||
| 图片访问 | `agent-image-t2i.py` | `GET /output/{filename}` | — |
|
||||
| 图片访问 | `agent-image-t2i.py` | `GET /output/{profile_name}/{filename}` | — |
|
||||
|
||||
## GPT 模型说明
|
||||
|
||||
@@ -56,7 +56,8 @@ curl -X POST http://localhost:8765/images/generations \
|
||||
"prompt": "a cute cat",
|
||||
"provider": "grok",
|
||||
"size": "1024x1024",
|
||||
"n": 1
|
||||
"n": 1,
|
||||
"profile_name": "your-agent-name"
|
||||
}'
|
||||
```
|
||||
|
||||
@@ -70,11 +71,12 @@ curl -X POST http://localhost:8765/images/generations \
|
||||
"provider": "gpt",
|
||||
"model": "gpt-image-2-1K",
|
||||
"size": "1536x1024",
|
||||
"n": 1
|
||||
"n": 1,
|
||||
"profile_name": "your-agent-name"
|
||||
}'
|
||||
```
|
||||
|
||||
生成结果中的 `url` 为图片访问地址,例如 `http://localhost:8765/output/{filename}`。
|
||||
生成结果中的 `url` 为图片访问地址,例如 `http://localhost:8765/output/{profile_name}/{filename}`。
|
||||
|
||||
## 调用建议
|
||||
|
||||
@@ -88,6 +90,7 @@ curl -X POST http://localhost:8765/images/generations \
|
||||
|
||||
请求体为 JSON,发送到 `POST /images/generations`:
|
||||
|
||||
- `profile_name`:**必填**,调用方 agent 的标识(建议用 agent 名称),不填会报 422 错误;图片会保存到 `output/<profile_name>/` 目录
|
||||
- `prompt`:必填,图片提示词
|
||||
- `provider`:可选,`grok` 或 `gpt`,默认 `grok`
|
||||
- `model`:可选,覆盖 provider 的默认模型
|
||||
@@ -96,10 +99,10 @@ curl -X POST http://localhost:8765/images/generations \
|
||||
|
||||
## 输出规范
|
||||
|
||||
- 图片默认保存在服务端 `output/` 目录
|
||||
- 图片保存在服务端 `output/<profile_name>/` 目录,按调用方 agent 区分
|
||||
- 响应返回 `filename`、`url` 和文件大小 `bytes`
|
||||
- 图片通过 `GET /output/{filename}` 访问
|
||||
- 服务启动后,响应中的相对路径 `url` 需要拼接服务地址,例如 `http://localhost:8765/output/{filename}`
|
||||
- 图片通过 `GET /output/{profile_name}/{filename}` 访问
|
||||
- 服务启动后,响应中的相对路径 `url` 需要拼接服务地址,例如 `http://localhost:8765/output/{profile_name}/{filename}`
|
||||
|
||||
## Prompt 技巧
|
||||
|
||||
|
||||
+8
-1
@@ -13,7 +13,7 @@ from dotenv import load_dotenv
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from openai import APIConnectionError, APIStatusError, APITimeoutError, OpenAI
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, Field, model_validator
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@@ -45,6 +45,13 @@ class ImageGenerationRequest(BaseModel):
|
||||
n: int = Field(default=1, ge=1, le=10, description="生成数量")
|
||||
profile_name: str = Field(pattern=r"^[\w-]+$", description="调用方标识(必填),图片会保存到 output/<profile_name>/ 下")
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def check_profile_name(cls, data):
|
||||
if isinstance(data, dict) and not data.get("profile_name"):
|
||||
raise ValueError("缺少 profile_name 参数,请在请求中带上你的 agent/profile 名称")
|
||||
return data
|
||||
|
||||
|
||||
class GeneratedImage(BaseModel):
|
||||
filename: str
|
||||
|
||||
Reference in New Issue
Block a user