-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
354 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// SwiftyGPTChatRole.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 11/04/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum SwiftyGPTChatRole: String, Codable { | ||
case system | ||
case user | ||
case assistant | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// SwiftyGPT+Completion.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 11/04/23. | ||
// | ||
|
||
import Foundation | ||
import SwiftyHTTP | ||
import SwiftyRanged | ||
|
||
// MARK: - Completion | ||
extension SwiftyGPT { | ||
|
||
public func completion(prompt: String, model: SwiftyGPTCompletionModel = .stable, suffix: String? = nil, @SwiftyOptionalRanged(0...4096) maxTokens: Int? = nil, @SwiftyOptionalRanged(0...2) temperature: Float? = nil, choices: Int? = nil, @SwiftyOptionalRanged(1...5) logprobs: Int? = nil, echo: Bool? = nil, @SwiftyOptionalRanged(-2...2) presencePenalty: Float? = nil, @SwiftyOptionalRanged(-2...2) frequencyPenalty: Float? = nil, user: String? = nil, completion: @escaping (Result<SwiftyGPTCompletionResponse, Error>) -> ()) { | ||
|
||
let request = SwiftyGPTCompletionRequest(prompt: prompt, model: model, suffix: suffix, maxTokens: maxTokens, temperature: temperature, choices: choices, stream: false, logprobs: logprobs, echo: echo, presencePenalty: presencePenalty, frequencyPenalty: frequencyPenalty, user: user) | ||
SwiftyHTTP.request(SwiftyGPTRouter.completion(apiKey, request)) { result in | ||
switch result { | ||
case .success(let response): | ||
if response.statusCode == 200 { | ||
guard let body = try? JSONDecoder().decode(SwiftyGPTCompletionResponse.self, from: response.body) else { | ||
completion(.failure(URLError(.badServerResponse))) | ||
return | ||
} | ||
|
||
let formattedBody = SwiftyGPTCompletionResponse(id: body.id, object: body.object, created: body.created, usage: body.usage, model: body.model, choices: body.choices.map { SwiftyGPTCompletionChoice(finishReason: $0.finishReason, index: $0.index, text: $0.text.trimmingCharacters(in: .whitespacesAndNewlines), logprobs: $0.logprobs)}) | ||
|
||
completion(.success(formattedBody)) | ||
} else { | ||
guard let error = try? JSONDecoder().decode(SwiftyGPTError.self, from: response.body) else { | ||
completion(.failure(URLError(.badServerResponse))) | ||
return | ||
} | ||
completion(.failure(error)) | ||
} | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
|
||
public func completion(prompt: String, model: SwiftyGPTCompletionModel = .stable, suffix: String? = nil, @SwiftyOptionalRanged(0...4096) maxTokens: Int? = nil, @SwiftyOptionalRanged(0...2) temperature: Float? = nil, choices: Int? = nil, @SwiftyOptionalRanged(1...5) logprobs: Int? = nil, echo: Bool? = nil, @SwiftyOptionalRanged(-2...2) presencePenalty: Float? = nil, @SwiftyOptionalRanged(-2...2) frequencyPenalty: Float? = nil, user: String? = nil) async -> Result<SwiftyGPTCompletionResponse, Error> { | ||
|
||
return await withCheckedContinuation { continuation in | ||
completion(prompt: prompt, model: model, suffix: suffix, maxTokens: maxTokens, temperature: temperature, choices: choices, logprobs: logprobs, echo: echo, presencePenalty: presencePenalty, frequencyPenalty: frequencyPenalty, user: user) { result in | ||
continuation.resume(returning: result) | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Sources/SwiftyGPT/Completion/SwiftyGPTCompletionModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// SwiftyGPTCompletionModel.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 11/04/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum SwiftyGPTCompletionModel: String, SwiftyGPTModel { | ||
|
||
case ada | ||
case text_ada_001 = "text-ada-001" | ||
|
||
case babbage | ||
case text_babbage_001 = "text-babbage-001" | ||
|
||
case curie | ||
case text_curie_001 = "text-curie-001" | ||
|
||
case davinci | ||
case text_davinci_002 = "text-davinci-002" | ||
case text_davinci_003 = "text-davinci-003" | ||
|
||
public static var stable: SwiftyGPTCompletionModel { | ||
.text_davinci_003 | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Sources/SwiftyGPT/Completion/SwiftyGPTCompletionRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// SwiftyGPTCompletionRequest.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 11/04/23. | ||
// | ||
|
||
import Foundation | ||
import SwiftyHTTP | ||
|
||
public struct SwiftyGPTCompletionRequest: SwiftyGPTRequest { | ||
public let prompt: String | ||
public let model: SwiftyGPTCompletionModel | ||
public let suffix: String? | ||
public let maxTokens: Int? | ||
public let temperature: Float? | ||
public let choices: Int? | ||
public let stream: Bool? | ||
public let logprobs: Int? | ||
public let echo: Bool? | ||
public let presencePenalty: Float? | ||
public let frequencyPenalty: Float? | ||
public let user: String? | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case prompt | ||
case model | ||
case suffix | ||
case maxTokens = "max_tokens" | ||
case temperature | ||
case choices = "n" | ||
case stream | ||
case logprobs | ||
case echo | ||
case presencePenalty = "presence_penalty" | ||
case frequencyPenalty = "frequency_penalty" | ||
case user | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Sources/SwiftyGPT/Completion/SwiftyGPTCompletionResponse.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// SwiftyGPTCompletionResponse.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 11/04/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct SwiftyGPTCompletionResponse: SwiftyGPTResponse { | ||
public let id: String | ||
public let object: String | ||
public let created: TimeInterval | ||
public let usage: SwiftyGPTUsage | ||
public let model: SwiftyGPTCompletionModel | ||
public let choices: [SwiftyGPTCompletionChoice] | ||
} | ||
|
||
// MARK: - Choice | ||
public struct SwiftyGPTCompletionChoice: SwiftyGPTChoice { | ||
public let finishReason: SwiftyGPTFinishReason | ||
public let index: Int | ||
public let text: String | ||
public let logprobs: Int? | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case text | ||
case finishReason = "finish_reason" | ||
case index | ||
case logprobs | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// SwiftyGPTModel.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 11/04/23. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol SwiftyGPTModel: Codable { | ||
static var stable: Self { get } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// SwiftyGPTRequest.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 11/04/23. | ||
// | ||
|
||
import Foundation | ||
import SwiftyHTTP | ||
|
||
protocol SwiftyGPTRequest: SwiftyHTTPRequestBody { | ||
associatedtype GPTModel: SwiftyGPTModel | ||
|
||
var model: GPTModel { get } | ||
var user: String? { get } | ||
var choices: Int? { get } | ||
var maxTokens: Int? { get } | ||
var temperature: Float? { get } | ||
var stream: Bool? { get } | ||
var presencePenalty: Float? { get } | ||
var frequencyPenalty: Float? { get } | ||
} |
Oops, something went wrong.