Spaces:
Sleeping
Sleeping
Update routes.py
Browse files
routes.py
CHANGED
@@ -10,14 +10,14 @@ from config import CUSTOM_PROMPT
|
|
10 |
from prompt_templates import PromptTemplates
|
11 |
|
12 |
router = APIRouter()
|
13 |
-
|
14 |
@router.post("/initialize_bot", response_model=InitializeBotResponse)
|
15 |
def initialize_bot(prompt_type: str):
|
16 |
"""
|
17 |
Initializes a new bot with a custom prompt template.
|
18 |
The custom_prompt parameter should be provided by the frontend (for example, via navigation).
|
19 |
"""
|
20 |
-
|
21 |
try:
|
22 |
bot_id = trainer.initialize_bot_id()
|
23 |
|
@@ -58,7 +58,6 @@ async def upload_document(bot_id: str = Form(...), file: UploadFile = File(...))
|
|
58 |
"""
|
59 |
Saves the uploaded file temporarily and adds it to the bot's knowledge base.
|
60 |
"""
|
61 |
-
trainer = get_trainer()
|
62 |
try:
|
63 |
# Save the file to a temporary location
|
64 |
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
|
@@ -80,7 +79,6 @@ def create_bot(bot_id: str):
|
|
80 |
"""
|
81 |
Creates (builds) the bot (e.g., builds its index) for the given bot_id.
|
82 |
"""
|
83 |
-
trainer = get_trainer()
|
84 |
try:
|
85 |
trainer.create_bot(bot_id)
|
86 |
return {"message": f"Bot {bot_id} created successfully."}
|
@@ -92,7 +90,6 @@ def new_chat(bot_id: str):
|
|
92 |
"""
|
93 |
Creates a new chat session for the specified bot.
|
94 |
"""
|
95 |
-
trainer = get_trainer()
|
96 |
try:
|
97 |
chat_id = trainer.new_chat(bot_id)
|
98 |
return NewChatResponse(chat_id=chat_id)
|
@@ -104,7 +101,6 @@ def send_query(query_request: QueryRequest):
|
|
104 |
"""
|
105 |
Processes a query and returns the bot's response along with any web sources.
|
106 |
"""
|
107 |
-
trainer = get_trainer()
|
108 |
try:
|
109 |
response, web_sources = trainer.get_response(
|
110 |
query_request.query, query_request.bot_id, query_request.chat_id
|
@@ -118,7 +114,6 @@ def list_chats(bot_id: str):
|
|
118 |
"""
|
119 |
Returns a list of previous chat sessions for the specified bot.
|
120 |
"""
|
121 |
-
trainer = get_trainer()
|
122 |
try:
|
123 |
chats = trainer.list_chats(bot_id)
|
124 |
return chats
|
@@ -131,7 +126,6 @@ def chat_history(chat_id: str, bot_id: str):
|
|
131 |
Returns the chat history for a given chat session.
|
132 |
ObjectId instances in the history are converted to strings.
|
133 |
"""
|
134 |
-
trainer = get_trainer()
|
135 |
try:
|
136 |
history = trainer.get_chat_by_id(chat_id=chat_id)
|
137 |
return jsonable_encoder(history, custom_encoder={ObjectId: str})
|
|
|
10 |
from prompt_templates import PromptTemplates
|
11 |
|
12 |
router = APIRouter()
|
13 |
+
trainer = get_trainer()
|
14 |
@router.post("/initialize_bot", response_model=InitializeBotResponse)
|
15 |
def initialize_bot(prompt_type: str):
|
16 |
"""
|
17 |
Initializes a new bot with a custom prompt template.
|
18 |
The custom_prompt parameter should be provided by the frontend (for example, via navigation).
|
19 |
"""
|
20 |
+
|
21 |
try:
|
22 |
bot_id = trainer.initialize_bot_id()
|
23 |
|
|
|
58 |
"""
|
59 |
Saves the uploaded file temporarily and adds it to the bot's knowledge base.
|
60 |
"""
|
|
|
61 |
try:
|
62 |
# Save the file to a temporary location
|
63 |
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
|
|
|
79 |
"""
|
80 |
Creates (builds) the bot (e.g., builds its index) for the given bot_id.
|
81 |
"""
|
|
|
82 |
try:
|
83 |
trainer.create_bot(bot_id)
|
84 |
return {"message": f"Bot {bot_id} created successfully."}
|
|
|
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)
|
|
|
101 |
"""
|
102 |
Processes a query and returns the bot's response along with any web sources.
|
103 |
"""
|
|
|
104 |
try:
|
105 |
response, web_sources = trainer.get_response(
|
106 |
query_request.query, query_request.bot_id, query_request.chat_id
|
|
|
114 |
"""
|
115 |
Returns a list of previous chat sessions for the specified bot.
|
116 |
"""
|
|
|
117 |
try:
|
118 |
chats = trainer.list_chats(bot_id)
|
119 |
return chats
|
|
|
126 |
Returns the chat history for a given chat session.
|
127 |
ObjectId instances in the history are converted to strings.
|
128 |
"""
|
|
|
129 |
try:
|
130 |
history = trainer.get_chat_by_id(chat_id=chat_id)
|
131 |
return jsonable_encoder(history, custom_encoder={ObjectId: str})
|