Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update paging APIs to use updated SCM types #105

Merged
merged 75 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
f4ab277
Add project reference to SCM; move to new paging types
annelo-msft Jun 25, 2024
38dd09d
wire return values and stub out implementations
annelo-msft Jun 25, 2024
17c4078
update page collection to use new page token contract
annelo-msft Jun 26, 2024
9cf0447
Update tests and samples
annelo-msft Jun 26, 2024
7679061
fix page token serialization logic
annelo-msft Jun 26, 2024
6acd48b
add async page collections
annelo-msft Jun 26, 2024
6327af6
Add rehydration overload
annelo-msft Jun 26, 2024
e0c14eb
Add async rehydration overload
annelo-msft Jun 26, 2024
2d46435
backup WIP on generalized implementation
annelo-msft Jun 26, 2024
bae625a
backup WIP on generalized implementation
annelo-msft Jun 26, 2024
03e17b6
remove Assistant-specific page collection implementations in favor of…
annelo-msft Jun 26, 2024
1f150ea
remove Assistant-specific page collection implementations in favor of…
annelo-msft Jun 26, 2024
6ba8dbd
Backup of generalized protocol paging idea
annelo-msft Jun 26, 2024
7c1f74d
remove Assistant-specific page result enumerator implementations in f…
annelo-msft Jun 26, 2024
bc2004e
take BinaryData instead of ClientToken, which cannot be constructed a…
annelo-msft Jun 26, 2024
6138636
rework rehydration methods to use new ClientToken pattern
annelo-msft Jun 26, 2024
bae34b9
add test for rehydration method
annelo-msft Jun 26, 2024
5283bc5
add a second test for rehydration method
annelo-msft Jun 26, 2024
e0305ea
SCM renames
annelo-msft Jun 26, 2024
64bea2b
updates from mini pr review
annelo-msft Jun 26, 2024
ea8db96
rework RequestOptions
annelo-msft Jun 26, 2024
bcfed80
bug fix
annelo-msft Jun 26, 2024
e365566
GetPageCore
annelo-msft Jun 27, 2024
1ed51af
backup of initial thinking on operation-specific page token idea
annelo-msft Jun 27, 2024
2ad6dc1
extend operation-specific page token idea to protocol methods
annelo-msft Jun 27, 2024
5b236a6
rename ClientToken to ContinuationToken
annelo-msft Jun 27, 2024
444bee0
nits: tidy
annelo-msft Jun 27, 2024
0060d5f
add test to illustrate convenience/protocol interop
annelo-msft Jun 28, 2024
0aca908
updates based on feedback from Krzysztof
annelo-msft Jul 1, 2024
c4a8a63
add protocol implementations of all Assistant-related paged methods
annelo-msft Jul 1, 2024
4a9d6af
add convenience implementations of Assistant-related paged methods
annelo-msft Jul 1, 2024
f91969f
move to CurrentPageToken instead of FirstPageToken
annelo-msft Jul 1, 2024
90eed11
starting idea around client as enumerator; backing up
annelo-msft Jul 1, 2024
313753e
more backup
annelo-msft Jul 1, 2024
a8e17e3
tidy up before taking inventory
annelo-msft Jul 1, 2024
c5261d1
backup prior to attempt to converge around single internal collection…
annelo-msft Jul 1, 2024
9c05fc2
more refining
annelo-msft Jul 1, 2024
209b71d
implement interface explicitly
annelo-msft Jul 1, 2024
b0d5f04
more tidy
annelo-msft Jul 1, 2024
c5b568d
Make it work for async
annelo-msft Jul 1, 2024
e07fb0b
restructure to look more like mini-client
annelo-msft Jul 2, 2024
f3c5e96
simplify
annelo-msft Jul 2, 2024
8b762e2
more nits and simplify
annelo-msft Jul 2, 2024
7cd2e92
nits
annelo-msft Jul 2, 2024
67d70fa
renames and a little rework
annelo-msft Jul 2, 2024
daab108
move page tokens
annelo-msft Jul 2, 2024
818b6bd
Implement remaining Assistants endpoings
annelo-msft Jul 2, 2024
34264b5
Implement vector store paginated endpoints
annelo-msft Jul 2, 2024
74e8b34
follow-ups to convenience and tests for vector stores
annelo-msft Jul 2, 2024
827cf2b
bug fix
annelo-msft Jul 2, 2024
891ccf5
temp bug fix
annelo-msft Jul 2, 2024
b15bd18
Proof of concept of PageEnumerator pattern for MessageCollection
annelo-msft Jul 2, 2024
34175dc
Refactor to PageEnumerator pattern
annelo-msft Jul 2, 2024
4a175d8
bug fix
annelo-msft Jul 2, 2024
8656f12
nit
annelo-msft Jul 2, 2024
4d2bf33
updates from SCM polish work
annelo-msft Jul 3, 2024
f068567
Merge remote-tracking branch 'upstream/main' into oai-pageenumerator-…
annelo-msft Jul 15, 2024
4008584
move to SCM beta.5 package
annelo-msft Jul 15, 2024
2e97ef7
move over update for protocol-convenience cast
annelo-msft Jul 15, 2024
1ee0486
some fixes
annelo-msft Jul 15, 2024
4ce3888
updates from SCM code
annelo-msft Jul 15, 2024
0622853
fix tests
annelo-msft Jul 15, 2024
e492b7c
remove internal protocol method implementations that have moved to in…
annelo-msft Jul 15, 2024
f811de1
nits
annelo-msft Jul 15, 2024
e7e3de7
add refdocs for paginated endpoint service methods
annelo-msft Jul 15, 2024
88319b8
add refdocs for options types
annelo-msft Jul 15, 2024
7118d13
add refdocs for collection rehydration overloads
annelo-msft Jul 15, 2024
e275801
add test and fix bugs
annelo-msft Jul 15, 2024
8d66ade
add a test for vector store
annelo-msft Jul 15, 2024
6c637c7
Merge remote-tracking branch 'upstream/main' into oai-pageenumerator-…
annelo-msft Jul 16, 2024
8468c20
move files To.Be.Generated -> Custom
annelo-msft Jul 22, 2024
6a4e01d
address PR FB for examples
annelo-msft Jul 22, 2024
0a2d6f5
remove unused helper methods
annelo-msft Jul 22, 2024
9cc424a
Merge remote-tracking branch 'upstream/main' into oai-pageenumerator-…
annelo-msft Jul 23, 2024
a09c1f3
fix merge issue
annelo-msft Jul 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ When you request a chat completion, the default behavior is for the server to ge
The client library offers a convenient approach to working with streaming chat completions. If you wanted to re-write the example from the previous section using streaming, rather than calling the `ChatClient`'s `CompleteChat` method, you would call its `CompleteChatStreaming` method instead:

```csharp
ResultCollection<StreamingChatCompletionUpdate> updates
CollectionResult<StreamingChatCompletionUpdate> updates
= client.CompleteChatStreaming("Say 'this is a test.'");
```

Notice that the returned value is a `ResultCollection<StreamingChatCompletionUpdate>` instance, which can be enumerated to process the streaming response chunks as they arrive:
Notice that the returned value is a `CollectionResult<StreamingChatCompletionUpdate>` instance, which can be enumerated to process the streaming response chunks as they arrive:

```csharp
Console.WriteLine($"[ASSISTANT]:");
Expand All @@ -132,10 +132,10 @@ foreach (StreamingChatCompletionUpdate update in updates)
}
```

Alternatively, you can do this asynchronously by calling the `CompleteChatStreamingAsync` method to get an `AsyncResultCollection<StreamingChatCompletionUpdate>` and enumerate it using `await foreach`:
Alternatively, you can do this asynchronously by calling the `CompleteChatStreamingAsync` method to get an `AsyncCollectionResult<StreamingChatCompletionUpdate>` and enumerate it using `await foreach`:

```csharp
AsyncResultCollection<StreamingChatCompletionUpdate> updates
AsyncCollectionResult<StreamingChatCompletionUpdate> updates
= client.CompleteChatStreamingAsync("Say 'this is a test.'");

Console.WriteLine($"[ASSISTANT]:");
Expand Down Expand Up @@ -528,7 +528,7 @@ Finally, you can use the `AssistantClient`'s `GetMessages` method to retrieve th
For illustrative purposes, you could print the messages to the console and also save any images produced by the assistant to local storage:

```csharp
PageableCollection<ThreadMessage> messages = assistantClient.GetMessages(threadRun.ThreadId, ListOrder.OldestFirst);
PageCollection<ThreadMessage> messages = assistantClient.GetMessages(threadRun.ThreadId, ListOrder.OldestFirst);
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved

foreach (ThreadMessage message in messages)
{
Expand Down Expand Up @@ -640,10 +640,10 @@ AssistantThread thread = assistantClient.CreateThread(new ThreadCreationOptions(
});
```

With the assistant and thread prepared, use the `CreateRunStreaming` method to get an enumerable `ResultCollection<StreamingUpdate>`. You can then iterate over this collection with `foreach`. For async calling patterns, use `CreateRunStreamingAsync` and iterate over the `AsyncResultCollection<StreamingUpdate>` with `await foreach`, instead. Note that streaming variants also exist for `CreateThreadAndRunStreaming` and `SubmitToolOutputsToRunStreaming`.
With the assistant and thread prepared, use the `CreateRunStreaming` method to get an enumerable `CollectionResult<StreamingUpdate>`. You can then iterate over this collection with `foreach`. For async calling patterns, use `CreateRunStreamingAsync` and iterate over the `AsyncCollectionResult<StreamingUpdate>` with `await foreach`, instead. Note that streaming variants also exist for `CreateThreadAndRunStreaming` and `SubmitToolOutputsToRunStreaming`.

```csharp
ResultCollection<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreaming(
CollectionResult<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreaming(
thread,
assistant,
new RunCreationOptions()
Expand Down
5 changes: 3 additions & 2 deletions examples/Assistants/Example01_RetrievalAugmentedGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ public void Example01_RetrievalAugmentedGeneration()
} while (!threadRun.Status.IsTerminal);

// Finally, we'll print out the full history for the thread that includes the augmented generation
PageableCollection<ThreadMessage> messages
= assistantClient.GetMessages(threadRun.ThreadId, ListOrder.OldestFirst);
PageCollection<ThreadMessage> messagePages
= assistantClient.GetMessages(threadRun.ThreadId, new MessageCollectionOptions() { Order = ListOrder.OldestFirst });
IEnumerable<ThreadMessage> messages = messagePages.GetAllValues();

foreach (ThreadMessage message in messages)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ public async Task Example01_RetrievalAugmentedGenerationAsync()
} while (!threadRun.Status.IsTerminal);

// Finally, we'll print out the full history for the thread that includes the augmented generation
AsyncPageableCollection<ThreadMessage> messages
= assistantClient.GetMessagesAsync(threadRun.ThreadId, ListOrder.OldestFirst);
AsyncPageCollection<ThreadMessage> messagePages
= assistantClient.GetMessagesAsync(threadRun.ThreadId, new MessageCollectionOptions() { Order = ListOrder.OldestFirst });
IAsyncEnumerable<ThreadMessage> messages = messagePages.GetAllValuesAsync();

await foreach (ThreadMessage message in messages)
{
Expand Down
5 changes: 3 additions & 2 deletions examples/Assistants/Example02_FunctionCalling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ string GetCurrentWeather(string location, string unit = "celsius")
// With the run complete, list the messages and display their content
if (run.Status == RunStatus.Completed)
{
PageableCollection<ThreadMessage> messages
= client.GetMessages(run.ThreadId, resultOrder: ListOrder.OldestFirst);
PageCollection<ThreadMessage> messagePages
= client.GetMessages(run.ThreadId, new MessageCollectionOptions() { Order = ListOrder.OldestFirst });
IEnumerable<ThreadMessage> messages = messagePages.GetAllValues();

foreach (ThreadMessage message in messages)
{
Expand Down
5 changes: 3 additions & 2 deletions examples/Assistants/Example02_FunctionCallingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ string GetCurrentWeather(string location, string unit = "celsius")
// With the run complete, list the messages and display their content
if (run.Status == RunStatus.Completed)
{
AsyncPageableCollection<ThreadMessage> messages
= client.GetMessagesAsync(run.ThreadId, resultOrder: ListOrder.OldestFirst);
AsyncPageCollection<ThreadMessage> messagePages
= client.GetMessagesAsync(run.ThreadId, new MessageCollectionOptions() { Order = ListOrder.OldestFirst });
IAsyncEnumerable<ThreadMessage> messages = messagePages.GetAllValuesAsync();

await foreach (ThreadMessage message in messages)
{
Expand Down
3 changes: 1 addition & 2 deletions examples/Assistants/Example02b_FunctionCallingStreaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public async Task Example02b_FunctionCallingStreaming()
#endregion

#region Step 3 - Initiate a streaming run
// TODO: replace this with finalized enumerable result pattern
AsyncResultCollection<StreamingUpdate> asyncUpdates
AsyncCollectionResult<StreamingUpdate> asyncUpdates
= client.CreateRunStreamingAsync(thread, assistant);

ThreadRun currentRun = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OpenAI.Assistants;
using System;
using System.ClientModel;
using System.Collections.Generic;

namespace OpenAI.Examples;

Expand All @@ -16,7 +17,8 @@ public void Example03_ListAssistantsWithPagination()

int count = 0;

PageableCollection<Assistant> assistants = client.GetAssistants();
PageCollection<Assistant> assistantPages = client.GetAssistants();
IEnumerable<Assistant> assistants = assistantPages.GetAllValues();
foreach (Assistant assistant in assistants)
{
Console.WriteLine($"[{count,3}] {assistant.Id} {assistant.CreatedAt:s} {assistant.Name}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OpenAI.Assistants;
using System;
using System.ClientModel;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OpenAI.Examples;
Expand All @@ -17,7 +18,8 @@ public async Task Example03_ListAssistantsWithPaginationAsync()

int count = 0;

AsyncPageableCollection<Assistant> assistants = client.GetAssistantsAsync();
AsyncPageCollection<Assistant> assistantPages = client.GetAssistantsAsync();
IAsyncEnumerable<Assistant> assistants = assistantPages.GetAllValuesAsync();
await foreach (Assistant assistant in assistants)
{
Console.WriteLine($"[{count,3}] {assistant.Id} {assistant.CreatedAt:s} {assistant.Name}");
Expand Down
13 changes: 9 additions & 4 deletions examples/Assistants/Example04_AllTheTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ static string GetNameOfFamilyMember(string relation)
// With the run complete, list the messages and display their content
if (run.Status == RunStatus.Completed)
{
PageableCollection<ThreadMessage> messages
= client.GetMessages(run.ThreadId, resultOrder: ListOrder.OldestFirst);
PageCollection<ThreadMessage> messagePages
= client.GetMessages(run.ThreadId, new MessageCollectionOptions() { Order = ListOrder.OldestFirst });
IEnumerable<ThreadMessage> messages = messagePages.GetAllValues();

foreach (ThreadMessage message in messages)
{
Expand Down Expand Up @@ -171,8 +172,12 @@ PageableCollection<ThreadMessage> messages
#endregion

#region List run steps for details about tool calls
PageableCollection<RunStep> runSteps = client.GetRunSteps(run, resultOrder: ListOrder.OldestFirst);
foreach (RunStep step in runSteps)
PageCollection<RunStep> runSteps = client.GetRunSteps(
run, new RunStepCollectionOptions()
{
Order = ListOrder.OldestFirst
});
foreach (RunStep step in runSteps.GetAllValues())
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine($"Run step: {step.Status}");
foreach (RunStepToolCall toolCall in step.Details.ToolCalls)
Expand Down
2 changes: 1 addition & 1 deletion examples/Assistants/Example05_AssistantsWithVision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Example05_AssistantsWithVision()
}
});

ResultCollection<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreaming(
CollectionResult<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreaming(
thread,
assistant,
new RunCreationOptions()
Expand Down
2 changes: 1 addition & 1 deletion examples/Assistants/Example05_AssistantsWithVisionAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task Example05_AssistantsWithVisionAsync()
}
});

AsyncResultCollection<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreamingAsync(
AsyncCollectionResult<StreamingUpdate> streamingUpdates = assistantClient.CreateRunStreamingAsync(
thread,
assistant,
new RunCreationOptions()
Expand Down
2 changes: 1 addition & 1 deletion examples/Chat/Example02_SimpleChatStreaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Example02_SimpleChatStreaming()
{
ChatClient client = new(model: "gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

ResultCollection<StreamingChatCompletionUpdate> updates
CollectionResult<StreamingChatCompletionUpdate> updates
= client.CompleteChatStreaming("Say 'this is a test.'");

Console.WriteLine($"[ASSISTANT]:");
Expand Down
2 changes: 1 addition & 1 deletion examples/Chat/Example02_SimpleChatStreamingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task Example02_SimpleChatStreamingAsync()
{
ChatClient client = new(model: "gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

AsyncResultCollection<StreamingChatCompletionUpdate> updates
AsyncCollectionResult<StreamingChatCompletionUpdate> updates
= client.CompleteChatStreamingAsync("Say 'this is a test.'");

Console.WriteLine($"[ASSISTANT]:");
Expand Down
2 changes: 1 addition & 1 deletion examples/Chat/Example04_FunctionCallingStreaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Example04_FunctionCallingStreaming()
Dictionary<int, string> indexToFunctionName = [];
Dictionary<int, StringBuilder> indexToFunctionArguments = [];
StringBuilder contentBuilder = new();
ResultCollection<StreamingChatCompletionUpdate> chatUpdates
CollectionResult<StreamingChatCompletionUpdate> chatUpdates
= client.CompleteChatStreaming(messages, options);

foreach (StreamingChatCompletionUpdate chatUpdate in chatUpdates)
Expand Down
2 changes: 1 addition & 1 deletion examples/Chat/Example04_FunctionCallingStreamingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task Example04_FunctionCallingStreamingAsync()
Dictionary<int, string> indexToFunctionName = [];
Dictionary<int, StringBuilder> indexToFunctionArguments = [];
StringBuilder contentBuilder = new();
AsyncResultCollection<StreamingChatCompletionUpdate> chatUpdates
AsyncCollectionResult<StreamingChatCompletionUpdate> chatUpdates
= client.CompleteChatStreamingAsync(messages, options);

await foreach (StreamingChatCompletionUpdate chatUpdate in chatUpdates)
Expand Down
Loading
Loading