Skip to content

Commit 7dee5d8

Browse files
authored
Add pagination support to GetConversationItems and GetConversationItemsAsync in the ConversationClient (#784)
1 parent 8d8b4af commit 7dee5d8

27 files changed

+12876
-12367
lines changed

api/OpenAI.net8.0.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,8 +2623,8 @@ public class ConversationClient {
26232623
public virtual Task<ClientResult> GetConversationAsync(string conversationId, RequestOptions options = null);
26242624
public virtual ClientResult GetConversationItem(string conversationId, string itemId, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
26252625
public virtual Task<ClientResult> GetConversationItemAsync(string conversationId, string itemId, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
2626-
public virtual ClientResult GetConversationItems(string conversationId, long? limit = null, string order = null, string after = null, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
2627-
public virtual Task<ClientResult> GetConversationItemsAsync(string conversationId, long? limit = null, string order = null, string after = null, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
2626+
public virtual CollectionResult GetConversationItems(string conversationId, long? limit = null, string order = null, string after = null, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
2627+
public virtual AsyncCollectionResult GetConversationItemsAsync(string conversationId, long? limit = null, string order = null, string after = null, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
26282628
public virtual ClientResult UpdateConversation(string conversationId, BinaryContent content, RequestOptions options = null);
26292629
public virtual Task<ClientResult> UpdateConversationAsync(string conversationId, BinaryContent content, RequestOptions options = null);
26302630
}

api/OpenAI.netstandard2.0.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,8 +2257,8 @@ public class ConversationClient {
22572257
public virtual Task<ClientResult> GetConversationAsync(string conversationId, RequestOptions options = null);
22582258
public virtual ClientResult GetConversationItem(string conversationId, string itemId, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
22592259
public virtual Task<ClientResult> GetConversationItemAsync(string conversationId, string itemId, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
2260-
public virtual ClientResult GetConversationItems(string conversationId, long? limit = null, string order = null, string after = null, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
2261-
public virtual Task<ClientResult> GetConversationItemsAsync(string conversationId, long? limit = null, string order = null, string after = null, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
2260+
public virtual CollectionResult GetConversationItems(string conversationId, long? limit = null, string order = null, string after = null, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
2261+
public virtual AsyncCollectionResult GetConversationItemsAsync(string conversationId, long? limit = null, string order = null, string after = null, IEnumerable<IncludedConversationItemProperty> include = null, RequestOptions options = null);
22622262
public virtual ClientResult UpdateConversation(string conversationId, BinaryContent content, RequestOptions options = null);
22632263
public virtual Task<ClientResult> UpdateConversationAsync(string conversationId, BinaryContent content, RequestOptions options = null);
22642264
}

examples/Conversations/Example01_ConversationProtocol.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using OpenAI.Conversations;
33
using System;
44
using System.ClientModel;
5+
using System.ClientModel.Primitives;
6+
using System.Collections.Generic;
57
using System.Text.Json;
68

79
namespace OpenAI.Examples;
@@ -25,6 +27,11 @@ public void Example01_ConversationProtocol()
2527
"type": "message",
2628
"role": "user",
2729
"content": "Say 'this is a test.'"
30+
},
31+
{
32+
"type": "message",
33+
"role": "assistant",
34+
"content": "This is a test."
2835
}
2936
]
3037
}
@@ -37,26 +44,32 @@ public void Example01_ConversationProtocol()
3744
.GetProperty("id"u8)
3845
.GetString();
3946

40-
Console.WriteLine($"Conversation created. Conversation ID: {conversationId}");
47+
Console.WriteLine($"Conversation created.");
48+
Console.WriteLine($" Conversation ID: {conversationId}");
4149
Console.WriteLine();
4250

43-
ClientResult getConversationItemsResult = client.GetConversationItems(conversationId);
44-
using JsonDocument getConversationItemsResultAsJson = JsonDocument.Parse(getConversationItemsResult.GetRawResponse().Content.ToString());
45-
string messageId = getConversationItemsResultAsJson.RootElement
46-
.GetProperty("data"u8)[0]
47-
.GetProperty("id"u8)
48-
.ToString();
51+
CollectionResult getConversationItemsResults = client.GetConversationItems(conversationId);
52+
foreach (ClientResult result in getConversationItemsResults.GetRawPages())
53+
{
54+
using JsonDocument getConversationItemsResultAsJson = JsonDocument.Parse(result.GetRawResponse().Content.ToString());
55+
foreach (JsonElement element in getConversationItemsResultAsJson.RootElement.GetProperty("data").EnumerateArray())
56+
{
57+
string messageId = element.GetProperty("id"u8).ToString();
4958

50-
Console.WriteLine($"Message retrieved. Message ID: {messageId}");
51-
Console.WriteLine();
59+
Console.WriteLine($"Message retrieved.");
60+
Console.WriteLine($" Message ID: {messageId}");
61+
Console.WriteLine();
62+
}
63+
}
5264

5365
ClientResult deleteConversationResult = client.DeleteConversation(conversationId);
5466
using JsonDocument deleteConversationResultAsJson = JsonDocument.Parse(deleteConversationResult.GetRawResponse().Content.ToString());
5567
bool deleted = deleteConversationResultAsJson.RootElement
5668
.GetProperty("deleted"u8)
5769
.GetBoolean();
5870

59-
Console.WriteLine($"Conversation deleted: {deleted}");
71+
Console.WriteLine($"Conversation deleted.");
72+
Console.WriteLine($" Deleted: {deleted}");
6073
Console.WriteLine();
6174
}
6275
}

examples/Conversations/Example01_ConversationProtocolAsync.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using OpenAI.Conversations;
33
using System;
44
using System.ClientModel;
5+
using System.ClientModel.Primitives;
56
using System.Text.Json;
67
using System.Threading.Tasks;
78

@@ -26,6 +27,11 @@ public async Task Example01_ConversationProtocolAsync()
2627
"type": "message",
2728
"role": "user",
2829
"content": "Say 'this is a test.'"
30+
},
31+
{
32+
"type": "message",
33+
"role": "assistant",
34+
"content": "This is a test."
2935
}
3036
]
3137
}
@@ -38,26 +44,32 @@ public async Task Example01_ConversationProtocolAsync()
3844
.GetProperty("id"u8)
3945
.GetString();
4046

41-
Console.WriteLine($"Conversation created. Conversation ID: {conversationId}");
47+
Console.WriteLine($"Conversation created.");
48+
Console.WriteLine($" Conversation ID: {conversationId}");
4249
Console.WriteLine();
4350

44-
ClientResult getConversationItemsResult = await client.GetConversationItemsAsync(conversationId);
45-
using JsonDocument getConversationItemsResultAsJson = JsonDocument.Parse(getConversationItemsResult.GetRawResponse().Content.ToString());
46-
string messageId = getConversationItemsResultAsJson.RootElement
47-
.GetProperty("data"u8)[0]
48-
.GetProperty("id"u8)
49-
.ToString();
51+
AsyncCollectionResult getConversationItemsResults = client.GetConversationItemsAsync(conversationId);
52+
await foreach(ClientResult result in getConversationItemsResults.GetRawPagesAsync())
53+
{
54+
using JsonDocument getConversationItemsResultAsJson = JsonDocument.Parse(result.GetRawResponse().Content.ToString());
55+
foreach (JsonElement element in getConversationItemsResultAsJson.RootElement.GetProperty("data").EnumerateArray())
56+
{
57+
string messageId = element.GetProperty("id"u8).ToString();
5058

51-
Console.WriteLine($"Message retrieved. Message ID: {messageId}");
52-
Console.WriteLine();
59+
Console.WriteLine($"Message retrieved.");
60+
Console.WriteLine($" Message ID: {messageId}");
61+
Console.WriteLine();
62+
}
63+
}
5364

5465
ClientResult deleteConversationResult = await client.DeleteConversationAsync(conversationId);
5566
using JsonDocument deleteConversationResultAsJson = JsonDocument.Parse(deleteConversationResult.GetRawResponse().Content.ToString());
5667
bool deleted = deleteConversationResultAsJson.RootElement
5768
.GetProperty("deleted"u8)
5869
.GetBoolean();
5970

60-
Console.WriteLine($"Conversation deleted: {deleted}");
71+
Console.WriteLine($"Conversation deleted.");
72+
Console.WriteLine($" Deleted: {deleted}");
6173
Console.WriteLine();
6274
}
6375
}

specification/base/typespec/conversations/models.tsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ model ConversationItemList {
4646
object: "list";
4747

4848
/** A list of conversation items. */
49-
data: ItemResource[];
49+
@pageItems data: ItemResource[];
5050

5151
/** Whether there are more items available. */
5252
#suppress "@azure-tools/typespec-azure-core/casing-style" "Auto-suppressed warnings non-applicable rules during import."
@@ -58,7 +58,7 @@ model ConversationItemList {
5858

5959
/** The ID of the last item in the list. */
6060
#suppress "@azure-tools/typespec-azure-core/casing-style" "Auto-suppressed warnings non-applicable rules during import."
61-
last_id: string;
61+
@continuationToken last_id: string;
6262
}
6363

6464
#suppress "@azure-tools/typespec-azure-core/documentation-required" "Auto-suppressed warnings non-applicable rules during import."

specification/base/typespec/conversations/operations.tsp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace OpenAI;
3838
}
3939
)
4040
@tag("Conversations")
41+
@list
4142
op listConversationItems(
4243
/** The ID of the conversation to list items for. */
4344
#suppress "@azure-tools/typespec-azure-core/casing-style" "Auto-suppressed warnings non-applicable rules during import."

specification/client/models/assistants.models.tsp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "../../base/typespec/assistants/main.tsp";
2+
import "./common.models.tsp";
23
import "@azure-tools/typespec-client-generator-core";
34

45
using Azure.ClientGenerator.Core;
@@ -23,9 +24,9 @@ union AssistantCollectionOrder {
2324
@access(Access.public)
2425
@usage(Usage.input)
2526
model AssistantCollectionOptions {
26-
...CollectionAfterQueryParameter,
27-
...CollectionBeforeQueryParameter,
28-
...CollectionLimitQueryParameter,
27+
...DotNetCollectionAfterQueryParameter,
28+
...DotNetCollectionBeforeQueryParameter,
29+
...DotNetCollectionLimitQueryParameter,
2930
...AssistantCollectionOrderQueryParameter,
3031
}
3132

@@ -46,18 +47,18 @@ union MessageCollectionOrder {
4647
@access(Access.public)
4748
@usage(Usage.input)
4849
model MessageCollectionOptions {
49-
...CollectionAfterQueryParameter,
50-
...CollectionBeforeQueryParameter,
51-
...CollectionLimitQueryParameter,
50+
...DotNetCollectionAfterQueryParameter,
51+
...DotNetCollectionBeforeQueryParameter,
52+
...DotNetCollectionLimitQueryParameter,
5253
...MessageCollectionOrderQueryParameter,
5354
}
5455

5556
@access(Access.public)
5657
@usage(Usage.input)
5758
model RunCollectionOptions {
58-
...CollectionAfterQueryParameter,
59-
...CollectionBeforeQueryParameter,
60-
...CollectionLimitQueryParameter,
59+
...DotNetCollectionAfterQueryParameter,
60+
...DotNetCollectionBeforeQueryParameter,
61+
...DotNetCollectionLimitQueryParameter,
6162
...RunCollectionOrderQueryParameter,
6263
}
6364

@@ -78,9 +79,9 @@ union RunCollectionOrder {
7879
@access(Access.public)
7980
@usage(Usage.input)
8081
model RunStepCollectionOptions {
81-
...CollectionAfterQueryParameter,
82-
...CollectionBeforeQueryParameter,
83-
...CollectionLimitQueryParameter,
82+
...DotNetCollectionAfterQueryParameter,
83+
...DotNetCollectionBeforeQueryParameter,
84+
...DotNetCollectionLimitQueryParameter,
8485
...RunStepCollectionOrderQueryParameter,
8586
}
8687

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "../../base/typespec/batch/main.tsp";
2+
import "./common.models.tsp";
23
import "@azure-tools/typespec-client-generator-core";
34

45
using Azure.ClientGenerator.Core;
@@ -8,8 +9,8 @@ namespace OpenAI;
89
@access(Access.public)
910
@usage(Usage.input)
1011
model BatchCollectionOptions {
11-
...CollectionAfterQueryParameter,
12-
...CollectionLimitQueryParameter,
12+
...DotNetCollectionAfterQueryParameter,
13+
...DotNetCollectionLimitQueryParameter,
1314
}
1415

1516

specification/client/models/chat.models.tsp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import "../../base/typespec/chat/main.tsp";
2+
import "./common.models.tsp";
23
import "@azure-tools/typespec-client-generator-core";
34

45
using Azure.ClientGenerator.Core;
56
using TypeSpec.Http;
67

78
namespace OpenAI;
89

9-
1010
alias ChatCompletionCollectionOrderQueryParameter = {
1111
/**
1212
* Sort order by the `created_at` timestamp of the objects. `asc` for ascending order and`desc`
@@ -24,8 +24,8 @@ union ChatCompletionCollectionOrder {
2424
@access(Access.public)
2525
@usage(Usage.input)
2626
model ChatCompletionCollectionOptions {
27-
...CollectionAfterQueryParameter,
28-
...CollectionLimitQueryParameter,
27+
...DotNetCollectionAfterQueryParameter,
28+
...DotNetCollectionLimitQueryParameter,
2929
...ChatCompletionCollectionOrderQueryParameter,
3030
@query metadata?: Record<string>,
3131
@query `model`?: string,
@@ -48,8 +48,8 @@ union ChatCompletionMessageCollectionOrder {
4848
@access(Access.public)
4949
@usage(Usage.input)
5050
model ChatCompletionMessageCollectionOptions {
51-
...CollectionAfterQueryParameter,
52-
...CollectionLimitQueryParameter,
51+
...DotNetCollectionAfterQueryParameter,
52+
...DotNetCollectionLimitQueryParameter,
5353
...ChatCompletionMessageCollectionOrderQueryParameter,
5454
}
5555

specification/client/models/common.models.tsp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ union DotNetRealtimeVoiceIds {
119119

120120
// CollectionQueryParameters
121121

122-
alias CollectionLimitQueryParameter = {
122+
alias DotNetCollectionLimitQueryParameter = {
123123
/**
124124
* A limit on the number of objects to be returned. Limit can range between 1 and 100, and the
125125
* default is 20.
126126
*/
127127
@query pageSizeLimit?: int32 = 20;
128128
};
129129

130-
alias CollectionAfterQueryParameter = {
130+
alias DotNetCollectionAfterQueryParameter = {
131131
/**
132132
* A cursor for use in pagination. `after` is an object ID that defines your place in the list.
133133
* For instance, if you make a list request and receive 100 objects, ending with obj_foo, your
@@ -136,7 +136,7 @@ alias CollectionAfterQueryParameter = {
136136
@query @continuationToken afterId?: string;
137137
};
138138

139-
alias CollectionBeforeQueryParameter = {
139+
alias DotNetCollectionBeforeQueryParameter = {
140140
/**
141141
* A cursor for use in pagination. `before` is an object ID that defines your place in the list.
142142
* For instance, if you make a list request and receive 100 objects, starting with obj_foo, your

0 commit comments

Comments
 (0)