1414from ..core .globals import initialize_kernel , get_agent_registry
1515from ..auth .middleware import get_current_user
1616from ..services .agentic_vector_rag_service import agentic_rag_service
17+ from ..services .azure_ai_agents_service import azure_ai_agents_service
1718from ..services .token_usage_tracker import token_tracker
19+ from ..services .azure_services import get_azure_service_manager
1820
1921router = APIRouter ()
2022
@@ -61,6 +63,15 @@ async def generate():
6163 if not agentic_rag_service .search_client :
6264 await agentic_rag_service .initialize ()
6365
66+ azure_service_manager = await get_azure_service_manager ()
67+ user_message = {
68+ "role" : "user" ,
69+ "content" : request .prompt ,
70+ "timestamp" : datetime .utcnow ().isoformat (),
71+ "mode" : request .mode
72+ }
73+ await azure_service_manager .save_session_history (session_id , user_message )
74+
6475 yield f"data: { json .dumps ({'type' : 'metadata' , 'session_id' : session_id , 'mode' : request .mode , 'timestamp' : datetime .utcnow ().isoformat ()})} \n \n "
6576
6677 if request .mode == "agentic-rag" :
@@ -103,6 +114,16 @@ async def generate():
103114 }
104115 yield f"data: { json .dumps ({'type' : 'metadata' , 'processing' : processing_metadata })} \n \n "
105116
117+ assistant_message = {
118+ "role" : "assistant" ,
119+ "content" : answer ,
120+ "timestamp" : datetime .utcnow ().isoformat (),
121+ "citations" : citations ,
122+ "token_usage" : token_usage ,
123+ "processing_metadata" : processing_metadata
124+ }
125+ await azure_service_manager .save_session_history (session_id , assistant_message )
126+
106127 yield f"data: { json .dumps ({'type' : 'done' , 'session_id' : session_id })} \n \n "
107128
108129 except Exception as e :
@@ -161,9 +182,9 @@ async def process_fast_rag(prompt: str, session_id: str) -> Dict[str, Any]:
161182 "citations" : citations ,
162183 "query_rewrites" : [prompt ], # No rewrites in fast mode
163184 "token_usage" : {
164- "prompt_tokens" : len ( prompt . split ()),
165- "completion_tokens" : len ( answer . split ()) ,
166- "total_tokens" : len ( prompt . split ()) + len ( answer . split ())
185+ "prompt_tokens" : 0 , # Fast RAG doesn't use LLM, so no tokens
186+ "completion_tokens" : 0 ,
187+ "total_tokens" : 0
167188 },
168189 "processing_time_ms" : 0 , # Will be calculated by caller
169190 "retrieval_method" : "fast_rag" ,
@@ -180,38 +201,34 @@ async def process_fast_rag(prompt: str, session_id: str) -> Dict[str, Any]:
180201 }
181202
182203async def process_deep_research_rag (prompt : str , session_id : str , verification_level : str ) -> Dict [str , Any ]:
183- """Process Deep Research RAG mode with comprehensive verification """
204+ """Process Deep Research RAG mode using Azure AI Agents """
184205 try :
185- agentic_result = await agentic_rag_service .process_question (
186- question = prompt ,
187- rag_mode = "deep-research-rag" ,
188- session_id = session_id
189- )
190-
191- verification_docs = await retriever .invoke (prompt )
206+ from ..services .token_usage_tracker import ServiceType , OperationType
192207
193- combined_citations = agentic_result .get ("citations" , [])
208+ tracking_id = token_tracker .start_tracking (
209+ session_id = session_id ,
210+ service_type = ServiceType .DEEP_RESEARCH_RAG ,
211+ operation_type = OperationType .ANSWER_GENERATION ,
212+ endpoint = "/deep-research-rag" ,
213+ rag_mode = "deep-research-rag"
214+ )
194215
195- for i , doc in enumerate (verification_docs [:2 ]): # Add top 2 verification docs
196- combined_citations .append ({
197- 'id' : str (len (combined_citations ) + 1 ),
198- 'title' : doc .get ('title' , f'Verification Document { i + 1 } ' ),
199- 'content' : doc .get ('content' , '' )[:300 ],
200- 'source' : doc .get ('source' , '' ),
201- 'score' : doc .get ('score' , 0.0 ),
202- 'verification' : True
203- })
216+ agents_result = await azure_ai_agents_service .process_deep_research (
217+ question = prompt ,
218+ session_id = session_id ,
219+ tracking_id = tracking_id
220+ )
204221
205- base_answer = agentic_result .get ("answer" , "" )
206- verification_note = f"\n \n *This response has been enhanced with { verification_level } verification using additional sources .*"
222+ base_answer = agents_result .get ("answer" , "" )
223+ verification_note = f"\n \n *This response has been generated using Azure AI Agents deep research with { verification_level } verification.*"
207224
208225 return {
209226 "answer" : base_answer + verification_note ,
210- "citations" : combined_citations ,
211- "query_rewrites" : agentic_result .get ("query_rewrites" , [prompt ]),
212- "token_usage" : agentic_result .get ("token_usage" , {}),
213- "processing_time_ms" : agentic_result . get ( "processing_time_ms" , 0 ),
214- "retrieval_method" : "deep_research_rag " ,
227+ "citations" : agents_result . get ( "citations" , []) ,
228+ "query_rewrites" : agents_result .get ("query_rewrites" , [prompt ]),
229+ "token_usage" : agents_result .get ("token_usage" , {}),
230+ "processing_time_ms" : 0 , # Will be calculated by caller
231+ "retrieval_method" : "azure_ai_agents_deep_research " ,
215232 "verification_level" : verification_level ,
216233 "success" : True
217234 }
@@ -224,3 +241,65 @@ async def process_deep_research_rag(prompt: str, session_id: str, verification_l
224241 "token_usage" : {"total_tokens" : 0 , "error" : str (e )},
225242 "success" : False
226243 }
244+
245+ @router .get ("/chat/sessions/{session_id}/history" )
246+ async def get_session_history (session_id : str , current_user : dict = Depends (get_current_user )):
247+ """Get chat session history"""
248+ try :
249+ azure_service_manager = await get_azure_service_manager ()
250+ history = await azure_service_manager .get_session_history (session_id )
251+ return {"session_id" : session_id , "messages" : history }
252+ except Exception as e :
253+ raise HTTPException (status_code = 500 , detail = str (e ))
254+
255+ @router .delete ("/chat/sessions/{session_id}" )
256+ async def clear_session_history (session_id : str , current_user : dict = Depends (get_current_user )):
257+ """Clear chat session history"""
258+ try :
259+ azure_service_manager = await get_azure_service_manager ()
260+ empty_session = {
261+ "role" : "system" ,
262+ "content" : "Session cleared" ,
263+ "timestamp" : datetime .utcnow ().isoformat ()
264+ }
265+ await azure_service_manager .save_session_history (f"{ session_id } _cleared" , empty_session )
266+ return {"session_id" : session_id , "status" : "cleared" }
267+ except Exception as e :
268+ raise HTTPException (status_code = 500 , detail = str (e ))
269+
270+ @router .get ("/chat/sessions" )
271+ async def list_user_sessions (current_user : dict = Depends (get_current_user )):
272+ """List all sessions for the current user (placeholder implementation)"""
273+ try :
274+ return {"sessions" : [], "message" : "Session listing not yet implemented" }
275+ except Exception as e :
276+ raise HTTPException (status_code = 500 , detail = str (e ))
277+
278+ class FollowUpRequest (BaseModel ):
279+ original_question : str
280+ answer : str
281+ session_id : Optional [str ] = None
282+
283+ @router .post ("/chat/follow-up-questions" )
284+ async def generate_follow_up_questions (request : FollowUpRequest , current_user : dict = Depends (get_current_user )):
285+ """Generate follow-up questions based on the original question and answer"""
286+ try :
287+ session_id = request .session_id or str (uuid .uuid4 ())
288+
289+ if not azure_ai_agents_service .agents_client :
290+ await azure_ai_agents_service .initialize ()
291+
292+ result = await azure_ai_agents_service .generate_follow_up_questions (
293+ original_question = request .original_question ,
294+ answer = request .answer ,
295+ session_id = session_id
296+ )
297+
298+ return {
299+ "session_id" : session_id ,
300+ "follow_up_questions" : result .get ("follow_up_questions" , []),
301+ "token_usage" : result .get ("token_usage" , {}),
302+ "success" : result .get ("success" , False )
303+ }
304+ except Exception as e :
305+ raise HTTPException (status_code = 500 , detail = str (e ))
0 commit comments