@@ -35,6 +35,123 @@ zone_pivot_groups: selection-bing-custom-grounding
35
35
36
36
::: zone-end
37
37
38
+ ::: zone pivot="csharp"
39
+
40
+ ## Step 1: Create a project client
41
+
42
+ Create a client object, which will contain the connection string for connecting to your AI project and other resources.
43
+
44
+ ``` csharp
45
+ using System ;
46
+ using System .Collections .Generic ;
47
+ using System .Threading .Tasks ;
48
+ using Azure .Core ;
49
+ using Azure .Core .TestFramework ;
50
+ using NUnit .Framework ;
51
+
52
+ var connectionString = System .Environment .GetEnvironmentVariable (" PROJECT_CONNECTION_STRING" );
53
+ var modelDeploymentName = System .Environment .GetEnvironmentVariable (" MODEL_DEPLOYMENT_NAME" );
54
+ var bingConnectionName = System .Environment .GetEnvironmentVariable (" BING_CONNECTION_NAME" );
55
+
56
+ var projectClient = new AIProjectClient (connectionString , new DefaultAzureCredential ());
57
+
58
+ AgentsClient agentClient = projectClient .GetAgentsClient ();
59
+ ```
60
+
61
+ ## Step 2: Create an Agent with the Grounding with Bing search tool enabled
62
+
63
+ To make the Grounding with Bing search tool available to your agent, use a connection to initialize the tool and attach it to the agent. You can find your connection in the ** connected resources** section of your project in the [ Azure AI Foundry portal] ( https://ai.azure.com/ ) .
64
+
65
+ ``` csharp
66
+ ConnectionResponse bingConnection = projectClient .GetConnectionsClient ().GetConnection (bingConnectionName );
67
+ var connectionId = bingConnection .Id ;
68
+
69
+ ToolConnectionList connectionList = new ()
70
+ {
71
+ ConnectionList = { new ToolConnection (connectionId ) }
72
+ };
73
+ BingGroundingToolDefinition bingGroundingTool = new (connectionList );
74
+
75
+ Agent agent = agentClient .CreateAgent (
76
+ model : modelDeploymentName ,
77
+ name : " my-assistant" ,
78
+ instructions : " You are a helpful assistant." ,
79
+ tools : [bingGroundingTool ]);
80
+ ```
81
+
82
+ ## Step 3: Create a thread
83
+
84
+ ``` csharp
85
+ AgentThread thread = agentClient .CreateThread ();
86
+
87
+ // Create message to thread
88
+ ThreadMessage message = agentClient .CreateMessage (
89
+ thread .Id ,
90
+ MessageRole .User ,
91
+ " How does wikipedia explain Euler's Identity?" );
92
+ ```
93
+
94
+ ## Step 4: Create a run and check the output
95
+
96
+ Create a run and observe that the model uses the Grounding with Bing Search tool to provide a response to the user's question.
97
+
98
+
99
+ ``` csharp
100
+
101
+ // Run the agent
102
+ ThreadRun run = agentClient .CreateRun (thread , agent );
103
+ do
104
+ {
105
+ Thread .Sleep (TimeSpan .FromMilliseconds (500 ));
106
+ run = agentClient .GetRun (thread .Id , run .Id );
107
+ }
108
+ while (run .Status == RunStatus .Queued
109
+ || run .Status == RunStatus .InProgress );
110
+
111
+ Assert .AreEqual (
112
+ RunStatus .Completed ,
113
+ run .Status ,
114
+ run .LastError ? .Message );
115
+
116
+ PageableList < ThreadMessage > messages = agentClient .GetMessages (
117
+ threadId : thread .Id ,
118
+ order : ListSortOrder .Ascending
119
+ );
120
+
121
+ foreach (ThreadMessage threadMessage in messages )
122
+ {
123
+ Console .Write ($" {threadMessage .CreatedAt : yyyy - MM - dd HH : mm : ss } - {threadMessage .Role ,10 }: " );
124
+ foreach (MessageContent contentItem in threadMessage .ContentItems )
125
+ {
126
+ if (contentItem is MessageTextContent textItem )
127
+ {
128
+ string response = textItem .Text ;
129
+ if (textItem .Annotations != null )
130
+ {
131
+ foreach (MessageTextAnnotation annotation in textItem .Annotations )
132
+ {
133
+ if (annotation is MessageTextUrlCitationAnnotation urlAnnotation )
134
+ {
135
+ response = response .Replace (urlAnnotation .Text , $" [{urlAnnotation .UrlCitation .Title }]({urlAnnotation .UrlCitation .Url })" );
136
+ }
137
+ }
138
+ }
139
+ Console .Write ($" Agent response: {response }" );
140
+ }
141
+ else if (contentItem is MessageImageFileContent imageFileItem )
142
+ {
143
+ Console .Write ($" <image from ID: {imageFileItem .FileId }" );
144
+ }
145
+ Console .WriteLine ();
146
+ }
147
+ }
148
+
149
+ agentClient .DeleteThread (threadId : thread .Id );
150
+ agentClient .DeleteAgent (agentId : agent .Id );
151
+ ```
152
+
153
+ ::: zone-end
154
+
38
155
::: zone pivot="javascript"
39
156
40
157
## Step 1: Create a project client
0 commit comments