-
Notifications
You must be signed in to change notification settings - Fork 725
.NET: reformat event sequence in OpenAI Responses hosting #1542
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |||||
| using Microsoft.AspNetCore.Http.Features; | ||||||
| using Microsoft.Extensions.AI; | ||||||
| using OpenAI.Responses; | ||||||
| using ChatMessage = Microsoft.Extensions.AI.ChatMessage; | ||||||
|
|
||||||
| namespace Microsoft.Agents.AI.Hosting.OpenAI.Responses; | ||||||
|
|
||||||
|
|
@@ -101,6 +102,7 @@ private async IAsyncEnumerable<SseItem<StreamingResponseEventBase>> GetStreaming | |||||
| { | ||||||
| var sequenceNumber = 1; | ||||||
| var outputIndex = 1; | ||||||
| Dictionary<string, int> messageIdOutputIndexes = new(); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
| AgentThread? agentThread = null; | ||||||
|
|
||||||
| ResponseItem? lastResponseItem = null; | ||||||
|
|
@@ -115,6 +117,8 @@ private async IAsyncEnumerable<SseItem<StreamingResponseEventBase>> GetStreaming | |||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| // response.created | ||||||
| // response.in_progress | ||||||
| if (sequenceNumber == 1) | ||||||
| { | ||||||
| lastOpenAIResponse = update.AsChatResponse().AsOpenAIResponse(); | ||||||
|
|
@@ -124,6 +128,12 @@ private async IAsyncEnumerable<SseItem<StreamingResponseEventBase>> GetStreaming | |||||
| Response = lastOpenAIResponse | ||||||
| }; | ||||||
| yield return new(responseCreated, responseCreated.Type); | ||||||
|
|
||||||
| var inProgressResponse = new StreamingInProgressResponse(sequenceNumber++) | ||||||
| { | ||||||
| Response = lastOpenAIResponse | ||||||
| }; | ||||||
| yield return new(inProgressResponse, inProgressResponse.Type); | ||||||
| } | ||||||
|
|
||||||
| if (update.Contents is not { Count: > 0 }) | ||||||
|
|
@@ -139,6 +149,7 @@ private async IAsyncEnumerable<SseItem<StreamingResponseEventBase>> GetStreaming | |||||
| CreatedAt = update.CreatedAt, | ||||||
| RawRepresentation = update.RawRepresentation | ||||||
| }; | ||||||
| var messageId = chatMessage.MessageId ?? "<null>"; | ||||||
DeagleGross marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this using |
||||||
|
|
||||||
| foreach (var openAIResponseItem in MicrosoftExtensionsAIResponsesExtensions.AsOpenAIResponseItems([chatMessage])) | ||||||
| { | ||||||
|
|
@@ -147,21 +158,64 @@ private async IAsyncEnumerable<SseItem<StreamingResponseEventBase>> GetStreaming | |||||
| openAIResponseItem.SetId(chatMessage.MessageId); | ||||||
| } | ||||||
|
|
||||||
| lastResponseItem = openAIResponseItem; | ||||||
| if (!messageIdOutputIndexes.TryGetValue(messageId, out var index)) | ||||||
| { | ||||||
| messageIdOutputIndexes[messageId] = index = 0; | ||||||
| var responseContentPartAdded = new StreamingContentPartAddedResponse(sequenceNumber++) | ||||||
| { | ||||||
| ItemId = chatMessage.MessageId, | ||||||
| ContentIndex = 0, | ||||||
| OutputIndex = index++, | ||||||
| Part = null! | ||||||
| }; | ||||||
| yield return new(responseContentPartAdded, responseContentPartAdded.Type); | ||||||
|
|
||||||
| var responseOutputItemAddedResponse = new StreamingOutputItemAddedResponse(sequenceNumber++) | ||||||
| { | ||||||
| OutputIndex = index++, | ||||||
| Item = openAIResponseItem | ||||||
| }; | ||||||
| yield return new(responseOutputItemAddedResponse, responseOutputItemAddedResponse.Type); | ||||||
| } | ||||||
|
|
||||||
| var responseOutputItemAdded = new StreamingOutputItemAddedResponse(sequenceNumber++) | ||||||
| lastResponseItem = openAIResponseItem; | ||||||
| var responseOutputTextDeltaResponse = new StreamingOutputTextDeltaResponse(sequenceNumber++) | ||||||
| { | ||||||
| OutputIndex = outputIndex++, | ||||||
| Item = openAIResponseItem | ||||||
| ItemId = chatMessage.MessageId, | ||||||
| ContentIndex = 0, | ||||||
| OutputIndex = index++, | ||||||
| Delta = chatMessage.Text | ||||||
| }; | ||||||
| yield return new(responseOutputItemAdded, responseOutputItemAdded.Type); | ||||||
| yield return new(responseOutputTextDeltaResponse, responseOutputTextDeltaResponse.Type); | ||||||
|
|
||||||
| messageIdOutputIndexes[messageId] = index; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (lastResponseItem is not null) | ||||||
| { | ||||||
| // we were streaming "response.output_item.added" before | ||||||
| // so we should complete it now via "response.output_item.done" | ||||||
| // here goes a sequence of completions for earlier started events: | ||||||
|
|
||||||
| // "response.output_text.delta" should be completed with "response.output_text.done" | ||||||
| var index = messageIdOutputIndexes[lastResponseItem.Id ?? "<null>"]; | ||||||
| var responseOutputTextDeltaResponse = new StreamingOutputTextDoneResponse(sequenceNumber++) | ||||||
| { | ||||||
| ItemId = lastResponseItem.Id, | ||||||
| ContentIndex = 0, | ||||||
| OutputIndex = index++, | ||||||
| }; | ||||||
| yield return new(responseOutputTextDeltaResponse, responseOutputTextDeltaResponse.Type); | ||||||
|
|
||||||
| // then "response.content_part.added" should be completed with "response.content_part.done" | ||||||
| var streamingContentPartDoneResponse = new StreamingContentPartDoneResponse(sequenceNumber++) | ||||||
| { | ||||||
| ItemId = lastResponseItem.Id, | ||||||
| ContentIndex = 0, | ||||||
| OutputIndex = index++, | ||||||
| }; | ||||||
| yield return new(streamingContentPartDoneResponse, streamingContentPartDoneResponse.Type); | ||||||
|
|
||||||
| // then "response.output_item.added" should be completed with "response.output_item.done" | ||||||
DeagleGross marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| var responseOutputDoneAdded = new StreamingOutputItemDoneResponse(sequenceNumber++) | ||||||
| { | ||||||
| OutputIndex = outputIndex++, | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary?