glitchnexusapi / main.py
Ruzgar ?????
Update main.py
c3bf949 verified
from fastapi import FastAPI, HTTPException, Header
from pydantic import BaseModel
from typing import Optional
import requests
import os
from dotenv import load_dotenv
# .env dosyasını yükle
load_dotenv()
# API bilgileri
API_URL = os.getenv("API_URL")
API_KEY = os.getenv("API_KEY")
# API anahtarlarını virgülle ayırarak al
API_KEYS = os.getenv("API_KEYS").split(',')
app = FastAPI()
class Message(BaseModel):
user_input: str
selected_model: str
api_key: str
@app.post("/chat")
async def chat(message: Message, authorization: str = Header(None)):
# API anahtarının geçerli olup olmadığını kontrol et
if message.api_key not in API_KEYS:
raise HTTPException(status_code=403, detail="Invalid API key")
# API URL ve anahtar ayarla
api_url = API_URL
api_key = API_KEY
# Modeli seç
selected_model = message.selected_model
if selected_model not in ["claude-3-haiku", "gpt-4o-mini", "llama", "mixtral"]:
raise HTTPException(status_code=400, detail="Invalid model selected")
# Kullanıcı mesajını API'ye gönder
response = requests.post(
api_url,
headers={"Authorization": f"Bearer {api_key}"},
json={
"model": selected_model,
"messages": [{"role": "user", "content": message.user_input}]
}
)
# Yanıtı işleme
if response.status_code == 200:
data = response.json()
bot_response = data['choices'][0]['message']['content']
# Model adı ile yanıt döndürme
return {
"model": selected_model,
"response": bot_response
}
else:
raise HTTPException(status_code=response.status_code, detail="API request failed")