Skip to content

Commit 3c51102

Browse files
author
Shreyas-Microsoft
committed
cleanup duplicate agents
1 parent f2da255 commit 3c51102

File tree

3 files changed

+26
-48
lines changed

3 files changed

+26
-48
lines changed

src/App/WebApp.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ COPY --from=frontend /home/node/app/static /usr/src/app/static/
3636
WORKDIR /usr/src/app
3737
EXPOSE 80
3838

39-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80", "--log-level", "info", "--access-log"]
39+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80", "--workers", "1", "--log-level", "info", "--access-log"]

src/App/app.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,21 @@ def create_app():
7676
app.config["TEMPLATES_AUTO_RELOAD"] = True
7777

7878
# Setup agent initialization and cleanup
79-
# @app.before_serving
80-
# async def startup():
81-
# app.wealth_advisor_agent = await AgentFactory.get_wealth_advisor_agent()
82-
# logging.info("Wealth Advisor Agent initialized during application startup")
83-
# app.search_agent = await AgentFactory.get_search_agent()
84-
# logging.info(
85-
# "Call Transcript Search Agent initialized during application startup"
86-
# )
87-
88-
# @app.after_serving
89-
# async def shutdown():
90-
# await AgentFactory.delete_all_agent_instance()
91-
# app.wealth_advisor_agent = None
92-
# app.search_agent = None
93-
# logging.info("Agents cleaned up during application shutdown")
79+
@app.before_serving
80+
async def startup():
81+
app.wealth_advisor_agent = await AgentFactory.get_wealth_advisor_agent()
82+
logging.info("Wealth Advisor Agent initialized during application startup")
83+
app.search_agent = await AgentFactory.get_search_agent()
84+
logging.info(
85+
"Call Transcript Search Agent initialized during application startup"
86+
)
87+
88+
@app.after_serving
89+
async def shutdown():
90+
await AgentFactory.delete_all_agent_instance()
91+
app.wealth_advisor_agent = None
92+
app.search_agent = None
93+
logging.info("Agents cleaned up during application shutdown")
9494

9595
# app.secret_key = secrets.token_hex(16)
9696
# app.session_interface = SecureCookieSessionInterface()
@@ -1359,4 +1359,4 @@ def get_users():
13591359
return str(e), 500
13601360

13611361

1362-
app = create_app()
1362+
app = create_app()
Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1-
import logging
21
from quart import current_app
32
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentThread
43
from semantic_kernel.contents.chat_message_content import ChatMessageContent
54
from semantic_kernel.contents.utils.author_role import AuthorRole
65

7-
from backend.agents.agent_factory import AgentFactory
86
from backend.common.config import config
97
from 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

2610
async 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

Comments
 (0)