-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (79 loc) · 3 KB
/
main.py
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
103
104
# backend/main.py
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from .auth import create_token, verify_token
from pydantic import BaseModel
from typing import Dict, List, Optional
from .chatbot import generate_response
from .database import (
init_db,
register_user,
authenticate_user,
save_chat,
add_message_to_chat,
get_user_chats,
)
app = FastAPI()
security = HTTPBearer()
class UserCredentials(BaseModel):
username: str
password: str
class ChatMessage(BaseModel):
username: str
chat_id: Optional[int]
message: str
title: Optional[str]
messages: Optional[List[Dict[str, str]]] = [] # Add this field
@app.on_event("startup")
def startup_event():
init_db()
@app.post("/register")
def register(credentials: UserCredentials):
if not credentials.username or not credentials.password:
raise HTTPException(status_code=400, detail="Username and password required")
if len(credentials.username) < 3:
raise HTTPException(
status_code=400, detail="Username must be at least 3 characters"
)
if len(credentials.password) < 4:
raise HTTPException(
status_code=400, detail="Password must be at least 4 characters"
)
if register_user(credentials.username, credentials.password):
return {"message": "Registration successful"}
raise HTTPException(status_code=400, detail="Username already exists")
@app.post("/#")
def login(credentials: UserCredentials):
if authenticate_user(credentials.username, credentials.password):
return create_token(credentials.username)
raise HTTPException(status_code=401)
def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)):
token = credentials.credentials
username = verify_token(token)
if not username:
raise HTTPException(status_code=401, detail="Invalid token")
return username
@app.post("/verify")
def verify(username: str = Depends(get_current_user)):
return {"username": username}
@app.post("/chat")
async def chat(message: ChatMessage, current_user: str = Depends(get_current_user)):
if message.username != current_user:
raise HTTPException(status_code=403)
# Pass full message history to generate_response
response = generate_response(
session_id=message.username,
user_input=message.message,
message_history=message.messages,
)
if message.chat_id:
add_message_to_chat(message.chat_id, message.message, response)
else:
title = message.title or message.message[:30] + "..."
message.chat_id = save_chat(message.username, title, message.message, response)
return {"response": response, "chat_id": message.chat_id}
@app.get("/chats/{username}")
def get_chats(username: str, current_user: str = Depends(get_current_user)):
if username != current_user:
raise HTTPException(status_code=403)
return get_user_chats(username)