openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Chat/Streaming/StreamingChatCompletionUpdate.cs

115lines · modeblame

9f9f2936Jose Arriaga Maldonado2 years ago1using System;
2using System.Collections.Generic;
3
31c2ba63Jose Arriaga Maldonado1 years ago4namespace OpenAI.Chat;
5
6/// <summary> An incremental update corresponding to a streaming chat completion generated by the model. </summary>
9f9f2936Jose Arriaga Maldonado2 years ago7[CodeGenModel("CreateChatCompletionStreamResponse")]
8public partial class StreamingChatCompletionUpdate
9{
31c2ba63Jose Arriaga Maldonado1 years ago10private ChatMessageContent _contentUpdate;
9f9f2936Jose Arriaga Maldonado2 years ago11private IReadOnlyList<StreamingChatToolCallUpdate> _toolCallUpdates;
2ab1a942Jose Arriaga Maldonado1 years ago12private IReadOnlyList<ChatTokenLogProbabilityDetails> _contentTokenLogProbabilities;
13private IReadOnlyList<ChatTokenLogProbabilityDetails> _refusalTokenLogProbabilities;
31c2ba63Jose Arriaga Maldonado1 years ago14internal InternalCreateChatCompletionStreamResponseChoice InternalChoice => (Choices.Count > 0) ? Choices[0] : null;
15internal InternalChatCompletionStreamResponseDelta InternalChoiceDelta => InternalChoice?.Delta;
9f9f2936Jose Arriaga Maldonado2 years ago16
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")]
22internal InternalCreateChatCompletionStreamResponseObject Object { get; } = InternalCreateChatCompletionStreamResponseObject.ChatCompletionChunk;
23
31c2ba63Jose Arriaga Maldonado1 years ago24// CUSTOM: Renamed for clarity.
25[CodeGenMember("Id")]
26public string CompletionId { get; }
27
28// CUSTOM: Made internal.
29[CodeGenMember("ServiceTier")]
30internal InternalCreateChatCompletionStreamResponseServiceTier? ServiceTier { get; }
31
9f9f2936Jose Arriaga Maldonado2 years ago32// CUSTOM: Made internal.We only get back a single choice, and instead we flatten the structure for usability.
79014abcShivangiReja1 years ago33[CodeGenMember("Choices")]
9f9f2936Jose Arriaga Maldonado2 years ago34internal IReadOnlyList<InternalCreateChatCompletionStreamResponseChoice> Choices { get; }
35
36// CUSTOM: Renamed.
31c2ba63Jose Arriaga Maldonado1 years ago37/// <summary> The timestamp when the chat completion was created. </summary>
38/// <remarks> Each update has the same timestamp. </remarks>
9f9f2936Jose Arriaga Maldonado2 years ago39[CodeGenMember("Created")]
40public DateTimeOffset CreatedAt { get; }
41
42// CUSTOM: Changed type from InternalCreateChatCompletionStreamResponseUsage.
31c2ba63Jose Arriaga Maldonado1 years ago43/// <summary> The token usage statistics for the entire chat completion. </summary>
44/// <remarks> Present only in the last streaming update. </remarks>
9f9f2936Jose Arriaga Maldonado2 years ago45[CodeGenMember("Usage")]
46public ChatTokenUsage Usage { get; }
47
48// CUSTOM: Flattened choice property.
49/// <summary>
31c2ba63Jose Arriaga Maldonado1 years ago50/// 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>
9f9f2936Jose Arriaga Maldonado2 years ago68/// </summary>
2ab1a942Jose Arriaga Maldonado1 years ago69public ChatFinishReason? FinishReason => InternalChoice?.FinishReason;
9f9f2936Jose Arriaga Maldonado2 years ago70
71// CUSTOM: Flattened choice logprobs property.
31c2ba63Jose Arriaga Maldonado1 years ago72/// <summary> The message content tokens with log probability information. </summary>
73public IReadOnlyList<ChatTokenLogProbabilityDetails> ContentTokenLogProbabilities =>
74_contentTokenLogProbabilities
2ab1a942Jose Arriaga Maldonado1 years ago75??= InternalChoice?.Logprobs?.Content
76?? new ChangeTrackingList<ChatTokenLogProbabilityDetails>();
9f9f2936Jose Arriaga Maldonado2 years ago77
31c2ba63Jose Arriaga Maldonado1 years ago78// CUSTOM: Flattened choice logprobs property.
79/// <summary> The message refusal tokens with log probability information. </summary>
80public IReadOnlyList<ChatTokenLogProbabilityDetails> RefusalTokenLogProbabilities =>
81_refusalTokenLogProbabilities
2ab1a942Jose Arriaga Maldonado1 years ago82??= InternalChoice?.Logprobs?.Refusal
83?? new ChangeTrackingList<ChatTokenLogProbabilityDetails>();
3467b535Travis Wilson1 years ago84
9f9f2936Jose Arriaga Maldonado2 years ago85// CUSTOM: Flattened choice delta property.
31c2ba63Jose Arriaga Maldonado1 years ago86/// <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>
88public ChatMessageRole? Role => InternalChoiceDelta?.Role;
9f9f2936Jose Arriaga Maldonado2 years ago89
90// CUSTOM: Flattened choice delta property.
31c2ba63Jose Arriaga Maldonado1 years ago91/// <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.
9f9f2936Jose Arriaga Maldonado2 years ago95/// </remarks>
31c2ba63Jose Arriaga Maldonado1 years ago96public ChatMessageContent ContentUpdate =>
97_contentUpdate
98??= InternalChoiceDelta?.Content
99?? new ChatMessageContent();
9f9f2936Jose Arriaga Maldonado2 years ago100
101// CUSTOM: Flattened choice delta property.
31c2ba63Jose Arriaga Maldonado1 years ago102/// <summary> The tool calls generated by the model, such as function calls. </summary>
2ab1a942Jose Arriaga Maldonado1 years ago103public IReadOnlyList<StreamingChatToolCallUpdate> ToolCallUpdates
104=> _toolCallUpdates
31c2ba63Jose Arriaga Maldonado1 years ago105??= InternalChoiceDelta?.ToolCalls
2ab1a942Jose Arriaga Maldonado1 years ago106?? new ChangeTrackingList<StreamingChatToolCallUpdate>();
9f9f2936Jose Arriaga Maldonado2 years ago107
108// CUSTOM: Flattened choice delta property.
31c2ba63Jose Arriaga Maldonado1 years ago109/// <summary> The refusal message generated by the model. </summary>
110public string RefusalUpdate => InternalChoiceDelta?.Refusal;
79014abcShivangiReja1 years ago111
3467b535Travis Wilson1 years ago112// CUSTOM: Flattened choice delta property.
31c2ba63Jose Arriaga Maldonado1 years ago113[Obsolete($"This property is obsolete. Please use {nameof(ToolCallUpdates)} instead.")]
114public StreamingChatFunctionCallUpdate FunctionCallUpdate => InternalChoiceDelta?.FunctionCall;
9f9f2936Jose Arriaga Maldonado2 years ago115}