# 知格科技 API 文档

通过统一的 API 接入对话、图片与视频生成，可用于应用开发及智能体工具对接。

- 文档地址：[API 文档](https://zhigekeji.com/docs)
- Base URL：`https://zhigekeji.com/api/v1`
- 认证方式：`Authorization: Bearer <API_KEY>`
- JSON 请求使用 `Content-Type: application/json`
- 在[控制台创建 API Key](https://zhigekeji.com/console/api-keys)，示例中的密钥和素材地址均需自行替换。

## OpenAI SDK 接入

对话和图片生成接口兼容 OpenAI SDK，只需修改 `base_url` 和 `api_key`。视频生成、状态查询和取消是扩展接口，请按上方 HTTP 示例调用。

### Python（openai 库）

安装依赖：`pip install openai`。先设置环境变量 `ZHIGE_API_KEY`，并将示例模型名称替换为模型列表中实际可用的 `name`。

```python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["ZHIGE_API_KEY"],
    base_url="https://zhigekeji.com/api/v1"
)

# LLM 对话补全
response = client.chat.completions.create(
    model="Qwen3-27B",
    messages=[
        {"role": "user", "content": "你好，请介绍一下自己"}
    ]
)
print(response.choices[0].message.content)

# 多模态对话（带参考图）
response = client.chat.completions.create(
    model="Qwen3-27B",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "请描述这张图片"},
                {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
            ]
        }
    ]
)
print(response.choices[0].message.content)

# 文生图
response = client.images.generate(
    model="ernie-image",
    prompt="一个穿着棒球服的少女，树下",
    size="768x1344"
)
print(response.data[0].url)
```

### Node.js（openai 库）

安装依赖：`npm install openai`。使用支持 ES Module 的 Node.js 项目，并在运行前设置环境变量 `ZHIGE_API_KEY`。

```javascript
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.ZHIGE_API_KEY,
  baseURL: 'https://zhigekeji.com/api/v1',
});

// LLM 对话补全
const chatResponse = await client.chat.completions.create({
  model: 'Qwen3-27B',
  messages: [
    { role: 'user', content: '你好，请介绍一下自己' }
  ],
});
console.log(chatResponse.choices[0].message.content);

// 多模态对话（带参考图）
const visionResponse = await client.chat.completions.create({
  model: 'Qwen3-27B',
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: '请描述这张图片' },
        { type: 'image_url', image_url: { url: 'https://example.com/photo.jpg' } }
      ]
    }
  ],
});
console.log(visionResponse.choices[0].message.content);

// 图片生成
const imageResponse = await client.images.generate({
  model: 'ernie-image',
  prompt: '一个穿着棒球服的少女，树下',
  size: '768x1344',
});
console.log(imageResponse.data[0].url);
```

### cURL

```bash
# LLM 对话补全
curl https://zhigekeji.com/api/v1/chat/completions \
  -H "Authorization: Bearer sk-xxxx..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "Qwen3-27B",
    "messages": [{"role": "user", "content": "你好，请介绍一下自己"}]
  }'

# 多模态对话（带参考图）
curl https://zhigekeji.com/api/v1/chat/completions \
  -H "Authorization: Bearer sk-xxxx..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "Qwen3-27B",
    "messages": [{"role": "user", "content": [
      {"type": "text", "text": "请描述这张图片"},
      {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
    ]}]
  }'

# 图片生成
curl https://zhigekeji.com/api/v1/images/generations \
  -H "Authorization: Bearer sk-xxxx..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "ernie-image",
    "prompt": "一个穿着棒球服的少女，树下",
    "size": "768x1344"
  }'

# 异步视频生成（H3 Turbo 首尾帧）
curl https://zhigekeji.com/api/v1/videos/generations/async \
  -H "Authorization: Bearer sk-xxxx..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "minimax-h3-turbo-frames",
    "prompt": "人物从室内走到阳光下，镜头平稳跟随",
    "images": [
      "https://example.com/first-frame.png",
      "https://example.com/last-frame.png"
    ],
    "duration": 5,
    "size": "1344x768",
    "aspect_ratio": "16:9"
  }'
```
