Spaces:
Sleeping
Sleeping
from flask import Flask, request, jsonify, abort | |
import os | |
import httpx | |
from flask_cors import CORS | |
# Load Mistral API token from environment variables | |
MISTRAL_API_TOKEN = os.getenv("MISTRAL_API_TOKEN") | |
if not MISTRAL_API_TOKEN: | |
raise EnvironmentError("MISTRAL_API_TOKEN environment variable not set.") | |
# Initialize Flask app | |
app = Flask(__name__) | |
CORS(app) | |
def proxy_mistral(): | |
"""Proxy endpoint to send requests to Mistral API.""" | |
# Mistral endpoint and headers | |
url = "https://api.mistral.ai/v1/chat/completions" | |
headers = { | |
"Authorization": f"Bearer {MISTRAL_API_TOKEN}", | |
"Content-Type": "application/json" | |
} | |
# Parse the JSON body from the incoming request | |
request_data = request.get_json() | |
print("Attempting to send the request to Mistral") | |
print(request_data) | |
# Send the request to the Mistral API using httpx (synchronous) | |
with httpx.Client() as client: | |
response = client.post(url, headers=headers, json=request_data, timeout=None) | |
# If Mistral doesn’t return a 200, propagate the error | |
if response.status_code != 200: | |
# In Flask, you can either return an error explicitly: | |
# return (response.text, response.status_code) | |
# or raise an HTTPException via `abort()`. | |
return (response.text, response.status_code) | |
# Print the Mistral API response (for debug) and return it | |
print("Mistral response:", response.json()) | |
return jsonify(response.json()) | |
if __name__ == "__main__": | |
# Run the Flask app (use your desired host and port) | |
app.run(host="0.0.0.0", port=8000, debug=True) | |