mominah commited on
Commit
6b94b38
·
verified ·
1 Parent(s): 4cf11fc

Update routes.py

Browse files
Files changed (1) hide show
  1. routes.py +34 -33
routes.py CHANGED
@@ -11,43 +11,40 @@ from prompt_templates import PromptTemplates
11
 
12
  router = APIRouter()
13
 
14
- def select_prompt(prompt_type: str):
15
- """Selects a prompt template based on the provided prompt_type."""
16
- if prompt_type == "university":
17
- return PromptTemplates.get_university_chatbot_prompt()
18
- elif prompt_type == "quiz_solving":
19
- return PromptTemplates.get_quiz_solving_prompt()
20
- elif prompt_type == "assignment_solving":
21
- return PromptTemplates.get_assignment_solving_prompt()
22
- elif prompt_type == "paper_solving":
23
- return PromptTemplates.get_paper_solving_prompt()
24
- elif prompt_type == "quiz_creation":
25
- return PromptTemplates.get_quiz_creation_prompt()
26
- elif prompt_type == "assignment_creation":
27
- return PromptTemplates.get_assignment_creation_prompt()
28
- elif prompt_type == "paper_creation":
29
- return PromptTemplates.get_paper_creation_prompt()
30
- elif prompt_type == "check_quiz":
31
- return PromptTemplates.get_check_quiz_prompt()
32
- elif prompt_type == "check_assignment":
33
- return PromptTemplates.get_check_assignment_prompt()
34
- elif prompt_type == "check_paper":
35
- return PromptTemplates.get_check_paper_prompt()
36
- else:
37
- # Fallback to a default prompt if no matching type is found
38
- return PromptTemplates.get_quiz_solving_prompt()
39
-
40
  @router.post("/initialize_bot", response_model=InitializeBotResponse)
41
  def initialize_bot(prompt_type: str):
42
  """
43
  Initializes a new bot with a custom prompt template.
44
- The prompt_type query parameter determines which prompt to use.
45
  """
46
  trainer = get_trainer()
47
  try:
48
  bot_id = trainer.initialize_bot_id()
49
- prompt = select_prompt(prompt_type)
50
- trainer.set_custom_prompt_template(bot_id, prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  return InitializeBotResponse(bot_id=bot_id)
52
  except Exception as e:
53
  raise HTTPException(status_code=500, detail=str(e))
@@ -59,12 +56,16 @@ async def upload_document(bot_id: str = Form(...), file: UploadFile = File(...))
59
  """
60
  trainer = get_trainer()
61
  try:
 
62
  with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
63
  contents = await file.read()
64
  tmp.write(contents)
65
  tmp_path = tmp.name
66
 
 
67
  trainer.add_document_from_path(tmp_path, bot_id)
 
 
68
  os.remove(tmp_path)
69
  return {"message": "Document uploaded and added successfully."}
70
  except Exception as e:
@@ -73,7 +74,7 @@ async def upload_document(bot_id: str = Form(...), file: UploadFile = File(...))
73
  @router.post("/create_bot/{bot_id}")
74
  def create_bot(bot_id: str):
75
  """
76
- Creates the bot (builds its index) for the given bot_id.
77
  """
78
  trainer = get_trainer()
79
  try:
@@ -85,7 +86,7 @@ def create_bot(bot_id: str):
85
  @router.post("/new_chat/{bot_id}", response_model=NewChatResponse)
86
  def new_chat(bot_id: str):
87
  """
88
- Creates a new chat session for the bot.
89
  """
90
  trainer = get_trainer()
91
  try:
@@ -97,7 +98,7 @@ def new_chat(bot_id: str):
97
  @router.post("/query", response_model=QueryResponse)
98
  def send_query(query_request: QueryRequest):
99
  """
100
- Processes a user's query and returns the bot's response along with any web sources.
101
  """
102
  trainer = get_trainer()
103
  try:
@@ -124,7 +125,7 @@ def list_chats(bot_id: str):
124
  def chat_history(chat_id: str, bot_id: str):
125
  """
126
  Returns the chat history for a given chat session.
127
- ObjectId instances are converted to strings.
128
  """
129
  trainer = get_trainer()
130
  try:
 
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
  trainer = get_trainer()
21
  try:
22
  bot_id = trainer.initialize_bot_id()
23
+
24
+ if prompt_type == "university":
25
+ PromptTemplates.get_university_chatbot_prompt()
26
+ elif prompt_type == "quiz_solving":
27
+ PromptTemplates.get_quiz_solving_prompt()
28
+ elif prompt_type == "assignment_solving":
29
+ PromptTemplates.get_assignment_solving_prompt()
30
+ elif prompt_type == "paper_solving":
31
+ PromptTemplates.get_paper_solving_prompt()
32
+ elif prompt_type == "quiz_creation":
33
+ PromptTemplates.get_quiz_creation_prompt()
34
+ elif prompt_type == "assignment_creation":
35
+ PromptTemplates.get_assignment_creation_prompt()
36
+ elif prompt_type == "paper_creation":
37
+ PromptTemplates.get_paper_creation_prompt()
38
+ elif prompt_type == "check_quiz":
39
+ PromptTemplates.get_check_quiz_prompt()
40
+ elif prompt_type == "check_assignment":
41
+ PromptTemplates.get_check_assignment_prompt()
42
+ elif prompt_type == "check_paper":
43
+ PromptTemplates.get_check_paper_prompt()
44
+ else:
45
+ prompt_type=PromptTemplates.get_quiz_solving_prompt()
46
+
47
+ trainer.set_custom_prompt_template(bot_id, prompt_type)
48
  return InitializeBotResponse(bot_id=bot_id)
49
  except Exception as e:
50
  raise HTTPException(status_code=500, detail=str(e))
 
56
  """
57
  trainer = get_trainer()
58
  try:
59
+ # Save the file to a temporary location
60
  with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp:
61
  contents = await file.read()
62
  tmp.write(contents)
63
  tmp_path = tmp.name
64
 
65
+ # Add the document using the temporary file path
66
  trainer.add_document_from_path(tmp_path, bot_id)
67
+
68
+ # Remove the temporary file
69
  os.remove(tmp_path)
70
  return {"message": "Document uploaded and added successfully."}
71
  except Exception as e:
 
74
  @router.post("/create_bot/{bot_id}")
75
  def create_bot(bot_id: str):
76
  """
77
+ Creates (builds) the bot (e.g., builds its index) for the given bot_id.
78
  """
79
  trainer = get_trainer()
80
  try:
 
86
  @router.post("/new_chat/{bot_id}", response_model=NewChatResponse)
87
  def new_chat(bot_id: str):
88
  """
89
+ Creates a new chat session for the specified bot.
90
  """
91
  trainer = get_trainer()
92
  try:
 
98
  @router.post("/query", response_model=QueryResponse)
99
  def send_query(query_request: QueryRequest):
100
  """
101
+ Processes a query and returns the bot's response along with any web sources.
102
  """
103
  trainer = get_trainer()
104
  try:
 
125
  def chat_history(chat_id: str, bot_id: str):
126
  """
127
  Returns the chat history for a given chat session.
128
+ ObjectId instances in the history are converted to strings.
129
  """
130
  trainer = get_trainer()
131
  try: