try: import requests, json # If changing, remember to change below from flask import Flask, request, Response, stream_with_context except ModuleNotFoundError: print("""You are missing a required package to run this script. If you press enter, it will attempt to install the missing packages (flask, requests). This is equivalent to running: pip install flask requests If you don't know what this means, press enter.""") input() import subprocess, sys try: subprocess.run([sys.executable, "-m", "pip", "install", "flask", "requests"], check=True) except Exception as e: print(f""" Got {e} while trying to install. You may be able to install them manually. Open Powershell and install Flask/Requests by typing the following: pip install flask requests Press enter and wait for Flask/Requests to finish installing, then close Powershell.""") input() exit() import requests, json # If changing, remember to change below from flask import Flask, request, Response, stream_with_context # Инициализация Flask приложения app = Flask(__name__) # Конфигурация проксей. PROXIES = {"https": "127.0.0.1:2080"} # API CLAUDE_API = "YouApiHere" # Функция для отправки запроса к API Claude def claude_request(data, stream=False): headers = { "x-api-key": CLAUDE_API, "anthropic-version": "2023-06-01" } try: response = requests.post( "https://api.anthropic.com/v1/messages", headers=headers, json=data, stream=stream, proxies=PROXIES ) return response if response.ok else None except Exception as e: print(f"Error: {e}") return None # Обработчик маршрута для сообщений @app.route("/messages", methods=["POST"]) def handle_chat(): data = request.json stream = data.get("stream", False) response = claude_request(data, stream) if not response: return {"type": "message", "content": [{"type": "text", "text": "ERROR: Something went wrong"}]} if stream: def stream_response(): for chunk in response.iter_lines(): if chunk: yield chunk.decode("utf-8") + "\n\n" return Response(stream_with_context(stream_response()), content_type='text/event-stream', headers={'Cache-Control': 'no-cache', 'Connection': 'keep-alive'}) return response.text # Запуск Flask приложения на 5000 порту if __name__ == "__main__": app.run()