PhilSad
save
c6f7173
raw
history blame
1.38 kB
from typing import Dict
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import os
import httpx
# 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 FastAPI app
app = FastAPI()
# # Define the request model for input validation
# class MistralRequest(BaseModel):
# system_prompt : str
# input_text: str
@app.post("/proxy/mistral")
async def proxy_mistral(request_data: Dict):
"""Proxy endpoint to send requests to Mistral API."""
url = "https://api.mistral.ai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {MISTRAL_API_TOKEN}",
"Content-Type": "application/json"
}
# Construct the data payload
print("attempting to sendc the request")
print(request_data)
# Send the request to the Mistral API
async with httpx.AsyncClient() as client:
response = await client.post(url, headers=headers, json=request_data)
print("my response" , response)
# Check the response status
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail=response.text)
print(response.json())
# Return the Mistral API response
return response.json()