import requests
import json
url = "https://api.incredible.one/v1/chat-completion"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"model": "small-1",
"messages": [
{"role": "user", "content": "Write a short poem about coding"}
],
"stream": True
}
# Stream response
response = requests.post(url, headers=headers, json=payload, stream=True)
for line in response.iter_lines():
if line:
line_str = line.decode('utf-8')
if line_str.startswith('data: '):
data_str = line_str[6:] # Remove 'data: ' prefix
if data_str == '[DONE]':
print("\n[Stream complete]")
break
try:
data = json.loads(data_str)
if 'content' in data and data['content'].get('type') == 'text_chunk':
print(data['content']['content'], end='', flush=True)
except json.JSONDecodeError:
pass