Spaces:
Sleeping
Sleeping
Create routes.py
Browse files
routes.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# routes.py
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
import tempfile
|
5 |
+
from fastapi import APIRouter, HTTPException, UploadFile, File, Form
|
6 |
+
from models import InitializeBotResponse, DocumentPath, NewChatResponse, QueryRequest, QueryResponse
|
7 |
+
from trainer_manager import get_trainer
|
8 |
+
from config import CUSTOM_PROMPT
|
9 |
+
|
10 |
+
router = APIRouter()
|
11 |
+
|
12 |
+
@router.post("/initialize_bot", response_model=InitializeBotResponse)
|
13 |
+
def initialize_bot():
|
14 |
+
trainer = get_trainer()
|
15 |
+
try:
|
16 |
+
bot_id = trainer.initialize_bot_id()
|
17 |
+
trainer.set_custom_prompt_template(bot_id, CUSTOM_PROMPT)
|
18 |
+
return InitializeBotResponse(bot_id=bot_id)
|
19 |
+
except Exception as e:
|
20 |
+
raise HTTPException(status_code=500, detail=str(e))
|
21 |
+
|
22 |
+
@router.post("/add_document")
|
23 |
+
def add_document(document: DocumentPath):
|
24 |
+
trainer = get_trainer()
|
25 |
+
try:
|
26 |
+
trainer.add_document_from_path(document.data_path, document.bot_id)
|
27 |
+
return {"message": "Document added successfully."}
|
28 |
+
except Exception as e:
|
29 |
+
raise HTTPException(status_code=500, detail=str(e))
|
30 |
+
|
31 |
+
@router.post("/upload_document")
|
32 |
+
async def upload_document(bot_id: str = Form(...), file: UploadFile = File(...)):
|
33 |
+
trainer = get_trainer()
|
34 |
+
try:
|
35 |
+
# Save the uploaded file to a temporary directory
|
36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
|
37 |
+
contents = await file.read()
|
38 |
+
tmp.write(contents)
|
39 |
+
tmp_path = tmp.name
|
40 |
+
|
41 |
+
# Add the document from the temporary file path
|
42 |
+
trainer.add_document_from_path(tmp_path, bot_id)
|
43 |
+
|
44 |
+
# Remove the temporary file
|
45 |
+
os.remove(tmp_path)
|
46 |
+
return {"message": "Document uploaded and added successfully."}
|
47 |
+
except Exception as e:
|
48 |
+
raise HTTPException(status_code=500, detail=str(e))
|
49 |
+
|
50 |
+
@router.post("/create_bot/{bot_id}")
|
51 |
+
def create_bot(bot_id: str):
|
52 |
+
trainer = get_trainer()
|
53 |
+
try:
|
54 |
+
trainer.create_bot(bot_id)
|
55 |
+
return {"message": f"Bot {bot_id} created successfully."}
|
56 |
+
except Exception as e:
|
57 |
+
raise HTTPException(status_code=500, detail=str(e))
|
58 |
+
|
59 |
+
@router.post("/new_chat/{bot_id}", response_model=NewChatResponse)
|
60 |
+
def new_chat(bot_id: str):
|
61 |
+
trainer = get_trainer()
|
62 |
+
try:
|
63 |
+
chat_id = trainer.new_chat(bot_id)
|
64 |
+
return NewChatResponse(chat_id=chat_id)
|
65 |
+
except Exception as e:
|
66 |
+
raise HTTPException(status_code=500, detail=str(e))
|
67 |
+
|
68 |
+
@router.post("/query", response_model=QueryResponse)
|
69 |
+
def send_query(query_request: QueryRequest):
|
70 |
+
trainer = get_trainer()
|
71 |
+
try:
|
72 |
+
response, web_sources = trainer.get_response(
|
73 |
+
query_request.query, query_request.bot_id, query_request.chat_id
|
74 |
+
)
|
75 |
+
return QueryResponse(response=response, web_sources=web_sources)
|
76 |
+
except Exception as e:
|
77 |
+
raise HTTPException(status_code=500, detail=str(e))
|
78 |
+
|
79 |
+
@router.get("/list_chats/{bot_id}")
|
80 |
+
def list_chats(bot_id: str):
|
81 |
+
trainer = get_trainer()
|
82 |
+
try:
|
83 |
+
chats = trainer.list_chats(bot_id)
|
84 |
+
return chats
|
85 |
+
except Exception as e:
|
86 |
+
raise HTTPException(status_code=500, detail=str(e))
|
87 |
+
|
88 |
+
@router.get("/chat_history/{chat_id}")
|
89 |
+
def chat_history(chat_id: str, bot_id: str):
|
90 |
+
trainer = get_trainer()
|
91 |
+
try:
|
92 |
+
history = trainer.get_chat_by_id(chat_id=chat_id)
|
93 |
+
return history
|
94 |
+
except Exception as e:
|
95 |
+
raise HTTPException(status_code=500, detail=str(e))
|