API 接入
使用现有 SDK 连接 Kangjian AI:配置网关地址,使用 API Key 认证,并选择模型支持的接口协议。
服务地址与认证
| 客户端 / 协议 | Base URL / 接口 | 认证方式 |
|---|---|---|
| OpenAI SDK | https://api.kangjianai.cn/v1 | Authorization: Bearer YOUR_API_KEY |
| Chat Completions | POST /v1/chat/completions | Bearer 令牌 |
| Responses | POST /v1/responses | Bearer 令牌 |
| Anthropic SDK | https://api.kangjianai.cn | x-api-key: YOUR_API_KEY |
| Messages | POST /v1/messages | x-api-key 与 anthropic-version |
准备运行环境
macOS / Linux / WSL
export GATEWAY_KEY="YOUR_API_KEY"
export BASE_URL="https://api.kangjianai.cn/v1"
export MODEL="YOUR_MODEL_ID"上方均为示例值。只在自己的本地环境或密钥管理工具中替换 YOUR_API_KEY,不要将真实 Key 写入本文档、源代码或浏览器构建产物。
Chat Completions 接口
先使用支持 chat.completions 的模型。以下三种语言示例发起的是相同的非流式请求。
cURL
curl --fail-with-body "https://api.kangjianai.cn/v1/chat/completions" \
-H "Authorization: Bearer $GATEWAY_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL_ID",
"messages": [{"role": "user", "content": "Say hello in one sentence."}]
}'Python
# Install: python -m pip install openai
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["GATEWAY_KEY"],
base_url="https://api.kangjianai.cn/v1",
)
response = client.chat.completions.create(
model="YOUR_MODEL_ID",
messages=[{"role": "user", "content": "Say hello in one sentence."}],
)
print(response.choices[0].message.content)JavaScript
// Install: npm install openai
// Save as hello.mjs, then run: node hello.mjs
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.GATEWAY_KEY,
baseURL: 'https://api.kangjianai.cn/v1',
});
const response = await client.chat.completions.create({
model: 'YOUR_MODEL_ID',
messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
});
console.log(response.choices[0].message.content);预期返回 HTTP 200 和 choices 数组,通过 choices[0].message.content 读取回复文本。Token 用量取决于上游是否返回该信息。
Responses 接口
cURL
curl --fail-with-body "https://api.kangjianai.cn/v1/responses" \
-H "Authorization: Bearer $GATEWAY_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "YOUR_MODEL_ID", "input": "Say hello in one sentence."}'Python
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["GATEWAY_KEY"], base_url="https://api.kangjianai.cn/v1")
response = client.responses.create(model="YOUR_MODEL_ID", input="Say hello in one sentence.")
print(response.output_text)Anthropic Messages 接口
请选择支持 Messages 的模型。Anthropic SDK 会自行追加 /v1/messages,因此 Base URL 使用不带 /v1 的网关根地址。
cURL
curl --fail-with-body "https://api.kangjianai.cn/v1/messages" \
-H "x-api-key: $GATEWAY_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL_ID",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Say hello in one sentence."}]
}'Python
# Install: python -m pip install anthropic
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["GATEWAY_KEY"], base_url="https://api.kangjianai.cn")
response = client.messages.create(
model="YOUR_MODEL_ID",
max_tokens=1024,
messages=[{"role": "user", "content": "Say hello in one sentence."}],
)
for block in response.content:
if block.type == "text":
print(block.text)