openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Chat/Streaming/StreamingChatCompletionUpdate.cs

115lines · modecode

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