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

Streaming Client Result investigation - assistants 2 #49

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 9 additions & 3 deletions .dotnet/scripts/Add-Customizations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ function Set-LangVersionToLatest {
$xml.Save($filePath)
}

function Edit-RunObjectSerialization {
function Edit-DateTimeOffsetSerialization {
param(
[string]$filename
)

$root = Split-Path $PSScriptRoot -Parent
$directory = Join-Path -Path $root -ChildPath "src\Generated\Models"

$file = Get-ChildItem -Path $directory -Filter "RunObject.Serialization.cs"
$file = Get-ChildItem -Path $directory -Filter $filename
$content = Get-Content -Path $file -Raw

Write-Output "Editing $($file.FullName)"
Expand All @@ -52,11 +56,13 @@ function Edit-RunObjectSerialization {
$content = $content -creplace "cancelledAt = property\.Value\.GetDateTimeOffset\(`"O`"\);", "// BUG: https://github.com/Azure/autorest.csharp/issues/4296`r`n // cancelledAt = property.Value.GetDateTimeOffset(`"O`");`r`n cancelledAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());"
$content = $content -creplace "failedAt = property\.Value\.GetDateTimeOffset\(`"O`"\);", "// BUG: https://github.com/Azure/autorest.csharp/issues/4296`r`n // failedAt = property.Value.GetDateTimeOffset(`"O`");`r`n failedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());"
$content = $content -creplace "completedAt = property\.Value\.GetDateTimeOffset\(`"O`"\);", "// BUG: https://github.com/Azure/autorest.csharp/issues/4296`r`n // completedAt = property.Value.GetDateTimeOffset(`"O`");`r`n completedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());"
$content = $content -creplace "incompleteAt = property\.Value\.GetDateTimeOffset\(`"O`"\);", "// BUG: https://github.com/Azure/autorest.csharp/issues/4296`r`n // completedAt = property.Value.GetDateTimeOffset(`"O`");`r`n completedAt = DateTimeOffset.FromUnixTimeSeconds(property.Value.GetInt64());"

$content | Set-Content -Path $file.FullName -NoNewline
}

Update-SystemTextJsonPackage
Update-MicrosoftBclAsyncInterfacesPackage
Set-LangVersionToLatest
Edit-RunObjectSerialization
Edit-DateTimeOffsetSerialization -filename "RunObject.Serialization.cs"
Edit-DateTimeOffsetSerialization -filename "MessageObject.Serialization.cs"
93 changes: 93 additions & 0 deletions .dotnet/src/Custom/Assistants/AssistantClient.Protocol.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.ComponentModel;
using System.Text;
using System.Threading.Tasks;

namespace OpenAI.Assistants;
Expand Down Expand Up @@ -336,6 +338,15 @@ public virtual ClientResult CreateRun(
RequestOptions options = null)
=> RunShim.CreateRun(threadId, content, options);

/// <inheritdoc cref="Internal.Runs.CreateRun(string, BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual ClientResult CreateRunStreaming(string threadId, BinaryContent content, RequestOptions options = null)
{
PipelineMessage message = CreateCreateRunRequest(threadId, content, stream: true, options);
RunShim.Pipeline.Send(message);
return ClientResult.FromResponse(message.ExtractResponse());
}

/// <inheritdoc cref="Internal.Runs.CreateRunAsync(string, BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual async Task<ClientResult> CreateRunAsync(
Expand All @@ -344,20 +355,47 @@ public virtual async Task<ClientResult> CreateRunAsync(
RequestOptions options = null)
=> await RunShim.CreateRunAsync(threadId, content, options).ConfigureAwait(false);

/// <inheritdoc cref="Internal.Runs.CreateRunAsync(string, BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual async Task<ClientResult> CreateRunStreamingAsync(string threadId, BinaryContent content, RequestOptions options = null)
{
PipelineMessage message = CreateCreateRunRequest(threadId, content, stream: true, options);
await RunShim.Pipeline.SendAsync(message);
return ClientResult.FromResponse(message.ExtractResponse());
}

/// <inheritdoc cref="Internal.Runs.CreateThreadAndRun(BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual ClientResult CreateThreadAndRun(
BinaryContent content,
RequestOptions options = null)
=> RunShim.CreateThreadAndRun(content, options);

/// <inheritdoc cref="Internal.Runs.CreateThreadAndRun(BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual ClientResult CreateThreadAndRunStreaming(BinaryContent content, RequestOptions options = null)
{
PipelineMessage message = CreateCreateThreadAndRunRequest(content, stream: true, options);
RunShim.Pipeline.Send(message);
return ClientResult.FromResponse(message.ExtractResponse());
}

/// <inheritdoc cref="Internal.Runs.CreateThreadAndRunAsync(BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual async Task<ClientResult> CreateThreadAndRunAsync(
BinaryContent content,
RequestOptions options = null)
=> await RunShim.CreateThreadAndRunAsync(content, options).ConfigureAwait(false);

/// <inheritdoc cref="Internal.Runs.CreateThreadAndRun(BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual async Task<ClientResult> CreateThreadAndRunStreamingAsync(BinaryContent content, RequestOptions options = null)
{
PipelineMessage message = CreateCreateThreadAndRunRequest(content, stream: true, options);
await RunShim.Pipeline.SendAsync(message);
return ClientResult.FromResponse(message.ExtractResponse());
}

/// <inheritdoc cref="Internal.Runs.GetRun(string, string, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual ClientResult GetRun(
Expand Down Expand Up @@ -439,6 +477,19 @@ public virtual ClientResult SubmitToolOutputs(
RequestOptions options = null)
=> RunShim.SubmitToolOuputsToRun(threadId, runId, content, options);

/// <inheritdoc cref="Internal.Runs.SubmitToolOuputsToRun(string, string, BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual ClientResult SubmitToolOutputsStreaming(
string threadId,
string runId,
BinaryContent content,
RequestOptions options = null)
{
PipelineMessage message = CreateSubmitToolOutputsRequest(threadId, runId, content, stream: true, options);
RunShim.Pipeline.Send(message);
return ClientResult.FromResponse(message.ExtractResponse());
}

/// <inheritdoc cref="Internal.Runs.SubmitToolOuputsToRunAsync(string, string, BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual async Task<ClientResult> SubmitToolOutputsAsync(
Expand All @@ -448,6 +499,19 @@ public virtual async Task<ClientResult> SubmitToolOutputsAsync(
RequestOptions options = null)
=> await RunShim.SubmitToolOuputsToRunAsync(threadId, runId, content, options).ConfigureAwait(false);

/// <inheritdoc cref="Internal.Runs.SubmitToolOuputsToRunAsync(string, string, BinaryContent, RequestOptions)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual async Task<ClientResult> SubmitToolOutputsStreamingAsync(
string threadId,
string runId,
BinaryContent content,
RequestOptions options = null)
{
PipelineMessage message = CreateSubmitToolOutputsRequest(threadId, runId, content, stream: true, options);
await RunShim.Pipeline.SendAsync(message);
return ClientResult.FromResponse(message.ExtractResponse());
}

/// <inheritdoc cref="Internal.Runs.GetRunStep(string, string, string, RequestOptions)"/>
public virtual ClientResult GetRunStep(
string threadId,
Expand Down Expand Up @@ -485,4 +549,33 @@ public virtual async Task<ClientResult> GetRunStepsAsync(
string subsequentStepId,
RequestOptions options)
=> await RunShim.GetRunStepsAsync(threadId, runId, maxResults, createdSortOrder, previousStepId, subsequentStepId, options).ConfigureAwait(false);

internal PipelineMessage CreateCreateRunRequest(string threadId, BinaryContent content, bool? stream = null, RequestOptions options = null)
=> CreateAssistantProtocolRequest($"/threads/{threadId}/runs", content, stream, options);

internal PipelineMessage CreateCreateThreadAndRunRequest(BinaryContent content, bool? stream = null, RequestOptions options = null)
=> CreateAssistantProtocolRequest($"/threads/runs", content, stream, options);

internal PipelineMessage CreateSubmitToolOutputsRequest(string threadId, string runId, BinaryContent content, bool? stream = null, RequestOptions options = null)
=> CreateAssistantProtocolRequest($"/threads/{threadId}/runs/{runId}/submit_tool_outputs", content, stream, options);

internal PipelineMessage CreateAssistantProtocolRequest(string path, BinaryContent content, bool? stream = null, RequestOptions options = null)
{
PipelineMessage message = Shim.Pipeline.CreateMessage();
message.ResponseClassifier = ResponseErrorClassifier200;
if (stream == true)
{
message.BufferResponse = false;
}
PipelineRequest request = message.Request;
request.Method = "POST";
UriBuilder uriBuilder = new(_clientConnector.Endpoint.AbsoluteUri);
uriBuilder.Path += path;
request.Uri = uriBuilder.Uri;
request.Headers.Set("Content-Type", "application/json");
request.Headers.Set("Accept", stream == true ? "text/event-stream" : "application/json");
request.Content = content;
message.Apply(options ?? new());
return message;
}
}
Loading