Skip to content

Commit d01aefa

Browse files
authored
Add samples and README (#3)
1 parent 48f0c6a commit d01aefa

39 files changed

+2254
-716
lines changed

.dotnet/README.md

Lines changed: 538 additions & 0 deletions
Large diffs are not rendered by default.

.dotnet/src/Custom/Assistants/AssistantClient.cs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1+
using OpenAI.ClientShared.Internal;
12
using System;
2-
using System.ClientModel;
3-
using System.ClientModel.Internal;
4-
53
using System.ClientModel;
64
using System.ClientModel.Primitives;
75
using System.Collections.Generic;
8-
using System.Threading;
96
using System.Threading.Tasks;
10-
using OpenAI.Internal;
11-
using System.Text.Json;
12-
using OpenAI.ClientShared.Internal;
137

148
namespace OpenAI.Assistants;
159

@@ -530,13 +524,13 @@ Internal.Models.CreateThreadAndRunRequest request
530524
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
531525
}
532526

533-
public virtual ClientResult<ThreadRun> GetRun(string threadId, string runId)
527+
public virtual ClientResult<ThreadRun> GetRun(string threadId, string runId)
534528
{
535529
ClientResult<Internal.Models.RunObject> internalResult = RunShim.GetRun(threadId, runId);
536530
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
537531
}
538532

539-
public virtual async Task<ClientResult<ThreadRun>> GetRunAsync(string threadId, string runId)
533+
public virtual async Task<ClientResult<ThreadRun>> GetRunAsync(string threadId, string runId)
540534
{
541535
ClientResult<Internal.Models.RunObject> internalResult
542536
= await RunShim.GetRunAsync(threadId, runId).ConfigureAwait(false);
@@ -575,60 +569,60 @@ public virtual Task<ClientResult<ListQueryPage<ThreadRun>>> GetRunsAsync(
575569
return GetListQueryPageAsync<ThreadRun, Internal.Models.ListRunsResponse>(internalFunc);
576570
}
577571

578-
public virtual ClientResult<ThreadRun> ModifyRun(string threadId, string runId, RunModificationOptions options)
572+
public virtual ClientResult<ThreadRun> ModifyRun(string threadId, string runId, RunModificationOptions options)
579573
{
580574
Internal.Models.ModifyRunRequest request = new(options.Metadata, serializedAdditionalRawData: null);
581575
ClientResult<Internal.Models.RunObject> internalResult = RunShim.ModifyRun(threadId, runId, request);
582576
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
583577
}
584578

585-
public virtual async Task<ClientResult<ThreadRun>> ModifyRunAsync(string threadId, string runId, RunModificationOptions options)
579+
public virtual async Task<ClientResult<ThreadRun>> ModifyRunAsync(string threadId, string runId, RunModificationOptions options)
586580
{
587581
Internal.Models.ModifyRunRequest request = new(options.Metadata, serializedAdditionalRawData: null);
588582
ClientResult<Internal.Models.RunObject> internalResult
589583
= await RunShim.ModifyRunAsync(threadId, runId, request).ConfigureAwait(false);
590584
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
591585
}
592586

593-
public virtual ClientResult<bool> CancelRun(string threadId, string runId)
587+
public virtual ClientResult<bool> CancelRun(string threadId, string runId)
594588
{
595589
ClientResult<Internal.Models.RunObject> internalResult = RunShim.CancelRun(threadId, runId);
596590
return ClientResult.FromValue(true, internalResult.GetRawResponse());
597591
}
598592

599-
public virtual async Task<ClientResult<bool>> CancelRunAsync(string threadId, string runId)
593+
public virtual async Task<ClientResult<bool>> CancelRunAsync(string threadId, string runId)
600594
{
601595
ClientResult<Internal.Models.RunObject> internalResult
602596
= await RunShim.CancelRunAsync(threadId, runId);
603597
return ClientResult.FromValue(true, internalResult.GetRawResponse());
604598
}
605599

606-
public virtual ClientResult<bool> SubmitToolOutputs(string threadId, string runId, IEnumerable<ToolOutput> toolOutputs)
600+
public virtual ClientResult<ThreadRun> SubmitToolOutputs(string threadId, string runId, IEnumerable<ToolOutput> toolOutputs)
607601
{
608-
BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new
609-
{
610-
tool_outputs = toolOutputs
611-
},
612-
new JsonSerializerOptions()
613-
{
614-
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
615-
}));
616-
ClientResult internalResult = RunShim.SubmitToolOuputsToRun(threadId, runId, content, default);
617-
return ClientResult.FromValue(true, internalResult.GetRawResponse());
602+
List<Internal.Models.SubmitToolOutputsRunRequestToolOutput> requestToolOutputs = [];
603+
604+
foreach (ToolOutput toolOutput in toolOutputs)
605+
{
606+
requestToolOutputs.Add(new(toolOutput.Id, toolOutput.Output, null));
607+
}
608+
609+
Internal.Models.SubmitToolOutputsRunRequest request = new(requestToolOutputs, null);
610+
ClientResult<Internal.Models.RunObject> internalResult = RunShim.SubmitToolOuputsToRun(threadId, runId, request);
611+
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
618612
}
619613

620-
public virtual async Task<ClientResult<bool>> SubmitToolOutputsAsync(string threadId, string runId, IEnumerable<ToolOutput> toolOutputs)
614+
public virtual async Task<ClientResult<bool>> SubmitToolOutputsAsync(string threadId, string runId, IEnumerable<ToolOutput> toolOutputs)
621615
{
622-
BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new
623-
{
624-
tool_outputs = toolOutputs
625-
},
626-
new JsonSerializerOptions()
627-
{
628-
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
629-
}));
630-
ClientResult internalResult
631-
= await RunShim.SubmitToolOuputsToRunAsync(threadId, runId, content, default).ConfigureAwait(false);
616+
List<Internal.Models.SubmitToolOutputsRunRequestToolOutput> requestToolOutputs = [];
617+
618+
foreach (ToolOutput toolOutput in toolOutputs)
619+
{
620+
requestToolOutputs.Add(new(toolOutput.Id, toolOutput.Output, null));
621+
}
622+
623+
Internal.Models.SubmitToolOutputsRunRequest request = new(requestToolOutputs, null);
624+
ClientResult<Internal.Models.RunObject> internalResult
625+
= await RunShim.SubmitToolOuputsToRunAsync(threadId, runId, request).ConfigureAwait(false);
632626
return ClientResult.FromValue(true, internalResult.GetRawResponse());
633627
}
634628

.dotnet/src/Custom/Files/OpenAIFileInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public partial class OpenAIFileInfo
77
public string Id { get; }
88
public OpenAIFilePurpose Purpose { get; }
99
public string Filename { get; }
10-
public long Size { get; }
10+
public long? Size { get; }
1111
public DateTimeOffset CreatedAt { get; }
1212

1313
internal OpenAIFileInfo(Internal.Models.OpenAIFile internalFile)

.dotnet/src/Generated/Models/OpenAIFile.Serialization.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ void IJsonModel<OpenAIFile>.Write(Utf8JsonWriter writer, ModelReaderWriterOption
2121
writer.WriteStartObject();
2222
writer.WritePropertyName("id"u8);
2323
writer.WriteStringValue(Id);
24-
writer.WritePropertyName("bytes"u8);
25-
writer.WriteNumberValue(Bytes);
24+
if (Bytes != null)
25+
{
26+
writer.WritePropertyName("bytes"u8);
27+
writer.WriteNumberValue(Bytes.Value);
28+
}
29+
else
30+
{
31+
writer.WriteNull("bytes");
32+
}
2633
writer.WritePropertyName("created_at"u8);
2734
writer.WriteNumberValue(CreatedAt, "U");
2835
writer.WritePropertyName("filename"u8);
@@ -77,7 +84,7 @@ internal static OpenAIFile DeserializeOpenAIFile(JsonElement element, ModelReade
7784
return null;
7885
}
7986
string id = default;
80-
long bytes = default;
87+
long? bytes = default;
8188
DateTimeOffset createdAt = default;
8289
string filename = default;
8390
OpenAIFileObject @object = default;
@@ -95,6 +102,11 @@ internal static OpenAIFile DeserializeOpenAIFile(JsonElement element, ModelReade
95102
}
96103
if (property.NameEquals("bytes"u8))
97104
{
105+
if (property.Value.ValueKind == JsonValueKind.Null)
106+
{
107+
bytes = null;
108+
continue;
109+
}
98110
bytes = property.Value.GetInt64();
99111
continue;
100112
}

.dotnet/src/Generated/Models/OpenAIFile.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal partial class OpenAIFile
5555
/// `error`.
5656
/// </param>
5757
/// <exception cref="ArgumentNullException"> <paramref name="id"/> or <paramref name="filename"/> is null. </exception>
58-
internal OpenAIFile(string id, long bytes, DateTimeOffset createdAt, string filename, OpenAIFilePurpose purpose, OpenAIFileStatus status)
58+
internal OpenAIFile(string id, long? bytes, DateTimeOffset createdAt, string filename, OpenAIFilePurpose purpose, OpenAIFileStatus status)
5959
{
6060
if (id is null) throw new ArgumentNullException(nameof(id));
6161
if (filename is null) throw new ArgumentNullException(nameof(filename));
@@ -87,7 +87,7 @@ internal OpenAIFile(string id, long bytes, DateTimeOffset createdAt, string file
8787
/// field on `fine_tuning.job`.
8888
/// </param>
8989
/// <param name="serializedAdditionalRawData"> Keeps track of any properties unknown to the library. </param>
90-
internal OpenAIFile(string id, long bytes, DateTimeOffset createdAt, string filename, OpenAIFileObject @object, OpenAIFilePurpose purpose, OpenAIFileStatus status, string statusDetails, IDictionary<string, BinaryData> serializedAdditionalRawData)
90+
internal OpenAIFile(string id, long? bytes, DateTimeOffset createdAt, string filename, OpenAIFileObject @object, OpenAIFilePurpose purpose, OpenAIFileStatus status, string statusDetails, IDictionary<string, BinaryData> serializedAdditionalRawData)
9191
{
9292
Id = id;
9393
Bytes = bytes;
@@ -108,7 +108,7 @@ internal OpenAIFile()
108108
/// <summary> The file identifier, which can be referenced in the API endpoints. </summary>
109109
public string Id { get; }
110110
/// <summary> The size of the file, in bytes. </summary>
111-
public long Bytes { get; }
111+
public long? Bytes { get; }
112112
/// <summary> The Unix timestamp (in seconds) for when the file was created. </summary>
113113
public DateTimeOffset CreatedAt { get; }
114114
/// <summary> The name of the file. </summary>

.dotnet/src/Generated/Models/RunObject.Serialization.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ void IJsonModel<RunObject>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions
4949
{
5050
writer.WriteNull("last_error");
5151
}
52-
writer.WritePropertyName("expires_at"u8);
53-
writer.WriteNumberValue(ExpiresAt, "U");
52+
if (ExpiresAt != null)
53+
{
54+
writer.WritePropertyName("expires_at"u8);
55+
writer.WriteStringValue(ExpiresAt.Value, "O");
56+
}
57+
else
58+
{
59+
writer.WriteNull("expires_at");
60+
}
5461
if (StartedAt != null)
5562
{
5663
writer.WritePropertyName("started_at"u8);
@@ -187,7 +194,7 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
187194
RunObjectStatus status = default;
188195
RunObjectRequiredAction requiredAction = default;
189196
RunObjectLastError lastError = default;
190-
DateTimeOffset expiresAt = default;
197+
DateTimeOffset? expiresAt = default;
191198
DateTimeOffset? startedAt = default;
192199
DateTimeOffset? cancelledAt = default;
193200
DateTimeOffset? failedAt = default;
@@ -254,6 +261,13 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
254261
}
255262
if (property.NameEquals("expires_at"u8))
256263
{
264+
if (property.Value.ValueKind == JsonValueKind.Null)
265+
{
266+
expiresAt = null;
267+
continue;
268+
}
269+
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
270+
// expiresAt = property.Value.GetDateTimeOffset("O");
257271
expiresAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
258272
continue;
259273
}
@@ -264,7 +278,9 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
264278
startedAt = null;
265279
continue;
266280
}
267-
startedAt = property.Value.GetDateTimeOffset("O");
281+
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
282+
// startedAt = property.Value.GetDateTimeOffset("O");
283+
startedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
268284
continue;
269285
}
270286
if (property.NameEquals("cancelled_at"u8))
@@ -274,7 +290,9 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
274290
cancelledAt = null;
275291
continue;
276292
}
277-
cancelledAt = property.Value.GetDateTimeOffset("O");
293+
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
294+
// cancelledAt = property.Value.GetDateTimeOffset("O");
295+
cancelledAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
278296
continue;
279297
}
280298
if (property.NameEquals("failed_at"u8))
@@ -284,7 +302,9 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
284302
failedAt = null;
285303
continue;
286304
}
287-
failedAt = property.Value.GetDateTimeOffset("O");
305+
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
306+
// failedAt = property.Value.GetDateTimeOffset("O");
307+
failedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
288308
continue;
289309
}
290310
if (property.NameEquals("completed_at"u8))
@@ -294,7 +314,9 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
294314
completedAt = null;
295315
continue;
296316
}
297-
completedAt = property.Value.GetDateTimeOffset("O");
317+
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
318+
// completedAt = property.Value.GetDateTimeOffset("O");
319+
completedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
298320
continue;
299321
}
300322
if (property.NameEquals("model"u8))

.dotnet/src/Generated/Models/RunObject.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal partial class RunObject
7878
/// </param>
7979
/// <param name="usage"></param>
8080
/// <exception cref="ArgumentNullException"> <paramref name="id"/>, <paramref name="threadId"/>, <paramref name="assistantId"/>, <paramref name="model"/>, <paramref name="instructions"/>, <paramref name="tools"/> or <paramref name="fileIds"/> is null. </exception>
81-
internal RunObject(string id, DateTimeOffset createdAt, string threadId, string assistantId, RunObjectStatus status, RunObjectRequiredAction requiredAction, RunObjectLastError lastError, DateTimeOffset expiresAt, DateTimeOffset? startedAt, DateTimeOffset? cancelledAt, DateTimeOffset? failedAt, DateTimeOffset? completedAt, string model, string instructions, IEnumerable<BinaryData> tools, IEnumerable<string> fileIds, IReadOnlyDictionary<string, string> metadata, RunCompletionUsage usage)
81+
internal RunObject(string id, DateTimeOffset createdAt, string threadId, string assistantId, RunObjectStatus status, RunObjectRequiredAction requiredAction, RunObjectLastError lastError, DateTimeOffset? expiresAt, DateTimeOffset? startedAt, DateTimeOffset? cancelledAt, DateTimeOffset? failedAt, DateTimeOffset? completedAt, string model, string instructions, IEnumerable<BinaryData> tools, IEnumerable<string> fileIds, IReadOnlyDictionary<string, string> metadata, RunCompletionUsage usage)
8282
{
8383
if (id is null) throw new ArgumentNullException(nameof(id));
8484
if (threadId is null) throw new ArgumentNullException(nameof(threadId));
@@ -145,7 +145,7 @@ internal RunObject(string id, DateTimeOffset createdAt, string threadId, string
145145
/// </param>
146146
/// <param name="usage"></param>
147147
/// <param name="serializedAdditionalRawData"> Keeps track of any properties unknown to the library. </param>
148-
internal RunObject(string id, RunObjectObject @object, DateTimeOffset createdAt, string threadId, string assistantId, RunObjectStatus status, RunObjectRequiredAction requiredAction, RunObjectLastError lastError, DateTimeOffset expiresAt, DateTimeOffset? startedAt, DateTimeOffset? cancelledAt, DateTimeOffset? failedAt, DateTimeOffset? completedAt, string model, string instructions, IReadOnlyList<BinaryData> tools, IReadOnlyList<string> fileIds, IReadOnlyDictionary<string, string> metadata, RunCompletionUsage usage, IDictionary<string, BinaryData> serializedAdditionalRawData)
148+
internal RunObject(string id, RunObjectObject @object, DateTimeOffset createdAt, string threadId, string assistantId, RunObjectStatus status, RunObjectRequiredAction requiredAction, RunObjectLastError lastError, DateTimeOffset? expiresAt, DateTimeOffset? startedAt, DateTimeOffset? cancelledAt, DateTimeOffset? failedAt, DateTimeOffset? completedAt, string model, string instructions, IReadOnlyList<BinaryData> tools, IReadOnlyList<string> fileIds, IReadOnlyDictionary<string, string> metadata, RunCompletionUsage usage, IDictionary<string, BinaryData> serializedAdditionalRawData)
149149
{
150150
Id = id;
151151
Object = @object;
@@ -201,7 +201,7 @@ internal RunObject()
201201
/// <summary> The last error associated with this run. Will be `null` if there are no errors. </summary>
202202
public RunObjectLastError LastError { get; }
203203
/// <summary> The Unix timestamp (in seconds) for when the run will expire. </summary>
204-
public DateTimeOffset ExpiresAt { get; }
204+
public DateTimeOffset? ExpiresAt { get; }
205205
/// <summary> The Unix timestamp (in seconds) for when the run was started. </summary>
206206
public DateTimeOffset? StartedAt { get; }
207207
/// <summary> The Unix timestamp (in seconds) for when the run was cancelled. </summary>

.dotnet/src/Generated/OpenAIModelFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public static CreateThreadAndRunRequest CreateThreadAndRunRequest(string assista
531531
/// </param>
532532
/// <param name="usage"></param>
533533
/// <returns> A new <see cref="Models.RunObject"/> instance for mocking. </returns>
534-
public static RunObject RunObject(string id = null, RunObjectObject @object = default, DateTimeOffset createdAt = default, string threadId = null, string assistantId = null, RunObjectStatus status = default, RunObjectRequiredAction requiredAction = null, RunObjectLastError lastError = null, DateTimeOffset expiresAt = default, DateTimeOffset? startedAt = null, DateTimeOffset? cancelledAt = null, DateTimeOffset? failedAt = null, DateTimeOffset? completedAt = null, string model = null, string instructions = null, IEnumerable<BinaryData> tools = null, IEnumerable<string> fileIds = null, IReadOnlyDictionary<string, string> metadata = null, RunCompletionUsage usage = null)
534+
public static RunObject RunObject(string id = null, RunObjectObject @object = default, DateTimeOffset createdAt = default, string threadId = null, string assistantId = null, RunObjectStatus status = default, RunObjectRequiredAction requiredAction = null, RunObjectLastError lastError = null, DateTimeOffset? expiresAt = null, DateTimeOffset? startedAt = null, DateTimeOffset? cancelledAt = null, DateTimeOffset? failedAt = null, DateTimeOffset? completedAt = null, string model = null, string instructions = null, IEnumerable<BinaryData> tools = null, IEnumerable<string> fileIds = null, IReadOnlyDictionary<string, string> metadata = null, RunCompletionUsage usage = null)
535535
{
536536
tools ??= new List<BinaryData>();
537537
fileIds ??= new List<string>();

0 commit comments

Comments
 (0)