Beispiele¶
Praktische Beispiele für die häufigsten Anwendungsfälle.
curl – Einfache Frage¶
curl -s -X POST https://lenny-api.mycubeserver.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-lenny-DEIN-KEY" \
-d '{
"messages": [{"role": "user", "content": "Erkläre mir das Konzept einer API."}]
}' | python -m json.tool
curl – Streaming¶
curl -N -X POST https://lenny-api.mycubeserver.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-lenny-DEIN-KEY" \
-d '{
"messages": [{"role": "user", "content": "Was ist Maschinelles Lernen?"}],
"stream": true
}'
Die -N-Flag deaktiviert curl-Pufferung, sodass Tokens direkt sichtbar werden.
curl – User Brain aktivieren¶
# Persönliche Konzepte mit einbeziehen
curl -s -X POST https://lenny-api.mycubeserver.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-lenny-DEIN-KEY" \
-d '{
"messages": [{"role": "user", "content": "Was habe ich über Datenbankindizes gespeichert?"}],
"user": "dein-username"
}'
curl – Konzept ins User Brain speichern¶
curl -s -X POST https://lenny-api.mycubeserver.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-lenny-DEIN-KEY" \
-d '{
"messages": [{"role": "user", "content": "/merke B-Tree-Index: Datenbankindex-Struktur, die eine balancierte Baumstruktur verwendet. Ermöglicht O(log n) Suche und unterstützt Bereichsabfragen."}],
"user": "dein-username"
}'
Lenny antwortet mit einer Bestätigung; das Konzept ist ab sofort aktiv.
Python-Client¶
import json
import urllib.request
API_BASE = "https://lenny-api.mycubeserver.com"
API_KEY = "sk-lenny-DEIN-KEY"
def ask(question: str, user_id: str | None = None) -> str:
payload = {
"messages": [{"role": "user", "content": question}],
}
if user_id:
payload["user"] = user_id
data = json.dumps(payload).encode()
req = urllib.request.Request(
f"{API_BASE}/v1/chat/completions",
data=data,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}",
},
method="POST",
)
with urllib.request.urlopen(req, timeout=60) as resp:
result = json.loads(resp.read())
return result["choices"][0]["message"]["content"]
# Beispiel
antwort = ask("Was ist eine REST-API?")
print(antwort)
# Mit User Brain
antwort = ask("Zeig mir meine gespeicherten Konzepte zu Datenbanken.", user_id="dein-username")
print(antwort)
Python-Client mit Streaming¶
import json
import urllib.request
API_BASE = "https://lenny-api.mycubeserver.com"
API_KEY = "sk-lenny-DEIN-KEY"
def stream_ask(question: str) -> None:
payload = json.dumps({
"messages": [{"role": "user", "content": question}],
"stream": True,
}).encode()
req = urllib.request.Request(
f"{API_BASE}/v1/chat/completions",
data=payload,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}",
},
method="POST",
)
with urllib.request.urlopen(req, timeout=120) as resp:
for raw_line in resp:
line = raw_line.decode("utf-8").strip()
if not line or not line.startswith("data: "):
continue
payload_str = line[6:]
if payload_str == "[DONE]":
break
chunk = json.loads(payload_str)
token = chunk["choices"][0]["delta"].get("content", "")
if token:
print(token, end="", flush=True)
print()
stream_ask("Erkläre mir Backpropagation in einem Satz.")