openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/ChatCompletion.cs
109lines · modeblame
5dce104aJose Arriaga Maldonado1 years ago | 1 | using OpenAI.Internal; |
9f9f2936Jose Arriaga Maldonado2 years ago | 2 | using System; |
5dce104aJose Arriaga Maldonado1 years ago | 3 | using System.ClientModel; |
| 4 | using System.ClientModel.Primitives; | |
9f9f2936Jose Arriaga Maldonado2 years ago | 5 | using System.Collections.Generic; |
5dce104aJose Arriaga Maldonado1 years ago | 6 | using System.Diagnostics.CodeAnalysis; |
0ca4c062Jose Arriaga Maldonado1 years ago | 7 | using System.Linq; |
5dce104aJose Arriaga Maldonado1 years ago | 8 | using System.Text.Json; |
9f9f2936Jose Arriaga Maldonado2 years ago | 9 | |
| 10 | namespace OpenAI.Chat; | |
| 11 | | |
31c2ba63Jose Arriaga Maldonado1 years ago | 12 | /// <summary> A chat completion generated by the model. </summary> |
0ca4c062Jose Arriaga Maldonado1 years ago | 13 | [CodeGenType("CreateChatCompletionResponse")] |
9f9f2936Jose Arriaga Maldonado2 years ago | 14 | public partial class ChatCompletion |
| 15 | { | |
2ab1a942Jose Arriaga Maldonado1 years ago | 16 | private IReadOnlyList<ChatTokenLogProbabilityDetails> _contentTokenLogProbabilities; |
| 17 | private IReadOnlyList<ChatTokenLogProbabilityDetails> _refusalTokenLogProbabilities; | |
9f9f2936Jose Arriaga Maldonado2 years ago | 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")] | |
5dce104aJose Arriaga Maldonado1 years ago | 22 | internal string Object { get; } = "chat.completion"; |
31c2ba63Jose Arriaga Maldonado1 years ago | 23 | |
| 24 | // CUSTOM: Made internal. | |
| 25 | [CodeGenMember("ServiceTier")] | |
5dce104aJose Arriaga Maldonado1 years ago | 26 | internal InternalServiceTier? ServiceTier { get; } |
9f9f2936Jose Arriaga Maldonado2 years ago | 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. | |
31c2ba63Jose Arriaga Maldonado1 years ago | 33 | /// <summary> The timestamp when the chat completion was created. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 34 | [CodeGenMember("Created")] |
| 35 | public DateTimeOffset CreatedAt { get; } | |
| 36 | | |
| 37 | // CUSTOM: Flattened choice property. | |
| 38 | /// <summary> | |
31c2ba63Jose Arriaga Maldonado1 years ago | 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> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 57 | /// </summary> |
| 58 | public ChatFinishReason FinishReason => Choices[0].FinishReason; | |
| 59 | | |
| 60 | // CUSTOM: Flattened choice logprobs property. | |
31c2ba63Jose Arriaga Maldonado1 years ago | 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>(); | |
9f9f2936Jose Arriaga Maldonado2 years ago | 66 | |
31c2ba63Jose Arriaga Maldonado1 years ago | 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>(); | |
3467b535Travis Wilson1 years ago | 73 | |
9f9f2936Jose Arriaga Maldonado2 years ago | 74 | // CUSTOM: Flattened choice message property. |
31c2ba63Jose Arriaga Maldonado1 years ago | 75 | /// <summary> The role of the author of this message. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 76 | public ChatMessageRole Role => Choices[0].Message.Role; |
| 77 | | |
| 78 | // CUSTOM: Flattened choice message property. | |
31c2ba63Jose Arriaga Maldonado1 years ago | 79 | /// <summary> The contents of the message. </summary> |
| 80 | public ChatMessageContent Content => Choices[0].Message.Content; | |
9f9f2936Jose Arriaga Maldonado2 years ago | 81 | |
| 82 | // CUSTOM: Flattened choice message property. | |
31c2ba63Jose Arriaga Maldonado1 years ago | 83 | /// <summary> The tool calls generated by the model, such as function calls. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 84 | public IReadOnlyList<ChatToolCall> ToolCalls => Choices[0].Message.ToolCalls; |
| 85 | | |
| 86 | // CUSTOM: Flattened choice message property. | |
31c2ba63Jose Arriaga Maldonado1 years ago | 87 | /// <summary> The refusal message generated by the model. </summary> |
3467b535Travis Wilson1 years ago | 88 | public string Refusal => Choices[0].Message.Refusal; |
| 89 | | |
31c2ba63Jose Arriaga Maldonado1 years ago | 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; | |
0e0c460cJose Arriaga Maldonado1 years ago | 93 | |
5dce104aJose Arriaga Maldonado1 years ago | 94 | // CUSTOM: Added Experimental attribute. |
0e0c460cJose Arriaga Maldonado1 years ago | 95 | /// <summary> The audio response generated by the model. </summary> |
5dce104aJose Arriaga Maldonado1 years ago | 96 | [Experimental("OPENAI001")] |
0e0c460cJose Arriaga Maldonado1 years ago | 97 | public ChatOutputAudio OutputAudio => Choices[0].Message.Audio; |
0ca4c062Jose Arriaga Maldonado1 years ago | 98 | |
5dce104aJose Arriaga Maldonado1 years ago | 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 | } | |
9f9f2936Jose Arriaga Maldonado2 years ago | 109 | } |