Skip to content

Commit bd616bd

Browse files
committed
adding C# code
1 parent dc06a03 commit bd616bd

File tree

2 files changed

+168
-14
lines changed

2 files changed

+168
-14
lines changed

articles/ai-foundry/agents/how-to/tools/deep-research-samples.md

Lines changed: 156 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ms.date: 07/10/2025
1010
author: aahill
1111
ms.author: aahi
1212
ms.custom: references_regions
13+
zone_pivot_groups: selection-deep-research
1314
---
1415

1516
# How to use the Deep Research tool
@@ -23,20 +24,6 @@ Use this article to learn how to use the Deep Research tool with the Azure AI Pr
2324
## Prerequisites
2425

2526
* The requirements in the [Deep Research overview](./deep-research.md).
26-
* The Deep Research tool requires the latest prerelease versions of the `azure-ai-projects` library. First we recommend creating a [virtual environment](https://docs.python.org/3/library/venv.html) to work in:
27-
28-
```console
29-
python -m venv env
30-
# after creating the virtual environment, activate it with:
31-
.\env\Scripts\activate
32-
```
33-
34-
You can install the package with the following command:
35-
36-
```console
37-
pip install --pre azure-ai-projects
38-
```
39-
4027
* Your Azure AI Foundry Project endpoint.
4128

4229
You can find your endpoint in the main project **overview** for your project in the [Azure AI Foundry portal](https://ai.azure.com/?cid=learnDocs), under **Endpoint and keys** > **Libraries** > **Azure AI Foundry**.
@@ -62,6 +49,159 @@ Use this article to learn how to use the Deep Research tool with the Azure AI Pr
6249
6350
## Create an agent with the Deep Research tool
6451

52+
:::zone pivot="csharp"
53+
54+
>[!NOTE]
55+
> You need version `1.1.0-beta.4` or later of the `Azure.AI.Agents.Persistent` package, and the `Azure.Identity` package.
56+
57+
```csharp
58+
using Azure;
59+
using Azure.AI.Agents.Persistent;
60+
using Azure.Identity;
61+
using System.Collections.Generic;
62+
using System.Text;
63+
64+
var projectEndpoint = System.Environment.GetEnvironmentVariable("PROJECT_ENDPOINT");
65+
var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME");
66+
var deepResearchModelDeploymentName = System.Environment.GetEnvironmentVariable("DEEP_RESEARCH_MODEL_DEPLOYMENT_NAME");
67+
var connectionId = System.Environment.GetEnvironmentVariable("AZURE_BING_CONECTION_ID");
68+
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
69+
70+
// DeepResearchToolDefinition should be initialized with the name of deep research model and the Bing connection ID,
71+
// needed to perform the search in the internet.
72+
73+
DeepResearchToolDefinition deepResearch = new(
74+
new DeepResearchDetails(
75+
model: deepResearchModelDeploymentName,
76+
bingGroundingConnections: [
77+
new DeepResearchBingGroundingConnection(connectionId)
78+
]
79+
)
80+
);
81+
82+
// NOTE: To reuse existing agent, fetch it with get_agent(agent_id)
83+
PersistentAgent agent = client.Administration.CreateAgent(
84+
model: modelDeploymentName,
85+
name: "Science Tutor",
86+
instructions: "You are a helpful Agent that assists in researching scientific topics.",
87+
tools: [deepResearch]
88+
);
89+
90+
//Create a thread and run and wait for the run to complete.
91+
92+
PersistentAgentThreadCreationOptions threadOp = new();
93+
threadOp.Messages.Add(new ThreadMessageOptions(
94+
role: MessageRole.User,
95+
content: "Research the current state of studies on orca intelligence and orca language, " +
96+
"including what is currently known about orcas' cognitive capabilities, " +
97+
"communication systems and problem-solving reflected in recent publications in top thier scientific " +
98+
"journals like Science, Nature and PNAS."
99+
));
100+
ThreadAndRunOptions opts = new()
101+
{
102+
ThreadOptions = threadOp,
103+
};
104+
ThreadRun run = client.CreateThreadAndRun(
105+
assistantId: agent.Id,
106+
options: opts
107+
);
108+
109+
Console.WriteLine("Start processing the message... this may take a few minutes to finish. Be patient!");
110+
do
111+
{
112+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
113+
run = client.Runs.GetRun(run.ThreadId, run.Id);
114+
}
115+
while (run.Status == RunStatus.Queued
116+
|| run.Status == RunStatus.InProgress);
117+
118+
// We will create a helper function PrintMessagesAndSaveSummary, which prints the response from the agent,
119+
// and replaces the reference placeholders by links in Markdown format.
120+
// It also saves the research summary in the file for convenience.
121+
122+
static void PrintMessagesAndSaveSummary(IEnumerable<PersistentThreadMessage> messages, string summaryFilePath)
123+
{
124+
string lastAgentMessage = default;
125+
foreach (PersistentThreadMessage threadMessage in messages)
126+
{
127+
StringBuilder sbAgentMessage = new();
128+
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
129+
foreach (MessageContent contentItem in threadMessage.ContentItems)
130+
{
131+
if (contentItem is MessageTextContent textItem)
132+
{
133+
string response = textItem.Text;
134+
if (textItem.Annotations != null)
135+
{
136+
foreach (MessageTextAnnotation annotation in textItem.Annotations)
137+
{
138+
if (annotation is MessageTextUriCitationAnnotation uriAnnotation)
139+
{
140+
response = response.Replace(uriAnnotation.Text, $" [{uriAnnotation.UriCitation.Title}]({uriAnnotation.UriCitation.Uri})");
141+
}
142+
}
143+
}
144+
if (threadMessage.Role == MessageRole.Agent)
145+
sbAgentMessage.Append(response);
146+
Console.Write($"Agent response: {response}");
147+
}
148+
else if (contentItem is MessageImageFileContent imageFileItem)
149+
{
150+
Console.Write($"<image from ID: {imageFileItem.FileId}");
151+
}
152+
Console.WriteLine();
153+
}
154+
if (threadMessage.Role == MessageRole.Agent)
155+
lastAgentMessage = sbAgentMessage.ToString();
156+
}
157+
if (!string.IsNullOrEmpty(lastAgentMessage))
158+
{
159+
File.WriteAllText(
160+
path: summaryFilePath,
161+
contents: lastAgentMessage);
162+
}
163+
}
164+
165+
//List the messages, print them and save the result in research_summary.md file.
166+
//The file will be saved next to the compiled executable.
167+
168+
Pageable<PersistentThreadMessage> messages
169+
= client.Messages.GetMessages(
170+
threadId: run.ThreadId, order: ListSortOrder.Ascending);
171+
PrintMessagesAndSaveSummary([.. messages], "research_summary.md");
172+
173+
// NOTE: Comment out these two lines if you want to delete the agent.
174+
client.Threads.DeleteThread(threadId: run.ThreadId);
175+
client.Administration.DeleteAgent(agentId: agent.Id);
176+
```
177+
178+
:::zone-end
179+
180+
:::zone pivot="javascript"
181+
182+
```javascript
183+
TBD
184+
```
185+
186+
:::zone-end
187+
188+
:::zone pivot="python"
189+
190+
The Deep Research tool requires the latest prerelease versions of the `azure-ai-projects` library. First we recommend creating a [virtual environment](https://docs.python.org/3/library/venv.html) to work in:
191+
192+
```console
193+
python -m venv env
194+
# after creating the virtual environment, activate it with:
195+
.\env\Scripts\activate
196+
```
197+
198+
You can install the package with the following command:
199+
200+
```console
201+
pip install --pre azure-ai-projects
202+
```
203+
204+
65205
```python
66206
import os, time
67207
from typing import Optional
@@ -198,6 +338,8 @@ with project_client:
198338
print("Deleted agent")
199339
```
200340

341+
:::zone-end
342+
201343
> [!NOTE]
202344
> Limitation: The Deep Research tool is currently recommended only in nonstreaming scenarios. Using it with streaming can work, but it might occasionally time out and is therefore not recommended.
203345

zone-pivots/zone-pivot-groups.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,18 @@ groups:
10981098
title: Python
10991099
- id: rest-api
11001100
title: REST API
1101+
- id: selection-deep-research
1102+
title: Selections
1103+
prompt: What would you like to see?
1104+
pivots:
1105+
- id: portal
1106+
title: Azure AI Foundry portal
1107+
- id: csharp
1108+
title: C#
1109+
- id: javascript
1110+
title: JavaScript
1111+
- id: python
1112+
title: Python
11011113
# owner: aahi
11021114
- id: selection-fabric-data-agent
11031115
title: Selections

0 commit comments

Comments
 (0)