跳转到正文

聊天 API 快速开始 ​

OpenAI 兼容聊天

一个 API Key 即可调用聊天和语音模型。接口同步返回结果,模型名称、调用 ID 与价格以实时目录为准。

1. 创建密钥并充值 ​

注册或登录控制台,在 API 密钥 → 创建 API 密钥 中创建密钥,保存仅显示一次的完整密钥,再到 钱包 → 充值积分。无需订阅也能使用。创建密钥不会赠送积分,调用前需要有可用余额。

密钥仅保存在服务端,详见鉴权与限流。

本地支付测试

Stripe 测试支付只增加沙盒积分,不能支付真实模型调用。本地联调时,管理员可在后台为测试用户的 API 余额充值。正式支付确认后会增加可用于 API 调用的积分。

2. 运行聊天和语音示例 ​

将下方代码保存为 chat.py,仅需 Python 3,无需安装依赖。设置环境变量 GIRLLAB_API_KEY,然后运行 python chat.py。

bash
export GIRLLAB_API_KEY='sk-your-key'
python3 chat.py
powershell
$env:GIRLLAB_API_KEY = 'sk-your-key'
python chat.py
python
import base64
import json
import os
from pathlib import Path
from urllib.error import HTTPError
from urllib.request import Request, urlopen

BASE = os.environ.get("GIRLLAB_API_BASE", "https://api.coldstart.win").rstrip("/")
KEY = os.environ["GIRLLAB_API_KEY"]
MODEL = os.environ.get("GIRLLAB_CHAT_MODEL")
VOICE = os.environ.get("GIRLLAB_VOICE")

def api(path, body=None):
    request = Request(
        BASE + path,
        data=json.dumps(body).encode() if body is not None else None,
        headers={"Authorization": "Bearer " + KEY,
                 "Content-Type": "application/json"},
    )
    try:
        with urlopen(request, timeout=90) as response:
            return json.load(response)
    except HTTPError as error:
        raise SystemExit(f"HTTP {error.code}: {error.read().decode()}")

catalog = api("/v1/chat/catalog")
MODEL = MODEL or catalog["defaultModel"]
VOICE = VOICE or catalog["defaultVoice"]
model = next((m for m in catalog["models"] if m["id"] == MODEL), None)
voice = next((v for v in catalog["voices"] if v["id"] == VOICE), None)
if not model or not model["available"] or not voice or not voice["available"]:
    raise SystemExit("Choose an available model and voice from /v1/chat/catalog.")

reply = api("/v1/chat/completions", {
    "model": MODEL,
    "messages": [
        {"role": "system", "content": "You are a friendly assistant."},
        {"role": "user", "content": "Say hello in one short sentence."},
    ],
    "max_tokens": 64,
})
print(json.dumps(reply, ensure_ascii=False, indent=2))
# For another turn, append the assistant reply and the next user message to messages.
print(reply["choices"][0]["message"]["content"])

speech = api("/v1/chat/speech", {"text": "Hello.", "voice": VOICE})
header, encoded = speech["audio"].split(",", 1)
file = Path("speech.mp3")
file.write_bytes(base64.b64decode(encoded))
print("Saved", file, "— speech billing:", json.dumps(speech["billing"]))

示例发起一次文本聊天和一次语音合成,分别计费;终端显示聊天回复、token 用量与语音积分,并保存 MP3 文件。聊天实际积分请在 Usage history 中核对。API 地址为 https://api.coldstart.win,其他部署可通过 GIRLLAB_API_BASE 修改。

3. 切换模型 ​

将 GIRLLAB_CHAT_MODEL 改为 chat-creative 或 chat-fast 即可切换聊天模型。环境变量 GIRLLAB_VOICE 独立选择语音模型和音色:

下方列表展示后台当前配置的对外名称、调用 ID 和音色 ID。

实时模型目录 · 积分价格

正在加载模型目录…

GET /v1/chat/catalog 返回可用模型、全部音色 ID 和实时价格。聊天请求使用实时目录中的模型 ID,不要填写上游厂商的原始模型 ID。TTS 通过 voice 选择模型,不接收聊天的 model 参数。

文本聊天见 OpenAI 兼容聊天,语音与音色见语音与模型目录。

也可以在控制台的 聊天 中体验。交互式 API 文档列出了请求字段。

© 2026 GirlLab. 保留所有权利。