Girish Jeswani commited on
Commit
2619dcc
·
1 Parent(s): 3251fad

add routes for orchestrator

Browse files
multi_llm_chatbot_backend/app/api/routes.py CHANGED
@@ -2,39 +2,101 @@ from fastapi import APIRouter, Body
2
  from app.llm.mistral_client import MistralClient
3
  from app.models.persona import Persona
4
  from app.core.orchestrator import ChatOrchestrator
 
 
 
5
 
6
  router = APIRouter()
7
 
8
- # Singleton for now
9
  llm = MistralClient()
10
- orchestrator = ChatOrchestrator()
 
11
 
12
- # Register initial personas
13
- orchestrator.register_persona(Persona(
14
  id="methodist",
15
  name="Methodist Advisor",
16
- system_prompt="You are a highly methodical PhD advisor who believes in structure and planning.",
17
  llm=llm
18
  ))
19
 
20
- orchestrator.register_persona(Persona(
21
- id="theorist",
22
  name="Theorist Advisor",
23
- system_prompt="You are a philosophical PhD advisor who focuses on abstract theories and ideas.",
24
  llm=llm
25
  ))
26
 
27
- orchestrator.register_persona(Persona(
28
  id="pragmatist",
29
- name="Pragmatist Advisor",
30
- system_prompt="You are a practical PhD advisor who focuses on real-world outcomes and utility.",
31
  llm=llm
32
  ))
33
 
 
 
 
 
34
  @router.post("/chat")
35
- async def chat(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  user_input: str = Body(...),
37
- active_personas: list[str] = Body(default=["methodist", "theorist", "pragmatist"])
38
  ):
39
- orchestrator.set_active_personas(active_personas)
40
- return await orchestrator.process_user_input(user_input)
 
 
 
2
  from app.llm.mistral_client import MistralClient
3
  from app.models.persona import Persona
4
  from app.core.orchestrator import ChatOrchestrator
5
+ from app.core.seamless_orchestrator import SeamlessOrchestrator
6
+ from pydantic import BaseModel
7
+ from typing import Optional, List
8
 
9
  router = APIRouter()
10
 
11
+ # Singletons
12
  llm = MistralClient()
13
+ chat_orchestrator = ChatOrchestrator()
14
+ seamless_orchestrator = SeamlessOrchestrator()
15
 
16
+ # Register personas
17
+ chat_orchestrator.register_persona(Persona(
18
  id="methodist",
19
  name="Methodist Advisor",
20
+ system_prompt="You are a highly methodical PhD advisor who believes in structure and planning. Provide organized, step-by-step guidance.",
21
  llm=llm
22
  ))
23
 
24
+ chat_orchestrator.register_persona(Persona(
25
+ id="theorist",
26
  name="Theorist Advisor",
27
+ system_prompt="You are a philosophical PhD advisor who focuses on abstract theories and ideas. Help students think deeply about concepts and frameworks.",
28
  llm=llm
29
  ))
30
 
31
+ chat_orchestrator.register_persona(Persona(
32
  id="pragmatist",
33
+ name="Pragmatist Advisor",
34
+ system_prompt="You are a practical PhD advisor who focuses on real-world outcomes and utility. Give actionable, concrete advice.",
35
  llm=llm
36
  ))
37
 
38
+ class ChatMessage(BaseModel):
39
+ user_input: str
40
+ session_id: Optional[str] = None
41
+
42
  @router.post("/chat")
43
+ async def chat_with_orchestrator(message: ChatMessage):
44
+ """Main chat endpoint with seamless orchestrator integration"""
45
+
46
+ # Process message through seamless orchestrator
47
+ orchestrator_result = await seamless_orchestrator.process_message(message.user_input)
48
+
49
+ if orchestrator_result["status"] == "orchestrator_asking":
50
+ # Orchestrator needs more info - return orchestrator question
51
+ return {
52
+ "type": "orchestrator_question",
53
+ "responses": [{
54
+ "persona": "PhD Advisor Assistant",
55
+ "response": orchestrator_result["orchestrator_question"]
56
+ }],
57
+ "collected_info": orchestrator_result["collected_info"]
58
+ }
59
+
60
+ elif orchestrator_result["status"] == "ready_for_advisors":
61
+ # Ready for advisor consultation
62
+ enhanced_context = orchestrator_result["enhanced_context"]
63
+
64
+ # Set active advisors
65
+ chat_orchestrator.set_active_personas(["methodist", "theorist", "pragmatist"])
66
+
67
+ # Get advisor responses
68
+ advisor_responses = await chat_orchestrator.process_user_input(enhanced_context)
69
+
70
+ return {
71
+ "type": "advisor_responses",
72
+ "responses": advisor_responses,
73
+ "collected_info": orchestrator_result["collected_info"]
74
+ }
75
+
76
+ # Fallback - shouldn't reach here normally
77
+ return {
78
+ "type": "error",
79
+ "responses": [{
80
+ "persona": "System",
81
+ "response": "I'm having trouble processing your request. Could you please try again?"
82
+ }]
83
+ }
84
+
85
+ @router.post("/reset-session")
86
+ async def reset_session():
87
+ """Reset the orchestrator session"""
88
+ seamless_orchestrator.reset()
89
+ # Also reset chat orchestrator history if needed
90
+ chat_orchestrator.history = []
91
+ return {"status": "reset", "message": "Session reset successfully"}
92
+
93
+ # Keep the old endpoints for backward compatibility
94
+ @router.post("/chat-direct")
95
+ async def chat_direct(
96
  user_input: str = Body(...),
97
+ active_personas: List[str] = Body(default=["methodist", "theorist", "pragmatist"])
98
  ):
99
+ """Direct chat endpoint bypassing orchestrator (for testing)"""
100
+ chat_orchestrator.set_active_personas(active_personas)
101
+ responses = await chat_orchestrator.process_user_input(user_input)
102
+ return responses