openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Chat/ChatCompletion.cs

94lines · modecode

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