27 lines
863 B
Python
27 lines
863 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 mozout import Client, ApiError
|
|
|
|
# Key from MOZOUT_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()
|