跳轉到主要內容

概覽

LemonData 可與官方 OpenAI SDK 一起使用,方法是將客戶端指向 https://api.lemondata.cc/v1 對於大多數新專案,預設建議使用 Chat Completions 作為 OpenAI 相容路徑。僅在你明確需要 Responses 特定行為時才使用 Responses API Responses 特有的欄位不保證在每個模型與路由的路徑中行為一致。
Python、JavaScript 和 Go 有官方的 OpenAI SDK。PHP 與 OpenAI 相容的社群客戶端相容性良好,但 PHP 並不是官方的 OpenAI SDK。
類型: Native SDK主要路徑: OpenAI-compatible / Chat Completions支援信心: Supported core path

安裝

pip install openai
僅在你明確需要 Responses 特定行為時使用 POST /v1/responses。有些僅限 Responses 的欄位仍可能依賴所選模型與路由路徑。

設定用戶端

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-lemondata-key",
    base_url="https://api.lemondata.cc/v1",
)

建議:Chat Completions

response = client.chat.completions.create(
    model="gpt-5.4",
    messages=[{"role": "user", "content": "Explain what LemonData does in one sentence."}]
)

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

進階:Responses API

僅在你的工具或工作流程明確依賴 OpenAI Responses 語意時使用此路徑。

使用 Responses 進行串流

stream = client.responses.create(
    model="gpt-5.4",
    input="Write a short poem about coding.",
    stream=True,
)

for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="")

工具 / 函數調用

response = client.responses.create(
    model="gpt-5.4",
    input="What's the weather in Tokyo?",
    tools=[{
        "type": "function",
        "name": "get_weather",
        "description": "Get weather for a location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            },
            "required": ["location"]
        }
    }]
)

for item in response.output:
    if item.type == "function_call":
        print(item.name)
        print(item.arguments)

Responses 的視覺功能

response = client.responses.create(
    model="gpt-4o",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "What's in this image?"},
            {"type": "input_image", "image_url": "https://example.com/image.jpg"}
        ]
    }]
)

print(response.output_text)

Embeddings

response = client.embeddings.create(
    model="text-embedding-3-small",
    input="Hello world"
)

print(response.data[0].embedding[:5])

Chat Completions

Chat Completions 是 LemonData 的預設 OpenAI 相容路徑:
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

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

疑難排解

  • 驗證 base URL 是否完全為 https://api.lemondata.cc/v1
  • 檢查是否有代理干擾或自訂的 HTTP 客戶端覆寫
  • 在偵錯供應商行為前,確保你的 SDK 版本是最新的
  • 檢查你的 API key 是否以 sk- 開頭
  • 驗證該金鑰在 LemonData 控制台中是否為啟用狀態
  • 確認 SDK 是否傳送 Authorization: Bearer ...
  • responses.create(...) 會發送請求到 /v1/responses
  • chat.completions.create(...) 會發送請求到 /v1/chat/completions
  • 預設使用 Chat Completions,除非你明確需要 Responses 特定行為