Skip to content

Commit

Permalink
Update OpenAI models (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
SwiftBeta authored Nov 9, 2023
1 parent 596beb9 commit 6d15a5c
Show file tree
Hide file tree
Showing 28 changed files with 144 additions and 376 deletions.
10 changes: 0 additions & 10 deletions MyPlayground.playground/Contents.swift

This file was deleted.

4 changes: 0 additions & 4 deletions MyPlayground.playground/contents.xcplayground

This file was deleted.

This file was deleted.

81 changes: 81 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
{
"pins" : [
{
"identity" : "collectionconcurrencykit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/CollectionConcurrencyKit.git",
"state" : {
"revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95",
"version" : "0.2.0"
}
},
{
"identity" : "cryptoswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
"state" : {
"revision" : "32f641cf24fc7abc1c591a2025e9f2f572648b0f",
"version" : "1.7.2"
}
},
{
"identity" : "sourcekitten",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/SourceKitten.git",
"state" : {
"revision" : "b6dc09ee51dfb0c66e042d2328c017483a1a5d56",
"version" : "0.34.1"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
"version" : "1.2.3"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
Expand All @@ -17,6 +53,51 @@
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "74203046135342e4a4a627476dd6caf8b28fe11b",
"version" : "509.0.0"
}
},
{
"identity" : "swiftlint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/SwiftLint",
"state" : {
"revision" : "6d2e58271ebc14c37bf76d7c9f4082cc15bad718",
"version" : "0.53.0"
}
},
{
"identity" : "swiftytexttable",
"kind" : "remoteSourceControl",
"location" : "https://github.com/scottrhoyt/SwiftyTextTable.git",
"state" : {
"revision" : "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3",
"version" : "0.9.0"
}
},
{
"identity" : "swxmlhash",
"kind" : "remoteSourceControl",
"location" : "https://github.com/drmohundro/SWXMLHash.git",
"state" : {
"revision" : "a853604c9e9a83ad9954c7e3d2a565273982471f",
"version" : "7.0.2"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "0d9ee7ea8c4ebd4a489ad7a73d5c6cad55d6fed3",
"version" : "5.0.6"
}
}
],
"version" : 2
Expand Down
28 changes: 7 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

## Introduction

`SwiftOpenAI` is a powerful and easy-to-use Swift SDK designed to seamlessly integrate with `OpenAI's API`. The main goal of this SDK is to simplify the process of accessing and interacting with OpenAI's cutting-edge AI models, such as GPT-4, GPT-3, and future models, all within your Swift applications.
`SwiftOpenAI` is a (community-maintained) powerful and easy-to-use Swift SDK designed to seamlessly integrate with `OpenAI's API`. The main goal of this SDK is to simplify the process of accessing and interacting with OpenAI's cutting-edge AI models, such as GPT-4, GPT-3, and future models, all within your Swift applications.

![Untitled](https://user-images.githubusercontent.com/74316958/228327592-e45e7dc1-fa04-4042-81c8-dc1fca52ff47.gif)

Expand Down Expand Up @@ -60,7 +60,7 @@ let prompt = "Once upon a time, in a land far, far away,"
let optionalParameters = CompletionsOptionalParameters(prompt: prompt, maxTokens: 50, temperature: 0.7, n: 1)

do {
let completions = try await openAI.completions(model: .gpt3_5(.text_davinci_003), optionalParameters: optionalParameters)
let completions = try await openAI.completions(model: .gpt3_5(.turbo), optionalParameters: optionalParameters)
print(completions)
} catch {
print("Error: \(error)")
Expand Down Expand Up @@ -106,30 +106,16 @@ do {
}
```

## [Edits](https://platform.openai.com/docs/api-reference/edits)
Given a prompt and an instruction, the model will return an edited version of the prompt.

```swift
let inputText = "The car have four weels."
let instruction = "Please correct any grammatical errors in the text."

do {
let edits = try await openAI.edits(model: .edit(.text_davinci_edit_001), input: inputText, instruction: instruction)
print(edits)
} catch {
print("Error: \(error)")
}
```

## [Create Image with DALL·E 2](https://platform.openai.com/docs/api-reference/images/create)
## [Create Image with DALL·E 3](https://platform.openai.com/docs/api-reference/images/create)
Given a prompt and/or an input image, the model will generate a new image.

```swift
do {
let images = try await openAI.createImages(prompt: prompt,
numberOfImages: 4,
let image = try await openAI.createImages(model: .dalle(.dalle3),
prompt: prompt,
numberOfImages: 1,
size: .s1024)
print(images)
print(image)
} catch {
print("Error: \(error)")
}
Expand Down
19 changes: 0 additions & 19 deletions Sources/SwiftOpenAI/OpenAI/DataModels/Edits/EditsDataModel.swift

This file was deleted.

2 changes: 2 additions & 0 deletions Sources/SwiftOpenAI/OpenAI/DataModels/Images/ImageSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public enum ImageSize: String {
case s256 = "256x256"
case s512 = "512x512"
case s1024 = "1024x1024"
case sw1792h1024 = "1792x1024"
case sw1024h1792 = "1024x1792"
}
33 changes: 21 additions & 12 deletions Sources/SwiftOpenAI/OpenAI/DataModels/OpenAIModelType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Foundation
public enum OpenAIModelType {
case gpt4(GPT4)
case gpt3_5(GPT3_5)
case edit(EditModel)
case embedding(EmbeddingModel)

var name: String {
Expand All @@ -12,32 +11,42 @@ public enum OpenAIModelType {
return gpt4Model.rawValue
case .gpt3_5(let gpt3_5Model):
return gpt3_5Model.rawValue
case .edit(let editModel):
return editModel.rawValue
case .embedding(let embeddingModel):
return embeddingModel.rawValue
}
}
}

public enum OpenAIImageModelType {
case dalle(Dalle)

var name: String {
switch self {
case .dalle(let model):
return model.rawValue
}
}
}

public enum GPT4: String {
case base = "gpt-4"
case gpt_4_0314 = "gpt-4-0314"
case gpt_4_1106_preview = "gpt-4-1106-preview"
case gpt_4_vision_preview = "gpt-4-vision-preview"
case gpt_4_32k = "gpt-4-32k"
case gpt_4_32k_0314 = "gpt-4-32k-0314"
case gpt_4_0613 = "gpt-4-0613"
case gpt_4_32k_0613 = "gpt-4-32k-0613"
}

public enum GPT3_5: String {
case turbo = "gpt-3.5-turbo"
case gpt_3_5_turbo_0301 = "gpt-3.5-turbo-0301"
case text_davinci_003 = "text-davinci-003"
case text_davinci_002 = "text-davinci-002"
case code_davinci_002 = "code-davinci-002"
case gpt_3_5_turbo_1106 = "gpt-3.5-turbo-1106"
case gpt_3_5_turbo_16k = "gpt-3.5-turbo-16k"
case gpt_3_5_turbo_instruct = "gpt-3.5-turbo-instruct"
}

public enum EditModel: String {
case text_davinci_edit_001 = "text-davinci-edit-001"
case code_davinci_edit_001 = "code-davinci-edit-001"
public enum Dalle: String {
case dalle2 = "dall-e-2"
case dalle3 = "dall-e-3"
}

public enum EmbeddingModel: String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation

struct CreateImageEndpoint: Endpoint {
private let model: OpenAIImageModelType
private let prompt: String
private let numberOfImages: Int
private let size: String
Expand All @@ -11,16 +12,19 @@ struct CreateImageEndpoint: Endpoint {

var path: String = "images/generations"

init(prompt: String,
init(model: OpenAIImageModelType,
prompt: String,
numberOfImages: Int,
size: ImageSize) {
self.model = model
self.prompt = prompt
self.numberOfImages = numberOfImages
self.size = size.rawValue
}

var parameters: [String: Any]? {
["prompt": self.prompt as Any,
["model": self.model.name as Any,
"prompt": self.prompt as Any,
"n": self.numberOfImages as Any,
"size": self.size as Any]
}
Expand Down

This file was deleted.

16 changes: 5 additions & 11 deletions Sources/SwiftOpenAI/OpenAI/OpenAIEndpoints/OpenAIEndpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ enum OpenAIEndpoints {
messages: [MessageChatGPT],
optionalParameters: ChatCompletionsOptionalParameters?)

case edits(model: OpenAIModelType,
input: String,
instruction: String)

case createImage(prompt: String,
case createImage(model: OpenAIImageModelType,
prompt: String,
numberOfImages: Int,
size: ImageSize)

Expand All @@ -33,12 +30,9 @@ enum OpenAIEndpoints {
return ChatCompletionsEndpoint(model: model,
messages: messages,
optionalParameters: optionalParameters)
case .edits(model: let model, input: let input, instruction: let instruction):
return EditsEndpoint(model: model,
input: input,
instruction: instruction)
case .createImage(prompt: let prompt, numberOfImages: let numberOfImages, size: let size):
return CreateImageEndpoint(prompt: prompt,
case .createImage(model: let model, prompt: let prompt, numberOfImages: let numberOfImages, size: let size):
return CreateImageEndpoint(model: model,
prompt: prompt,
numberOfImages: numberOfImages,
size: size)
case .embeddings(model: let model, input: let input):
Expand Down
43 changes: 0 additions & 43 deletions Sources/SwiftOpenAI/OpenAI/Requests/Edits/EditsRequest.swift

This file was deleted.

Loading

0 comments on commit 6d15a5c

Please sign in to comment.