Chuyển đến nội dung chính

Tổng quan

LemonData hoàn toàn tương thích với OpenAI SDK. Chỉ cần thay đổi base URL và bạn có thể truy cập hơn 300 mô hình.

Cài đặt

pip install openai

Cấu hình

curl https://api.lemondata.cc/v1/chat/completions \
  -H "Authorization: Bearer sk-your-lemondata-key" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello!"}]}'

Chat Completions

Hoạt động hoàn toàn giống với OpenAI API:
response = client.chat.completions.create(
    model="gpt-4o",  # Or any LemonData model
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    temperature=0.7,
    max_tokens=1000
)

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

Streaming

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Function Calling / Tools

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"]
            }
        }
    }]
)

# Check if model wants to call a function
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    print(f"Function: {tool_call.function.name}")
    print(f"Arguments: {tool_call.function.arguments}")

Vision

response = client.chat.completions.create(
    model="gpt-4o",  # Or claude-sonnet-4-5
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What's in this image?"},
            {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
        ]
    }]
)

Image Generation

response = client.images.generate(
    model="dall-e-3",
    prompt="A white siamese cat",
    size="1024x1024",
    quality="standard",
    n=1
)

print(response.data[0].url)

Embeddings

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

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

Audio - Text to Speech

response = client.audio.speech.create(
    model="tts-1",
    voice="alloy",
    input="Hello, welcome to LemonData!"
)

response.stream_to_file("output.mp3")

Audio - Transcription

with open("audio.mp3", "rb") as audio_file:
    response = client.audio.transcriptions.create(
        model="whisper-1",
        file=audio_file
    )

print(response.text)

Sử dụng các mô hình khác nhau

Ưu điểm then chốt của LemonData là khả năng truy cập nhiều nhà cung cấp khác nhau:
# OpenAI
response = client.chat.completions.create(model="gpt-4o", messages=messages)

# Anthropic Claude
response = client.chat.completions.create(model="claude-sonnet-4-5", messages=messages)

# Google Gemini
response = client.chat.completions.create(model="gemini-2.5-flash", messages=messages)

# DeepSeek
response = client.chat.completions.create(model="deepseek-r1", messages=messages)

Xử lý lỗi

from openai import APIError, RateLimitError, AuthenticationError

try:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello"}]
    )
except AuthenticationError:
    print("Invalid API key")
except RateLimitError:
    print("Rate limit exceeded, please wait")
except APIError as e:
    print(f"API error: {e.status_code} - {e.message}")