Tutorials
Praktische Beispiele und Anleitungen für fortgeschrittene Features.
Streaming-Antworten
Einsteiger
5 Min.
Erhalten Sie Antworten in Echtzeit mit Server-Sent Events.
streaming.pypython
1from openai import OpenAI23client = OpenAI(4 api_key="sk_vantero_...",5 base_url="https://api.vantero.chat/v1"6)78# Streaming aktivieren9stream = client.chat.completions.create(10 model="mistral-small",11 messages=[{"role": "user", "content": "Erkläre mir KI"}],12 stream=True # Wichtig!13)1415for chunk in stream:16 if chunk.choices[0].delta.content:17 print(chunk.choices[0].delta.content, end="")1819# SSE Format Beispiel:20# data: {"id":"chatcmpl-abc123","choices":[{"delta":{"content":"KI"},...}]}21# data: {"id":"chatcmpl-abc123","choices":[{"delta":{"content":" steht"},...}]}22# data: [DONE]Function Calling (Tools)
Fortgeschritten
10 Min.
Ermöglichen Sie dem Modell, externe Funktionen aufzurufen.
tools.pypython
1from openai import OpenAI23client = OpenAI(4 api_key="sk_vantero_...",5 base_url="https://api.vantero.chat/v1"6)78# Tool/Function definieren9tools = [10 {11 "type": "function",12 "function": {13 "name": "get_weather",14 "description": "Aktuelle Wetterdaten für einen Ort abrufen",15 "parameters": {16 "type": "object",17 "properties": {18 "location": {19 "type": "string",20 "description": "Stadt, z.B. 'Berlin'"21 },22 "unit": {23 "type": "string",24 "enum": ["celsius", "fahrenheit"],25 "description": "Temperatureinheit"26 }27 },28 "required": ["location"]29 }30 }31 }32]3334response = client.chat.completions.create(35 model="mistral-small",36 messages=[{"role": "user", "content": "Wie ist das Wetter in Berlin?"}],37 tools=tools,38 tool_choice="auto" # oder "required" oder "none"39)4041# Prüfen ob Tool aufgerufen wurde42if response.choices[0].finish_reason == "tool_calls":43 tool_call = response.choices[0].message.tool_calls[0]44 print(f"Tool: {tool_call.function.name}")45 print(f"Arguments: {tool_call.function.arguments}")4647# Tool-Call Response Beispiel:48# {49# "id": "chatcmpl-abc123",50# "choices": [{51# "message": {52# "role": "assistant",53# "tool_calls": [{54# "id": "call_xyz789",55# "type": "function",56# "function": {57# "name": "get_weather",58# "arguments": "{\"location\": \"Berlin\", \"unit\": \"celsius\"}"59# }60# }]61# },62# "finish_reason": "tool_calls"63# }]64# }Bildanalyse (Vision)
Einsteiger
5 Min.
Analysieren Sie Bilder mit multimodalen Modellen.
vision.pypython
1from openai import OpenAI23client = OpenAI(4 api_key="sk_vantero_...",5 base_url="https://api.vantero.chat/v1"6)78# Bild-Analyse mit URL9response = client.chat.completions.create(10 model="mistral-small", # Muss Bild-Feature unterstützen11 messages=[12 {13 "role": "user",14 "content": [15 {"type": "text", "text": "Was siehst du auf diesem Bild?"},16 {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}17 ]18 }19 ]20)21print(response.choices[0].message.content)2223# Oder mit Base6424import base6425with open("bild.jpg", "rb") as f:26 image_data = base64.b64encode(f.read()).decode()2728response = client.chat.completions.create(29 model="mistral-small",30 messages=[31 {32 "role": "user",33 "content": [34 {"type": "text", "text": "Beschreibe dieses Bild"},35 {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}36 ]37 }38 ]39)Strukturierte JSON-Ausgabe
Einsteiger
5 Min.
Erzwingen Sie JSON-Ausgaben für einfache Weiterverarbeitung.
json.pypython
1from openai import OpenAI23client = OpenAI(4 api_key="sk_vantero_...",5 base_url="https://api.vantero.chat/v1"6)78# JSON Mode aktivieren9response = client.chat.completions.create(10 model="mistral-small",11 messages=[12 {13 "role": "user",14 "content": "Liste 3 deutsche Städte mit Einwohnerzahl als JSON"15 }16 ],17 response_format={"type": "json_object"} # Erzwingt JSON-Ausgabe18)1920# Antwort ist garantiert valides JSON21import json22data = json.loads(response.choices[0].message.content)23print(data)2425# Beispiel-Ausgabe:26# {"cities": [27# {"name": "Berlin", "population": 3645000},28# {"name": "Hamburg", "population": 1841000},29# {"name": "München", "population": 1472000}30# ]}