diff --git a/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Completion/ChatFunctionCallProvider.swift b/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Completion/ChatFunctionCallProvider.swift index 5da17d8..9ec092e 100644 --- a/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Completion/ChatFunctionCallProvider.swift +++ b/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Completion/ChatFunctionCallProvider.swift @@ -96,7 +96,7 @@ enum FunctionCallDefinition: String, CaseIterable { let parameters = ChatCompletionParameters( messages: chatMessageParameters, model: .gpt41106Preview, - toolChoice: ChatCompletionParameters.ToolChoice.auto, + toolChoice: ToolChoice.auto, tools: tools) do { diff --git a/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Stream/ChatFunctionsCallStreamProvider.swift b/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Stream/ChatFunctionsCallStreamProvider.swift index 4a5d13d..6015825 100644 --- a/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Stream/ChatFunctionsCallStreamProvider.swift +++ b/Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Stream/ChatFunctionsCallStreamProvider.swift @@ -92,7 +92,7 @@ struct FunctionCallStreamedResponse { let parameters = ChatCompletionParameters( messages: chatMessageParameters, model: .gpt35Turbo1106, - toolChoice: ChatCompletionParameters.ToolChoice.auto, + toolChoice: ToolChoice.auto, tools: tools) do { diff --git a/README.md b/README.md index 3dc1733..9ba1375 100644 --- a/README.md +++ b/README.md @@ -1126,6 +1126,10 @@ public struct FineTuningJobParameters: Encodable { /// Your dataset must be formatted as a JSONL file. You must upload your file with the purpose fine-tune. /// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) for more details. let validationFile: String? + /// A list of integrations to enable for your fine-tuning job. + let integrations: [Integration]? + /// The seed controls the reproducibility of the job. Passing in the same seed and job parameters should produce the same results, but may differ in rare cases. If a seed is not specified, one will be generated for you. + let seed: Int? /// Fine-tuning is [currently available](https://platform.openai.com/docs/guides/fine-tuning/what-models-can-be-fine-tuned) for the following models: /// gpt-3.5-turbo-0613 (recommended) @@ -2014,7 +2018,9 @@ Parameters ```swift public struct MessageParameter: Encodable { - /// The role of the entity that is creating the message. Currently only user is supported. + /// The role of the entity that is creating the message. Allowed values include: + /// user: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages. + /// assistant: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation. let role: String /// The content of the message. let content: String @@ -2256,12 +2262,29 @@ public struct RunParameter: Encodable { let instructions: String? /// Appends additional instructions at the end of the instructions for the run. This is useful for modifying the behavior on a per-run basis without overriding other instructions. let additionalInstructions: String? + /// Adds additional messages to the thread before creating the run. + let additionalMessages: [MessageParameter]? /// Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis. let tools: [AssistantObject.Tool]? /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long. let metadata: [String: String]? + /// 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. + /// Optional Defaults to 1 + let temperature: Double? /// If true, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a data: [DONE] message. var stream: Bool + /// The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status complete. See incomplete_details for more info. + let maxPromptTokens: Int? + /// The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status complete. See incomplete_details for more info. + let maxCompletionTokens: Int? + + let truncationStrategy: TruncationStrategy? + /// Controls which (if any) tool is called by the model. none means the model will not call any tools and instead generates a message. auto is the default value and means the model can pick between generating a message or calling a tool. Specifying a particular tool like {"type": "TOOL_TYPE"} or {"type": "function", "function": {"name": "my_function"}} forces the model to call that tool. + let toolChoice: ToolChoice? + /// Specifies the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. + /// 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 also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length. + let responseFormat: ResponseFormat? } ``` [Modify a Run](https://platform.openai.com/docs/api-reference/runs/modifyRun) @@ -2339,6 +2362,8 @@ public struct RunObject: Decodable { public let failedAt: Int? /// The Unix timestamp (in seconds) for when the run was completed. public let completedAt: Int? + /// Details on why the run is incomplete. Will be null if the run is not incomplete. + public let incompleteDetails: IncompleteDetails? /// The model that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run. public let model: String /// The instructions that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run. @@ -2349,6 +2374,14 @@ public struct RunObject: Decodable { public let fileIDS: [String] /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long. public let metadata: [String: String] + /// Usage statistics related to the run. This value will be null if the run is not in a terminal state (i.e. in_progress, queued, etc.). + public let usage: Usage? + /// The sampling temperature used for this run. If not set, defaults to 1. + public let temperature: Double? + /// The maximum number of prompt tokens specified to have been used over the course of the run. + public let maxPromptTokens: Int? + /// The maximum number of completion tokens specified to have been used over the course of the run. + public let maxCompletionTokens: Int? } ``` Usage diff --git a/Sources/OpenAI/AIProxy/AIProxyService.swift b/Sources/OpenAI/AIProxy/AIProxyService.swift index d1194fc..0a96441 100644 --- a/Sources/OpenAI/AIProxy/AIProxyService.swift +++ b/Sources/OpenAI/AIProxy/AIProxyService.swift @@ -449,7 +449,8 @@ struct AIProxyService: OpenAIService { limit: Int? = nil, order: String? = nil, after: String? = nil, - before: String? = nil) + before: String? = nil, + runID: String? = nil) async throws -> OpenAIResponse { var queryItems: [URLQueryItem] = [] diff --git a/Sources/OpenAI/Azure/DefaultOpenAIAzureService.swift b/Sources/OpenAI/Azure/DefaultOpenAIAzureService.swift index 2a3e704..5832f9e 100644 --- a/Sources/OpenAI/Azure/DefaultOpenAIAzureService.swift +++ b/Sources/OpenAI/Azure/DefaultOpenAIAzureService.swift @@ -205,7 +205,7 @@ final public class DefaultOpenAIAzureService: OpenAIService { fatalError("Currently, this API is not supported. We welcome and encourage contributions to our open-source project. Please consider opening an issue or submitting a pull request to add support for this feature.") } - public func listMessages(threadID: String, limit: Int?, order: String?, after: String?, before: String?) async throws -> OpenAIResponse { + public func listMessages(threadID: String, limit: Int?, order: String?, after: String?, before: String?, runID: String?) async throws -> OpenAIResponse { fatalError("Currently, this API is not supported. We welcome and encourage contributions to our open-source project. Please consider opening an issue or submitting a pull request to add support for this feature.") } diff --git a/Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift b/Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift index 76d5654..ad54d1e 100644 --- a/Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift +++ b/Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift @@ -202,42 +202,6 @@ 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 = "function", name: String) - - enum CodingKeys: String, CodingKey { - case none = "none" - case auto = "auto" - case type = "type" - case function = "function" - } - - enum FunctionCodingKeys: String, CodingKey { - case name = "name" - } - - 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(type, forKey: .type) - var functionContainer = container.nestedContainer(keyedBy: FunctionCodingKeys.self, forKey: .function) - try functionContainer.encode(name, forKey: .name) - } - } - } - public struct Tool: Encodable { /// The type of the tool. Currently, only `function` is supported. @@ -430,22 +394,6 @@ public struct ChatCompletionParameters: Encodable { } } - 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 init( - type: String?) - { - self.type = type - } - } - enum CodingKeys: String, CodingKey { case messages case model diff --git a/Sources/OpenAI/Public/Parameters/FineTuning/FineTuningJobParameters.swift b/Sources/OpenAI/Public/Parameters/FineTuning/FineTuningJobParameters.swift index dff5c68..3231ce8 100644 --- a/Sources/OpenAI/Public/Parameters/FineTuning/FineTuningJobParameters.swift +++ b/Sources/OpenAI/Public/Parameters/FineTuning/FineTuningJobParameters.swift @@ -29,14 +29,21 @@ public struct FineTuningJobParameters: Encodable { /// Your dataset must be formatted as a JSONL file. You must upload your file with the purpose fine-tune. /// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) for more details. let validationFile: String? + /// A list of integrations to enable for your fine-tuning job. + let integrations: [Integration]? + /// The seed controls the reproducibility of the job. Passing in the same seed and job parameters should produce the same results, but may differ in rare cases. If a seed is not specified, one will be generated for you. + let seed: Int? enum CodingKeys: String, CodingKey { case model case trainingFile = "training_file" + case hyperparameters + case suffix case validationFile = "validation_file" + case integrations + case seed } - /// Fine-tuning is [currently available](https://platform.openai.com/docs/guides/fine-tuning/what-models-can-be-fine-tuned) for the following models: /// gpt-3.5-turbo-0613 (recommended) /// babbage-002 @@ -64,17 +71,39 @@ public struct FineTuningJobParameters: Encodable { } } + public struct Integration: Encodable { + /// The type of integration to enable. Currently, only "wandb" (Weights and Biases) is supported. + let type: String + + let wandb: Wandb + + public struct Wandb: Encodable { + /// The name of the project that the new run will be created under. + let project: String + /// A display name to set for the run. If not set, we will use the Job ID as the name. + let name: String? + /// The entity to use for the run. This allows you to set the team or username of the WandB user that you would like associated with the run. If not set, the default entity for the registered WandB API key is used. + let entity: String? + /// A list of tags to be attached to the newly created run. These tags are passed through directly to WandB. Some default tags are generated by OpenAI: "openai/finetune", "openai/{base-model}", "openai/{ftjob-abcdef}". + let tags: [String]? + } + } + public init( model: Model, trainingFile: String, hyperparameters: HyperParameters? = nil, suffix: String? = nil, - validationFile: String? = nil) + validationFile: String? = nil, + integrations: [Integration]? = nil, + seed: Int? = nil) { self.model = model.rawValue self.trainingFile = trainingFile self.hyperparameters = hyperparameters self.suffix = suffix self.validationFile = validationFile + self.integrations = integrations + self.seed = seed } } diff --git a/Sources/OpenAI/Public/Parameters/Message/MessageParameter.swift b/Sources/OpenAI/Public/Parameters/Message/MessageParameter.swift index 4fd1055..cbd650a 100644 --- a/Sources/OpenAI/Public/Parameters/Message/MessageParameter.swift +++ b/Sources/OpenAI/Public/Parameters/Message/MessageParameter.swift @@ -10,7 +10,9 @@ import Foundation /// [Create a message.](https://platform.openai.com/docs/api-reference/messages/createMessage) public struct MessageParameter: Encodable { - /// The role of the entity that is creating the message. Currently only `user` is supported. + /// The role of the entity that is creating the message. Allowed values include: + /// user: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages. + /// assistant: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation. let role: String /// The content of the message. let content: String @@ -21,6 +23,7 @@ public struct MessageParameter: Encodable { public enum Role: String { case user + case assistant } enum CodingKeys: String, CodingKey { diff --git a/Sources/OpenAI/Public/Parameters/Model.swift b/Sources/OpenAI/Public/Parameters/Model.swift index 4fddf32..c429f8c 100644 --- a/Sources/OpenAI/Public/Parameters/Model.swift +++ b/Sources/OpenAI/Public/Parameters/Model.swift @@ -23,6 +23,15 @@ public enum Model { case gpt4TurboPreview // Currently points to gpt-4-0125-preview. /// The latest GPT-4 model intended to reduce cases of “laziness” where the model doesn’t complete a task. Returns a maximum of 4,096 output tokens. [Learn more.](https://openai.com/blog/new-embedding-models-and-api-updates) case gpt40125Preview // 128,000 tokens + /// GPT-4 Turbo with Vision model. Vision requests can now use JSON mode and function calling. gpt-4-turbo currently points to this version. + /// 128,000 tokens + /// Up to Dec 2023 + case gpt4Turbo20240409 + /// GPT-4 Turbo with Vision + /// The latest GPT-4 Turbo model with vision capabilities. Vision requests can now use JSON mode and function calling. Currently points to gpt-4-turbo-2024-04-09. + /// 128,000 tokens + /// Up to Dec 2023 + case gpt4turbo /// Vision case gpt4VisionPreview // Vision @@ -48,6 +57,8 @@ public enum Model { case .dalle3: return "dall-e-3" case .gpt4TurboPreview: return "gpt-4-turbo-preview" case .gpt40125Preview: return "gpt-4-0125-preview" + case .gpt4Turbo20240409: return "gpt-4-turbo-2024-04-09" + case .gpt4turbo: return "gpt-4-turbo" case .custom(let model): return model } } diff --git a/Sources/OpenAI/Public/Parameters/Runs/RunParameter.swift b/Sources/OpenAI/Public/Parameters/Runs/RunParameter.swift index 9207b8c..76eb09c 100644 --- a/Sources/OpenAI/Public/Parameters/Runs/RunParameter.swift +++ b/Sources/OpenAI/Public/Parameters/Runs/RunParameter.swift @@ -14,25 +14,49 @@ public struct RunParameter: Encodable { public let assistantID: String /// The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used. let model: String? - /// Override the default system message of the assistant. This is useful for modifying the behavior on a per-run basis. + /// Overrides the [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant) of the assistant. This is useful for modifying the behavior on a per-run basis. let instructions: String? /// Appends additional instructions at the end of the instructions for the run. This is useful for modifying the behavior on a per-run basis without overriding other instructions. let additionalInstructions: String? + /// Adds additional messages to the thread before creating the run. + let additionalMessages: [MessageParameter]? /// Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis. let tools: [AssistantObject.Tool]? /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long. let metadata: [String: String]? + /// 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. + /// Optional Defaults to 1 + let temperature: Double? /// If true, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a data: [DONE] message. var stream: Bool = false + /// The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status complete. See incomplete_details for more info. + let maxPromptTokens: Int? + /// The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status complete. See incomplete_details for more info. + let maxCompletionTokens: Int? + let truncationStrategy: TruncationStrategy? + /// Controls which (if any) tool is called by the model. none means the model will not call any tools and instead generates a message. auto is the default value and means the model can pick between generating a message or calling a tool. Specifying a particular tool like {"type": "TOOL_TYPE"} or {"type": "function", "function": {"name": "my_function"}} forces the model to call that tool. + let toolChoice: ToolChoice? + /// Specifies the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. + /// 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 also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length. + let responseFormat: ResponseFormat? + enum CodingKeys: String, CodingKey { case assistantID = "assistant_id" case model case instructions case additionalInstructions = "additional_instructions" + case additionalMessages = "additional_messages" case tools case metadata case stream + case temperature + case maxPromptTokens = "max_prompt_tokens" + case maxCompletionTokens = "max_completion_tokens" + case truncationStrategy + case toolChoice = "tool_choice" + case responseFormat = "response_format" } public init( @@ -40,14 +64,28 @@ public struct RunParameter: Encodable { model: String? = nil, instructions: String? = nil, additionalInstructions: String? = nil, + additionalMessages: [MessageParameter]? = nil, tools: [AssistantObject.Tool]? = nil, - metadata: [String : String]? = nil) + metadata: [String : String]? = nil, + temperature: Double? = nil, + maxPromptTokens: Int? = nil, + maxCompletionTokens: Int? = nil, + truncationStrategy: TruncationStrategy? = nil, + toolChoice: ToolChoice? = nil, + responseFormat: ResponseFormat? = nil) { self.assistantID = assistantID self.model = model self.instructions = instructions self.additionalInstructions = additionalInstructions + self.additionalMessages = additionalMessages self.tools = tools self.metadata = metadata + self.temperature = temperature + self.maxPromptTokens = maxPromptTokens + self.maxCompletionTokens = maxCompletionTokens + self.truncationStrategy = truncationStrategy + self.toolChoice = toolChoice + self.responseFormat = responseFormat } } diff --git a/Sources/OpenAI/Public/ResponseModels/Runs/RunObject.swift b/Sources/OpenAI/Public/ResponseModels/Runs/RunObject.swift index 4497765..48579c9 100644 --- a/Sources/OpenAI/Public/ResponseModels/Runs/RunObject.swift +++ b/Sources/OpenAI/Public/ResponseModels/Runs/RunObject.swift @@ -38,6 +38,8 @@ public struct RunObject: Decodable { public let failedAt: Int? /// The Unix timestamp (in seconds) for when the run was completed. public let completedAt: Int? + /// Details on why the run is incomplete. Will be null if the run is not incomplete. + public let incompleteDetails: IncompleteDetails? /// The model that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run. public let model: String /// The instructions that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run. @@ -48,7 +50,23 @@ public struct RunObject: Decodable { public let fileIDS: [String] /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long. public let metadata: [String: String] - + /// Usage statistics related to the run. This value will be null if the run is not in a terminal state (i.e. in_progress, queued, etc.). + public let usage: Usage? + /// The sampling temperature used for this run. If not set, defaults to 1. + public let temperature: Double? + /// The maximum number of prompt tokens specified to have been used over the course of the run. + public let maxPromptTokens: Int? + /// The maximum number of completion tokens specified to have been used over the course of the run. + public let maxCompletionTokens: Int? + + public let truncationStrategy: TruncationStrategy? + /// Controls which (if any) tool is called by the model. none means the model will not call any tools and instead generates a message. auto is the default value and means the model can pick between generating a message or calling a tool. Specifying a particular tool like {"type": "TOOL_TYPE"} or {"type": "function", "function": {"name": "my_function"}} forces the model to call that tool. + public let toolChoice: ToolChoice? + /// Specifies the format that the model must output. Compatible with GPT-4 Turbo and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. + /// 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 also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length. + public let responseFormat: ResponseFormat? + public enum Status: String { case queued case inProgress = "in_progress" @@ -68,7 +86,6 @@ public struct RunObject: Decodable { public let submitToolsOutputs: SubmitToolOutput public struct SubmitToolOutput: Decodable { - /// A list of the relevant tool calls. /// - Object: ToolCall /// - id: The ID of the tool call. This ID must be referenced when you submit the tool outputs in using the [Submit tool outputs to run](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) endpoint. @@ -88,12 +105,15 @@ public struct RunObject: Decodable { } public struct LastError: Codable { - /// One of server_error or rate_limit_exceeded. let code: String /// A human-readable description of the error. let message: String - + } + + public struct IncompleteDetails: Decodable { + /// The reason why the run is incomplete. This will point to which specific token limit was reached over the course of the run. + let reason: String } public var displayStatus: Status? { .init(rawValue: status) } @@ -112,50 +132,18 @@ public struct RunObject: Decodable { case cancelledAt = "cancelled_at" case failedAt = "failed_at" case completedAt = "completed_at" + case incompleteDetails = "incomplete_details" case model case instructions case fileIDS = "file_ids" case tools case metadata - } - - public init( - id: String, - object: String, - createdAt: Int, - threadID: String, - assistantID: String, - status: String, - requiredAction: RequiredAction?, - lastError: LastError?, - expiresAt: Int, - startedAt: Int?, - cancelledAt: Int?, - failedAt: Int?, - completedAt: Int?, - model: String, - instructions: String?, - tools: [AssistantObject.Tool], - fileIDS: [String], - metadata: [String : String]) - { - self.id = id - self.object = object - self.createdAt = createdAt - self.threadID = threadID - self.assistantID = assistantID - self.status = status - self.requiredAction = requiredAction - self.lastError = lastError - self.expiresAt = expiresAt - self.startedAt = startedAt - self.cancelledAt = cancelledAt - self.failedAt = failedAt - self.completedAt = completedAt - self.model = model - self.instructions = instructions - self.tools = tools - self.fileIDS = fileIDS - self.metadata = metadata + case usage + case temperature + case maxPromptTokens = "max_prompt_tokens" + case maxCompletionTokens = "max_completion_tokens" + case truncationStrategy + case toolChoice = "tool_choice" + case responseFormat = "response_format" } } diff --git a/Sources/OpenAI/Public/ResponseModels/Runs/RunStepObject.swift b/Sources/OpenAI/Public/ResponseModels/Runs/RunStepObject.swift index 39c449d..ad7cd71 100644 --- a/Sources/OpenAI/Public/ResponseModels/Runs/RunStepObject.swift +++ b/Sources/OpenAI/Public/ResponseModels/Runs/RunStepObject.swift @@ -51,22 +51,6 @@ public struct RunStepObject: Codable { case expired } - public struct Usage: Codable { - - /// Number of completion tokens used over the course of the run step. - public let completionTokens: Int - /// Number of prompt tokens used over the course of the run step. - public let promptTokens: Int - /// Total number of tokens used (prompt + completion). - public let totalTokens: Int - - enum CodingKeys: String, CodingKey { - case completionTokens = "completion_tokens" - case promptTokens = "prompt_tokens" - case totalTokens = "total_tokens" - } - } - enum CodingKeys: String, CodingKey { case id case object diff --git a/Sources/OpenAI/Public/Service/DefaultOpenAIService.swift b/Sources/OpenAI/Public/Service/DefaultOpenAIService.swift index 94fd819..25bf030 100644 --- a/Sources/OpenAI/Public/Service/DefaultOpenAIService.swift +++ b/Sources/OpenAI/Public/Service/DefaultOpenAIService.swift @@ -445,7 +445,8 @@ struct DefaultOpenAIService: OpenAIService { limit: Int? = nil, order: String? = nil, after: String? = nil, - before: String? = nil) + before: String? = nil, + runID: String? = nil) async throws -> OpenAIResponse { var queryItems: [URLQueryItem] = [] @@ -461,6 +462,9 @@ struct DefaultOpenAIService: OpenAIService { if let before { queryItems.append(.init(name: "before", value: before)) } + if let runID { + queryItems.append(.init(name: "run_id", value: runID)) + } let request = try OpenAIAPI.message(.list(threadID: threadID)).request(apiKey: apiKey, organizationID: organizationID, method: .get, queryItems: queryItems, betaHeaderField: Self.assistantsBeta) return try await fetch(type: OpenAIResponse.self, with: request) } diff --git a/Sources/OpenAI/Public/Service/OpenAIService.swift b/Sources/OpenAI/Public/Service/OpenAIService.swift index 686333a..bc5c371 100644 --- a/Sources/OpenAI/Public/Service/OpenAIService.swift +++ b/Sources/OpenAI/Public/Service/OpenAIService.swift @@ -547,6 +547,7 @@ public protocol OpenAIService { /// - Parameter order: Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order. Defaults to desc /// - Parameter after: A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list. /// - Parameter before: A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list. + /// - Parameter runID: Filter messages by the run ID that generated them. /// - Returns: A list of [message](https://platform.openai.com/docs/api-reference/messages) objects. /// - Throws: An error if the request fails /// @@ -556,7 +557,8 @@ public protocol OpenAIService { limit: Int?, order: String?, after: String?, - before: String?) + before: String?, + runID: String?) async throws -> OpenAIResponse // MARK: Message File [BETA] diff --git a/Sources/OpenAI/Public/Shared/ResponseFormat.swift b/Sources/OpenAI/Public/Shared/ResponseFormat.swift new file mode 100644 index 0000000..a11d06a --- /dev/null +++ b/Sources/OpenAI/Public/Shared/ResponseFormat.swift @@ -0,0 +1,25 @@ +// +// ResponseFormat.swift +// +// +// Created by James Rochabrun on 4/13/24. +// + +import Foundation + + +public struct ResponseFormat: Codable { + + /// 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 init( + type: String?) + { + self.type = type + } +} diff --git a/Sources/OpenAI/Public/Codable/ToolCall.swift b/Sources/OpenAI/Public/Shared/ToolCall.swift similarity index 98% rename from Sources/OpenAI/Public/Codable/ToolCall.swift rename to Sources/OpenAI/Public/Shared/ToolCall.swift index 516fbc9..9ed362e 100644 --- a/Sources/OpenAI/Public/Codable/ToolCall.swift +++ b/Sources/OpenAI/Public/Shared/ToolCall.swift @@ -1,6 +1,6 @@ // -// File.swift -// +// ToolCall.swift +// // // Created by James Rochabrun on 11/14/23. // diff --git a/Sources/OpenAI/Public/Shared/ToolChoice.swift b/Sources/OpenAI/Public/Shared/ToolChoice.swift new file mode 100644 index 0000000..63def42 --- /dev/null +++ b/Sources/OpenAI/Public/Shared/ToolChoice.swift @@ -0,0 +1,60 @@ +// +// ToolChoice.swift +// +// +// Created by James Rochabrun on 4/13/24. +// + +import Foundation + +/// 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: Codable, Equatable { + case none + case auto + case function(type: String = "function", name: String) + + enum CodingKeys: String, CodingKey { + case none = "none" + case auto = "auto" + case type = "type" + case function = "function" + } + + enum FunctionCodingKeys: String, CodingKey { + case name = "name" + } + + 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(type, forKey: .type) + var functionContainer = container.nestedContainer(keyedBy: FunctionCodingKeys.self, forKey: .function) + try functionContainer.encode(name, forKey: .name) + } + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + if let _ = try? container.decode(String.self, forKey: .none) { + self = .none + return + } + if let _ = try? container.decode(String.self, forKey: .auto) { + self = .auto + return + } + let functionContainer = try container.nestedContainer(keyedBy: FunctionCodingKeys.self, forKey: .function) + let name = try functionContainer.decode(String.self, forKey: .name) + // Assuming the type is always "function" as default if decoding this case. + self = .function(type: "function", name: name) + } +} diff --git a/Sources/OpenAI/Public/Shared/TruncationStrategy.swift b/Sources/OpenAI/Public/Shared/TruncationStrategy.swift new file mode 100644 index 0000000..fbe2d65 --- /dev/null +++ b/Sources/OpenAI/Public/Shared/TruncationStrategy.swift @@ -0,0 +1,20 @@ +// +// TruncationStrategy.swift +// +// +// Created by James Rochabrun on 4/13/24. +// + +import Foundation + +public struct TruncationStrategy: Codable { + /// The truncation strategy to use for the thread. The default is auto. If set to last_messages, the thread will be truncated to the n most recent messages in the thread. When set to auto, messages in the middle of the thread will be dropped to fit the context length of the model, max_prompt_tokens. + let type: String + /// The number of most recent messages from the thread when constructing the context for the run. + let lastMessage: Int? + + enum CodingKeys: String, CodingKey { + case type + case lastMessage = "last_messages" + } +} diff --git a/Sources/OpenAI/Public/Shared/Usage.swift b/Sources/OpenAI/Public/Shared/Usage.swift new file mode 100644 index 0000000..9688256 --- /dev/null +++ b/Sources/OpenAI/Public/Shared/Usage.swift @@ -0,0 +1,26 @@ +// +// Usage.swift +// +// +// Created by James Rochabrun on 4/13/24. +// + +import Foundation + + +/// Usage statistics related to the run. This value will be null if the run is not in a terminal state (i.e. in_progress, queued, etc.). +public struct Usage: Codable { + + /// Number of completion tokens used over the course of the run step. + public let completionTokens: Int + /// Number of prompt tokens used over the course of the run step. + public let promptTokens: Int + /// Total number of tokens used (prompt + completion). + public let totalTokens: Int + + enum CodingKeys: String, CodingKey { + case completionTokens = "completion_tokens" + case promptTokens = "prompt_tokens" + case totalTokens = "total_tokens" + } +}