@@ -4,12 +4,10 @@ To enable your Agent to perform search through Bing search API, you use `BingGro
441 . First we need to create an agent and read the environment variables, which will be used in the next steps.
55
66``` C# Snippet:AgentsBingGrounding_CreateProject
7-
8- // Get Connection information from Environment Variables
9- // To use App Config instead: https://learn.microsoft.com/en-us/visualstudio/ide/managing-application-settings-dotnet
10- var projectEndpoint = System .Environment .GetEnvironmentVariable (" PROJECT_ENDPOINT" );
11- var modelDeploymentName = System .Environment .GetEnvironmentVariable (" MODEL_DEPLOYMENT_NAME" );
12- var connectionId = System .Environment .GetEnvironmentVariable (" AZURE_BING_CONECTION_ID" );
7+ // Get Connection information from App Configuration
8+ var projectEndpoint = configuration [" ProjectEndpoint" ];
9+ var modelDeploymentName = configuration [" ModelDeploymentName" ];
10+ var bingConnectionId = configuration [" BingConnectionId" ];
1311
1412// Create the Agent Client
1513PersistentAgentsClient agentClient = new (projectEndpoint , new DefaultAzureCredential ());
@@ -18,53 +16,58 @@ PersistentAgentsClient agentClient = new(projectEndpoint, new DefaultAzureCreden
18162 . We will use the Bing connection Id to initialize the ` BingGroundingToolDefinition ` .
1917
2018``` C# Snippet:AgentsBingGrounding_GetConnection
21- ToolConnectionList connectionList = new ()
22- {
23- ConnectionList = { new ToolConnection (bingConnectionId ) }
24- };
25- BingGroundingToolDefinition bingGroundingTool = new (connectionList );
26- ```
19+ // Create the BingGroundingToolDefinition object used when creating the agent
20+ BingGroundingToolDefinition bingGroundingTool = new BingGroundingToolDefinition (
21+ new BingGroundingSearchConfigurationList (
22+ [
23+ new BingGroundingSearchConfiguration (bingConnectionId )
24+ ]
25+ )
26+ );
2727
28+ ```
28293 . We will use the ` BingGroundingToolDefinition ` during the agent initialization.
2930
3031Synchronous sample:
3132``` C# Snippet:AgentsBingGrounding_CreateAgent
3233// Create the Agent
33- PersistentAgent agent = agentClient .CreateAgent (
34- model : modelDeploymentName ,
35- name : " my-agent" ,
36- instructions : " You are a helpful agent." ,
37- tools : [bingGroundingTool ]);
34+ PersistentAgent agent = agentClient .Administration .CreateAgent (
35+ model : modelDeploymentName ,
36+ name : " my-agent" ,
37+ instructions : " You are a helpful agent." ,
38+ tools : [bingGroundingTool ]
39+ );
3840```
3941
4042Asynchronous sample:
4143``` C# Snippet:AgentsBingGroundingAsync_CreateAgent
4244// Create the Agent
43- PersistentAgent agent = await agentClient .CreateAgentAsync (
44- model : modelDeploymentName ,
45- name : " my-agent" ,
46- instructions : " You are a helpful agent." ,
47- tools : [ bingGroundingTool ]);
45+ PersistentAgent agent = await agentClient .Administration .CreateAgentAsync (
46+ model : modelDeploymentName ,
47+ name : " my-agent" ,
48+ instructions : " You are a helpful agent." ,
49+ tools : [bingGroundingTool ]
50+ );
4851```
4952
50534 . Now we will create the thread, add the message containing a question for agent and start the run.
5154
5255Synchronous sample:
5356``` C# Snippet:AgentsBingGrounding_CreateThreadMessage
54- PersistentAgentThread thread = agentClient .CreateThread ();
57+ PersistentAgentThread thread = agentClient .Threads . CreateThread ();
5558
5659// Create message and run the agent
57- ThreadMessage message = agentClient .CreateMessage (
60+ ThreadMessage message = agentClient .Messages . CreateMessage (
5861 thread .Id ,
5962 MessageRole .User ,
6063 " How does wikipedia explain Euler's Identity?" );
61- ThreadRun run = agentClient .CreateRun (thread , agent );
64+ ThreadRun run = agentClient .Runs . CreateRun (thread , agent );
6265
6366// Wait for the agent to finish running
6467do
6568{
6669 Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
67- run = agentClient .GetRun (thread .Id , run .Id );
70+ run = agentClient .Runs . GetRun (thread .Id , run .Id );
6871}
6972while (run .Status == RunStatus .Queued
7073 || run .Status == RunStatus .InProgress );
@@ -79,20 +82,20 @@ if (run.Status != RunStatus.Completed)
7982
8083Asynchronous sample:
8184``` C# Snippet:AgentsBingGroundingAsync_CreateThreadMessage
82- PersistentAgentThread thread = await agentClient .CreateThreadAsync ();
85+ PersistentAgentThread thread = await agentClient .Threads . CreateThreadAsync ();
8386
8487// Create message and run the agent
85- ThreadMessage message = await agentClient .CreateMessageAsync (
88+ ThreadMessage message = await agentClient .Messages . CreateMessageAsync (
8689 thread .Id ,
8790 MessageRole .User ,
8891 " How does wikipedia explain Euler's Identity?" );
89- ThreadRun run = await agentClient .CreateRunAsync (thread , agent );
92+ ThreadRun run = await agentClient .Runs . CreateRunAsync (thread , agent );
9093
9194// Wait for the agent to finish running
9295do
9396{
94- await Task . Delay (TimeSpan .FromMilliseconds (500 ));
95- run = await agentClient .GetRunAsync (thread .Id , run .Id );
97+ Thread . Sleep (TimeSpan .FromMilliseconds (500 ));
98+ run = await agentClient .Runs . GetRunAsync (thread .Id , run .Id );
9699}
97100while (run .Status == RunStatus .Queued
98101 || run .Status == RunStatus .InProgress );
@@ -102,15 +105,14 @@ if (run.Status != RunStatus.Completed)
102105{
103106 throw new Exception (" Run did not complete successfully, error: " + run .LastError ? .Message );
104107}
105-
106108```
107109
1081105 . Print the agent messages to console in chronological order (including formatting URL citations).
109111
110112Synchronous sample:
111113``` C# Snippet:AgentsBingGrounding_Print
112114// Retrieve all messages from the agent client
113- PageableList < ThreadMessage > messages = agentClient .GetMessages (
115+ Pageable < ThreadMessage > messages = agentClient . Messages .GetMessages (
114116 threadId : thread .Id ,
115117 order : ListSortOrder .Ascending
116118);
@@ -149,15 +151,16 @@ foreach (ThreadMessage threadMessage in messages)
149151
150152Asynchronous sample:
151153``` C# Snippet:AgentsBingGroundingAsync_Print
152- PageableList < ThreadMessage > messages = await agentClient .GetMessagesAsync (
154+ // Retrieve all messages from the agent client
155+ AsyncPageable < ThreadMessage > messages = agentClient .Messages .GetMessagesAsync (
153156 threadId : thread .Id ,
154157 order : ListSortOrder .Ascending
155158);
156159
157- foreach (ThreadMessage threadMessage in messages )
160+ // Process messages in order
161+ await foreach (ThreadMessage threadMessage in messages )
158162{
159163 Console .Write ($" {threadMessage .CreatedAt : yyyy - MM - dd HH : mm : ss } - {threadMessage .Role ,10 }: " );
160-
161164 foreach (MessageContent contentItem in threadMessage .ContentItems )
162165 {
163166 if (contentItem is MessageTextContent textItem )
@@ -191,13 +194,13 @@ foreach (ThreadMessage threadMessage in messages)
191194Synchronous sample:
192195``` C# Snippet:AgentsBingGrounding_Cleanup
193196// Delete thread and agent
194- agentClient .DeleteThread (threadId : thread .Id );
195- agentClient .DeleteAgent (agentId : agent .Id );
197+ agentClient .Threads . DeleteThread (threadId : thread .Id );
198+ agentClient .Administration . DeleteAgent (agentId : agent .Id );
196199```
197200
198201Asynchronous sample:
199202``` C# Snippet:AgentsBingGroundingAsync_Cleanup
200203// Delete thread and agent
201- await agentClient .DeleteThreadAsync (threadId : thread .Id );
202- await agentClient .DeleteAgentAsync (agentId : agent .Id );
204+ agentClient .Threads . DeleteThread (threadId : thread .Id );
205+ agentClient .Administration . DeleteAgent (agentId : agent .Id );
203206```
0 commit comments