api-python-sdk/examples/ai_chat.py
2026-11-28 15:28:00 +02:00

27 lines
870 B
Python

"""Chat with an AI model (with streaming)."""
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) # run from the repository without installing
from interweb_ao import Client, ApiError
# Key from INTERWEB_API_KEY.
client = Client()
answer = client.ai.chat_create({
"model": "deepseek-ai/deepseek-v4-flash",
"messages": [{"role": "user", "content": "Write a two-sentence welcome message for our website."}],
"max_tokens": 200,
})
print(answer["choices"][0]["message"]["content"])
for event in client.ai.chat_create({
"model": "deepseek-ai/deepseek-v4-flash",
"messages": [{"role": "user", "content": "Say hello."}],
"stream": True,
}):
if isinstance(event, dict):
for choice in event.get("choices", []):
print((choice.get("delta") or {}).get("content") or "", end="")
print()