3 Commits

Author SHA1 Message Date
9abe55db31 fill nas agent api token 2026-05-09 19:29:58 +08:00
415c5b6474 add nas agent api guide 2026-05-09 19:16:39 +08:00
84ebdbd793 inline compose environment for container station 2026-05-09 19:08:21 +08:00
5 changed files with 170 additions and 26 deletions

View File

@@ -0,0 +1,140 @@
# API 图片绘制及修改 Agent 调用说明 NAS 版
本文件用于部署在 NAS 上的 Gemini Draw。默认 NAS 服务地址:
- 前端页面:`http://192.168.31.5:4001`
- API 地址:`http://192.168.31.5:4000`
- 健康检查:`http://192.168.31.5:4000/api/health`
NAS 专用包 `Gemini_Draw_nas_gemini-drawpicture.zip` 已适配路径:
```txt
/share/Container/gemini-drawpicture
```
Container Station 创建应用时不要再使用相对环境文件路径。NAS 版 `docker_compose_Nas.yaml` 已把变量写在 `environment` 中,避免出现 `/tmp/.env.local not found`
## 必须携带 API 访问密钥
所有受保护接口都需要 API 访问密钥。这个密钥不是 Gemini API Key而是本服务自己的调用密钥。
调用时二选一携带:
```txt
Authorization: Bearer 5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978
```
或:
```txt
x-api-key: 5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978
```
## Gemini API Key
Gemini API Key 可以通过三种方式提供:
1. 已在 NAS 版 `docker_compose_Nas.yaml``environment.GEMINI_API_KEY` 中配置。
2. 单次请求 Header`x-gemini-api-key: YOUR_GEMINI_API_KEY`
3. 单次请求 JSON/Form 字段:`apiKey=YOUR_GEMINI_API_KEY`
运行中更新服务端 Gemini Key
```bash
curl -X POST http://192.168.31.5:4000/api/config/api-key \
-H "Content-Type: application/json" \
-H "Authorization: Bearer 5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978" \
-d "{\"apiKey\":\"YOUR_GEMINI_API_KEY\",\"persist\":false}"
```
NAS 的 Container Station 场景建议把固定 Key 写在 `docker_compose_Nas.yaml``environment` 中,然后重建容器;`persist:true` 主要用于普通本地运行。
## 健康检查
```bash
curl http://192.168.31.5:4000/api/health
```
返回示例:
```json
{
"ok": true,
"apiPort": 3002,
"hasGeminiApiKey": true,
"authEnabled": true,
"authRequired": true,
"acceptsPerRequestApiKey": true
}
```
## 纯文字绘制图片
```bash
curl -X POST http://192.168.31.5:4000/api/generate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer 5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978" \
-d "{\"prompt\":\"画一个蓝天白云下的极简风景插画\",\"imageSize\":\"1K\",\"aspectRatio\":\"1:1\"}"
```
PowerShell 保存返回图片:
```powershell
$token='5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978'; $body=@{prompt='画一个蓝天白云下的极简风景插画'; imageSize='1K'; aspectRatio='1:1'} | ConvertTo-Json -Depth 5; $r=Invoke-RestMethod -Uri 'http://192.168.31.5:4000/api/generate' -Method Post -Headers @{Authorization="Bearer $token"} -ContentType 'application/json; charset=utf-8' -Body $body; [IO.File]::WriteAllBytes((Join-Path (Get-Location) 'nas-output.png'), [Convert]::FromBase64String($r.images[0].data))
```
## 修改已有图片
```bash
curl -X POST http://192.168.31.5:4000/api/edit-image \
-H "Authorization: Bearer 5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978" \
-F "prompt=保留主体不变,把背景改成干净的白色摄影棚,增强产品质感" \
-F "imageSize=1K" \
-F "aspectRatio=1:1" \
-F "files=@input.png"
```
## 上传文档分析
```bash
curl -X POST http://192.168.31.5:4000/api/analyze-document \
-H "Authorization: Bearer 5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978" \
-F "prompt=用中文总结这份文档,提取关键结论和待办事项" \
-F "files=@report.pdf"
```
## JSON/Base64 方式修改图片
```bash
curl -X POST http://192.168.31.5:4000/api/generate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer 5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978" \
-d "{
\"prompt\":\"把这张图改成赛博朋克夜景风格,但保留人物脸部特征\",
\"imageSize\":\"1K\",
\"aspectRatio\":\"1:1\",
\"images\":[
{
\"mimeType\":\"image/png\",
\"base64\":\"BASE64_IMAGE_DATA\"
}
]
}"
```
## 常见错误
- `Unauthorized. Send Authorization: Bearer YOUR_API_AUTH_TOKEN or x-api-key.`:缺少 API 访问密钥。
- `Gemini API key is required.`:没有配置 Gemini API Key。
- `prompt or instruction is required.`:缺少绘制或修改指令。
- `Unsupported MIME type`:上传文件类型不被模型支持,建议使用 PNG/JPEG/WebP/PDF/TXT/MD。
## Agent 调用建议
1. NAS 固定 API 地址使用 `http://192.168.31.5:4000`
2. 每次调用都必须携带 `Authorization: Bearer 5065a4dbb20509600252e6c6a3e9ef075de1d81e0dde19d40800fbbad402d978``x-api-key`
3. 绘制新图用 `POST /api/generate`
4. 修改已有图片用 `POST /api/edit-image`
5. 文档分析用 `POST /api/analyze-document`
6. 返回图片使用 `images[0].dataUrl` 预览,或解码 `images[0].data` 保存为文件。

View File

@@ -101,11 +101,14 @@ curl -X POST http://localhost:3002/api/analyze-document \
API auth is required by default. For local-only development, you can set `API_AUTH_DISABLED=true`, but do not use that on a LAN or server.
For Agent-facing image generation and editing instructions, see `API图片绘制及修改-Agent.md`.
For Agent-facing image generation and editing instructions, see:
- `API图片绘制及修改-Agent.md`: generic/local API usage.
- `API图片绘制及修改-Agent-NAS.md`: NAS usage with `192.168.31.5:4000`.
## Docker Compose Deploy
Create `.env.local` first:
For Docker Compose deployment, edit `docker_compose.yaml` and fill the `environment` values:
```env
GEMINI_API_KEY="YOUR_GEMINI_API_KEY"

View File

@@ -6,11 +6,13 @@ services:
container_name: gemini-draw
init: true
restart: unless-stopped
env_file:
- .env.local
environment:
GEMINI_API_KEY: "CHANGE_ME_TO_YOUR_GEMINI_API_KEY"
API_AUTH_TOKEN: "CHANGE_ME_TO_A_LONG_RANDOM_TOKEN"
API_PORT: "3002"
API_AUTH_DISABLED: "false"
APP_PASSWORD: "123456"
APP_URL: "http://localhost:3000"
ports:
- "3000:3000"
- "3002:3002"

View File

@@ -8,17 +8,11 @@ services:
# If your project is in a different folder, update `build.context` below
# or run compose from the project folder and change it to `.`.
#
# First-time setup on NAS:
# First-time setup on NAS source folder:
# cd /share/Container/gemini-drawpicture
# cp .env.example .env.local
# vi .env.local
#
# Required values in .env.local:
# GEMINI_API_KEY="your_gemini_api_key"
# API_AUTH_TOKEN="a_long_random_api_access_token"
# API_AUTH_DISABLED="false"
# APP_PASSWORD="your_web_login_password"
# APP_URL="http://192.168.31.5:4001"
# Container Station validates compose files from /tmp, so do not use
# relative environment-file paths here. Fill values directly below.
#
# Build and start:
# cd /share/Container/gemini-drawpicture
@@ -40,12 +34,14 @@ services:
container_name: gemini-drawpicture
init: true
restart: unless-stopped
env_file:
- .env.local
environment:
# Keep API auth enabled on NAS. Use API_AUTH_TOKEN from .env.local.
# Required business settings.
GEMINI_API_KEY: "CHANGE_ME_TO_YOUR_GEMINI_API_KEY"
API_AUTH_TOKEN: "CHANGE_ME_TO_A_LONG_RANDOM_TOKEN"
API_AUTH_DISABLED: "false"
API_PORT: "3002"
APP_PASSWORD: "CHANGE_ME_TO_YOUR_WEB_PASSWORD"
APP_URL: "http://192.168.31.5:4001"
# Optional proxy settings. Keep them if NAS needs OpenClash/proxy
# to reach Google/Gemini; otherwise comment these lines out.

View File

@@ -12,7 +12,7 @@
## 必须修改的配置
在目标机器项目根目录创建 `.env.local`
如果使用 `npm run dev` / `npm run api` 本地运行,在目标机器项目根目录创建 `.env.local`
```env
GEMINI_API_KEY="你的_Gemini_API_Key"
@@ -32,6 +32,8 @@ APP_URL="http://目标机器IP:3000"
- `APP_PASSWORD`:网页登录密码,默认用户名是 `admin`
- `APP_URL`:前端访问地址,迁移到 NAS 时建议写成 `http://NAS_IP:3000`
如果使用 Docker Compose尤其是在 Container Station 里粘贴 YAML 创建应用,请直接修改 `docker_compose.yaml``docker_compose_Nas.yaml` 里的 `environment` 字段。Container Station 会从 `/tmp` 校验 YAML相对环境文件路径容易被解析到 `/tmp` 并失败,所以当前 compose 文件把必要变量直接写在 `environment` 中。
生成随机 `API_AUTH_TOKEN` 的示例:
```bash
@@ -168,15 +170,16 @@ cd /share/Container/gemini-drawpicture
docker compose -f docker_compose_Nas.yaml up -d --build
```
`.env.local` 示例:
`docker_compose_Nas.yaml``environment` 示例:
```env
GEMINI_API_KEY="你的_Gemini_API_Key"
API_AUTH_TOKEN="你的_API_访问密钥"
API_AUTH_DISABLED="false"
API_PORT="3002"
APP_PASSWORD="你的网页登录密码"
APP_URL="http://192.168.31.5:3000"
```yaml
environment:
GEMINI_API_KEY: "你的_Gemini_API_Key"
API_AUTH_TOKEN: "你的_API_访问密钥"
API_AUTH_DISABLED: "false"
API_PORT: "3002"
APP_PASSWORD: "你的网页登录密码"
APP_URL: "http://192.168.31.5:4001"
```
如果 NAS 上 `3000``3002` 已被占用,就只改 `docker_compose.yaml` 左侧宿主机端口:
@@ -227,7 +230,7 @@ GEMINI_API_KEY="你的_Gemini_API_Key"
x-gemini-api-key: 你的_Gemini_API_Key
```
### 修改 `.env.local` 后没有生效
### 修改环境变量后没有生效
重启容器: