Chat API 快速開始
一個 API Key 即可呼叫聊天和語音模型。介面同步返回結果,模型名稱、呼叫 ID 與價格以即時目錄為準。
1. 建立金鑰並儲值
註冊或登入控制臺,在 API 金鑰 → 建立 API 金鑰 中建立金鑰,儲存僅顯示一次的完整金鑰,再到 錢包 → 儲值點數。無需訂閱也能使用。建立金鑰不會贈送點數,呼叫前需要有可用餘額。
金鑰僅儲存在服務端,詳見鑒權與限流。
本機付款測試
Stripe 測試支付只增加沙盒點數,不能支付真實模型呼叫。本地聯調時,管理員可在後臺為測試使用者的 API 餘額儲值。正式支付確認後會增加可用於 API 呼叫的點數。
2. 運行聊天和語音示例
將下方程式碼儲存為 chat.py,僅需 Python 3,無需安裝依賴。設置環境變量 GIRLLAB_API_KEY,然後運行 python chat.py。
export GIRLLAB_API_KEY='sk-your-key'
python3 chat.py$env:GIRLLAB_API_KEY = 'sk-your-key'
python chat.pyimport 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 文件列出了請求字段。