Skip to content

Commit

Permalink
Add samples and README (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
joseharriaga authored Feb 29, 2024
1 parent 48f0c6a commit d01aefa
Show file tree
Hide file tree
Showing 39 changed files with 2,254 additions and 716 deletions.
538 changes: 538 additions & 0 deletions .dotnet/README.md

Large diffs are not rendered by default.

64 changes: 29 additions & 35 deletions .dotnet/src/Custom/Assistants/AssistantClient.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using OpenAI.ClientShared.Internal;
using System;
using System.ClientModel;
using System.ClientModel.Internal;

using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using OpenAI.Internal;
using System.Text.Json;
using OpenAI.ClientShared.Internal;

namespace OpenAI.Assistants;

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

public virtual ClientResult<ThreadRun> GetRun(string threadId, string runId)
public virtual ClientResult<ThreadRun> GetRun(string threadId, string runId)
{
ClientResult<Internal.Models.RunObject> internalResult = RunShim.GetRun(threadId, runId);
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
}

public virtual async Task<ClientResult<ThreadRun>> GetRunAsync(string threadId, string runId)
public virtual async Task<ClientResult<ThreadRun>> GetRunAsync(string threadId, string runId)
{
ClientResult<Internal.Models.RunObject> internalResult
= await RunShim.GetRunAsync(threadId, runId).ConfigureAwait(false);
Expand Down Expand Up @@ -575,60 +569,60 @@ public virtual Task<ClientResult<ListQueryPage<ThreadRun>>> GetRunsAsync(
return GetListQueryPageAsync<ThreadRun, Internal.Models.ListRunsResponse>(internalFunc);
}

public virtual ClientResult<ThreadRun> ModifyRun(string threadId, string runId, RunModificationOptions options)
public virtual ClientResult<ThreadRun> ModifyRun(string threadId, string runId, RunModificationOptions options)
{
Internal.Models.ModifyRunRequest request = new(options.Metadata, serializedAdditionalRawData: null);
ClientResult<Internal.Models.RunObject> internalResult = RunShim.ModifyRun(threadId, runId, request);
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
}

public virtual async Task<ClientResult<ThreadRun>> ModifyRunAsync(string threadId, string runId, RunModificationOptions options)
public virtual async Task<ClientResult<ThreadRun>> ModifyRunAsync(string threadId, string runId, RunModificationOptions options)
{
Internal.Models.ModifyRunRequest request = new(options.Metadata, serializedAdditionalRawData: null);
ClientResult<Internal.Models.RunObject> internalResult
= await RunShim.ModifyRunAsync(threadId, runId, request).ConfigureAwait(false);
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
}

public virtual ClientResult<bool> CancelRun(string threadId, string runId)
public virtual ClientResult<bool> CancelRun(string threadId, string runId)
{
ClientResult<Internal.Models.RunObject> internalResult = RunShim.CancelRun(threadId, runId);
return ClientResult.FromValue(true, internalResult.GetRawResponse());
}

public virtual async Task<ClientResult<bool>> CancelRunAsync(string threadId, string runId)
public virtual async Task<ClientResult<bool>> CancelRunAsync(string threadId, string runId)
{
ClientResult<Internal.Models.RunObject> internalResult
= await RunShim.CancelRunAsync(threadId, runId);
return ClientResult.FromValue(true, internalResult.GetRawResponse());
}

public virtual ClientResult<bool> SubmitToolOutputs(string threadId, string runId, IEnumerable<ToolOutput> toolOutputs)
public virtual ClientResult<ThreadRun> SubmitToolOutputs(string threadId, string runId, IEnumerable<ToolOutput> toolOutputs)
{
BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new
{
tool_outputs = toolOutputs
},
new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}));
ClientResult internalResult = RunShim.SubmitToolOuputsToRun(threadId, runId, content, default);
return ClientResult.FromValue(true, internalResult.GetRawResponse());
List<Internal.Models.SubmitToolOutputsRunRequestToolOutput> requestToolOutputs = [];

foreach (ToolOutput toolOutput in toolOutputs)
{
requestToolOutputs.Add(new(toolOutput.Id, toolOutput.Output, null));
}

Internal.Models.SubmitToolOutputsRunRequest request = new(requestToolOutputs, null);
ClientResult<Internal.Models.RunObject> internalResult = RunShim.SubmitToolOuputsToRun(threadId, runId, request);
return ClientResult.FromValue(new ThreadRun(internalResult.Value), internalResult.GetRawResponse());
}

public virtual async Task<ClientResult<bool>> SubmitToolOutputsAsync(string threadId, string runId, IEnumerable<ToolOutput> toolOutputs)
public virtual async Task<ClientResult<bool>> SubmitToolOutputsAsync(string threadId, string runId, IEnumerable<ToolOutput> toolOutputs)
{
BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new
{
tool_outputs = toolOutputs
},
new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}));
ClientResult internalResult
= await RunShim.SubmitToolOuputsToRunAsync(threadId, runId, content, default).ConfigureAwait(false);
List<Internal.Models.SubmitToolOutputsRunRequestToolOutput> requestToolOutputs = [];

foreach (ToolOutput toolOutput in toolOutputs)
{
requestToolOutputs.Add(new(toolOutput.Id, toolOutput.Output, null));
}

Internal.Models.SubmitToolOutputsRunRequest request = new(requestToolOutputs, null);
ClientResult<Internal.Models.RunObject> internalResult
= await RunShim.SubmitToolOuputsToRunAsync(threadId, runId, request).ConfigureAwait(false);
return ClientResult.FromValue(true, internalResult.GetRawResponse());
}

Expand Down
2 changes: 1 addition & 1 deletion .dotnet/src/Custom/Files/OpenAIFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class OpenAIFileInfo
public string Id { get; }
public OpenAIFilePurpose Purpose { get; }
public string Filename { get; }
public long Size { get; }
public long? Size { get; }
public DateTimeOffset CreatedAt { get; }

internal OpenAIFileInfo(Internal.Models.OpenAIFile internalFile)
Expand Down
18 changes: 15 additions & 3 deletions .dotnet/src/Generated/Models/OpenAIFile.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ void IJsonModel<OpenAIFile>.Write(Utf8JsonWriter writer, ModelReaderWriterOption
writer.WriteStartObject();
writer.WritePropertyName("id"u8);
writer.WriteStringValue(Id);
writer.WritePropertyName("bytes"u8);
writer.WriteNumberValue(Bytes);
if (Bytes != null)
{
writer.WritePropertyName("bytes"u8);
writer.WriteNumberValue(Bytes.Value);
}
else
{
writer.WriteNull("bytes");
}
writer.WritePropertyName("created_at"u8);
writer.WriteNumberValue(CreatedAt, "U");
writer.WritePropertyName("filename"u8);
Expand Down Expand Up @@ -77,7 +84,7 @@ internal static OpenAIFile DeserializeOpenAIFile(JsonElement element, ModelReade
return null;
}
string id = default;
long bytes = default;
long? bytes = default;
DateTimeOffset createdAt = default;
string filename = default;
OpenAIFileObject @object = default;
Expand All @@ -95,6 +102,11 @@ internal static OpenAIFile DeserializeOpenAIFile(JsonElement element, ModelReade
}
if (property.NameEquals("bytes"u8))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
bytes = null;
continue;
}
bytes = property.Value.GetInt64();
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions .dotnet/src/Generated/Models/OpenAIFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal partial class OpenAIFile
/// `error`.
/// </param>
/// <exception cref="ArgumentNullException"> <paramref name="id"/> or <paramref name="filename"/> is null. </exception>
internal OpenAIFile(string id, long bytes, DateTimeOffset createdAt, string filename, OpenAIFilePurpose purpose, OpenAIFileStatus status)
internal OpenAIFile(string id, long? bytes, DateTimeOffset createdAt, string filename, OpenAIFilePurpose purpose, OpenAIFileStatus status)
{
if (id is null) throw new ArgumentNullException(nameof(id));
if (filename is null) throw new ArgumentNullException(nameof(filename));
Expand Down Expand Up @@ -87,7 +87,7 @@ internal OpenAIFile(string id, long bytes, DateTimeOffset createdAt, string file
/// field on `fine_tuning.job`.
/// </param>
/// <param name="serializedAdditionalRawData"> Keeps track of any properties unknown to the library. </param>
internal OpenAIFile(string id, long bytes, DateTimeOffset createdAt, string filename, OpenAIFileObject @object, OpenAIFilePurpose purpose, OpenAIFileStatus status, string statusDetails, IDictionary<string, BinaryData> serializedAdditionalRawData)
internal OpenAIFile(string id, long? bytes, DateTimeOffset createdAt, string filename, OpenAIFileObject @object, OpenAIFilePurpose purpose, OpenAIFileStatus status, string statusDetails, IDictionary<string, BinaryData> serializedAdditionalRawData)
{
Id = id;
Bytes = bytes;
Expand All @@ -108,7 +108,7 @@ internal OpenAIFile()
/// <summary> The file identifier, which can be referenced in the API endpoints. </summary>
public string Id { get; }
/// <summary> The size of the file, in bytes. </summary>
public long Bytes { get; }
public long? Bytes { get; }
/// <summary> The Unix timestamp (in seconds) for when the file was created. </summary>
public DateTimeOffset CreatedAt { get; }
/// <summary> The name of the file. </summary>
Expand Down
36 changes: 29 additions & 7 deletions .dotnet/src/Generated/Models/RunObject.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ void IJsonModel<RunObject>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions
{
writer.WriteNull("last_error");
}
writer.WritePropertyName("expires_at"u8);
writer.WriteNumberValue(ExpiresAt, "U");
if (ExpiresAt != null)
{
writer.WritePropertyName("expires_at"u8);
writer.WriteStringValue(ExpiresAt.Value, "O");
}
else
{
writer.WriteNull("expires_at");
}
if (StartedAt != null)
{
writer.WritePropertyName("started_at"u8);
Expand Down Expand Up @@ -187,7 +194,7 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
RunObjectStatus status = default;
RunObjectRequiredAction requiredAction = default;
RunObjectLastError lastError = default;
DateTimeOffset expiresAt = default;
DateTimeOffset? expiresAt = default;
DateTimeOffset? startedAt = default;
DateTimeOffset? cancelledAt = default;
DateTimeOffset? failedAt = default;
Expand Down Expand Up @@ -254,6 +261,13 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
}
if (property.NameEquals("expires_at"u8))
{
if (property.Value.ValueKind == JsonValueKind.Null)
{
expiresAt = null;
continue;
}
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
// expiresAt = property.Value.GetDateTimeOffset("O");
expiresAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
continue;
}
Expand All @@ -264,7 +278,9 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
startedAt = null;
continue;
}
startedAt = property.Value.GetDateTimeOffset("O");
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
// startedAt = property.Value.GetDateTimeOffset("O");
startedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
continue;
}
if (property.NameEquals("cancelled_at"u8))
Expand All @@ -274,7 +290,9 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
cancelledAt = null;
continue;
}
cancelledAt = property.Value.GetDateTimeOffset("O");
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
// cancelledAt = property.Value.GetDateTimeOffset("O");
cancelledAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
continue;
}
if (property.NameEquals("failed_at"u8))
Expand All @@ -284,7 +302,9 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
failedAt = null;
continue;
}
failedAt = property.Value.GetDateTimeOffset("O");
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
// failedAt = property.Value.GetDateTimeOffset("O");
failedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
continue;
}
if (property.NameEquals("completed_at"u8))
Expand All @@ -294,7 +314,9 @@ internal static RunObject DeserializeRunObject(JsonElement element, ModelReaderW
completedAt = null;
continue;
}
completedAt = property.Value.GetDateTimeOffset("O");
// BUG: https://github.com/Azure/autorest.csharp/issues/4296
// completedAt = property.Value.GetDateTimeOffset("O");
completedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());
continue;
}
if (property.NameEquals("model"u8))
Expand Down
6 changes: 3 additions & 3 deletions .dotnet/src/Generated/Models/RunObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal partial class RunObject
/// </param>
/// <param name="usage"></param>
/// <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>
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)
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)
{
if (id is null) throw new ArgumentNullException(nameof(id));
if (threadId is null) throw new ArgumentNullException(nameof(threadId));
Expand Down Expand Up @@ -145,7 +145,7 @@ internal RunObject(string id, DateTimeOffset createdAt, string threadId, string
/// </param>
/// <param name="usage"></param>
/// <param name="serializedAdditionalRawData"> Keeps track of any properties unknown to the library. </param>
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)
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)
{
Id = id;
Object = @object;
Expand Down Expand Up @@ -201,7 +201,7 @@ internal RunObject()
/// <summary> The last error associated with this run. Will be `null` if there are no errors. </summary>
public RunObjectLastError LastError { get; }
/// <summary> The Unix timestamp (in seconds) for when the run will expire. </summary>
public DateTimeOffset ExpiresAt { get; }
public DateTimeOffset? ExpiresAt { get; }
/// <summary> The Unix timestamp (in seconds) for when the run was started. </summary>
public DateTimeOffset? StartedAt { get; }
/// <summary> The Unix timestamp (in seconds) for when the run was cancelled. </summary>
Expand Down
2 changes: 1 addition & 1 deletion .dotnet/src/Generated/OpenAIModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public static CreateThreadAndRunRequest CreateThreadAndRunRequest(string assista
/// </param>
/// <param name="usage"></param>
/// <returns> A new <see cref="Models.RunObject"/> instance for mocking. </returns>
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)
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)
{
tools ??= new List<BinaryData>();
fileIds ??= new List<string>();
Expand Down
Loading

0 comments on commit d01aefa

Please sign in to comment.