Skip to content

快速开始 ​

欢迎使用 GWLLM 统一 API 网关!GWLLM 完全兼容 OpenAI 官方接口协议。您无需学习新 SDK,只需将原有的请求地址和 Key 替换为 GWLLM 提供的凭证,即可直接使用。


核心配置参数 ​

在开始接入前,请准备好以下两项关键信息:

参数项说明示例值
API Base URL接口请求基础地址https://api.gwllm.com/v1 (或控制台展示的地址)
API Key (令牌)您的身份认证密钥sk-xxxxxxxxxxxxxxxxxxxxxxxx

获取凭证

  1. 登录 GWLLM 控制台;
  2. 进入「令牌」页面,点击「添加令牌」;
  3. 设置名称、额度后生成您的 API Key。

代码接入示例 ​

1. Python (推荐使用官方 openai SDK) ​

确保已安装官方库:pip install openai

python
from openai import OpenAI

# 1. 初始化客户端,指定 GWLLM 的 base_url 与 api_key
client = OpenAI(
    api_key="sk-您的GWLLM令牌",
    base_url="https://api.gwllm.com/v1"  # 务必带上 /v1
)

# 2. 发起对话请求 (流式输出示例)
response = client.chat.completions.create(
    model="gpt-4o",  # 或 deepseek-chat, claude-3-5-sonnet 等
    messages=[
        {"role": "system", "content": "你是一位优秀的专业技术助理。"},
        {"role": "user", "content": "请用一句话介绍什么是 API 网关。"}
    ],
    stream=True
)

# 3. 流式打印回复
for chunk in response:
    content = chunk.choices[0].delta.content or ""
    print(content, end="", flush=True)
print()

2. Node.js / TypeScript (使用官方 openai 包) ​

确保已安装官方库:npm install openai

typescript
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: 'sk-您的GWLLM令牌',
  baseURL: 'https://api.gwllm.com/v1',
});

async function main() {
  const stream = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: '你好!' }],
    stream: true,
  });

  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta?.content || '');
  }
}

main();

3. cURL (命令行测试) ​

您可以在任何终端中运行以下命令,快速验证接口连通性:

bash
curl https://api.gwllm.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-您的GWLLM令牌" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": "Hello!"
      }
    ],
    "stream": false
  }'

基于 OpenAI 兼容协议构建 · 高可用 AI 网关