Spaces:
Sleeping
Sleeping
Update routes.py
Browse files
routes.py
CHANGED
@@ -12,32 +12,23 @@ from prompt_templates import PromptTemplates
|
|
12 |
router = APIRouter()
|
13 |
trainer = get_trainer()
|
14 |
|
15 |
-
# Global bot identifier.
|
16 |
-
GLOBAL_BOT_ID = None
|
17 |
-
|
18 |
@router.post("/initialize_bot", response_model=InitializeBotResponse)
|
19 |
def initialize_bot():
|
20 |
"""
|
21 |
-
Initializes a
|
22 |
-
If the bot is already initialized, the existing bot_id is returned.
|
23 |
"""
|
24 |
-
global GLOBAL_BOT_ID
|
25 |
try:
|
26 |
-
|
27 |
-
|
28 |
-
return InitializeBotResponse(bot_id=GLOBAL_BOT_ID)
|
29 |
except Exception as e:
|
30 |
raise HTTPException(status_code=500, detail=str(e))
|
31 |
|
32 |
|
33 |
@router.post("/upload_document")
|
34 |
-
async def upload_document(file: UploadFile = File(...)):
|
35 |
"""
|
36 |
-
Saves the uploaded file temporarily and adds it to the
|
37 |
"""
|
38 |
-
global GLOBAL_BOT_ID
|
39 |
-
if GLOBAL_BOT_ID is None:
|
40 |
-
raise HTTPException(status_code=400, detail="Global bot not initialized.")
|
41 |
try:
|
42 |
# Save the file to a temporary location.
|
43 |
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
|
@@ -45,8 +36,8 @@ async def upload_document(file: UploadFile = File(...)):
|
|
45 |
tmp.write(contents)
|
46 |
tmp_path = tmp.name
|
47 |
|
48 |
-
# Add the document using the temporary file path to the
|
49 |
-
trainer.add_document_from_path(tmp_path,
|
50 |
|
51 |
# Remove the temporary file.
|
52 |
os.remove(tmp_path)
|
@@ -56,14 +47,11 @@ async def upload_document(file: UploadFile = File(...)):
|
|
56 |
|
57 |
|
58 |
@router.post("/create_bot")
|
59 |
-
def create_bot(prompt_type: str = Form(...)):
|
60 |
"""
|
61 |
-
Finalizes the creation (build) of the
|
62 |
This endpoint assigns the appropriate custom prompt template before finalizing bot creation.
|
63 |
"""
|
64 |
-
global GLOBAL_BOT_ID
|
65 |
-
if GLOBAL_BOT_ID is None:
|
66 |
-
raise HTTPException(status_code=400, detail="Global bot not initialized.")
|
67 |
try:
|
68 |
# Determine the appropriate prompt template based on the provided prompt_type.
|
69 |
if prompt_type == "university":
|
@@ -90,23 +78,20 @@ def create_bot(prompt_type: str = Form(...)):
|
|
90 |
# Default prompt template if prompt_type is not recognized.
|
91 |
prompt_template = PromptTemplates.get_quiz_solving_prompt()
|
92 |
|
93 |
-
#
|
94 |
-
trainer.create_bot(
|
95 |
-
return {"message": f"Bot {
|
96 |
except Exception as e:
|
97 |
raise HTTPException(status_code=500, detail=str(e))
|
98 |
|
99 |
|
100 |
-
@router.post("/new_chat", response_model=NewChatResponse)
|
101 |
-
def new_chat():
|
102 |
"""
|
103 |
-
Creates a new chat session for the
|
104 |
"""
|
105 |
-
global GLOBAL_BOT_ID
|
106 |
-
if GLOBAL_BOT_ID is None:
|
107 |
-
raise HTTPException(status_code=400, detail="Global bot not initialized.")
|
108 |
try:
|
109 |
-
chat_id = trainer.new_chat(
|
110 |
return NewChatResponse(chat_id=chat_id)
|
111 |
except Exception as e:
|
112 |
raise HTTPException(status_code=500, detail=str(e))
|
@@ -116,30 +101,24 @@ def new_chat():
|
|
116 |
def send_query(query_request: QueryRequest):
|
117 |
"""
|
118 |
Processes a query and returns the bot's response along with any web sources.
|
119 |
-
|
120 |
"""
|
121 |
-
global GLOBAL_BOT_ID
|
122 |
-
if GLOBAL_BOT_ID is None:
|
123 |
-
raise HTTPException(status_code=400, detail="Global bot not initialized.")
|
124 |
try:
|
125 |
response, web_sources = trainer.get_response(
|
126 |
-
query_request.query,
|
127 |
)
|
128 |
return QueryResponse(response=response, web_sources=web_sources)
|
129 |
except Exception as e:
|
130 |
raise HTTPException(status_code=500, detail=str(e))
|
131 |
|
132 |
|
133 |
-
@router.get("/list_chats")
|
134 |
-
def list_chats():
|
135 |
"""
|
136 |
-
Returns a list of previous chat sessions for the
|
137 |
"""
|
138 |
-
global GLOBAL_BOT_ID
|
139 |
-
if GLOBAL_BOT_ID is None:
|
140 |
-
raise HTTPException(status_code=400, detail="Global bot not initialized.")
|
141 |
try:
|
142 |
-
chats = trainer.list_chats(
|
143 |
return chats
|
144 |
except Exception as e:
|
145 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
12 |
router = APIRouter()
|
13 |
trainer = get_trainer()
|
14 |
|
|
|
|
|
|
|
15 |
@router.post("/initialize_bot", response_model=InitializeBotResponse)
|
16 |
def initialize_bot():
|
17 |
"""
|
18 |
+
Initializes a bot and returns its bot_id.
|
|
|
19 |
"""
|
|
|
20 |
try:
|
21 |
+
bot_id = trainer.initialize_bot_id()
|
22 |
+
return InitializeBotResponse(bot_id=bot_id)
|
|
|
23 |
except Exception as e:
|
24 |
raise HTTPException(status_code=500, detail=str(e))
|
25 |
|
26 |
|
27 |
@router.post("/upload_document")
|
28 |
+
async def upload_document(bot_id: str = Form(...), file: UploadFile = File(...)):
|
29 |
"""
|
30 |
+
Saves the uploaded file temporarily and adds it to the specified bot's knowledge base.
|
31 |
"""
|
|
|
|
|
|
|
32 |
try:
|
33 |
# Save the file to a temporary location.
|
34 |
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
|
|
|
36 |
tmp.write(contents)
|
37 |
tmp_path = tmp.name
|
38 |
|
39 |
+
# Add the document using the temporary file path to the specified bot.
|
40 |
+
trainer.add_document_from_path(tmp_path, bot_id)
|
41 |
|
42 |
# Remove the temporary file.
|
43 |
os.remove(tmp_path)
|
|
|
47 |
|
48 |
|
49 |
@router.post("/create_bot")
|
50 |
+
def create_bot(bot_id: str = Form(...), prompt_type: str = Form(...)):
|
51 |
"""
|
52 |
+
Finalizes the creation (build) of the bot using the specified prompt type.
|
53 |
This endpoint assigns the appropriate custom prompt template before finalizing bot creation.
|
54 |
"""
|
|
|
|
|
|
|
55 |
try:
|
56 |
# Determine the appropriate prompt template based on the provided prompt_type.
|
57 |
if prompt_type == "university":
|
|
|
78 |
# Default prompt template if prompt_type is not recognized.
|
79 |
prompt_template = PromptTemplates.get_quiz_solving_prompt()
|
80 |
|
81 |
+
# Create the bot using the specified bot_id and prompt template.
|
82 |
+
trainer.create_bot(bot_id, prompt_template)
|
83 |
+
return {"message": f"Bot {bot_id} created successfully."}
|
84 |
except Exception as e:
|
85 |
raise HTTPException(status_code=500, detail=str(e))
|
86 |
|
87 |
|
88 |
+
@router.post("/new_chat/{bot_id}", response_model=NewChatResponse)
|
89 |
+
def new_chat(bot_id: str):
|
90 |
"""
|
91 |
+
Creates a new chat session for the specified bot.
|
92 |
"""
|
|
|
|
|
|
|
93 |
try:
|
94 |
+
chat_id = trainer.new_chat(bot_id)
|
95 |
return NewChatResponse(chat_id=chat_id)
|
96 |
except Exception as e:
|
97 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
101 |
def send_query(query_request: QueryRequest):
|
102 |
"""
|
103 |
Processes a query and returns the bot's response along with any web sources.
|
104 |
+
The QueryRequest model must include the bot_id, chat_id, and query.
|
105 |
"""
|
|
|
|
|
|
|
106 |
try:
|
107 |
response, web_sources = trainer.get_response(
|
108 |
+
query_request.query, query_request.bot_id, query_request.chat_id
|
109 |
)
|
110 |
return QueryResponse(response=response, web_sources=web_sources)
|
111 |
except Exception as e:
|
112 |
raise HTTPException(status_code=500, detail=str(e))
|
113 |
|
114 |
|
115 |
+
@router.get("/list_chats/{bot_id}")
|
116 |
+
def list_chats(bot_id: str):
|
117 |
"""
|
118 |
+
Returns a list of previous chat sessions for the specified bot.
|
119 |
"""
|
|
|
|
|
|
|
120 |
try:
|
121 |
+
chats = trainer.list_chats(bot_id)
|
122 |
return chats
|
123 |
except Exception as e:
|
124 |
raise HTTPException(status_code=500, detail=str(e))
|