Skip to content

Commit 9fb9fcd

Browse files
nick863trrwilson
andauthored
Fix samples and start fixing the Memory stores, add couple of tests (#53943)
* Fix samples and start fixing the Memory stores, add couple of tests * update headers to CRUD samples * Fix --------- Co-authored-by: Travis Wilson <[email protected]>
1 parent f0fb6f7 commit 9fb9fcd

38 files changed

+324
-539
lines changed

sdk/ai/Azure.AI.Agents/samples/Sample2_Agents_CRUD.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Sample for Create, Read, Update and Delete (CRUD) agent in Azure.AI.Agents.V2.
1+
# Sample for Agent Administration (creation, retrieval, update, deletion) in Azure.AI.Projects
22

33
In this example we will demonstrate creation and basic use of an agent step by step.
44

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

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ The Azure.AI.Projects.OpenAI framework organized in a way that for each call, re
106106

107107
Synchronous call:
108108
```C# Snippet:Sample_CreateResponse_Sync
109-
OpenAIResponseClient responseClient = client.GetProjectResponsesClientForModel(modelDeploymentName);
109+
ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForModel(modelDeploymentName);
110110
OpenAIResponse response = responseClient.CreateResponse("What is the size of France in square miles?");
111111
```
112112

113113
Asynchronous call:
114114

115115
```C# Snippet:Sample_CreateResponse_Async
116-
OpenAIResponseClient responseClient = client.GetProjectResponsesClientForModel(modelDeploymentName);
116+
ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForModel(modelDeploymentName);
117117
OpenAIResponse response = await responseClient.CreateResponseAsync("What is the size of France in square miles?");
118118
```
119119

@@ -156,19 +156,13 @@ The code above will result in creation of `AgentVersion` object, which is the da
156156
OpenAI API allows you to get the response without creating an agent by using the response API. In this scenario we first create the response object.
157157

158158
```C# Snippet:Sample_CreateResponse_Async
159-
OpenAIResponseClient responseClient = client.GetProjectResponsesClientForModel(modelDeploymentName);
159+
ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForModel(modelDeploymentName);
160160
OpenAIResponse response = await responseClient.CreateResponseAsync("What is the size of France in square miles?");
161161
```
162162

163163
After the response was created we need to wait for it to complete.
164164

165165
```C# Snippet:Sample_WriteOutput_ResponseBasic_Async
166-
while (response.Status != ResponseStatus.Incomplete || response.Status != ResponseStatus.Failed || response.Status != ResponseStatus.Completed)
167-
{
168-
await Task.Delay(TimeSpan.FromMilliseconds(500));
169-
response = await responseClient.GetResponseAsync(responseId: response.Id);
170-
}
171-
172166
Console.WriteLine(response.GetOutputText());
173167
```
174168

@@ -363,17 +357,13 @@ AgentVersion agentVersion = await projectClient.Agents.CreateAgentVersionAsync(
363357
Now we can ask the agent a question, which requires running python code in the container.
364358

365359
```C# Snippet:Sample_CreateResponse_CodeInterpreter_Async
366-
OpenAIResponseClient responseClient = projectClient.OpenAI.GetOpenAIResponseClient(modelDeploymentName);
367-
ResponseCreationOptions responseOptions = new();
368-
responseOptions.Agent = agentVersion;
360+
AgentReference agentReference = new(name: agentVersion.Name, version: agentVersion.Version);
361+
ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForAgent(agentReference);
369362

370-
ResponseItem request = ResponseItem.CreateUserMessageItem("I need to solve the equation sin(x) + x^2 = 42");
371-
OpenAIResponse response = await responseClient.CreateResponseAsync(
372-
[request],
373-
responseOptions);
363+
OpenAIResponse response = await responseClient.CreateResponseAsync("I need to solve the equation sin(x) + x^2 = 42");
374364
```
375365

376-
### Computer tool
366+
### Computer use
377367

378368
`ComputerTool` allows Agents to assist customer in computer related tasks. Its constructor is provided with description of an operation system and screen resolution.
379369

@@ -415,7 +405,7 @@ int limitIteration = 10;
415405
OpenAIResponse response;
416406
do
417407
{
418-
response = await CreateAndWaitForResponseAsync(
408+
response = await CreateResponseAsync(
419409
responseClient,
420410
inputItems,
421411
responseOptions
@@ -643,41 +633,32 @@ AgentVersion agentVersion = await projectClient.Agents.CreateAgentVersionAsync(
643633
options: new(agentDefinition));
644634
```
645635

646-
To supply functions outputs, we will need to wait for response multiple times. We will define method `CreateAndWaitForResponseAsync` for brevity.
636+
To supply functions outputs, we will need to obtain responses multiple times. We will define method `CreateAndWaitForResponseAsync` for brevity.
647637

648-
```C# Snippet:Sample_WaitForResponse_Function_Async
649-
public static async Task<OpenAIResponse> CreateAndWaitForResponseAsync(OpenAIResponseClient responseClient, IEnumerable<ResponseItem> items, ResponseCreationOptions options)
638+
```C# Snippet:Sample_CheckResponse_Function_Async
639+
public static async Task<OpenAIResponse> CreateAndCheckReponseAsync(OpenAIResponseClient responseClient, IEnumerable<ResponseItem> items)
650640
{
651641
OpenAIResponse response = await responseClient.CreateResponseAsync(
652-
inputItems: items,
653-
options: options);
654-
while (response.Status != ResponseStatus.Incomplete && response.Status != ResponseStatus.Failed && response.Status != ResponseStatus.Completed)
655-
{
656-
await Task.Delay(TimeSpan.FromMilliseconds(500));
657-
response = await responseClient.GetResponseAsync(responseId: response.Id);
658-
}
642+
inputItems: items);
659643
Assert.That(response.Status, Is.EqualTo(ResponseStatus.Completed));
660644
return response;
661645
}
662646
```
663647

664-
Wait for the response; if the local function call is required, the response item will be of `FunctionCallResponseItem` type and will contain the function name needed by the Agent. In this case we will use our helper method `GetResolvedToolOutput` to get the `FunctionCallOutputResponseItem` with function call result. To provide the right answer, we need to supply all the response items to `CreateResponse` or `CreateResponseAsync` call. At the end we will print out the function response.
648+
If the local function call is required, the response item will be of `FunctionCallResponseItem` type and will contain the function name needed by the Agent. In this case we will use our helper method `GetResolvedToolOutput` to get the `FunctionCallOutputResponseItem` with function call result. To provide the right answer, we need to supply all the response items to `CreateResponse` or `CreateResponseAsync` call. At the end we will print out the function response.
665649

666650
```C# Snippet:Sample_CreateResponse_Function_Async
667-
OpenAIResponseClient responseClient = projectClient.OpenAI.GetOpenAIResponseClient(modelDeploymentName);
668-
ResponseCreationOptions responseOptions = new();
669-
responseOptions.Agent = agentVersion;
651+
ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForAgent(agentVersion.Name);
670652

671653
ResponseItem request = ResponseItem.CreateUserMessageItem("What's the weather like in my favorite city?");
672654
List<ResponseItem> inputItems = [request];
673655
bool funcionCalled = false;
674656
OpenAIResponse response;
675657
do
676658
{
677-
response = await CreateAndWaitForResponseAsync(
659+
response = await CreateAndCheckReponseAsync(
678660
responseClient,
679-
inputItems,
680-
responseOptions);
661+
inputItems);
681662
funcionCalled = false;
682663
foreach (ResponseItem responseItem in response.OutputItems)
683664
{

sdk/ai/Azure.AI.Projects.OpenAI/samples/Sample10_ComputerUse.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,37 +137,27 @@ private static string ProcessComputerUseCall(ComputerCallResponseItem item, stri
137137
}
138138
```
139139

140-
5. For brevity create the methods to wait for response to be returned.
140+
5. For brevity create the methods to get the response.
141141

142142
Synchronous sample:
143-
```C# Snippet:Sample_WaitForResponse_ComputerUse_Sync
144-
public static OpenAIResponse CreateAndWaitForResponse(OpenAIResponseClient responseClient, IEnumerable<ResponseItem> items, ResponseCreationOptions options)
143+
```C# Snippet:Sample_CreateNextResponse_ComputerUse_Sync
144+
public static OpenAIResponse CreateResponse(OpenAIResponseClient responseClient, IEnumerable<ResponseItem> items, ResponseCreationOptions options)
145145
{
146146
OpenAIResponse response = responseClient.CreateResponse(
147147
inputItems: items,
148148
options: options);
149-
while (response.Status != ResponseStatus.Incomplete && response.Status != ResponseStatus.Failed && response.Status != ResponseStatus.Completed)
150-
{
151-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
152-
response = responseClient.GetResponse(responseId: response.Id);
153-
}
154149
Assert.That(response.Status, Is.EqualTo(ResponseStatus.Completed));
155150
return response;
156151
}
157152
```
158153

159154
Asynchronous sample:
160-
```C# Snippet:Sample_WaitForResponse_ComputerUse_Async
161-
public static async Task<OpenAIResponse> CreateAndWaitForResponseAsync(OpenAIResponseClient responseClient, IEnumerable<ResponseItem> items, ResponseCreationOptions options)
155+
```C# Snippet:Sample_CreateNextResponse_ComputerUse_Async
156+
public static async Task<OpenAIResponse> CreateResponseAsync(OpenAIResponseClient responseClient, IEnumerable<ResponseItem> items, ResponseCreationOptions options)
162157
{
163158
OpenAIResponse response = await responseClient.CreateResponseAsync(
164159
inputItems: items,
165160
options: options);
166-
while (response.Status != ResponseStatus.Incomplete && response.Status != ResponseStatus.Failed && response.Status != ResponseStatus.Completed)
167-
{
168-
await Task.Delay(TimeSpan.FromMilliseconds(500));
169-
response = await responseClient.GetResponseAsync(responseId: response.Id);
170-
}
171161
Assert.That(response.Status, Is.EqualTo(ResponseStatus.Completed));
172162
return response;
173163
}
@@ -193,7 +183,7 @@ int limitIteration = 10;
193183
OpenAIResponse response;
194184
do
195185
{
196-
response = CreateAndWaitForResponse(
186+
response = CreateResponse(
197187
responseClient,
198188
inputItems,
199189
responseOptions);
@@ -233,7 +223,7 @@ int limitIteration = 10;
233223
OpenAIResponse response;
234224
do
235225
{
236-
response = await CreateAndWaitForResponseAsync(
226+
response = await CreateResponseAsync(
237227
responseClient,
238228
inputItems,
239229
responseOptions

sdk/ai/Azure.AI.Projects.OpenAI/samples/Sample11_FileSearch_Streaming.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ AgentVersion agentVersion = await projectClient.Agents.CreateAgentVersionAsync(
8282
);
8383
```
8484

85-
3. In this example we will ask a question to the file contents.
85+
3. Create the conversation to store respones.
8686

8787
Synchronous sample:
8888
```C# Snippet:Sample_CreateResponse_FileSearch_Streaming_Sync
@@ -92,8 +92,6 @@ ResponseCreationOptions responseOptions = new()
9292
Agent = agentVersion,
9393
AgentConversationId = conversation.Id,
9494
};
95-
96-
ResponseItem request = ResponseItem.CreateUserMessageItem("Can you give me the documented codes for 'banana' and 'orange'?");
9795
```
9896

9997
Asynchronous sample:
@@ -104,8 +102,6 @@ ResponseCreationOptions responseOptions = new()
104102
Agent = agentVersion,
105103
AgentConversationId = conversation.Id,
106104
};
107-
108-
ResponseItem request = ResponseItem.CreateUserMessageItem("Can you give me the documented codes for 'banana' and 'orange'?");
109105
```
110106

111107
4. To format streaming response output we will create a helper method `ParseResponse`. If the stream ends up in error state, it will throw an error.
@@ -154,15 +150,15 @@ private static void ParseResponse(StreamingResponseUpdate streamResponse)
154150

155151
Synchronous sample:
156152
```C# Snippet:Sample_StreamingResponse_FileSearch_Streaming_Sync
157-
foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Responses.CreateResponseStreaming([request], responseOptions))
153+
foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Responses.CreateResponseStreaming("Can you give me the documented codes for 'banana' and 'orange'?", responseOptions))
158154
{
159155
ParseResponse(streamResponse);
160156
}
161157
```
162158

163159
Asynchronous sample:
164160
```C# Snippet:Sample_StreamingResponse_FileSearch_Streaming_Async
165-
await foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Responses.CreateResponseStreamingAsync([request], responseOptions))
161+
await foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Responses.CreateResponseStreamingAsync("Can you give me the documented codes for 'banana' and 'orange'?", responseOptions))
166162
{
167163
ParseResponse(streamResponse);
168164
}
@@ -173,8 +169,7 @@ await foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Re
173169
Synchronous sample:
174170
```C# Snippet:Sample_FollowUp_FileSearch_Streaming_Sync
175171
Console.WriteLine("Demonstrating follow-up query with streaming...");
176-
request = ResponseItem.CreateUserMessageItem("What was my previous question about?");
177-
foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Responses.CreateResponseStreaming([request], responseOptions))
172+
foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Responses.CreateResponseStreaming("What was my previous question about?", responseOptions))
178173
{
179174
ParseResponse(streamResponse);
180175
}
@@ -183,8 +178,7 @@ foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Response
183178
Asynchronous sample:
184179
```C# Snippet:Sample_FollowUp_FileSearch_Streaming_Async
185180
Console.WriteLine("Demonstrating follow-up query with streaming...");
186-
request = ResponseItem.CreateUserMessageItem("What was my previous question about?");
187-
await foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Responses.CreateResponseStreamingAsync([request], responseOptions))
181+
await foreach (StreamingResponseUpdate streamResponse in projectClient.OpenAI.Responses.CreateResponseStreamingAsync("What was my previous question about?", responseOptions))
188182
{
189183
ParseResponse(streamResponse);
190184
}

sdk/ai/Azure.AI.Projects.OpenAI/samples/Sample12_ResponseStreaming.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ In this example we will demonstrate how to get a response in streaming scenarios
77
```C# Snippet:Sample_CreateAgentClient_ResponseStreaming
88
var projectEndpoint = System.Environment.GetEnvironmentVariable("PROJECT_ENDPOINT");
99
var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME");
10-
11-
ProjectOpenAIClient client = new(projectEndpoint: new Uri(projectEndpoint), tokenProvider: new DefaultAzureCredential());
10+
AIProjectClient projectClient = new(endpoint: new Uri(projectEndpoint), tokenProvider: new DefaultAzureCredential());
1211
```
1312

1413
2. Use the client to create a `ProjectOpenAIResponseClient`, which will be used to create stream.
1514

1615
Synchronous sample:
1716
```C# Snippet:Sample_CreateResponseStreaming
18-
ProjectResponsesClient responsesClient = client.GetProjectResponsesClientForModel(modelDeploymentName);
17+
ProjectResponsesClient responsesClient = projectClient.OpenAI.GetProjectResponsesClientForModel(modelDeploymentName);
1918
```
2019

2120
3. Stream the results; raise the error if the request was not successful.

sdk/ai/Azure.AI.Projects.OpenAI/samples/Sample1_Agent_Versions.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,15 @@ ResponseCreationOptions responseOptions = new()
9292
OpenAIResponse response = await responseClient.CreateResponseAsync("Hello, tell me a joke.");
9393
```
9494

95-
6. Wait for the agent to respond and print out the output; raise the error if the request was not successful.
95+
6. Print the output; raise the error if the request was not successful.
9696

9797
Synchronous sample:
9898
```C# Snippet:Sample_WriteOutput_Sync
99-
while (response.Status != ResponseStatus.Incomplete && response.Status != ResponseStatus.Failed && response.Status != ResponseStatus.Completed)
100-
{
101-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
102-
response = responseClient.GetResponse(responseId: response.Id);
103-
}
104-
10599
Console.WriteLine(response.GetOutputText());
106100
```
107101

108102
Asynchronous sample:
109103
```C# Snippet:Sample_WriteOutput_Async
110-
while (response.Status != ResponseStatus.Incomplete && response.Status != ResponseStatus.Failed && response.Status != ResponseStatus.Completed){
111-
await Task.Delay(TimeSpan.FromMilliseconds(500));
112-
response = await responseClient.GetResponseAsync(responseId: response.Id);
113-
}
114-
115104
Console.WriteLine(response.GetOutputText());
116105
```
117106

sdk/ai/Azure.AI.Projects.OpenAI/samples/Sample2_Image_Generation.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,7 @@ ProjectResponsesClient responseClient = openAIClient.GetProjectResponsesClientFo
9797
OpenAIResponse response = await responseClient.CreateResponseAsync("Generate parody of Newton with apple.");
9898
```
9999

100-
5. Waiting for response.
101-
102-
Synchronous sample:
103-
```C# Snippet:Sample_WaitForRun_ImageGeneration_Sync
104-
while (response.Status != ResponseStatus.Incomplete && response.Status != ResponseStatus.Failed && response.Status != ResponseStatus.Completed)
105-
{
106-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
107-
response = responseClient.GetResponse(responseId: response.Id);
108-
}
109-
```
110-
111-
Asynchronous sample:
112-
```C# Snippet:Sample_WaitForRun_ImageGeneration_Async
113-
while (response.Status != ResponseStatus.Incomplete && response.Status != ResponseStatus.Failed && response.Status != ResponseStatus.Completed){
114-
await Task.Delay(TimeSpan.FromMilliseconds(500));
115-
response = await responseClient.GetResponseAsync(responseId: response.Id);
116-
}
117-
```
118-
119-
6. Parse the `OpenAIResponse` object and save the generated image.
100+
5. Parse the `OpenAIResponse` object and save the generated image.
120101

121102
```C# Snippet:Sample_SaveImage_ImageGeneration
122103
foreach (ResponseItem item in response.OutputItems)
@@ -129,7 +110,7 @@ foreach (ResponseItem item in response.OutputItems)
129110
}
130111
```
131112

132-
7. Clean up resources by deleting the Agent.
113+
6. Clean up resources by deleting the Agent.
133114

134115
Synchronous sample:
135116
```C# Snippet:Sample_Cleanup_ImageGeneration_Sync

sdk/ai/Azure.AI.Projects.OpenAI/samples/Sample4_ResponseBasic.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,31 @@ In this example we will demonstrate how to get a response without an Agent.
77
```C# Snippet:Sample_CreateAgentClient_ResponseBasic
88
var projectEndpoint = System.Environment.GetEnvironmentVariable("PROJECT_ENDPOINT");
99
var modelDeploymentName = System.Environment.GetEnvironmentVariable("MODEL_DEPLOYMENT_NAME");
10-
ProjectOpenAIClient client = GetTestProjectOpenAIClient();
10+
AIProjectClient projectClient = new(endpoint: new Uri(projectEndpoint), tokenProvider: new DefaultAzureCredential());
1111
```
1212

1313
2. Use the client to create a `Responses`, which will be used to create `OpenAIResponse` object.
1414

1515
Synchronous sample:
1616
```C# Snippet:Sample_CreateResponse_Sync
17-
OpenAIResponseClient responseClient = client.GetProjectResponsesClientForModel(modelDeploymentName);
17+
ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForModel(modelDeploymentName);
1818
OpenAIResponse response = responseClient.CreateResponse("What is the size of France in square miles?");
1919
```
2020

2121
Asynchronous sample:
2222
```C# Snippet:Sample_CreateResponse_Async
23-
OpenAIResponseClient responseClient = client.GetProjectResponsesClientForModel(modelDeploymentName);
23+
ProjectResponsesClient responseClient = projectClient.OpenAI.GetProjectResponsesClientForModel(modelDeploymentName);
2424
OpenAIResponse response = await responseClient.CreateResponseAsync("What is the size of France in square miles?");
2525
```
2626

27-
3. Wait for request to complete; raise the error if the request was not successful.
27+
3. Write the response output, raise the error if the request was not successful.
2828

2929
Synchronous sample:
3030
```C# Snippet:Sample_WriteOutput_ResponseBasic_Sync
31-
while (response.Status != ResponseStatus.Incomplete || response.Status != ResponseStatus.Failed || response.Status != ResponseStatus.Completed)
32-
{
33-
Thread.Sleep(TimeSpan.FromMilliseconds(500));
34-
response = responseClient.GetResponse(responseId: response.Id);
35-
}
36-
3731
Console.WriteLine(response.GetOutputText());
3832
```
3933

4034
Asynchronous sample:
4135
```C# Snippet:Sample_WriteOutput_ResponseBasic_Async
42-
while (response.Status != ResponseStatus.Incomplete || response.Status != ResponseStatus.Failed || response.Status != ResponseStatus.Completed)
43-
{
44-
await Task.Delay(TimeSpan.FromMilliseconds(500));
45-
response = await responseClient.GetResponseAsync(responseId: response.Id);
46-
}
47-
4836
Console.WriteLine(response.GetOutputText());
4937
```

0 commit comments

Comments
 (0)