File size: 1,747 Bytes
3f2435b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3bf949
 
 
 
 
 
3f2435b
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
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")