using Microsoft.TypeSpec.Generator.Customizations; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace OpenAI.Chat; /// An incremental update corresponding to a streaming chat completion generated by the model. [CodeGenType("CreateChatCompletionStreamResponse")] [CodeGenSuppress("StreamingChatCompletionUpdate", typeof(System.ClientModel.ClientResult))] public partial class StreamingChatCompletionUpdate { private ChatMessageContent _contentUpdate; private IReadOnlyList _toolCallUpdates; private IReadOnlyList _contentTokenLogProbabilities; private IReadOnlyList _refusalTokenLogProbabilities; internal InternalCreateChatCompletionStreamResponseChoice InternalChoice => (Choices?.Count > 0) ? Choices[0] : null; internal InternalChatCompletionStreamResponseDelta InternalChoiceDelta => InternalChoice?.Delta; // CUSTOM: // - Made private. This property does not add value in the context of a strongly-typed class. // - Changed type from string. /// The object type, which is always `chat.completion.chunk`. [CodeGenMember("Object")] internal string Object { get; } = "chat.completion.chunk"; // CUSTOM: Renamed for clarity. [CodeGenMember("Id")] public string CompletionId { get; } // CUSTOM: Made internal.We only get back a single choice, and instead we flatten the structure for usability. [CodeGenMember("Choices")] internal IReadOnlyList Choices { get; } // CUSTOM: Renamed. /// The timestamp when the chat completion was created. /// Each update has the same timestamp. [CodeGenMember("Created")] public DateTimeOffset CreatedAt { get; } // CUSTOM: Changed type from InternalCreateChatCompletionStreamResponseUsage. /// The token usage statistics for the entire chat completion. /// Present only in the last streaming update. [CodeGenMember("Usage")] public ChatTokenUsage Usage { get; } // CUSTOM: Flattened choice property. /// /// The reason the model stopped generating tokens. This will be: /// /// /// if the model hit a natural stop point or a provided stop sequence /// /// /// if the maximum number of tokens specified in the request was reached /// /// /// if content was omitted due to a flag from our content filters /// /// /// if the model called a tool /// /// /// (obsolete) if the model called a function /// /// /// public ChatFinishReason? FinishReason => InternalChoice?.FinishReason; // CUSTOM: Flattened choice logprobs property. /// The message content tokens with log probability information. public IReadOnlyList ContentTokenLogProbabilities => _contentTokenLogProbabilities ??= InternalChoice?.Logprobs?.Content ?? new ChangeTrackingList(); // CUSTOM: Flattened choice logprobs property. /// The message refusal tokens with log probability information. public IReadOnlyList RefusalTokenLogProbabilities => _refusalTokenLogProbabilities ??= InternalChoice?.Logprobs?.Refusal ?? new ChangeTrackingList(); // CUSTOM: Flattened choice delta property. /// The role of the author of this message. /// Typically present in a single streaming update but applicable to the entire chat completion. public ChatMessageRole? Role => InternalChoiceDelta?.Role; // CUSTOM: Flattened choice delta property. /// The contents of the message update. /// /// Each streaming update contains only a small portion of tokens. To reconstitute the entire chat completion, /// all values across streaming updates must be combined. /// public ChatMessageContent ContentUpdate => _contentUpdate ??= InternalChoiceDelta?.Content ?? []; // CUSTOM: Flattened choice delta property. /// The tool calls generated by the model, such as function calls. public IReadOnlyList ToolCallUpdates => _toolCallUpdates ??= InternalChoiceDelta?.ToolCalls ?? new ChangeTrackingList(); // CUSTOM: Flattened choice delta property. /// The refusal message generated by the model. public string RefusalUpdate => InternalChoiceDelta?.Refusal; // CUSTOM: Flattened choice delta property. [Obsolete($"This property is obsolete. Please use {nameof(ToolCallUpdates)} instead.")] public StreamingChatFunctionCallUpdate FunctionCallUpdate => InternalChoiceDelta?.FunctionCall; // CUSTOM: // - Added Experimental attribute. // - Flattened choice delta property. /// /// Incremental output audio generated by the model. Only expected when output audio has been requested via providing /// to and only available with /// supported models. /// [Experimental("OPENAI001")] public StreamingChatOutputAudioUpdate OutputAudioUpdate => InternalChoiceDelta?.Audio; }