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