Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- from my_config import openrouter_api
- question = "How would you build the tallest building ever?"
- url = "https://openrouter.ai/api/v1/chat/completions"
- headers = {
- "Authorization": f"Bearer {openrouter_api}",
- "Content-Type": "application/json"
- }
- payload = {
- "model": "openai/gpt-4o", # или "anthropic/claude-3.5-sonnet", "google/gemini-pro"
- "messages": [{"role": "user", "content": question}],
- "stream": True, # Потоковый режим для вывода по частям
- "max_tokens": 1000
- }
- print(f"❓ Вопрос: {question}\n")
- print("🤖 Ответ: ", end="", flush=True)
- # Отправляем запрос
- response = requests.post(url, headers=headers, json=payload, stream=True)
- # Обрабатываем потоковый ответ
- for line in response.iter_lines():
- if line:
- line = line.decode('utf-8')
- if line.startswith('data: '):
- data = line[6:] # Убираем "data: "
- if data != '[DONE]':
- try:
- chunk = json.loads(data)
- if chunk['choices'][0]['delta'].get('content'):
- content = chunk['choices'][0]['delta']['content']
- print(content, end='', flush=True)
- except:
- pass
- print("\n\n✅ Готово!")
Advertisement
Add Comment
Please, Sign In to add comment