Skip to content

Commit

Permalink
OpenAI latest updates. (#28)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jamesrochabrun authored Apr 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d235702 commit c09b9d5
Showing 19 changed files with 298 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ struct FunctionCallStreamedResponse {
let parameters = ChatCompletionParameters(
messages: chatMessageParameters,
model: .gpt35Turbo1106,
toolChoice: ChatCompletionParameters.ToolChoice.auto,
toolChoice: ToolChoice.auto,
tools: tools)

do {
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion Sources/OpenAI/AIProxy/AIProxyService.swift
Original file line number Diff line number Diff line change
@@ -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<MessageObject>
{
var queryItems: [URLQueryItem] = []
2 changes: 1 addition & 1 deletion Sources/OpenAI/Azure/DefaultOpenAIAzureService.swift
Original file line number Diff line number Diff line change
@@ -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<MessageObject> {
public func listMessages(threadID: String, limit: Int?, order: String?, after: String?, before: String?, runID: String?) async throws -> OpenAIResponse<MessageObject> {
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.")
}

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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 {
11 changes: 11 additions & 0 deletions Sources/OpenAI/Public/Parameters/Model.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading

0 comments on commit c09b9d5

Please sign in to comment.