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/ChatCompletion.cs

109lines · modecode

1using OpenAI.Internal;
2using System;
3using System.ClientModel;
4using System.ClientModel.Primitives;
5using System.Collections.Generic;
6using System.Diagnostics.CodeAnalysis;
7using System.Linq;
8using System.Text.Json;
9
10namespace OpenAI.Chat;
11
12/// <summary> A chat completion generated by the model. </summary>
13[CodeGenType("CreateChatCompletionResponse")]
14public partial class ChatCompletion
15{
16 private IReadOnlyList<ChatTokenLogProbabilityDetails> _contentTokenLogProbabilities;
17 private IReadOnlyList<ChatTokenLogProbabilityDetails> _refusalTokenLogProbabilities;
18
19 // CUSTOM: Made private. This property does not add value in the context of a strongly-typed class.
20 /// <summary> The object type, which is always `chat.completion`. </summary>
21 [CodeGenMember("Object")]
22 internal string Object { get; } = "chat.completion";
23
24 // CUSTOM: Made internal.
25 [CodeGenMember("ServiceTier")]
26 internal InternalServiceTier? ServiceTier { get; }
27
28 // CUSTOM: Made internal. We only get back a single choice, and instead we flatten the structure for usability.
29 [CodeGenMember("Choices")]
30 internal IReadOnlyList<InternalCreateChatCompletionResponseChoice> Choices { get; }
31
32 // CUSTOM: Renamed.
33 /// <summary> The timestamp when the chat completion was created. </summary>
34 [CodeGenMember("Created")]
35 public DateTimeOffset CreatedAt { get; }
36
37 // CUSTOM: Flattened choice property.
38 /// <summary>
39 /// The reason the model stopped generating tokens. This will be:
40 /// <list>
41 /// <item>
42 /// <see cref="ChatFinishReason.Stop"/> if the model hit a natural stop point or a provided stop sequence
43 /// </item>
44 /// <item>
45 /// <see cref="ChatFinishReason.Length"/> if the maximum number of tokens specified in the request was reached
46 /// </item>
47 /// <item>
48 /// <see cref="ChatFinishReason.ContentFilter"/> if content was omitted due to a flag from our content filters
49 /// </item>
50 /// <item>
51 /// <see cref="ChatFinishReason.ToolCalls"/> if the model called a tool
52 /// </item>
53 /// <item>
54 /// <see cref="ChatFinishReason.FunctionCall"/> (obsolte) if the model called a function
55 /// </item>
56 /// </list>
57 /// </summary>
58 public ChatFinishReason FinishReason => Choices[0].FinishReason;
59
60 // CUSTOM: Flattened choice logprobs property.
61 /// <summary> The message content tokens with log probability information. </summary>
62 public IReadOnlyList<ChatTokenLogProbabilityDetails> ContentTokenLogProbabilities =>
63 _contentTokenLogProbabilities
64 ??= Choices[0].Logprobs?.Content
65 ?? new ChangeTrackingList<ChatTokenLogProbabilityDetails>();
66
67 // CUSTOM: Flattened choice logprobs property.
68 /// <summary> The message refusal tokens with log probability information. </summary>
69 public IReadOnlyList<ChatTokenLogProbabilityDetails> RefusalTokenLogProbabilities =>
70 _refusalTokenLogProbabilities
71 ??= Choices[0].Logprobs?.Refusal
72 ?? new ChangeTrackingList<ChatTokenLogProbabilityDetails>();
73
74 // CUSTOM: Flattened choice message property.
75 /// <summary> The role of the author of this message. </summary>
76 public ChatMessageRole Role => Choices[0].Message.Role;
77
78 // CUSTOM: Flattened choice message property.
79 /// <summary> The contents of the message. </summary>
80 public ChatMessageContent Content => Choices[0].Message.Content;
81
82 // CUSTOM: Flattened choice message property.
83 /// <summary> The tool calls generated by the model, such as function calls. </summary>
84 public IReadOnlyList<ChatToolCall> ToolCalls => Choices[0].Message.ToolCalls;
85
86 // CUSTOM: Flattened choice message property.
87 /// <summary> The refusal message generated by the model. </summary>
88 public string Refusal => Choices[0].Message.Refusal;
89
90 // CUSTOM: Flattened choice message property.
91 [Obsolete($"This property is obsolete. Please use {nameof(ToolCalls)} instead.")]
92 public ChatFunctionCall FunctionCall => Choices[0].Message.FunctionCall;
93
94 // CUSTOM: Added Experimental attribute.
95 /// <summary> The audio response generated by the model. </summary>
96 [Experimental("OPENAI001")]
97 public ChatOutputAudio OutputAudio => Choices[0].Message.Audio;
98
99 // CUSTOM: Added Experimental attribute.
100 [Experimental("OPENAI001")]
101 public IReadOnlyList<ChatMessageAnnotation> Annotations => [.. Choices[0].Message.Annotations];
102
103 internal static ChatCompletion FromClientResult(ClientResult result)
104 {
105 using PipelineResponse response = result.GetRawResponse();
106 using JsonDocument document = JsonDocument.Parse(response.Content);
107 return DeserializeChatCompletion(document.RootElement, ModelSerializationExtensions.WireOptions);
108 }
109}
110