跳转到正文

发起第一个请求 ​

请使用控制台中创建的 API 密钥。登录密码、邮箱验证码和邀请码都不能用作 API 调用凭据。

设置环境变量 ​

将密钥保存在你的服务端,不要提交到代码仓库。托管服务的基础地址如下:

sh
export STREAMINGTELLIGENCE_BASE_URL="https://streamingtelligence.com/v1"
export STREAMINGTELLIGENCE_API_KEY="YOUR_API_KEY"
export STREAMINGTELLIGENCE_MODEL="MODEL_ALLOWED_BY_YOUR_INVITATION"

模型标识符请以控制台显示的内容为准。可用模型取决于邀请码授权范围和已连接的上游服务;示例名称不代表访问承诺。

发送流式请求 ​

sh
curl --no-buffer "$STREAMINGTELLIGENCE_BASE_URL/chat/completions" \
  -H "Authorization: Bearer $STREAMINGTELLIGENCE_API_KEY" \
  -H "Content-Type: application/json" \
  -d @- <<JSON
{
  "model": "${STREAMINGTELLIGENCE_MODEL}",
  "messages": [
    {"role": "user", "content": "Describe this moment."}
  ],
  "stream": true
}
JSON

流式响应的内容类型为 text/event-stream。每条 data: 消息包含一个 JSON 数据块,[DONE] 表示输出结束。SDK 示例和取消请求的处理方式见流式响应。

使用 Python 客户端 ​

安装 OpenAI 官方 Python 客户端,并将其指向托管服务地址:

sh
pip install openai
python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["STREAMINGTELLIGENCE_API_KEY"],
    base_url=os.environ["STREAMINGTELLIGENCE_BASE_URL"],
)

with client.chat.completions.create(
    model=os.environ["STREAMINGTELLIGENCE_MODEL"],
    messages=[{"role": "user", "content": "Describe this moment."}],
    stream=True,
) as stream:
    for chunk in stream:
        if chunk.choices:
            print(chunk.choices[0].delta.content or "", end="", flush=True)

后续步骤 ​

StreamingTelligence Lab