|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from datetime import timedelta |
| 5 | + |
| 6 | +from azure.ai.agents.models import DeepResearchTool |
| 7 | +from azure.identity.aio import AzureCliCredential |
| 8 | + |
| 9 | +from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread, RunPollingOptions |
| 10 | +from semantic_kernel.contents import AnnotationContent, ChatMessageContent, FunctionCallContent, FunctionResultContent |
| 11 | + |
| 12 | +""" |
| 13 | +The following sample demonstrates how to create an AzureAIAgent along |
| 14 | +with the Deep Research Tool. Please visit the following documentation for more info |
| 15 | +on what is required to run the sample: https://aka.ms/agents-deep-research. Please pay |
| 16 | +attention to the purple `Note` boxes in the Azure docs. |
| 17 | +
|
| 18 | +Note that when you use your Bing Connection ID, it needs to be the connection ID from the project, not the resource. |
| 19 | +It has the following format: |
| 20 | +
|
| 21 | +'/subscriptions/<sub_id>/resourceGroups/<rg_name>/providers/<provider_name>/accounts/<account_name>/projects/<project_name>/connections/<connection_name>' |
| 22 | +""" |
| 23 | + |
| 24 | +TASK = ( |
| 25 | + "Research the current state of studies on orca intelligence and orca language, " |
| 26 | + "including what is currently known about orcas' cognitive capabilities and communication systems." |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +async def handle_intermediate_messages(message: ChatMessageContent) -> None: |
| 31 | + for item in message.items or []: |
| 32 | + if isinstance(item, FunctionResultContent): |
| 33 | + print(f"Function Result:> {item.result} for function: {item.name}") |
| 34 | + elif isinstance(item, FunctionCallContent): |
| 35 | + print(f"Function Call:> {item.name} with arguments: {item.arguments}") |
| 36 | + else: |
| 37 | + print(f"{item}") |
| 38 | + print() |
| 39 | + |
| 40 | + |
| 41 | +async def main() -> None: |
| 42 | + async with ( |
| 43 | + AzureCliCredential() as creds, |
| 44 | + AzureAIAgent.create_client(credential=creds) as client, |
| 45 | + ): |
| 46 | + azure_ai_agent_settings = AzureAIAgentSettings() |
| 47 | + # 1. Define the Deep Research tool |
| 48 | + deep_research_tool = DeepResearchTool( |
| 49 | + bing_grounding_connection_id=azure_ai_agent_settings.bing_connection_id, |
| 50 | + deep_research_model=azure_ai_agent_settings.deep_research_model, |
| 51 | + ) |
| 52 | + |
| 53 | + # 2. Create an agent with the tool on the Azure AI agent service |
| 54 | + agent_definition = await client.agents.create_agent( |
| 55 | + model="gpt-4o", |
| 56 | + tools=deep_research_tool.definitions, |
| 57 | + instructions="You are a helpful Agent that assists in researching scientific topics.", |
| 58 | + ) |
| 59 | + |
| 60 | + # 3. Create a Semantic Kernel agent for the Azure AI agent |
| 61 | + agent = AzureAIAgent(client=client, definition=agent_definition, name="DeepResearchAgent") |
| 62 | + |
| 63 | + # 4. Create a thread for the agent |
| 64 | + # If no thread is provided, a new thread will be |
| 65 | + # created and returned with the initial response |
| 66 | + thread: AzureAIAgentThread | None = None |
| 67 | + |
| 68 | + try: |
| 69 | + print(f"# User: '{TASK}'") |
| 70 | + # 5. Invoke the agent for the specified thread for response |
| 71 | + async for response in agent.invoke( |
| 72 | + messages=TASK, |
| 73 | + thread=thread, |
| 74 | + on_intermediate_message=handle_intermediate_messages, |
| 75 | + polling_options=RunPollingOptions(run_polling_timeout=timedelta(minutes=10)), |
| 76 | + ): |
| 77 | + # Note that the underlying Deep Research Tool uses the o3 reasoning model. |
| 78 | + # When using the non-streaming invoke, it is normal for there to be |
| 79 | + # several minutes of processing before agent response(s) appear. |
| 80 | + # For a fast response, consider using the streaming invoke method. |
| 81 | + # A sample exists in the `concepts/agents/azure_ai_agent` directory. |
| 82 | + print(f"# {response.name}: {response}") |
| 83 | + for item in response.items or []: |
| 84 | + if isinstance(item, AnnotationContent): |
| 85 | + label = item.title or item.url or (item.quote or "Annotation") |
| 86 | + print(f"\n[Annotation] {label} -> {item.url}") |
| 87 | + thread = response.thread |
| 88 | + finally: |
| 89 | + # 6. Cleanup: Delete the thread, agent, and file |
| 90 | + await thread.delete() if thread else None |
| 91 | + await client.agents.delete_agent(agent.id) |
| 92 | + |
| 93 | + """ |
| 94 | + Sample Output: |
| 95 | + |
| 96 | + # User: 'Research the current state of studies on orca intelligence and orca language, including what is |
| 97 | + currently known about orcas' cognitive capabilities and communication systems.' |
| 98 | +
|
| 99 | + Function Call:> deep_research with arguments: {'input': '{"prompt": "Research the current state of studies on |
| 100 | + orca intelligence and orca language. Include up-to-date findings on both their cognitive capabilities and |
| 101 | + communication systems. Structure the findings as a detailed report with well-formatted headers that break down |
| 102 | + topics such as orcas\' cognitive skills (e.g., problem-solving, memory, and social intelligence), their |
| 103 | + language or communication methods (e.g., vocalizations, dialects, and symbolic communication), and comparisons |
| 104 | + to other highly intelligent species. Include sources, prioritize recent peer-reviewed studies and reputable |
| 105 | + marine biology publications, and ensure the report is well-cited and clear."}'} |
| 106 | +
|
| 107 | + # DeepResearchAgent: Got it! I'll gather recent studies and findings on orca intelligence and their |
| 108 | + # communication systems, focusing on cognitive abilities and the mechanisms of their language. |
| 109 | + # I'll update you with a comprehensive overview soon. |
| 110 | +
|
| 111 | + Title: Current Research on Orca Intelligence and Language |
| 112 | +
|
| 113 | + Starting deep research... |
| 114 | +
|
| 115 | + # DeepResearchAgent: cot_summary: **Examining orca intelligence** |
| 116 | + The task involves looking into orca cognitive abilities, communication methods, and comparisons with |
| 117 | + other intelligent species. It includes recent peer-reviewed studies and credible sources. 【1†Bing Search】 |
| 118 | +
|
| 119 | +
|
| 120 | + [Annotation] Bing Search: '2024 orca intelligence cognitive study research' -> https://www.bing.com/search?q=2024%20orca%20intelligence%20cognitive%20study%20research |
| 121 | + # DeepResearchAgent: cot_summary: **Investigating orca intelligence** |
| 122 | +
|
| 123 | + I'm curious about headlines discussing orcas' cognitive abilities and behaviors, especially their tool use, |
| 124 | + recent attacks, and comparisons with dolphins. 【1†Bing Search】 |
| 125 | +
|
| 126 | +
|
| 127 | + [Annotation] Bing Search: 'orca cognition recent peer-reviewed studies orca communication recent study' -> https://www.bing.com/search?q=orca%20cognition%20recent%20peer-reviewed%20studies%20orca%20communication%20recent%20study |
| 128 | + # DeepResearchAgent: cot_summary: **Researching social dynamics** |
| 129 | +
|
| 130 | + I'm gathering info on killer whale social dynamics from what appears to be a 2024 article on ResearchGate. |
| 131 | +
|
| 132 | + # DeepResearchAgent: cot_summary: **Assessing publication type** |
| 133 | + I'm exploring if the ResearchGate link points to a preprint, student paper, or journal article. |
| 134 | + Progress is steady as I look into its initiation and source. |
| 135 | +
|
| 136 | + # DeepResearchAgent: cot_summary: **Investigating PDF issues** |
| 137 | +
|
| 138 | + ... |
| 139 | + """ |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + asyncio.run(main()) |
0 commit comments