diff --git a/README.md b/README.md index eb9f10a..e683e1b 100644 --- a/README.md +++ b/README.md @@ -355,13 +355,15 @@ playAudio(from: audioObjectData) ### Chat Parameters ```swift -/// [Create chat completion](https://platform.openai.com/docs/api-reference/chat/create) public struct ChatCompletionParameters: Encodable { /// A list of messages comprising the conversation so far. [Example Python code](https://cookbook.openai.com/examples/how_to_format_inputs_to_chatgpt_models) public var messages: [Message] /// ID of the model to use. See the [model endpoint compatibility](https://platform.openai.com/docs/models/how-we-use-your-data) table for details on which models work with the Chat API. - let model: String + public var model: String + /// Whether or not to store the output of this chat completion request for use in our [model distillation](https://platform.openai.com/docs/guides/distillation) or [evals](https://platform.openai.com/docs/guides/evals) products. + /// Defaults to false + public var store: Bool? /// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. Defaults to 0 /// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/gpt/parameter-details) public var frequencyPenalty: Double? @@ -377,6 +379,8 @@ public struct ChatCompletionParameters: Encodable { public var functions: [ChatFunction]? /// A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. public var tools: [Tool]? + /// Whether to enable parallel function calling during tool use. Defaults to true. + public var parallelToolCalls: Bool? /// Modify the likelihood of specified tokens appearing in the completion. /// Accepts a json object that 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. Defaults to null. public var logitBias: [Int: Double]? @@ -384,11 +388,20 @@ public struct ChatCompletionParameters: Encodable { public var logprobs: Bool? /// An integer between 0 and 5 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used. public var topLogprobs: Int? - /// The maximum number of [tokens](https://platform.openai.com/tokenizer) to generate in the chat completion. - /// The total length of input tokens and generated tokens is limited by the model's context length. Example [Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. + /// The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the chat completion. This value can be used to control [costs](https://openai.com/api/pricing/) for text generated via API. + /// This value is now deprecated in favor of max_completion_tokens, and is not compatible with [o1 series models](https://platform.openai.com/docs/guides/reasoning) public var maxTokens: Int? + /// An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and [reasoning tokens](https://platform.openai.com/docs/guides/reasoning) + public var maCompletionTokens: Int? /// How many chat completion choices to generate for each input message. Defaults to 1. public var n: Int? + /// Output types that you would like the model to generate for this request. Most models are capable of generating text, which is the default: + /// ["text"] + ///The gpt-4o-audio-preview model can also be used to [generate audio](https://platform.openai.com/docs/guides/audio). To request that this model generate both text and audio responses, you can use: + /// ["text", "audio"] + public var modalities: [String]? + /// Parameters for audio output. Required when audio output is requested with modalities: ["audio"]. [Learn more.](https://platform.openai.com/docs/guides/audio) + public var audio: Audio? /// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. Defaults to 0 /// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/gpt/parameter-details) public var presencePenalty: Double? @@ -396,6 +409,11 @@ public struct ChatCompletionParameters: Encodable { /// Setting to `{ type: "json_object" }` enables `JSON` mode, which guarantees the message the model generates is valid JSON. ///Important: when using `JSON` mode you must still instruct the model to produce `JSON` yourself via some conversation message, for example via your system message. If you don't do this, the model may generate an unending stream of whitespace until the generation reaches the token limit, which may take a lot of time and give the appearance of a "stuck" request. Also note that the message content may be partial (i.e. cut off) if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. public var responseFormat: ResponseFormat? + /// Specifies the latency tier to use for processing the request. This parameter is relevant for customers subscribed to the scale tier service: + /// If set to 'auto', the system will utilize scale tier credits until they are exhausted. + /// If set to 'default', the request will be processed in the shared cluster. + /// When this parameter is set, the response body will include the service_tier utilized. + public var serviceTier: String? /// This feature is in `Beta`. 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. /// Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. public var seed: Int? @@ -404,6 +422,8 @@ public struct ChatCompletionParameters: Encodable { /// 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 ). /// Defaults to false. var stream: Bool? = nil + /// Options for streaming response. Only set this when you set stream: true + var streamOptions: StreamOptions? /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. /// We generally recommend altering this or `top_p` but not both. Defaults to 1. public var temperature: Double? @@ -412,18 +432,7 @@ public struct ChatCompletionParameters: Encodable { public var topP: Double? /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. /// [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids). - let user: String? - - public enum Model: String { - case gpt35Turbo = "gpt-3.5-turbo" - case gpt35Turbo1106 = "gpt-3.5-turbo-1106" // Most updated - Supports parallel function calls - case gpt4 = "gpt-4" - case gpt41106Preview = "gpt-4-1106-preview" // Most updated - Supports parallel function calls - case gpt35Turbo0613 = "gpt-3.5-turbo-0613" // To be deprecated "2024-06-13" - case gpt35Turbo16k0613 = "gpt-3.5-turbo-16k-0613" // To be deprecated "2024-06-13" - - case gpt4VisionPreview = "gpt-4-vision-preview" // Vision - } + public var user: String? public struct Message: Encodable { @@ -455,6 +464,7 @@ public struct ChatCompletionParameters: Encodable { try container.encode(contentArray) } } + public enum MessageContent: Encodable, Equatable, Hashable { case text(String) @@ -519,7 +529,7 @@ public struct ChatCompletionParameters: Encodable { return false } } - } + } } public enum Role: String { @@ -529,6 +539,15 @@ public struct ChatCompletionParameters: Encodable { case tool // content, role, tool_call_id } + enum CodingKeys: String, CodingKey { + case role + case content + case name + case functionCall = "function_call" + case toolCalls = "tool_calls" + case toolCallID = "tool_call_id" + } + public init( role: Role, content: ContentType, @@ -573,39 +592,7 @@ public struct ChatCompletionParameters: Encodable { } } - /// string `none` means the model will not call a function and instead generates a message. - /// `auto` means the model can pick between generating a message or calling a function. - /// `object` Specifies a tool the model should use. Use to force the model to call a specific function. The type of the tool. Currently, only` function` is supported. `{"type: "function", "function": {"name": "my_function"}}` - public enum ToolChoice: Encodable, Equatable { - case none - case auto - case function(type: String?, name: String) - - enum CodingKeys: String, CodingKey { - case none = "none" - case auto = "auto" - case name = "name" - case type = "type" - } - - public func encode(to encoder: Encoder) throws { - switch self { - case .none: - var container = encoder.singleValueContainer() - try container.encode(CodingKeys.none.rawValue) - case .auto: - var container = encoder.singleValueContainer() - try container.encode(CodingKeys.auto.rawValue) - case .function(let type, let name): - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(name, forKey: .name) - if let type { - try container.encode(type, forKey: .type) - } - } - } - } - + /// [Documentation](https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools) public struct Tool: Encodable { /// The type of the tool. Currently, only `function` is supported. @@ -622,211 +609,119 @@ public struct ChatCompletionParameters: Encodable { } } - public struct ChatFunction: Encodable, Equatable { + public struct ChatFunction: Codable, Equatable { /// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. let name: String /// A description of what the function does, used by the model to choose when and how to call the function. let description: String? /// The parameters the functions accepts, described as a JSON Schema object. See the [guide](https://platform.openai.com/docs/guides/gpt/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema) for documentation about the format. - /// To describe a function that accepts no parameters, provide the value `{"type": "object", "properties": {}}`. - let parameters: JSONSchema - - public struct JSONSchema: Encodable, Equatable { - - public let type: JSONType - public let properties: [String: Property]? - public let required: [String]? - public let pattern: String? - public let const: String? - public let enumValues: [String]? - public let multipleOf: Int? - public let minimum: Int? - public let maximum: Int? - - private enum CodingKeys: String, CodingKey { - case type, properties, required, pattern, const - case enumValues = "enum" - case multipleOf, minimum, maximum - } - - public struct Property: Encodable, Equatable { - - public let type: JSONType - public let description: String? - public let format: String? - public let items: Items? - public let required: [String]? - public let pattern: String? - public let const: String? - public let enumValues: [String]? - public let multipleOf: Int? - public let minimum: Double? - public let maximum: Double? - public let minItems: Int? - public let maxItems: Int? - public let uniqueItems: Bool? - - private enum CodingKeys: String, CodingKey { - case type, description, format, items, required, pattern, const - case enumValues = "enum" - case multipleOf, minimum, maximum - case minItems, maxItems, uniqueItems - } - - public init( - type: JSONType, - description: String? = nil, - format: String? = nil, - items: Items? = nil, - required: [String]? = nil, - pattern: String? = nil, - const: String? = nil, - enumValues: [String]? = nil, - multipleOf: Int? = nil, - minimum: Double? = nil, - maximum: Double? = nil, - minItems: Int? = nil, - maxItems: Int? = nil, - uniqueItems: Bool? = nil) - { - self.type = type - self.description = description - self.format = format - self.items = items - self.required = required - self.pattern = pattern - self.const = const - self.enumValues = enumValues - self.multipleOf = multipleOf - self.minimum = minimum - self.maximum = maximum - self.minItems = minItems - self.maxItems = maxItems - self.uniqueItems = uniqueItems - } - } - - public enum JSONType: String, Encodable { - case integer = "integer" - case string = "string" - case boolean = "boolean" - case array = "array" - case object = "object" - case number = "number" - case `null` = "null" - } - - public struct Items: Encodable, Equatable { - - public let type: JSONType - public let properties: [String: Property]? - public let pattern: String? - public let const: String? - public let enumValues: [String]? - public let multipleOf: Int? - public let minimum: Double? - public let maximum: Double? - public let minItems: Int? - public let maxItems: Int? - public let uniqueItems: Bool? - - private enum CodingKeys: String, CodingKey { - case type, properties, pattern, const - case enumValues = "enum" - case multipleOf, minimum, maximum, minItems, maxItems, uniqueItems - } - - public init( - type: JSONType, - properties: [String : Property]? = nil, - pattern: String? = nil, - const: String? = nil, - enumValues: [String]? = nil, - multipleOf: Int? = nil, - minimum: Double? = nil, - maximum: Double? = nil, - minItems: Int? = nil, - maxItems: Int? = nil, - uniqueItems: Bool? = nil) - { - self.type = type - self.properties = properties - self.pattern = pattern - self.const = const - self.enumValues = enumValues - self.multipleOf = multipleOf - self.minimum = minimum - self.maximum = maximum - self.minItems = minItems - self.maxItems = maxItems - self.uniqueItems = uniqueItems - } - } - - public init( - type: JSONType, - properties: [String : Property]? = nil, - required: [String]? = nil, - pattern: String? = nil, - const: String? = nil, - enumValues: [String]? = nil, - multipleOf: Int? = nil, - minimum: Int? = nil, - maximum: Int? = nil) - { - self.type = type - self.properties = properties - self.required = required - self.pattern = pattern - self.const = const - self.enumValues = enumValues - self.multipleOf = multipleOf - self.minimum = minimum - self.maximum = maximum - } - } + /// Omitting parameters defines a function with an empty parameter list. + let parameters: JSONSchema? + /// Defaults to false, Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the parameters field. Only a subset of JSON Schema is supported when strict is true. Learn more about Structured Outputs in the [function calling guide].(https://platform.openai.com/docs/api-reference/chat/docs/guides/function-calling) + let strict: Bool? public init( name: String, + strict: Bool?, description: String?, - parameters: JSONSchema) + parameters: JSONSchema?) { self.name = name + self.strict = strict self.description = description self.parameters = parameters } } - public struct ResponseFormat: Encodable { - - /// Defaults to text - /// Setting to `json_object` enables JSON mode. This guarantees that the message the model generates is valid JSON. - /// Note that your system prompt must still instruct the model to produce JSON, and to help ensure you don't forget, the API will throw an error if the string JSON does not appear in your system message. - /// Also note that the message content may be partial (i.e. cut off) if `finish_reason="length"`, which indicates the generation exceeded max_tokens or the conversation exceeded the max context length. - /// Must be one of `text `or `json_object`. - public var type: String? + public enum ServiceTier: String, Encodable { + /// Specifies the latency tier to use for processing the request. This parameter is relevant for customers subscribed to the scale tier service: + /// If set to 'auto', the system will utilize scale tier credits until they are exhausted. + /// If set to 'default', the request will be processed in the shared cluster. + /// When this parameter is set, the response body will include the service_tier utilized. + case auto + case `default` + } + + public struct StreamOptions: Encodable { + /// If set, an additional chunk will be streamed before the data: [DONE] message. + /// The usage field on this chunk shows the token usage statistics for the entire request, + /// and the choices field will always be an empty array. All other chunks will also include + /// a usage field, but with a null value. + let includeUsage: Bool + + enum CodingKeys: String, CodingKey { + case includeUsage = "include_usage" + } + } + + /// Parameters for audio output. Required when audio output is requested with modalities: ["audio"] + /// [Learn more.](https://platform.openai.com/docs/guides/audio) + public struct Audio: Encodable { + /// Specifies the voice type. Supported voices are alloy, echo, fable, onyx, nova, and shimmer. + public let voice: String + /// Specifies the output audio format. Must be one of wav, mp3, flac, opus, or pcm16. + public let format: String public init( - type: String?) + voice: String, + format: String) { - self.type = type + self.voice = voice + self.format = format } } + + enum CodingKeys: String, CodingKey { + case messages + case model + case store + case frequencyPenalty = "frequency_penalty" + case toolChoice = "tool_choice" + case functionCall = "function_call" + case tools + case parallelToolCalls = "parallel_tool_calls" + case functions + case logitBias = "logit_bias" + case logprobs + case topLogprobs = "top_logprobs" + case maxTokens = "max_tokens" + case maCompletionTokens = "max_completion_tokens" + case n + case modalities + case audio + case responseFormat = "response_format" + case presencePenalty = "presence_penalty" + case seed + case serviceTier = "service_tier" + case stop + case stream + case streamOptions = "stream_options" + case temperature + case topP = "top_p" + case user + } public init( messages: [Message], model: Model, + store: Bool? = nil, frequencyPenalty: Double? = nil, functionCall: FunctionCall? = nil, toolChoice: ToolChoice? = nil, functions: [ChatFunction]? = nil, tools: [Tool]? = nil, + parallelToolCalls: Bool? = nil, logitBias: [Int: Double]? = nil, + logProbs: Bool? = nil, + topLogprobs: Int? = nil, maxTokens: Int? = nil, n: Int? = nil, + modalities: [String]? = nil, + audio: Audio? = nil, responseFormat: ResponseFormat? = nil, presencePenalty: Double? = nil, + serviceTier: ServiceTier? = nil, seed: Int? = nil, stop: [String]? = nil, temperature: Double? = nil, @@ -835,16 +730,23 @@ public struct ChatCompletionParameters: Encodable { { self.messages = messages self.model = model.value + self.store = store self.frequencyPenalty = frequencyPenalty self.functionCall = functionCall self.toolChoice = toolChoice self.functions = functions self.tools = tools + self.parallelToolCalls = parallelToolCalls self.logitBias = logitBias + self.logprobs = logProbs + self.topLogprobs = topLogprobs self.maxTokens = maxTokens self.n = n + self.modalities = modalities + self.audio = audio self.responseFormat = responseFormat self.presencePenalty = presencePenalty + self.serviceTier = serviceTier?.rawValue self.seed = seed self.stop = stop self.temperature = temperature @@ -868,6 +770,8 @@ public struct ChatCompletionObject: Decodable { public let created: Int /// The model used for the chat completion. public let model: String + /// The service tier used for processing the request. This field is only included if the service_tier parameter is specified in the request. + public let serviceTier: String? /// This fingerprint represents the backend configuration that the model runs with. /// Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism. public let systemFingerprint: String? @@ -900,11 +804,33 @@ public struct ChatCompletionObject: Decodable { public let role: String /// Provided by the Vision API. public let finishDetails: FinishDetails? + /// The refusal message generated by the model. + public let refusal: String? + /// If the audio output modality is requested, this object contains data about the audio response from the model. [Learn more](https://platform.openai.com/docs/guides/audio). + public let audio: Audio? /// Provided by the Vision API. public struct FinishDetails: Decodable { let type: String } + + public struct Audio: Decodable { + /// Unique identifier for this audio response. + public let id: String + /// The Unix timestamp (in seconds) for when this audio response will no longer be accessible on the server for use in multi-turn conversations. + public let expiresAt: Int + /// Base64 encoded audio bytes generated by the model, in the format specified in the request. + public let data: String + /// Transcript of the audio generated by the model. + public let transcript: String + + enum CodingKeys: String, CodingKey { + case id + case expiresAt = "expires_at" + case data + case transcript + } + } } public struct LogProb: Decodable { @@ -971,6 +897,8 @@ public struct ChatCompletionChunkObject: Decodable { public let created: Int /// The model to generate the completion. public let model: String + /// The service tier used for processing the request. This field is only included if the service_tier parameter is specified in the request. + public let serviceTier: String? /// This fingerprint represents the backend configuration that the model runs with. /// Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism. public let systemFingerprint: String? diff --git a/Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift b/Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift index 57e6094..081058e 100644 --- a/Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift +++ b/Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift @@ -15,6 +15,9 @@ public struct ChatCompletionParameters: Encodable { public var messages: [Message] /// ID of the model to use. See the [model endpoint compatibility](https://platform.openai.com/docs/models/how-we-use-your-data) table for details on which models work with the Chat API. public var model: String + /// Whether or not to store the output of this chat completion request for use in our [model distillation](https://platform.openai.com/docs/guides/distillation) or [evals](https://platform.openai.com/docs/guides/evals) products. + /// Defaults to false + public var store: Bool? /// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. Defaults to 0 /// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/gpt/parameter-details) public var frequencyPenalty: Double? @@ -39,11 +42,20 @@ public struct ChatCompletionParameters: Encodable { public var logprobs: Bool? /// An integer between 0 and 5 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used. public var topLogprobs: Int? - /// The maximum number of [tokens](https://platform.openai.com/tokenizer) to generate in the chat completion. - /// The total length of input tokens and generated tokens is limited by the model's context length. Example [Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. + /// The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the chat completion. This value can be used to control [costs](https://openai.com/api/pricing/) for text generated via API. + /// This value is now deprecated in favor of max_completion_tokens, and is not compatible with [o1 series models](https://platform.openai.com/docs/guides/reasoning) public var maxTokens: Int? + /// An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and [reasoning tokens](https://platform.openai.com/docs/guides/reasoning) + public var maCompletionTokens: Int? /// How many chat completion choices to generate for each input message. Defaults to 1. public var n: Int? + /// Output types that you would like the model to generate for this request. Most models are capable of generating text, which is the default: + /// ["text"] + ///The gpt-4o-audio-preview model can also be used to [generate audio](https://platform.openai.com/docs/guides/audio). To request that this model generate both text and audio responses, you can use: + /// ["text", "audio"] + public var modalities: [String]? + /// Parameters for audio output. Required when audio output is requested with modalities: ["audio"]. [Learn more.](https://platform.openai.com/docs/guides/audio) + public var audio: Audio? /// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. Defaults to 0 /// [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/gpt/parameter-details) public var presencePenalty: Double? @@ -111,9 +123,9 @@ public struct ChatCompletionParameters: Encodable { case text(String) case imageUrl(ImageDetail) + case inputAudio(AudioDetail) public struct ImageDetail: Encodable, Equatable, Hashable { - public let url: URL public let detail: String? @@ -134,10 +146,32 @@ public struct ChatCompletionParameters: Encodable { } } + public struct AudioDetail: Encodable, Equatable, Hashable { + public let data: String + public let format: String + + enum CodingKeys: String, CodingKey { + case data + case format + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(data, forKey: .data) + try container.encode(format, forKey: .format) + } + + public init(data: String, format: String) { + self.data = data + self.format = format + } + } + enum CodingKeys: String, CodingKey { case type case text case imageUrl = "image_url" + case inputAudio = "input_audio" } public func encode(to encoder: Encoder) throws { @@ -149,6 +183,9 @@ public struct ChatCompletionParameters: Encodable { case .imageUrl(let imageDetail): try container.encode("image_url", forKey: .type) try container.encode(imageDetail, forKey: .imageUrl) + case .inputAudio(let audioDetail): + try container.encode("input_audio", forKey: .type) + try container.encode(audioDetail, forKey: .inputAudio) } } @@ -158,6 +195,8 @@ public struct ChatCompletionParameters: Encodable { hasher.combine(string) case .imageUrl(let imageDetail): hasher.combine(imageDetail) + case .inputAudio(let audioDetail): + hasher.combine(audioDetail) } } @@ -167,13 +206,15 @@ public struct ChatCompletionParameters: Encodable { return a == b case let (.imageUrl(a), .imageUrl(b)): return a == b + case let (.inputAudio(a), .inputAudio(b)): + return a == b default: return false } } } } - + public enum Role: String { case system // content, role case user // content, role @@ -296,10 +337,28 @@ public struct ChatCompletionParameters: Encodable { case includeUsage = "include_usage" } } + + /// Parameters for audio output. Required when audio output is requested with modalities: ["audio"] + /// [Learn more.](https://platform.openai.com/docs/guides/audio) + public struct Audio: Encodable { + /// Specifies the voice type. Supported voices are alloy, echo, fable, onyx, nova, and shimmer. + public let voice: String + /// Specifies the output audio format. Must be one of wav, mp3, flac, opus, or pcm16. + public let format: String + + public init( + voice: String, + format: String) + { + self.voice = voice + self.format = format + } + } enum CodingKeys: String, CodingKey { case messages case model + case store case frequencyPenalty = "frequency_penalty" case toolChoice = "tool_choice" case functionCall = "function_call" @@ -310,7 +369,10 @@ public struct ChatCompletionParameters: Encodable { case logprobs case topLogprobs = "top_logprobs" case maxTokens = "max_tokens" + case maCompletionTokens = "max_completion_tokens" case n + case modalities + case audio case responseFormat = "response_format" case presencePenalty = "presence_penalty" case seed @@ -326,6 +388,7 @@ public struct ChatCompletionParameters: Encodable { public init( messages: [Message], model: Model, + store: Bool? = nil, frequencyPenalty: Double? = nil, functionCall: FunctionCall? = nil, toolChoice: ToolChoice? = nil, @@ -337,6 +400,8 @@ public struct ChatCompletionParameters: Encodable { topLogprobs: Int? = nil, maxTokens: Int? = nil, n: Int? = nil, + modalities: [String]? = nil, + audio: Audio? = nil, responseFormat: ResponseFormat? = nil, presencePenalty: Double? = nil, serviceTier: ServiceTier? = nil, @@ -348,6 +413,7 @@ public struct ChatCompletionParameters: Encodable { { self.messages = messages self.model = model.value + self.store = store self.frequencyPenalty = frequencyPenalty self.functionCall = functionCall self.toolChoice = toolChoice @@ -359,6 +425,8 @@ public struct ChatCompletionParameters: Encodable { self.topLogprobs = topLogprobs self.maxTokens = maxTokens self.n = n + self.modalities = modalities + self.audio = audio self.responseFormat = responseFormat self.presencePenalty = presencePenalty self.serviceTier = serviceTier?.rawValue diff --git a/Sources/OpenAI/Public/Parameters/Model.swift b/Sources/OpenAI/Public/Parameters/Model.swift index 5e2acad..105483e 100644 --- a/Sources/OpenAI/Public/Parameters/Model.swift +++ b/Sources/OpenAI/Public/Parameters/Model.swift @@ -11,6 +11,8 @@ import Foundation /// [Models](https://platform.openai.com/docs/models) public enum Model { + case gpt4oAudioPreview + /// O1 models /// an early preview of the o1 model, designed to reason about hard problems using broad general knowledge about the world. @@ -71,6 +73,7 @@ public enum Model { public var value: String { switch self { + case .gpt4oAudioPreview: return "gpt-4o-audio-preview" case .o1Preview: return "o1-preview" case .o1Mini: return "o1-mini" case .gpt4o: return "gpt-4o" diff --git a/Sources/OpenAI/Public/ResponseModels/Chat/ChatCompletionChunkObject.swift b/Sources/OpenAI/Public/ResponseModels/Chat/ChatCompletionChunkObject.swift index 7bd9a70..bbd2df9 100644 --- a/Sources/OpenAI/Public/ResponseModels/Chat/ChatCompletionChunkObject.swift +++ b/Sources/OpenAI/Public/ResponseModels/Chat/ChatCompletionChunkObject.swift @@ -18,6 +18,8 @@ public struct ChatCompletionChunkObject: Decodable { public let created: Int /// The model to generate the completion. public let model: String + /// The service tier used for processing the request. This field is only included if the service_tier parameter is specified in the request. + public let serviceTier: String? /// This fingerprint represents the backend configuration that the model runs with. /// Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism. public let systemFingerprint: String? @@ -109,6 +111,7 @@ public struct ChatCompletionChunkObject: Decodable { case choices case created case model + case serviceTier = "service_tier" case systemFingerprint = "system_fingerprint" case object } diff --git a/Sources/OpenAI/Public/ResponseModels/Chat/ChatCompletionObject.swift b/Sources/OpenAI/Public/ResponseModels/Chat/ChatCompletionObject.swift index a2a6608..ceb6b35 100644 --- a/Sources/OpenAI/Public/ResponseModels/Chat/ChatCompletionObject.swift +++ b/Sources/OpenAI/Public/ResponseModels/Chat/ChatCompletionObject.swift @@ -18,6 +18,8 @@ public struct ChatCompletionObject: Decodable { public let created: Int /// The model used for the chat completion. public let model: String + /// The service tier used for processing the request. This field is only included if the service_tier parameter is specified in the request. + public let serviceTier: String? /// This fingerprint represents the backend configuration that the model runs with. /// Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism. public let systemFingerprint: String? @@ -52,12 +54,32 @@ public struct ChatCompletionObject: Decodable { public let finishDetails: FinishDetails? /// The refusal message generated by the model. public let refusal: String? + /// If the audio output modality is requested, this object contains data about the audio response from the model. [Learn more](https://platform.openai.com/docs/guides/audio). + public let audio: Audio? /// Provided by the Vision API. public struct FinishDetails: Decodable { let type: String } + public struct Audio: Decodable { + /// Unique identifier for this audio response. + public let id: String + /// The Unix timestamp (in seconds) for when this audio response will no longer be accessible on the server for use in multi-turn conversations. + public let expiresAt: Int + /// Base64 encoded audio bytes generated by the model, in the format specified in the request. + public let data: String + /// Transcript of the audio generated by the model. + public let transcript: String + + enum CodingKeys: String, CodingKey { + case id + case expiresAt = "expires_at" + case data + case transcript + } + } + enum CodingKeys: String, CodingKey { case content case toolCalls = "tool_calls" @@ -65,6 +87,7 @@ public struct ChatCompletionObject: Decodable { case role case finishDetails = "finish_details" case refusal + case audio } } @@ -111,6 +134,7 @@ public struct ChatCompletionObject: Decodable { case choices case created case model + case serviceTier = "service_tier" case systemFingerprint = "system_fingerprint" case object case usage