openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Custom/Chat/Streaming/StreamingChatCompletionUpdate.cs
122lines · modecode
| 1 | using Microsoft.TypeSpec.Generator.Customizations; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | |
| 6 | namespace OpenAI.Chat; |
| 7 | |
| 8 | /// <summary> An incremental update corresponding to a streaming chat completion generated by the model. </summary> |
| 9 | [CodeGenType("CreateChatCompletionStreamResponse")] |
| 10 | [CodeGenSuppress("StreamingChatCompletionUpdate", typeof(System.ClientModel.ClientResult))] |
| 11 | public partial class StreamingChatCompletionUpdate |
| 12 | { |
| 13 | private ChatMessageContent _contentUpdate; |
| 14 | private IReadOnlyList<StreamingChatToolCallUpdate> _toolCallUpdates; |
| 15 | private IReadOnlyList<ChatTokenLogProbabilityDetails> _contentTokenLogProbabilities; |
| 16 | private IReadOnlyList<ChatTokenLogProbabilityDetails> _refusalTokenLogProbabilities; |
| 17 | internal InternalCreateChatCompletionStreamResponseChoice InternalChoice => (Choices?.Count > 0) ? Choices[0] : null; |
| 18 | internal InternalChatCompletionStreamResponseDelta InternalChoiceDelta => InternalChoice?.Delta; |
| 19 | |
| 20 | // CUSTOM: |
| 21 | // - Made private. This property does not add value in the context of a strongly-typed class. |
| 22 | // - Changed type from string. |
| 23 | /// <summary> The object type, which is always `chat.completion.chunk`. </summary> |
| 24 | [CodeGenMember("Object")] |
| 25 | internal string Object { get; } = "chat.completion.chunk"; |
| 26 | |
| 27 | // CUSTOM: Renamed for clarity. |
| 28 | [CodeGenMember("Id")] |
| 29 | public string CompletionId { get; } |
| 30 | |
| 31 | // CUSTOM: Made internal.We only get back a single choice, and instead we flatten the structure for usability. |
| 32 | [CodeGenMember("Choices")] |
| 33 | internal IReadOnlyList<InternalCreateChatCompletionStreamResponseChoice> Choices { get; } |
| 34 | |
| 35 | // CUSTOM: Renamed. |
| 36 | /// <summary> The timestamp when the chat completion was created. </summary> |
| 37 | /// <remarks> Each update has the same timestamp. </remarks> |
| 38 | [CodeGenMember("Created")] |
| 39 | public DateTimeOffset CreatedAt { get; } |
| 40 | |
| 41 | // CUSTOM: Changed type from InternalCreateChatCompletionStreamResponseUsage. |
| 42 | /// <summary> The token usage statistics for the entire chat completion. </summary> |
| 43 | /// <remarks> Present only in the last streaming update. </remarks> |
| 44 | [CodeGenMember("Usage")] |
| 45 | public ChatTokenUsage Usage { get; } |
| 46 | |
| 47 | // CUSTOM: Flattened choice property. |
| 48 | /// <summary> |
| 49 | /// The reason the model stopped generating tokens. This will be: |
| 50 | /// <list> |
| 51 | /// <item> |
| 52 | /// <see cref="ChatFinishReason.Stop"/> if the model hit a natural stop point or a provided stop sequence |
| 53 | /// </item> |
| 54 | /// <item> |
| 55 | /// <see cref="ChatFinishReason.Length"/> if the maximum number of tokens specified in the request was reached |
| 56 | /// </item> |
| 57 | /// <item> |
| 58 | /// <see cref="ChatFinishReason.ContentFilter"/> if content was omitted due to a flag from our content filters |
| 59 | /// </item> |
| 60 | /// <item> |
| 61 | /// <see cref="ChatFinishReason.ToolCalls"/> if the model called a tool |
| 62 | /// </item> |
| 63 | /// <item> |
| 64 | /// <see cref="ChatFinishReason.FunctionCall"/> (obsolete) if the model called a function |
| 65 | /// </item> |
| 66 | /// </list> |
| 67 | /// </summary> |
| 68 | public ChatFinishReason? FinishReason => InternalChoice?.FinishReason; |
| 69 | |
| 70 | // CUSTOM: Flattened choice logprobs property. |
| 71 | /// <summary> The message content tokens with log probability information. </summary> |
| 72 | public IReadOnlyList<ChatTokenLogProbabilityDetails> ContentTokenLogProbabilities => |
| 73 | _contentTokenLogProbabilities |
| 74 | ??= InternalChoice?.Logprobs?.Content |
| 75 | ?? new ChangeTrackingList<ChatTokenLogProbabilityDetails>(); |
| 76 | |
| 77 | // CUSTOM: Flattened choice logprobs property. |
| 78 | /// <summary> The message refusal tokens with log probability information. </summary> |
| 79 | public IReadOnlyList<ChatTokenLogProbabilityDetails> RefusalTokenLogProbabilities => |
| 80 | _refusalTokenLogProbabilities |
| 81 | ??= InternalChoice?.Logprobs?.Refusal |
| 82 | ?? new ChangeTrackingList<ChatTokenLogProbabilityDetails>(); |
| 83 | |
| 84 | // CUSTOM: Flattened choice delta property. |
| 85 | /// <summary> The role of the author of this message. </summary> |
| 86 | /// <remarks> Typically present in a single streaming update but applicable to the entire chat completion. </remarks> |
| 87 | public ChatMessageRole? Role => InternalChoiceDelta?.Role; |
| 88 | |
| 89 | // CUSTOM: Flattened choice delta property. |
| 90 | /// <summary> The contents of the message update. </summary> |
| 91 | /// <remarks> |
| 92 | /// Each streaming update contains only a small portion of tokens. To reconstitute the entire chat completion, |
| 93 | /// all <see cref="ContentUpdate"/> values across streaming updates must be combined. |
| 94 | /// </remarks> |
| 95 | public ChatMessageContent ContentUpdate => _contentUpdate ??= InternalChoiceDelta?.Content ?? []; |
| 96 | |
| 97 | // CUSTOM: Flattened choice delta property. |
| 98 | /// <summary> The tool calls generated by the model, such as function calls. </summary> |
| 99 | public IReadOnlyList<StreamingChatToolCallUpdate> ToolCallUpdates |
| 100 | => _toolCallUpdates |
| 101 | ??= InternalChoiceDelta?.ToolCalls |
| 102 | ?? new ChangeTrackingList<StreamingChatToolCallUpdate>(); |
| 103 | |
| 104 | // CUSTOM: Flattened choice delta property. |
| 105 | /// <summary> The refusal message generated by the model. </summary> |
| 106 | public string RefusalUpdate => InternalChoiceDelta?.Refusal; |
| 107 | |
| 108 | // CUSTOM: Flattened choice delta property. |
| 109 | [Obsolete($"This property is obsolete. Please use {nameof(ToolCallUpdates)} instead.")] |
| 110 | public StreamingChatFunctionCallUpdate FunctionCallUpdate => InternalChoiceDelta?.FunctionCall; |
| 111 | |
| 112 | // CUSTOM: |
| 113 | // - Added Experimental attribute. |
| 114 | // - Flattened choice delta property. |
| 115 | /// <summary> |
| 116 | /// Incremental output audio generated by the model. Only expected when output audio has been requested via providing |
| 117 | /// <see cref="ChatAudioOptions"/> to <see cref="ChatCompletionOptions.AudioOptions"/> and only available with |
| 118 | /// supported models. |
| 119 | /// </summary> |
| 120 | [Experimental("OPENAI001")] |
| 121 | public StreamingChatOutputAudioUpdate OutputAudioUpdate => InternalChoiceDelta?.Audio; |
| 122 | } |
| 123 | |