跳轉到主要內容

📋 前置要求

在開始之前,您需要:
  • 一個 Kaihoxz 帳戶
  • API 金鑰(在 Dashboard 中取得)
  • 基礎的程式設計知識

1️⃣ 取得 API 金鑰

1

登入控制台

造訪 Kaihoxz Dashboard 並登入
2

生成金鑰

在 API 金鑰管理頁面點擊「建立新金鑰」
3

儲存金鑰

複製並安全儲存您的 API 金鑰
安全提示: 請勿在公開程式碼或客戶端中暴露您的 API 金鑰

2️⃣ 發送第一個請求

使用 cURL

curl https://ai.kaiho.cc/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": "你好,介紹一下你自己"
      }
    ]
  }'

使用 Python

import openai

client = openai.OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://ai.kaiho.cc/v1"
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "你好,介紹一下你自己"}
    ]
)

print(response.choices[0].message.content)

使用 JavaScript/Node.js

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'YOUR_API_KEY',
  baseURL: 'https://ai.kaiho.cc/v1'
});

async function main() {
  const response = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      { role: 'user', content: '你好,介紹一下你自己' }
    ]
  });
  
  console.log(response.choices[0].message.content);
}

main();

3️⃣ 理解回應

成功的回應格式如下:
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-4o",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "你好!我是一個 AI 助手..."
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

4️⃣ 探索更多功能

📚 下一步

提示: 所有 API 都相容 OpenAI SDK,您可以直接使用現有的 OpenAI 整合程式碼!