Spaces:
Running
Running
Update routes.py
Browse files
routes.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import tempfile
|
4 |
-
from fastapi import APIRouter, HTTPException, UploadFile, File, Form
|
5 |
from fastapi.encoders import jsonable_encoder
|
6 |
from bson import ObjectId
|
7 |
from models import InitializeBotResponse, NewChatResponse, QueryRequest, QueryResponse
|
@@ -13,12 +13,14 @@ 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))
|
@@ -46,14 +48,18 @@ async def upload_document(bot_id: str = Form(...), file: UploadFile = File(...))
|
|
46 |
raise HTTPException(status_code=500, detail=str(e))
|
47 |
|
48 |
|
49 |
-
@router.post("/create_bot")
|
50 |
-
def create_bot(bot_id: str
|
51 |
"""
|
52 |
-
Finalizes the creation (build) of the bot
|
53 |
-
|
|
|
54 |
"""
|
55 |
try:
|
56 |
-
|
|
|
|
|
|
|
57 |
if prompt_type == "university":
|
58 |
prompt_template = PromptTemplates.get_university_chatbot_prompt()
|
59 |
elif prompt_type == "quiz_solving":
|
@@ -75,10 +81,9 @@ def create_bot(bot_id: str = Form(...), prompt_type: str = Form(...)):
|
|
75 |
elif prompt_type == "check_paper":
|
76 |
prompt_template = PromptTemplates.get_check_paper_prompt()
|
77 |
else:
|
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:
|
@@ -101,7 +106,7 @@ def new_chat(bot_id: str):
|
|
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
|
105 |
"""
|
106 |
try:
|
107 |
response, web_sources = trainer.get_response(
|
@@ -125,9 +130,10 @@ def list_chats(bot_id: str):
|
|
125 |
|
126 |
|
127 |
@router.get("/chat_history/{chat_id}")
|
128 |
-
def chat_history(chat_id: str):
|
129 |
"""
|
130 |
Returns the chat history for a given chat session.
|
|
|
131 |
ObjectId instances in the history are converted to strings.
|
132 |
"""
|
133 |
try:
|
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import tempfile
|
4 |
+
from fastapi import APIRouter, HTTPException, UploadFile, File, Form, Query
|
5 |
from fastapi.encoders import jsonable_encoder
|
6 |
from bson import ObjectId
|
7 |
from models import InitializeBotResponse, NewChatResponse, QueryRequest, QueryResponse
|
|
|
13 |
trainer = get_trainer()
|
14 |
|
15 |
@router.post("/initialize_bot", response_model=InitializeBotResponse)
|
16 |
+
def initialize_bot(prompt_type: str = Query(None)):
|
17 |
"""
|
18 |
+
Initializes a new bot and returns its bot_id.
|
19 |
+
Accepts an optional 'prompt_type' query parameter (provided by the frontend).
|
20 |
"""
|
21 |
try:
|
22 |
bot_id = trainer.initialize_bot_id()
|
23 |
+
# Optionally, you might want to store the prompt_type with the bot record.
|
24 |
return InitializeBotResponse(bot_id=bot_id)
|
25 |
except Exception as e:
|
26 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
48 |
raise HTTPException(status_code=500, detail=str(e))
|
49 |
|
50 |
|
51 |
+
@router.post("/create_bot/{bot_id}")
|
52 |
+
def create_bot(bot_id: str, prompt_type: str = Query(None)):
|
53 |
"""
|
54 |
+
Finalizes the creation (build) of the bot identified by bot_id.
|
55 |
+
Uses the provided (or default) prompt_type to determine the custom prompt template.
|
56 |
+
If no prompt_type is provided, it defaults to "quiz_solving".
|
57 |
"""
|
58 |
try:
|
59 |
+
if prompt_type is None:
|
60 |
+
prompt_type = "quiz_solving"
|
61 |
+
|
62 |
+
# Determine the appropriate prompt template.
|
63 |
if prompt_type == "university":
|
64 |
prompt_template = PromptTemplates.get_university_chatbot_prompt()
|
65 |
elif prompt_type == "quiz_solving":
|
|
|
81 |
elif prompt_type == "check_paper":
|
82 |
prompt_template = PromptTemplates.get_check_paper_prompt()
|
83 |
else:
|
|
|
84 |
prompt_template = PromptTemplates.get_quiz_solving_prompt()
|
85 |
+
|
86 |
+
# Create (build) the bot using the specified bot_id and prompt template.
|
87 |
trainer.create_bot(bot_id, prompt_template)
|
88 |
return {"message": f"Bot {bot_id} created successfully."}
|
89 |
except Exception as e:
|
|
|
106 |
def send_query(query_request: QueryRequest):
|
107 |
"""
|
108 |
Processes a query and returns the bot's response along with any web sources.
|
109 |
+
The request must include bot_id, chat_id, and the query text.
|
110 |
"""
|
111 |
try:
|
112 |
response, web_sources = trainer.get_response(
|
|
|
130 |
|
131 |
|
132 |
@router.get("/chat_history/{chat_id}")
|
133 |
+
def chat_history(chat_id: str, bot_id: str = Query(None)):
|
134 |
"""
|
135 |
Returns the chat history for a given chat session.
|
136 |
+
The bot_id can be provided as a query parameter (if needed).
|
137 |
ObjectId instances in the history are converted to strings.
|
138 |
"""
|
139 |
try:
|