Skip to content

Commit 7625550

Browse files
authored
[AI] [Projects] Update OpenApi sample to use defaultParams (Azure#49295)
* [AI] [Projects] Update OpenApi sample to use defaultParams * update readme and markdown * Add custom code for AzureAISearchResource class and update sample * update api * review feedback * review feedback
1 parent 6b6fcb2 commit 7625550

10 files changed

+139
-83
lines changed

sdk/ai/Azure.AI.Projects/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,16 @@ if (connections?.Value == null || connections.Value.Count == 0)
356356

357357
ConnectionResponse connection = connections.Value[0];
358358

359-
AISearchIndexResource indexList = new(connection.Id, "sample_index");
360-
indexList.QueryType = AzureAISearchQueryType.VectorSemanticHybrid;
361-
ToolResources searchResource = new ToolResources
359+
AzureAISearchResource searchResource = new(
360+
connection.Id,
361+
"sample_index",
362+
5,
363+
"category eq 'sleeping bag'",
364+
AzureAISearchQueryType.Simple
365+
);
366+
ToolResources toolResource = new()
362367
{
363-
AzureAISearch = new AzureAISearchResource
364-
{
365-
IndexList = { indexList }
366-
}
368+
AzureAISearch = searchResource
367369
};
368370

369371
AgentsClient agentClient = projectClient.GetAgentsClient();
@@ -373,7 +375,7 @@ Agent agent = await agentClient.CreateAgentAsync(
373375
name: "my-assistant",
374376
instructions: "You are a helpful assistant.",
375377
tools: [ new AzureAISearchToolDefinition() ],
376-
toolResources: searchResource);
378+
toolResources: toolResource);
377379
```
378380

379381
If the agent has found the relevant information in the index, the reference
@@ -844,7 +846,8 @@ OpenApiToolDefinition openapiTool = new(
844846
name: "get_weather",
845847
description: "Retrieve weather information for a location",
846848
spec: BinaryData.FromBytes(File.ReadAllBytes(file_path)),
847-
auth: oaiAuth
849+
auth: oaiAuth,
850+
defaultParams: ["format"]
848851
);
849852

850853
Agent agent = await client.CreateAgentAsync(

sdk/ai/Azure.AI.Projects/api/Azure.AI.Projects.net8.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ public enum AuthenticationType
641641
public partial class AzureAISearchResource : System.ClientModel.Primitives.IJsonModel<Azure.AI.Projects.AzureAISearchResource>, System.ClientModel.Primitives.IPersistableModel<Azure.AI.Projects.AzureAISearchResource>
642642
{
643643
public AzureAISearchResource() { }
644+
public AzureAISearchResource(string indexConnectionId, string indexName, int topK = 5, string filter = "", Azure.AI.Projects.AzureAISearchQueryType? queryType = default(Azure.AI.Projects.AzureAISearchQueryType?)) { }
644645
public System.Collections.Generic.IList<Azure.AI.Projects.AISearchIndexResource> IndexList { get { throw null; } }
645646
protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
646647
Azure.AI.Projects.AzureAISearchResource System.ClientModel.Primitives.IJsonModel<Azure.AI.Projects.AzureAISearchResource>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }

sdk/ai/Azure.AI.Projects/api/Azure.AI.Projects.netstandard2.0.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ public enum AuthenticationType
641641
public partial class AzureAISearchResource : System.ClientModel.Primitives.IJsonModel<Azure.AI.Projects.AzureAISearchResource>, System.ClientModel.Primitives.IPersistableModel<Azure.AI.Projects.AzureAISearchResource>
642642
{
643643
public AzureAISearchResource() { }
644+
public AzureAISearchResource(string indexConnectionId, string indexName, int topK = 5, string filter = "", Azure.AI.Projects.AzureAISearchQueryType? queryType = default(Azure.AI.Projects.AzureAISearchQueryType?)) { }
644645
public System.Collections.Generic.IList<Azure.AI.Projects.AISearchIndexResource> IndexList { get { throw null; } }
645646
protected virtual void JsonModelWriteCore(System.Text.Json.Utf8JsonWriter writer, System.ClientModel.Primitives.ModelReaderWriterOptions options) { }
646647
Azure.AI.Projects.AzureAISearchResource System.ClientModel.Primitives.IJsonModel<Azure.AI.Projects.AzureAISearchResource>.Create(ref System.Text.Json.Utf8JsonReader reader, System.ClientModel.Primitives.ModelReaderWriterOptions options) { throw null; }

sdk/ai/Azure.AI.Projects/samples/Sample17_Agent_OpenAPI.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ OpenApiToolDefinition openapiTool = new(
2626
name: "get_weather",
2727
description: "Retrieve weather information for a location",
2828
spec: BinaryData.FromBytes(File.ReadAllBytes(file_path)),
29-
auth: oaiAuth
29+
auth: oaiAuth,
30+
defaultParams: ["format"]
3031
);
3132

3233
Agent agent = client.CreateAgent(
@@ -44,7 +45,8 @@ OpenApiToolDefinition openapiTool = new(
4445
name: "get_weather",
4546
description: "Retrieve weather information for a location",
4647
spec: BinaryData.FromBytes(File.ReadAllBytes(file_path)),
47-
auth: oaiAuth
48+
auth: oaiAuth,
49+
defaultParams: ["format"]
4850
);
4951

5052
Agent agent = await client.CreateAgentAsync(

sdk/ai/Azure.AI.Projects/samples/Sample18_Agents_Azure_AI_Search.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ if (connections?.Value == null || connections.Value.Count == 0)
2828

2929
ConnectionResponse connection = connections.Value[0];
3030

31-
AISearchIndexResource indexList = new(connection.Id, "sample_index");
32-
indexList.QueryType = AzureAISearchQueryType.VectorSemanticHybrid;
33-
ToolResources searchResource = new ToolResources
31+
AzureAISearchResource searchResource = new(
32+
connection.Id,
33+
"sample_index",
34+
5,
35+
"category eq 'sleeping bag'",
36+
AzureAISearchQueryType.Simple
37+
);
38+
ToolResources toolResource = new()
3439
{
35-
AzureAISearch = new AzureAISearchResource
36-
{
37-
IndexList = { indexList }
38-
}
40+
AzureAISearch = searchResource
3941
};
4042

4143
AgentsClient agentClient = projectClient.GetAgentsClient();
@@ -45,7 +47,7 @@ Agent agent = agentClient.CreateAgent(
4547
name: "my-assistant",
4648
instructions: "You are a helpful assistant.",
4749
tools: [new AzureAISearchToolDefinition()],
48-
toolResources: searchResource);
50+
toolResources: toolResource);
4951
```
5052

5153
Asynchronous sample:
@@ -59,14 +61,16 @@ if (connections?.Value == null || connections.Value.Count == 0)
5961

6062
ConnectionResponse connection = connections.Value[0];
6163

62-
AISearchIndexResource indexList = new(connection.Id, "sample_index");
63-
indexList.QueryType = AzureAISearchQueryType.VectorSemanticHybrid;
64-
ToolResources searchResource = new ToolResources
64+
AzureAISearchResource searchResource = new(
65+
connection.Id,
66+
"sample_index",
67+
5,
68+
"category eq 'sleeping bag'",
69+
AzureAISearchQueryType.Simple
70+
);
71+
ToolResources toolResource = new()
6572
{
66-
AzureAISearch = new AzureAISearchResource
67-
{
68-
IndexList = { indexList }
69-
}
73+
AzureAISearch = searchResource
7074
};
7175

7276
AgentsClient agentClient = projectClient.GetAgentsClient();
@@ -76,7 +80,7 @@ Agent agent = await agentClient.CreateAgentAsync(
7680
name: "my-assistant",
7781
instructions: "You are a helpful assistant.",
7882
tools: [ new AzureAISearchToolDefinition() ],
79-
toolResources: searchResource);
83+
toolResources: toolResource);
8084
```
8185

8286
3. Now we will create a `ThreadRun` and wait until it is complete. If the run will not be successful, we will print the last error.
@@ -139,7 +143,7 @@ Assert.AreEqual(
139143
run.LastError?.Message);
140144
```
141145

142-
4. In our search we have used an index containing "embedding", "token", "url" and also "title" fields. This allowed us to get reference title and url. In the code below, we iterate messages in chronological order and replace the reference placeholders by url and title.
146+
4. In our search we have used an index containing "embedding", "token", "category" and also "title" fields. This allowed us to get reference title and url. In the code below, we iterate messages in chronological order and replace the reference placeholders by url and title.
143147

144148
Synchronous sample:
145149
```C# Snippet:PopulateReferencesAgentWithAzureAISearchTool_Sync

sdk/ai/Azure.AI.Projects/samples/Sample20_Agents_Azure_AI_Search_Streaming.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ if (connections?.Value == null || connections.Value.Count == 0)
2828

2929
ConnectionResponse connection = connections.Value[0];
3030

31-
AISearchIndexResource indexList = new(connection.Id, "sample_index");
32-
indexList.QueryType = AzureAISearchQueryType.VectorSemanticHybrid;
33-
ToolResources searchResource = new ToolResources
31+
AzureAISearchResource searchResource = new(
32+
connection.Id,
33+
"sample_index",
34+
5,
35+
"category eq 'sleeping bag'",
36+
AzureAISearchQueryType.Simple
37+
);
38+
ToolResources toolResource = new()
3439
{
35-
AzureAISearch = new AzureAISearchResource
36-
{
37-
IndexList = { indexList }
38-
}
40+
AzureAISearch = searchResource
3941
};
4042

4143
AgentsClient agentClient = projectClient.GetAgentsClient();
@@ -45,7 +47,7 @@ Agent agent = agentClient.CreateAgent(
4547
name: "my-assistant",
4648
instructions: "You are a helpful assistant.",
4749
tools: [new AzureAISearchToolDefinition()],
48-
toolResources: searchResource);
50+
toolResources: toolResource);
4951
```
5052

5153
Asynchronous sample:
@@ -59,14 +61,16 @@ if (connections?.Value == null || connections.Value.Count == 0)
5961

6062
ConnectionResponse connection = connections.Value[0];
6163

62-
AISearchIndexResource indexList = new(connection.Id, "sample_index");
63-
indexList.QueryType = AzureAISearchQueryType.VectorSemanticHybrid;
64-
ToolResources searchResource = new()
64+
AzureAISearchResource searchResource = new(
65+
connection.Id,
66+
"sample_index",
67+
5,
68+
"category eq 'sleeping bag'",
69+
AzureAISearchQueryType.Simple
70+
);
71+
ToolResources toolResource = new()
6572
{
66-
AzureAISearch = new AzureAISearchResource
67-
{
68-
IndexList = { indexList }
69-
}
73+
AzureAISearch = searchResource
7074
};
7175

7276
AgentsClient agentClient = projectClient.GetAgentsClient();
@@ -75,8 +79,8 @@ Agent agent = await agentClient.CreateAgentAsync(
7579
model: modelDeploymentName,
7680
name: "my-assistant",
7781
instructions: "You are a helpful assistant.",
78-
tools: [ new AzureAISearchToolDefinition() ],
79-
toolResources: searchResource);
82+
tools: [new AzureAISearchToolDefinition()],
83+
toolResources: toolResource);
8084
```
8185

8286
3. Now we will create a `ThreadRun`.
@@ -105,7 +109,7 @@ ThreadMessage message = await agentClient.CreateMessageAsync(
105109
"What is the temperature rating of the cozynights sleeping bag?");
106110
```
107111

108-
4. In our search we have used an index containing "embedding", "token", "url" and also "title" fields. This allowed us to get reference title and url. In the code below we are reading and printing out the stream output. We skip the reference placeholder, because the following message update will return the actual reference, which will be printed to the output.
112+
4. In our search we have used an index containing "embedding", "token", "category" and also "title" fields. This allowed us to get reference title and url. In the code below, we iterate messages in chronological order and replace the reference placeholders by url and title.
109113

110114
Synchronous sample:
111115
```C# Snippet:AzureAISearchStreamingExample_PrintMessages
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
using System.Collections.Generic;
4+
5+
namespace Azure.AI.Projects
6+
{
7+
public partial class AzureAISearchResource
8+
{
9+
public AzureAISearchResource(
10+
string indexConnectionId,
11+
string indexName,
12+
int topK = 5,
13+
string filter = "",
14+
AzureAISearchQueryType? queryType = null) // Removed default value
15+
{
16+
// Assign default value explicitly if queryType is null
17+
queryType ??= AzureAISearchQueryType.Simple;
18+
19+
// Initialize properties or perform other logic here
20+
var indexList = new AISearchIndexResource
21+
{
22+
IndexConnectionId = indexConnectionId,
23+
IndexName = indexName,
24+
TopK = topK,
25+
Filter = filter,
26+
QueryType = queryType
27+
};
28+
29+
// Additional initialization logic if needed
30+
this.IndexList = new List<AISearchIndexResource> { indexList };
31+
}
32+
}
33+
}

sdk/ai/Azure.AI.Projects/tests/Samples/Agent/Sample_Agent_OpenAPI.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public async Task OpenAPICallingExampleAsync()
4646
name: "get_weather",
4747
description: "Retrieve weather information for a location",
4848
spec: BinaryData.FromBytes(File.ReadAllBytes(file_path)),
49-
auth: oaiAuth
49+
auth: oaiAuth,
50+
defaultParams: ["format"]
5051
);
5152

5253
Agent agent = await client.CreateAgentAsync(
@@ -129,7 +130,8 @@ public void OpenAPICallingExample()
129130
name: "get_weather",
130131
description: "Retrieve weather information for a location",
131132
spec: BinaryData.FromBytes(File.ReadAllBytes(file_path)),
132-
auth: oaiAuth
133+
auth: oaiAuth,
134+
defaultParams: ["format"]
133135
);
134136

135137
Agent agent = client.CreateAgent(

sdk/ai/Azure.AI.Projects/tests/Samples/Agent/Sample_Agents_Azure_AI_Search.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
using System;
77
using System.Threading.Tasks;
8-
using Azure.Core;
98
using Azure.Core.TestFramework;
109
using NUnit.Framework;
11-
using System.Collections.Generic;
1210
using NUnit.Framework.Internal;
1311
using System.Threading;
1412

@@ -40,14 +38,16 @@ public async Task AzureAISearchExampleAsync()
4038

4139
ConnectionResponse connection = connections.Value[0];
4240

43-
AISearchIndexResource indexList = new(connection.Id, "sample_index");
44-
indexList.QueryType = AzureAISearchQueryType.VectorSemanticHybrid;
45-
ToolResources searchResource = new ToolResources
41+
AzureAISearchResource searchResource = new(
42+
connection.Id,
43+
"sample_index",
44+
5,
45+
"category eq 'sleeping bag'",
46+
AzureAISearchQueryType.Simple
47+
);
48+
ToolResources toolResource = new()
4649
{
47-
AzureAISearch = new AzureAISearchResource
48-
{
49-
IndexList = { indexList }
50-
}
50+
AzureAISearch = searchResource
5151
};
5252

5353
AgentsClient agentClient = projectClient.GetAgentsClient();
@@ -57,7 +57,7 @@ public async Task AzureAISearchExampleAsync()
5757
name: "my-assistant",
5858
instructions: "You are a helpful assistant.",
5959
tools: [ new AzureAISearchToolDefinition() ],
60-
toolResources: searchResource);
60+
toolResources: toolResource);
6161
#endregion
6262
#region Snippet:AzureAISearchExample_CreateRun
6363
// Create thread for communication
@@ -155,14 +155,16 @@ public void AzureAISearchExample()
155155

156156
ConnectionResponse connection = connections.Value[0];
157157

158-
AISearchIndexResource indexList = new(connection.Id, "sample_index");
159-
indexList.QueryType = AzureAISearchQueryType.VectorSemanticHybrid;
160-
ToolResources searchResource = new ToolResources
158+
AzureAISearchResource searchResource = new(
159+
connection.Id,
160+
"sample_index",
161+
5,
162+
"category eq 'sleeping bag'",
163+
AzureAISearchQueryType.Simple
164+
);
165+
ToolResources toolResource = new()
161166
{
162-
AzureAISearch = new AzureAISearchResource
163-
{
164-
IndexList = { indexList }
165-
}
167+
AzureAISearch = searchResource
166168
};
167169

168170
AgentsClient agentClient = projectClient.GetAgentsClient();
@@ -172,7 +174,7 @@ public void AzureAISearchExample()
172174
name: "my-assistant",
173175
instructions: "You are a helpful assistant.",
174176
tools: [new AzureAISearchToolDefinition()],
175-
toolResources: searchResource);
177+
toolResources: toolResource);
176178
#endregion
177179
#region Snippet:AzureAISearchExample_CreateRun_Sync
178180
// Create thread for communication

0 commit comments

Comments
 (0)