نظرة عامة
إن LemonData متوافق تماماً مع OpenAI SDK. ما عليك سوى تغييرbase URL لتتمكن من الوصول إلى أكثر من 300 نموذج.
التثبيت
نسخ
pip install openai
الإعداد
نسخ
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)
يعمل تماماً مثل 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
الصوت - تحويل النص إلى كلام (Text to Speech)
نسخ
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input="Hello, welcome to LemonData!"
)
response.stream_to_file("output.mp3")
الصوت - النسخ الصوتي (Transcription)
نسخ
with open("audio.mp3", "rb") as audio_file:
response = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
print(response.text)
استخدام نماذج مختلفة
الميزة الأساسية لـ LemonData هي الوصول إلى مزودين متعددين:نسخ
# 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)
معالجة الأخطاء (Error Handling)
نسخ
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}")