1- import logging
21from quart import current_app
32from semantic_kernel .agents import AzureAIAgent , AzureAIAgentThread
43from semantic_kernel .contents .chat_message_content import ChatMessageContent
54from semantic_kernel .contents .utils .author_role import AuthorRole
65
7- from backend .agents .agent_factory import AgentFactory
86from backend .common .config import config
97from backend .services .sqldb_service import get_client_name_from_db
108
11- async def ensure_agents (app ):
12- """Ensure both agents are initialized on demand."""
13- if not hasattr (app , "wealth_advisor_agent" ) or app .wealth_advisor_agent is None :
14- app .wealth_advisor_agent = await AgentFactory .get_wealth_advisor_agent ()
15- logging .info ("Wealth Advisor Agent initialized on demand" )
16- if not hasattr (app , "search_agent" ) or app .search_agent is None :
17- app .search_agent = await AgentFactory .get_search_agent ()
18- logging .info ("Search Agent initialized on demand" )
19-
20- async def delete_agents (app ):
21- """Delete both agents after use."""
22- await AgentFactory .delete_all_agent_instance ()
23- app .wealth_advisor_agent = None
24- app .search_agent = None
259
2610async def stream_response_from_wealth_assistant (query : str , client_id : str ):
2711 """
2812 Streams real-time chat response from the Wealth Assistant.
2913 Uses Semantic Kernel agent with SQL and Azure Cognitive Search based on the client ID.
3014 """
31- thread = None
3215 try :
3316 # Dynamically get the name from the database
34- selected_client_name = get_client_name_from_db (client_id )
17+ selected_client_name = get_client_name_from_db (
18+ client_id
19+ ) # Optionally fetch from DB
3520
3621 # Prepare fallback instructions with the single-line prompt
3722 additional_instructions = config .STREAM_TEXT_SYSTEM_PROMPT
@@ -52,35 +37,28 @@ async def stream_response_from_wealth_assistant(query: str, client_id: str):
5237 "{client_id}" , client_id
5338 )
5439
55- # Lazy agent initialization
56- await ensure_agents (current_app )
5740 agent : AzureAIAgent = current_app .wealth_advisor_agent
5841
42+ thread : AzureAIAgentThread = None
5943 message = ChatMessageContent (role = AuthorRole .USER , content = query )
6044 sk_response = agent .invoke_stream (
6145 messages = [message ],
62- thread = None ,
46+ thread = thread ,
6347 additional_instructions = additional_instructions ,
6448 )
6549
6650 async def generate ():
67- nonlocal thread
6851 try :
6952 # yields deltaText strings one-by-one
7053 async for chunk in sk_response :
7154 if not chunk or not chunk .content :
7255 continue
73- thread = chunk .thread if hasattr (chunk , "thread" ) else None
7456 yield chunk .content # just the deltaText
7557 finally :
76- if thread :
77- await thread .delete ()
78- # Delete agents after operation
79- await delete_agents (current_app )
58+ thread = chunk .thread if chunk else None
59+ await thread .delete () if thread else None
8060
8161 return generate
8262 except Exception as e :
83- if thread :
84- await thread .delete ()
85- await delete_agents (current_app )
86- raise e
63+ await thread .delete () if thread else None
64+ raise e
0 commit comments