-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (57 loc) · 1.9 KB
/
Copy pathmain.py
File metadata and controls
72 lines (57 loc) · 1.9 KB
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
# The 5th Member
import os
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
from dotenv import load_dotenv
from utils import ensure_collection, get_embedding, search_memories, upsert_memory, generate_rag_chat
from fastapi.responses import StreamingResponse
from db import init_db
from auth import login_user, register_user
load_dotenv()
init_db()
app = FastAPI(title='The 5th Member - Memory-Augmented Chatbot')
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class User(BaseModel):
username: str
password: str
TOP_K = int(os.getenv('TOP_K', '6'))
# Ensure qdrant collection on startup. Adjust embedding dim if your embed model uses other size.
@app.on_event('startup')
async def startup_event():
ensure_collection(dim=1024)
class ChatRequest(BaseModel):
user_id: str
session_id: str
message: str
class MemoryRequest(BaseModel):
user_id: str
text: str
tags: Optional[List[str]] = None
@app.post('/memory')
async def add_memory(payload: MemoryRequest):
# create embedding and upsert to qdrant
print("adding memory")
emb = await get_embedding(payload.text)
mid = await upsert_memory(payload.text, emb, payload.user_id, metadata={'tags': payload.tags or []})
return {'memory_id': mid}
sessions = {}
@app.post("/chat")
async def chat_endpoint(request: Request):
body = await request.json()
prompt = body.get("prompt", "")
user_id = body.get("user_id", "")
return StreamingResponse(generate_rag_chat(prompt, user_id), media_type="text/plain")
@app.post("/register")
def register(user: User):
return register_user(user.username, user.password)
@app.post("/login")
def login(user: User):
return login_user(user.username, user.password)