File size: 3,025 Bytes
62de77b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import openai
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi import FastAPI, HTTPException, Depends, Request, Response
from fastapi.security import OAuth2PasswordBearer
from tinydb import TinyDB
from tinydb import Query
from datetime import datetime
from utils import generate_token

query = Query()
db = TinyDB(".token.json")

app = FastAPI()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

REF_KEY = "F0eeQ419wvoCSPH7KBmsd9OiVkF0W0DxK1XE9T3BlbkFJ0"
model_name = ""

def verify_token(token: str = Depends(oauth2_scheme)):
    expiry = -1
    res = db.get(query.token == token)

    if res:
        expiry = (datetime.strptime(res["expiry_date"], '%Y-%m-%d') - datetime.strptime(res["created_at"], '%Y-%m-%d')).days
    
    if expiry < 0:
        return {"message": "Token is not Valid"}
    
    return token


class ChatInput(BaseModel):
    message: str
    openAI_token: str
    model_name: str


class RefToken(BaseModel):
    expiry_date: str
    ref_key: str


@app.post("/create")
async def create(data: RefToken):
    token = "Reference Key is incorrect"
    try:
        if data.ref_key == REF_KEY:
            token = generate_token(data.expiry_date, db) 
            return {"TOKEN": token}
    except Exception as e:
        raise HTTPException(status_code=500, detail="An error occurred while processing the request." + str(e))

@app.get("/list")
async def create(token: str = Depends(verify_token)):
    token = "Reference Key is incorrect"
    try:
        data = db.all()
        return {"data": data}
    except Exception as e:
        raise HTTPException(status_code=500, detail="An error occurred while processing the request." + str(e))

@app.post("/chat")
async def chat_with_gpt(chat_input: ChatInput, token: str = Depends(verify_token)):
    openai.api_key = chat_input.openAI_token
    prompt = f"User: {chat_input.message}\nAI:"
    model_name = chat_input.model_name
    try:
        if model_name == "text-davinci-002":
            response = openai.Completion.create(
                engine="text-davinci-002",
                prompt=prompt,
                max_tokens=50,
                n=1,
                stop=None,
                temperature=0.7,
            )
            message = response.choices[0].text.strip()
            usages = response["usage"]
        
        if model_name == "gpt-3.5-turbo":
            response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",   
            messages=[         
                {"role": "system", "content": prompt}   
            ])

            message = response.choices[0]["message"]["content"]
            usages = response["usage"]

        if model_name == "":
            message = "Plase select the model"
            usages = ""

        return {
            "message": message,
            "usages" : usages
            }
    except Exception as e:
        raise HTTPException(status_code=500, detail="An error occurred while processing the request." + str(e))