openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Chat/Streaming/StreamingChatCompletionUpdate.cs

125lines · modecode

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