openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/ChatCompletionOptions.cs
221lines · modecode
| 1 | using OpenAI.Internal; |
| 2 | using System; |
| 3 | using System.ClientModel; |
| 4 | using System.ClientModel.Primitives; |
| 5 | using System.Collections.Generic; |
| 6 | using System.Diagnostics.CodeAnalysis; |
| 7 | using System.Threading; |
| 8 | |
| 9 | namespace OpenAI.Chat; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Request-level options for chat completion. |
| 13 | /// </summary> |
| 14 | [CodeGenType("CreateChatCompletionRequest")] |
| 15 | [CodeGenVisibility(nameof(ChatCompletionOptions), CodeGenVisibility.Public)] |
| 16 | [CodeGenSuppress(nameof(ChatCompletionOptions), typeof(IEnumerable<ChatMessage>), typeof(string))] |
| 17 | [CodeGenSerialization(nameof(Messages), SerializationValueHook = nameof(SerializeMessagesValue))] |
| 18 | [CodeGenSerialization(nameof(StopSequences), SerializationValueHook = nameof(SerializeStopSequencesValue), DeserializationValueHook = nameof(DeserializeStopSequencesValue))] |
| 19 | [CodeGenSerialization(nameof(LogitBiases), SerializationValueHook = nameof(SerializeLogitBiasesValue), DeserializationValueHook = nameof(DeserializeLogitBiasesValue))] |
| 20 | public partial class ChatCompletionOptions |
| 21 | { |
| 22 | // CUSTOM: |
| 23 | // - Made internal. This value comes from a parameter on the client method. |
| 24 | // - Added setter. |
| 25 | /// <summary> |
| 26 | /// A list of messages comprising the conversation so far. [Example Python code](https://cookbook.openai.com/examples/how_to_format_inputs_to_chatgpt_models). |
| 27 | /// Please note <see cref="ChatMessage"/> is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. |
| 28 | /// The available derived classes include <see cref="AssistantChatMessage"/>, <see cref="FunctionChatMessage"/>, <see cref="SystemChatMessage"/>, <see cref="ToolChatMessage"/> and <see cref="UserChatMessage"/>. |
| 29 | /// </summary> |
| 30 | [CodeGenMember("Messages")] |
| 31 | internal IList<ChatMessage> Messages { get; set; } |
| 32 | |
| 33 | // CUSTOM: |
| 34 | // - Made internal. This value comes from a parameter on the client method. |
| 35 | // - Added setter. |
| 36 | /// <summary> |
| 37 | /// ID of the model to use. See the <see href="https://platform.openai.com/docs/models/model-endpoint-compatibility">model endpoint compatibility</see> table for details on which models work with the Chat API. |
| 38 | /// </summary> |
| 39 | [CodeGenMember("Model")] |
| 40 | internal string Model { get; set; } |
| 41 | |
| 42 | // CUSTOM: Made internal. We only ever request a single choice. |
| 43 | /// <summary> How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep `n` as `1` to minimize costs. </summary> |
| 44 | [CodeGenMember("N")] |
| 45 | internal int? N { get; set; } |
| 46 | |
| 47 | // CUSTOM: Made internal. We set this manually based on the client method that is called. |
| 48 | /// <summary> If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions). </summary> |
| 49 | [CodeGenMember("Stream")] |
| 50 | internal bool? Stream { get; set; } |
| 51 | |
| 52 | /// <summary> Gets or sets the stream options. </summary> |
| 53 | [CodeGenMember("StreamOptions")] |
| 54 | internal InternalChatCompletionStreamOptions StreamOptions { get; set; } |
| 55 | |
| 56 | // CUSTOM: Renamed. |
| 57 | /// <summary> Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the message content. </summary> |
| 58 | [CodeGenMember("Logprobs")] |
| 59 | public bool? IncludeLogProbabilities { get; set; } |
| 60 | |
| 61 | // CUSTOM: Renamed. |
| 62 | /// <summary> An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability. <see cref="IncludeLogProbabilities"/> must be set to <see langword="true"/> if this property is used. </summary> |
| 63 | [CodeGenMember("TopLogprobs")] |
| 64 | public int? TopLogProbabilityCount { get; set; } |
| 65 | |
| 66 | // CUSTOM: |
| 67 | // - Renamed. |
| 68 | // - Changed type to treat a single string as a collection of strings with one item. |
| 69 | /// <summary> Up to 4 sequences where the API will stop generating further tokens. </summary> |
| 70 | [CodeGenMember("Stop")] |
| 71 | public IList<string> StopSequences { get; } |
| 72 | |
| 73 | // CUSTOM: |
| 74 | // - Renamed. |
| 75 | // - Changed type to treat tokens as integers instead of strings. |
| 76 | /// <summary> |
| 77 | /// Modifies the likelihood of specified tokens appearing in the completion. It maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. |
| 78 | /// </summary> |
| 79 | [CodeGenMember("LogitBias")] |
| 80 | public IDictionary<int, int> LogitBiases { get; } |
| 81 | |
| 82 | // CUSTOM: Changed type to avoid BinaryData. |
| 83 | /// <summary> |
| 84 | /// Specifies which tool is called by the model, if any. <see cref="ChatToolChoice.None"/> means the model will not call any tool and instead generates a message. <see cref="ChatToolChoice.Auto"/> means the model can pick between generating a message or calling one or more tools. |
| 85 | /// <see cref="ChatToolChoice.Required"/> means the model must call one or more tools. The model can also be forced to call a specific tool by constructing a new instance of <see cref="ChatToolChoice"/> while passing the desired <see cref="ChatTool"/> as a constructor parameter. |
| 86 | /// <remarks> |
| 87 | /// <see cref="ChatToolChoice.None"/> is the default behavior when no tools are present, while <see cref="ChatToolChoice.Auto"/> is the default if tools are present. |
| 88 | /// </remarks> |
| 89 | /// </summary> |
| 90 | [CodeGenMember("ToolChoice")] |
| 91 | public ChatToolChoice ToolChoice { get; set; } |
| 92 | |
| 93 | // CUSTOM: |
| 94 | // - Renamed. |
| 95 | // - Changed type to avoid BinaryData. |
| 96 | [Obsolete($"This property is obsolete. Please use {nameof(ToolChoice)} instead.")] |
| 97 | [CodeGenMember("FunctionCall")] |
| 98 | public ChatFunctionChoice FunctionChoice { get; set; } |
| 99 | |
| 100 | // CUSTOM: Renamed. |
| 101 | /// <summary> |
| 102 | /// Whether to enable parallel function calling during tool use. |
| 103 | /// </summary> |
| 104 | /// <remarks> |
| 105 | /// Assumed <c>true</c> if not otherwise specified. |
| 106 | /// </remarks> |
| 107 | [CodeGenMember("ParallelToolCalls")] |
| 108 | public bool? AllowParallelToolCalls { get; set; } |
| 109 | |
| 110 | [CodeGenMember("ServiceTier")] |
| 111 | internal InternalServiceTier? _serviceTier; |
| 112 | |
| 113 | // CUSTOM: Renamed. |
| 114 | /// <summary> |
| 115 | /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. |
| 116 | /// <see href="https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids">Learn more</see>. |
| 117 | /// </summary> |
| 118 | [CodeGenMember("User")] |
| 119 | public string EndUserId { get; set; } |
| 120 | |
| 121 | |
| 122 | // CUSTOM: Hide deprecated max_tokens and reroute to newer max_completion_tokens. |
| 123 | [CodeGenMember("MaxTokens")] |
| 124 | internal int? _deprecatedMaxTokens { get; set; } |
| 125 | |
| 126 | // CUSTOM: Add legacy `max_tokens` routing. |
| 127 | /// <summary> |
| 128 | /// An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and, on applicable models, reasoning tokens. |
| 129 | /// </summary> |
| 130 | [CodeGenMember("MaxCompletionTokens")] |
| 131 | public int? MaxOutputTokenCount { get; set; } |
| 132 | |
| 133 | // CUSTOM: Added the Obsolete attribute. |
| 134 | [Obsolete($"This property is obsolete. Please use {nameof(Tools)} instead.")] |
| 135 | public IList<ChatFunction> Functions { get; } |
| 136 | |
| 137 | // CUSTOM: Removed public setter. |
| 138 | /// <summary> |
| 139 | /// Developer-defined tags and values used for filtering completions in the |
| 140 | /// <see href="https://platform.openai.com/chat-completions">OpenAI Platform dashboard</see>. |
| 141 | /// </summary> |
| 142 | [CodeGenMember("Metadata")] |
| 143 | public IDictionary<string, string> Metadata { get; } |
| 144 | |
| 145 | // CUSTOM: Renamed. |
| 146 | /// <summary> |
| 147 | /// Indicates whether to store the output of this chat completion request for use in |
| 148 | /// <see href="https://platform.openai.com/docs/guides/distillation">model distillation</see> |
| 149 | /// or <see href="https://platform.openai.com/docs/guides/evals">evals</see>. |
| 150 | /// </summary> |
| 151 | [CodeGenMember("Store")] |
| 152 | public bool? StoredOutputEnabled { get; set; } |
| 153 | |
| 154 | // CUSTOM: |
| 155 | // - Added Experimental attribute. |
| 156 | // - Renamed. |
| 157 | /// <summary> |
| 158 | /// (o1 and newer reasoning models only) Constrains effort on reasoning for reasoning models. |
| 159 | /// Currently supported values are <see cref="ChatReasoningEffortLevel.Low"/>, <see cref="ChatReasoningEffortLevel.Medium"/>, and <see cref="ChatReasoningEffortLevel.High"/>. |
| 160 | /// Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response. |
| 161 | /// </summary> |
| 162 | [Experimental("OPENAI001")] |
| 163 | [CodeGenMember("ReasoningEffort")] |
| 164 | public ChatReasoningEffortLevel? ReasoningEffortLevel { get; set; } |
| 165 | |
| 166 | // CUSTOM: Made internal for automatic enablement via audio options. |
| 167 | [CodeGenMember("Modalities")] |
| 168 | private IList<InternalCreateChatCompletionRequestModality> InternalModalities |
| 169 | { |
| 170 | get => _internalModalities; |
| 171 | set |
| 172 | { |
| 173 | _internalModalities = value; |
| 174 | _responseModalities = ChatResponseModalitiesExtensions.FromInternalModalities(value); |
| 175 | } |
| 176 | } |
| 177 | private IList<InternalCreateChatCompletionRequestModality> _internalModalities; |
| 178 | |
| 179 | // CUSTOM: |
| 180 | // - Added Experimental attribute. |
| 181 | // - Renamed. |
| 182 | /// <summary> |
| 183 | /// Specifies the content types that the model should generate in its responses. |
| 184 | /// </summary> |
| 185 | /// <remarks> |
| 186 | /// Most models can generate text and the default <c>["text"]</c> value, from <c><see cref="ChatResponseModalities.Text"/></c>, requests this. |
| 187 | /// Some models like <c>gpt-4o-audio-preview</c> can also generate audio, and this can be requested by combining <c>["text","audio"]</c> via |
| 188 | /// the flags <c><see cref="ChatResponseModalities.Text"/> | <see cref="ChatResponseModalities.Audio"/></c>. |
| 189 | /// </remarks> |
| 190 | [Experimental("OPENAI001")] |
| 191 | public ChatResponseModalities ResponseModalities |
| 192 | { |
| 193 | get => _responseModalities; |
| 194 | set |
| 195 | { |
| 196 | _responseModalities = value; |
| 197 | _internalModalities = value.ToInternalModalities(); |
| 198 | } |
| 199 | } |
| 200 | private ChatResponseModalities _responseModalities; |
| 201 | |
| 202 | // CUSTOM: Use scenario-specific type. |
| 203 | [CodeGenMember("ResponseFormat")] |
| 204 | public ChatResponseFormat ResponseFormat { get; set; } |
| 205 | |
| 206 | // CUSTOM: |
| 207 | // - Added Experimental attribute. |
| 208 | // - Renamed. |
| 209 | [Experimental("OPENAI001")] |
| 210 | [CodeGenMember("Audio")] |
| 211 | public ChatAudioOptions AudioOptions { get; set; } |
| 212 | |
| 213 | // CUSTOM: |
| 214 | // - Added Experimental attribute. |
| 215 | // - Renamed. |
| 216 | [Experimental("OPENAI001")] |
| 217 | [CodeGenMember("Prediction")] |
| 218 | public ChatOutputPrediction OutputPrediction { get; set; } |
| 219 | |
| 220 | internal BinaryContent ToBinaryContent() => BinaryContent.Create(this, ModelSerializationExtensions.WireOptions); |
| 221 | } |
| 222 | |