File size: 1,669 Bytes
a3c44d4
9ada063
 
a3c44d4
9ada063
 
 
 
 
 
a3c44d4
 
 
9ada063
a3c44d4
 
9ada063
a3c44d4
 
9ada063
 
 
 
 
 
a3c44d4
 
 
3ec5b0e
9ada063
a3c44d4
 
 
c6f7173
a3c44d4
c6f7173
a3c44d4
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)

@app.route("/proxy/mistral", methods=["POST"])
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)