Skip to content

Commit 0cfa0e2

Browse files
Refactor topic mapping agent calls to reuse agent instance for improved efficiency
1 parent 04c8348 commit 0cfa0e2

2 files changed

Lines changed: 41 additions & 39 deletions

File tree

infra/scripts/index_scripts/03_cu_process_data_text.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ async def process_files():
382382
docs.extend(await prepare_search_doc(content, conversation_id, path.name, embeddings_client))
383383
counter += 1
384384
except Exception:
385-
logging.exception("Error processing transcript file %s", path.name)
385+
pass
386386
if docs != [] and counter % 10 == 0:
387387
result = search_client.upload_documents(documents=docs)
388388
docs = []
@@ -536,22 +536,11 @@ async def call_topic_mining_agent(topics_str1):
536536
mined_topics = ", ".join(mined_topics_list)
537537
print(f"✓ Mined {len(mined_topics_list)} topics")
538538

539-
async def call_topic_mapping_agent(input_text, list_of_topics):
539+
async def call_topic_mapping_agent(agent, input_text, list_of_topics):
540540
"""Use Topic Mapping Agent with Agent Framework to map topic to category."""
541-
async with (
542-
AsyncAzureCliCredential(process_timeout=30) as async_cred,
543-
AIProjectClient(endpoint=AI_PROJECT_ENDPOINT, credential=async_cred) as project_client,
544-
):
545-
# Create provider for agent management
546-
provider = AzureAIProjectAgentProvider(project_client=project_client)
547-
548-
# Get agent using provider
549-
agent = await provider.get_agent(name=TOPIC_MAPPING_AGENT_NAME)
550-
551-
query = f"""Find the closest topic for this text: '{input_text}' from this list of topics: {list_of_topics}"""
552-
553-
result = await agent.run(query)
554-
return result.text.strip()
541+
query = f"""Find the closest topic for this text: '{input_text}' from this list of topics: {list_of_topics}"""
542+
result = await agent.run(query)
543+
return result.text.strip()
555544

556545
cursor.execute('SELECT * FROM processed_data')
557546
rows = [tuple(row) for row in cursor.fetchall()]
@@ -562,10 +551,22 @@ async def call_topic_mapping_agent(input_text, list_of_topics):
562551
# Map topics using agent asynchronously
563552
async def map_all_topics():
564553
"""Map all topics to categories using agent."""
565-
for _, row in df_processed_data.iterrows():
566-
mined_topic_str = await call_topic_mapping_agent(row['topic'], str(mined_topics_list))
567-
cursor.execute("UPDATE processed_data SET mined_topic = ? WHERE ConversationId = ?", (mined_topic_str, row['ConversationId']))
568-
conn.commit()
554+
# Create credential, project client, provider, and agent once for reuse
555+
async with (
556+
AsyncAzureCliCredential(process_timeout=30) as async_cred,
557+
AIProjectClient(endpoint=AI_PROJECT_ENDPOINT, credential=async_cred) as project_client,
558+
):
559+
# Create provider for agent management
560+
provider = AzureAIProjectAgentProvider(project_client=project_client)
561+
562+
# Get agent using provider
563+
agent = await provider.get_agent(name=TOPIC_MAPPING_AGENT_NAME)
564+
565+
# Process all rows using the same agent instance
566+
for _, row in df_processed_data.iterrows():
567+
mined_topic_str = await call_topic_mapping_agent(agent, row['topic'], str(mined_topics_list))
568+
cursor.execute("UPDATE processed_data SET mined_topic = ? WHERE ConversationId = ?", (mined_topic_str, row['ConversationId']))
569+
conn.commit()
569570

570571
asyncio.run(map_all_topics())
571572

infra/scripts/index_scripts/04_cu_process_custom_data.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -635,22 +635,11 @@ async def call_topic_mining_agent(topics_str1):
635635
mined_topics = ", ".join(mined_topics_list)
636636
print(f"✓ Mined {len(mined_topics_list)} topics")
637637

638-
async def call_topic_mapping_agent(input_text, list_of_topics):
638+
async def call_topic_mapping_agent(agent, input_text, list_of_topics):
639639
"""Use Topic Mapping Agent with Agent Framework to map topic to category."""
640-
async with (
641-
AsyncAzureCliCredential(process_timeout=30) as async_cred,
642-
AIProjectClient(endpoint=AI_PROJECT_ENDPOINT, credential=async_cred) as project_client,
643-
):
644-
# Create provider for agent management
645-
provider = AzureAIProjectAgentProvider(project_client=project_client)
646-
647-
# Get agent using provider
648-
agent = await provider.get_agent(name=TOPIC_MAPPING_AGENT_NAME)
649-
650-
query = f"""Find the closest topic for this text: '{input_text}' from this list of topics: {list_of_topics}"""
651-
652-
result = await agent.run(query)
653-
return result.text.strip()
640+
query = f"""Find the closest topic for this text: '{input_text}' from this list of topics: {list_of_topics}"""
641+
result = await agent.run(query)
642+
return result.text.strip()
654643

655644
cursor.execute('SELECT * FROM processed_data')
656645
rows = [tuple(row) for row in cursor.fetchall()]
@@ -661,10 +650,22 @@ async def call_topic_mapping_agent(input_text, list_of_topics):
661650
# Map topics using agent asynchronously
662651
async def map_all_topics():
663652
"""Map all topics to categories using agent."""
664-
for _, row in df_processed_data.iterrows():
665-
mined_topic_str = await call_topic_mapping_agent(row['topic'], str(mined_topics_list))
666-
cursor.execute("UPDATE processed_data SET mined_topic = ? WHERE ConversationId = ?", (mined_topic_str, row['ConversationId']))
667-
conn.commit()
653+
# Create credential, project client, provider, and agent once for reuse
654+
async with (
655+
AsyncAzureCliCredential(process_timeout=30) as async_cred,
656+
AIProjectClient(endpoint=AI_PROJECT_ENDPOINT, credential=async_cred) as project_client,
657+
):
658+
# Create provider for agent management
659+
provider = AzureAIProjectAgentProvider(project_client=project_client)
660+
661+
# Get agent using provider
662+
agent = await provider.get_agent(name=TOPIC_MAPPING_AGENT_NAME)
663+
664+
# Process all rows using the same agent instance
665+
for _, row in df_processed_data.iterrows():
666+
mined_topic_str = await call_topic_mapping_agent(agent, row['topic'], str(mined_topics_list))
667+
cursor.execute("UPDATE processed_data SET mined_topic = ? WHERE ConversationId = ?", (mined_topic_str, row['ConversationId']))
668+
conn.commit()
668669

669670
asyncio.run(map_all_topics())
670671

0 commit comments

Comments
 (0)