Skip to content

Commit c09b9d5

Browse files
OpenAI latest updates. (#28)
* Updating run parameters * Updating run parameters * update runs * filtering messages for runid * adding seed parameter to fine tune job * Adding new GPT turbo family members
1 parent d235702 commit c09b9d5

19 files changed

+298
-126
lines changed

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Completion/ChatFunctionCallProvider.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ enum FunctionCallDefinition: String, CaseIterable {
9696
let parameters = ChatCompletionParameters(
9797
messages: chatMessageParameters,
9898
model: .gpt41106Preview,
99-
toolChoice: ChatCompletionParameters.ToolChoice.auto,
99+
toolChoice: ToolChoice.auto,
100100
tools: tools)
101101

102102
do {

Examples/SwiftOpenAIExample/SwiftOpenAIExample/ChatFunctionsCall/Stream/ChatFunctionsCallStreamProvider.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct FunctionCallStreamedResponse {
9292
let parameters = ChatCompletionParameters(
9393
messages: chatMessageParameters,
9494
model: .gpt35Turbo1106,
95-
toolChoice: ChatCompletionParameters.ToolChoice.auto,
95+
toolChoice: ToolChoice.auto,
9696
tools: tools)
9797

9898
do {

README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,10 @@ public struct FineTuningJobParameters: Encodable {
11261126
/// Your dataset must be formatted as a JSONL file. You must upload your file with the purpose fine-tune.
11271127
/// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) for more details.
11281128
let validationFile: String?
1129+
/// A list of integrations to enable for your fine-tuning job.
1130+
let integrations: [Integration]?
1131+
/// 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.
1132+
let seed: Int?
11291133

11301134
/// Fine-tuning is [currently available](https://platform.openai.com/docs/guides/fine-tuning/what-models-can-be-fine-tuned) for the following models:
11311135
/// gpt-3.5-turbo-0613 (recommended)
@@ -2014,7 +2018,9 @@ Parameters
20142018
```swift
20152019
public struct MessageParameter: Encodable {
20162020

2017-
/// The role of the entity that is creating the message. Currently only user is supported.
2021+
/// The role of the entity that is creating the message. Allowed values include:
2022+
/// user: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages.
2023+
/// assistant: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation.
20182024
let role: String
20192025
/// The content of the message.
20202026
let content: String
@@ -2256,12 +2262,29 @@ public struct RunParameter: Encodable {
22562262
let instructions: String?
22572263
/// 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.
22582264
let additionalInstructions: String?
2265+
/// Adds additional messages to the thread before creating the run.
2266+
let additionalMessages: [MessageParameter]?
22592267
/// Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.
22602268
let tools: [AssistantObject.Tool]?
22612269
/// 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.
22622270
let metadata: [String: String]?
2271+
/// 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.
2272+
/// Optional Defaults to 1
2273+
let temperature: Double?
22632274
/// 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.
22642275
var stream: Bool
2276+
/// 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.
2277+
let maxPromptTokens: Int?
2278+
/// 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.
2279+
let maxCompletionTokens: Int?
2280+
2281+
let truncationStrategy: TruncationStrategy?
2282+
/// 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.
2283+
let toolChoice: ToolChoice?
2284+
/// 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.
2285+
/// Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.
2286+
/// 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.
2287+
let responseFormat: ResponseFormat?
22652288
}
22662289
```
22672290
[Modify a Run](https://platform.openai.com/docs/api-reference/runs/modifyRun)
@@ -2339,6 +2362,8 @@ public struct RunObject: Decodable {
23392362
public let failedAt: Int?
23402363
/// The Unix timestamp (in seconds) for when the run was completed.
23412364
public let completedAt: Int?
2365+
/// Details on why the run is incomplete. Will be null if the run is not incomplete.
2366+
public let incompleteDetails: IncompleteDetails?
23422367
/// The model that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run.
23432368
public let model: String
23442369
/// 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 {
23492374
public let fileIDS: [String]
23502375
/// 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.
23512376
public let metadata: [String: String]
2377+
/// 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.).
2378+
public let usage: Usage?
2379+
/// The sampling temperature used for this run. If not set, defaults to 1.
2380+
public let temperature: Double?
2381+
/// The maximum number of prompt tokens specified to have been used over the course of the run.
2382+
public let maxPromptTokens: Int?
2383+
/// The maximum number of completion tokens specified to have been used over the course of the run.
2384+
public let maxCompletionTokens: Int?
23522385
}
23532386
```
23542387
Usage

Sources/OpenAI/AIProxy/AIProxyService.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ struct AIProxyService: OpenAIService {
449449
limit: Int? = nil,
450450
order: String? = nil,
451451
after: String? = nil,
452-
before: String? = nil)
452+
before: String? = nil,
453+
runID: String? = nil)
453454
async throws -> OpenAIResponse<MessageObject>
454455
{
455456
var queryItems: [URLQueryItem] = []

Sources/OpenAI/Azure/DefaultOpenAIAzureService.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ final public class DefaultOpenAIAzureService: OpenAIService {
205205
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.")
206206
}
207207

208-
public func listMessages(threadID: String, limit: Int?, order: String?, after: String?, before: String?) async throws -> OpenAIResponse<MessageObject> {
208+
public func listMessages(threadID: String, limit: Int?, order: String?, after: String?, before: String?, runID: String?) async throws -> OpenAIResponse<MessageObject> {
209209
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.")
210210
}
211211

Sources/OpenAI/Public/Parameters/Chat/ChatCompletionParameters.swift

-52
Original file line numberDiff line numberDiff line change
@@ -202,42 +202,6 @@ public struct ChatCompletionParameters: Encodable {
202202
}
203203
}
204204

205-
/// string `none` means the model will not call a function and instead generates a message.
206-
/// `auto` means the model can pick between generating a message or calling a function.
207-
/// `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"}}`
208-
public enum ToolChoice: Encodable, Equatable {
209-
case none
210-
case auto
211-
case function(type: String = "function", name: String)
212-
213-
enum CodingKeys: String, CodingKey {
214-
case none = "none"
215-
case auto = "auto"
216-
case type = "type"
217-
case function = "function"
218-
}
219-
220-
enum FunctionCodingKeys: String, CodingKey {
221-
case name = "name"
222-
}
223-
224-
public func encode(to encoder: Encoder) throws {
225-
switch self {
226-
case .none:
227-
var container = encoder.singleValueContainer()
228-
try container.encode(CodingKeys.none.rawValue)
229-
case .auto:
230-
var container = encoder.singleValueContainer()
231-
try container.encode(CodingKeys.auto.rawValue)
232-
case .function(let type, let name):
233-
var container = encoder.container(keyedBy: CodingKeys.self)
234-
try container.encode(type, forKey: .type)
235-
var functionContainer = container.nestedContainer(keyedBy: FunctionCodingKeys.self, forKey: .function)
236-
try functionContainer.encode(name, forKey: .name)
237-
}
238-
}
239-
}
240-
241205
public struct Tool: Encodable {
242206

243207
/// The type of the tool. Currently, only `function` is supported.
@@ -430,22 +394,6 @@ public struct ChatCompletionParameters: Encodable {
430394
}
431395
}
432396

433-
public struct ResponseFormat: Encodable {
434-
435-
/// Defaults to text
436-
/// Setting to `json_object` enables JSON mode. This guarantees that the message the model generates is valid JSON.
437-
/// 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.
438-
/// 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.
439-
/// Must be one of `text `or `json_object`.
440-
public var type: String?
441-
442-
public init(
443-
type: String?)
444-
{
445-
self.type = type
446-
}
447-
}
448-
449397
enum CodingKeys: String, CodingKey {
450398
case messages
451399
case model

Sources/OpenAI/Public/Parameters/FineTuning/FineTuningJobParameters.swift

+31-2
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,21 @@ public struct FineTuningJobParameters: Encodable {
2929
/// Your dataset must be formatted as a JSONL file. You must upload your file with the purpose fine-tune.
3030
/// See the [fine-tuning guide](https://platform.openai.com/docs/guides/fine-tuning) for more details.
3131
let validationFile: String?
32+
/// A list of integrations to enable for your fine-tuning job.
33+
let integrations: [Integration]?
34+
/// 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.
35+
let seed: Int?
3236

3337
enum CodingKeys: String, CodingKey {
3438
case model
3539
case trainingFile = "training_file"
40+
case hyperparameters
41+
case suffix
3642
case validationFile = "validation_file"
43+
case integrations
44+
case seed
3745
}
3846

39-
4047
/// Fine-tuning is [currently available](https://platform.openai.com/docs/guides/fine-tuning/what-models-can-be-fine-tuned) for the following models:
4148
/// gpt-3.5-turbo-0613 (recommended)
4249
/// babbage-002
@@ -64,17 +71,39 @@ public struct FineTuningJobParameters: Encodable {
6471
}
6572
}
6673

74+
public struct Integration: Encodable {
75+
/// The type of integration to enable. Currently, only "wandb" (Weights and Biases) is supported.
76+
let type: String
77+
78+
let wandb: Wandb
79+
80+
public struct Wandb: Encodable {
81+
/// The name of the project that the new run will be created under.
82+
let project: String
83+
/// A display name to set for the run. If not set, we will use the Job ID as the name.
84+
let name: String?
85+
/// 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.
86+
let entity: String?
87+
/// 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}".
88+
let tags: [String]?
89+
}
90+
}
91+
6792
public init(
6893
model: Model,
6994
trainingFile: String,
7095
hyperparameters: HyperParameters? = nil,
7196
suffix: String? = nil,
72-
validationFile: String? = nil)
97+
validationFile: String? = nil,
98+
integrations: [Integration]? = nil,
99+
seed: Int? = nil)
73100
{
74101
self.model = model.rawValue
75102
self.trainingFile = trainingFile
76103
self.hyperparameters = hyperparameters
77104
self.suffix = suffix
78105
self.validationFile = validationFile
106+
self.integrations = integrations
107+
self.seed = seed
79108
}
80109
}

Sources/OpenAI/Public/Parameters/Message/MessageParameter.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import Foundation
1010
/// [Create a message.](https://platform.openai.com/docs/api-reference/messages/createMessage)
1111
public struct MessageParameter: Encodable {
1212

13-
/// The role of the entity that is creating the message. Currently only `user` is supported.
13+
/// The role of the entity that is creating the message. Allowed values include:
14+
/// user: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages.
15+
/// assistant: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation.
1416
let role: String
1517
/// The content of the message.
1618
let content: String
@@ -21,6 +23,7 @@ public struct MessageParameter: Encodable {
2123

2224
public enum Role: String {
2325
case user
26+
case assistant
2427
}
2528

2629
enum CodingKeys: String, CodingKey {

Sources/OpenAI/Public/Parameters/Model.swift

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public enum Model {
2323
case gpt4TurboPreview // Currently points to gpt-4-0125-preview.
2424
/// 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)
2525
case gpt40125Preview // 128,000 tokens
26+
/// GPT-4 Turbo with Vision model. Vision requests can now use JSON mode and function calling. gpt-4-turbo currently points to this version.
27+
/// 128,000 tokens
28+
/// Up to Dec 2023
29+
case gpt4Turbo20240409
30+
/// GPT-4 Turbo with Vision
31+
/// 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.
32+
/// 128,000 tokens
33+
/// Up to Dec 2023
34+
case gpt4turbo
2635

2736
/// Vision
2837
case gpt4VisionPreview // Vision
@@ -48,6 +57,8 @@ public enum Model {
4857
case .dalle3: return "dall-e-3"
4958
case .gpt4TurboPreview: return "gpt-4-turbo-preview"
5059
case .gpt40125Preview: return "gpt-4-0125-preview"
60+
case .gpt4Turbo20240409: return "gpt-4-turbo-2024-04-09"
61+
case .gpt4turbo: return "gpt-4-turbo"
5162
case .custom(let model): return model
5263
}
5364
}

0 commit comments

Comments
 (0)