openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/ChatCompletionOptions.cs
218lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | using System.Threading; |
| 6 | |
| 7 | namespace OpenAI.Chat; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Request-level options for chat completion. |
| 11 | /// </summary> |
| 12 | [CodeGenType("CreateChatCompletionRequest")] |
| 13 | [CodeGenSuppress("ChatCompletionOptions", typeof(IEnumerable<ChatMessage>), typeof(InternalCreateChatCompletionRequestModel?))] |
| 14 | [CodeGenSerialization(nameof(Messages), SerializationValueHook = nameof(SerializeMessagesValue))] |
| 15 | [CodeGenSerialization(nameof(StopSequences), SerializationValueHook = nameof(SerializeStopSequencesValue), DeserializationValueHook = nameof(DeserializeStopSequencesValue))] |
| 16 | [CodeGenSerialization(nameof(LogitBiases), SerializationValueHook = nameof(SerializeLogitBiasesValue), DeserializationValueHook = nameof(DeserializeLogitBiasesValue))] |
| 17 | public partial class ChatCompletionOptions |
| 18 | { |
| 19 | // CUSTOM: |
| 20 | // - Made internal. This value comes from a parameter on the client method. |
| 21 | // - Added setter. |
| 22 | /// <summary> |
| 23 | /// A list of messages comprising the conversation so far. [Example Python code](https://cookbook.openai.com/examples/how_to_format_inputs_to_chatgpt_models). |
| 24 | /// 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. |
| 25 | /// The available derived classes include <see cref="AssistantChatMessage"/>, <see cref="FunctionChatMessage"/>, <see cref="SystemChatMessage"/>, <see cref="ToolChatMessage"/> and <see cref="UserChatMessage"/>. |
| 26 | /// </summary> |
| 27 | [CodeGenMember("Messages")] |
| 28 | internal IList<ChatMessage> Messages { get; set; } |
| 29 | |
| 30 | // CUSTOM: |
| 31 | // - Made internal. This value comes from a parameter on the client method. |
| 32 | // - Added setter. |
| 33 | /// <summary> |
| 34 | /// 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. |
| 35 | /// </summary> |
| 36 | [CodeGenMember("Model")] |
| 37 | internal InternalCreateChatCompletionRequestModel? Model { get; set; } |
| 38 | |
| 39 | // CUSTOM: Made internal. We only ever request a single choice. |
| 40 | /// <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> |
| 41 | [CodeGenMember("N")] |
| 42 | internal int? N { get; set; } |
| 43 | |
| 44 | // CUSTOM: Made internal. We set this manually based on the client method that is called. |
| 45 | /// <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> |
| 46 | [CodeGenMember("Stream")] |
| 47 | internal bool? Stream { get; set; } |
| 48 | |
| 49 | /// <summary> Gets or sets the stream options. </summary> |
| 50 | [CodeGenMember("StreamOptions")] |
| 51 | internal InternalChatCompletionStreamOptions StreamOptions { get; set; } |
| 52 | |
| 53 | // CUSTOM: Made public now that there are no required properties. |
| 54 | /// <summary> Initializes a new instance of <see cref="ChatCompletionOptions"/> for deserialization. </summary> |
| 55 | public ChatCompletionOptions() |
| 56 | { |
| 57 | Messages = new ChangeTrackingList<ChatMessage>(); |
| 58 | LogitBiases = new ChangeTrackingDictionary<int, int>(); |
| 59 | StopSequences = new ChangeTrackingList<string>(); |
| 60 | Tools = new ChangeTrackingList<ChatTool>(); |
| 61 | Functions = new ChangeTrackingList<ChatFunction>(); |
| 62 | InternalModalities = new ChangeTrackingList<InternalCreateChatCompletionRequestModality>(); |
| 63 | Metadata = new ChangeTrackingDictionary<string, string>(); |
| 64 | } |
| 65 | |
| 66 | // CUSTOM: Renamed. |
| 67 | /// <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> |
| 68 | [CodeGenMember("Logprobs")] |
| 69 | public bool? IncludeLogProbabilities { get; set; } |
| 70 | |
| 71 | // CUSTOM: Renamed. |
| 72 | /// <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> |
| 73 | [CodeGenMember("TopLogprobs")] |
| 74 | public int? TopLogProbabilityCount { get; set; } |
| 75 | |
| 76 | // CUSTOM: |
| 77 | // - Renamed. |
| 78 | // - Changed type to treat a single string as a collection of strings with one item. |
| 79 | /// <summary> Up to 4 sequences where the API will stop generating further tokens. </summary> |
| 80 | [CodeGenMember("Stop")] |
| 81 | public IList<string> StopSequences { get; } |
| 82 | |
| 83 | // CUSTOM: |
| 84 | // - Renamed. |
| 85 | // - Changed type to treat tokens as integers instead of strings. |
| 86 | /// <summary> |
| 87 | /// 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. |
| 88 | /// </summary> |
| 89 | [CodeGenMember("LogitBias")] |
| 90 | public IDictionary<int, int> LogitBiases { get; } |
| 91 | |
| 92 | // CUSTOM: Changed type to avoid BinaryData. |
| 93 | /// <summary> |
| 94 | /// 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. |
| 95 | /// <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. |
| 96 | /// <remarks> |
| 97 | /// <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. |
| 98 | /// </remarks> |
| 99 | /// </summary> |
| 100 | [CodeGenMember("ToolChoice")] |
| 101 | public ChatToolChoice ToolChoice { get; set; } |
| 102 | |
| 103 | // CUSTOM: |
| 104 | // - Renamed. |
| 105 | // - Changed type to avoid BinaryData. |
| 106 | [Obsolete($"This property is obsolete. Please use {nameof(ToolChoice)} instead.")] |
| 107 | [CodeGenMember("FunctionCall")] |
| 108 | public ChatFunctionChoice FunctionChoice { get; set; } |
| 109 | |
| 110 | // CUSTOM: Renamed. |
| 111 | /// <summary> |
| 112 | /// Whether to enable parallel function calling during tool use. |
| 113 | /// </summary> |
| 114 | /// <remarks> |
| 115 | /// Assumed <c>true</c> if not otherwise specified. |
| 116 | /// </remarks> |
| 117 | [CodeGenMember("ParallelToolCalls")] |
| 118 | public bool? AllowParallelToolCalls { get; set; } |
| 119 | |
| 120 | [CodeGenMember("ServiceTier")] |
| 121 | internal InternalCreateChatCompletionRequestServiceTier? _serviceTier; |
| 122 | |
| 123 | // CUSTOM: Renamed. |
| 124 | /// <summary> |
| 125 | /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. |
| 126 | /// <see href="https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids">Learn more</see>. |
| 127 | /// </summary> |
| 128 | [CodeGenMember("User")] |
| 129 | public string EndUserId { get; set; } |
| 130 | |
| 131 | // CUSTOM: Added the Experimental attribute. |
| 132 | /// <summary> |
| 133 | /// If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. |
| 134 | /// </summary> |
| 135 | [Experimental("OPENAI001")] |
| 136 | public long? Seed { get; set; } |
| 137 | |
| 138 | // CUSTOM: Hide deprecated max_tokens and reroute to newer max_completion_tokens. |
| 139 | [CodeGenMember("MaxTokens")] |
| 140 | internal int? _deprecatedMaxTokens { get; set; } |
| 141 | |
| 142 | // CUSTOM: Add legacy `max_tokens` routing. |
| 143 | /// <summary> |
| 144 | /// 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. |
| 145 | /// </summary> |
| 146 | [CodeGenMember("MaxCompletionTokens")] |
| 147 | public int? MaxOutputTokenCount { get; set; } |
| 148 | |
| 149 | // CUSTOM: Added the Obsolete attribute. |
| 150 | [Obsolete($"This property is obsolete. Please use {nameof(Tools)} instead.")] |
| 151 | public IList<ChatFunction> Functions { get; } |
| 152 | |
| 153 | // CUSTOM: Removed public setter. |
| 154 | /// <summary> |
| 155 | /// Developer-defined tags and values used for filtering completions in the |
| 156 | /// <see href="https://platform.openai.com/chat-completions">OpenAI Platform dashboard</see>. |
| 157 | /// </summary> |
| 158 | [CodeGenMember("Metadata")] |
| 159 | public IDictionary<string, string> Metadata { get; } |
| 160 | |
| 161 | // CUSTOM: Renamed. |
| 162 | /// <summary> |
| 163 | /// Indicates whether to store the output of this chat completion request for use in |
| 164 | /// <see href="https://platform.openai.com/docs/guides/distillation">model distillation</see> |
| 165 | /// or <see href="https://platform.openai.com/docs/guides/evals">evals</see>. |
| 166 | /// </summary> |
| 167 | [CodeGenMember("Store")] |
| 168 | public bool? StoredOutputEnabled { get; set; } |
| 169 | |
| 170 | // CUSTOM: Renamed. |
| 171 | /// <summary> |
| 172 | /// (o1 and newer reasoning models only) Constrains effort on reasoning for reasoning models. |
| 173 | /// Currently supported values are <see cref="ChatReasoningEffortLevel.Low"/>, <see cref="ChatReasoningEffortLevel.Medium"/>, and <see cref="ChatReasoningEffortLevel.High"/>. |
| 174 | /// Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response. |
| 175 | /// </summary> |
| 176 | [CodeGenMember("ReasoningEffort")] |
| 177 | public ChatReasoningEffortLevel? ReasoningEffortLevel { get; set; } |
| 178 | |
| 179 | // CUSTOM: Made internal for automatic enablement via audio options. |
| 180 | [CodeGenMember("Modalities")] |
| 181 | private IList<InternalCreateChatCompletionRequestModality> InternalModalities |
| 182 | { |
| 183 | get => _internalModalities; |
| 184 | set |
| 185 | { |
| 186 | _internalModalities = value; |
| 187 | _responseModalities = ChatResponseModalitiesExtensions.FromInternalModalities(value); |
| 188 | } |
| 189 | } |
| 190 | private IList<InternalCreateChatCompletionRequestModality> _internalModalities; |
| 191 | |
| 192 | /// <summary> |
| 193 | /// Specifies the content types that the model should generate in its responses. |
| 194 | /// </summary> |
| 195 | /// <remarks> |
| 196 | /// Most models can generate text and the default <c>["text"]</c> value, from <c><see cref="ChatResponseModalities.Text"/></c>, requests this. |
| 197 | /// 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 |
| 198 | /// the flags <c><see cref="ChatResponseModalities.Text"/> | <see cref="ChatResponseModalities.Audio"/></c>. |
| 199 | /// </remarks> |
| 200 | public ChatResponseModalities ResponseModalities |
| 201 | { |
| 202 | get => _responseModalities; |
| 203 | set |
| 204 | { |
| 205 | _responseModalities = value; |
| 206 | _internalModalities = value.ToInternalModalities(); |
| 207 | } |
| 208 | } |
| 209 | private ChatResponseModalities _responseModalities; |
| 210 | |
| 211 | // CUSTOM: Renamed. |
| 212 | [CodeGenMember("Audio")] |
| 213 | public ChatAudioOptions AudioOptions { get; set; } |
| 214 | |
| 215 | // CUSTOM: rename. |
| 216 | [CodeGenMember("Prediction")] |
| 217 | public ChatOutputPrediction OutputPrediction { get; set; } |
| 218 | } |
| 219 | |