-
-
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
558 additions
and
149 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// SwiftyGPT+Chat.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 30/03/23. | ||
// | ||
|
||
import Foundation | ||
import SwiftyHTTP | ||
import SwiftyRanged | ||
|
||
// MARK: - Chat | ||
extension SwiftyGPT { | ||
|
||
public func chat(messages: [SwiftyGPTChatMessage], model: SwiftyGPTChatModel = .stable, @SwiftyOptionalRanged(0...2) temperature: Float? = nil, choices: Int? = nil, @SwiftyOptionalRanged(0...4096) maxTokens: Int? = nil, @SwiftyOptionalRanged(-2...2) presencePenalty: Float? = nil, @SwiftyOptionalRanged(-2...2) frequencyPenalty: Float? = nil, user: String? = nil, completion: @escaping (Result<SwiftyGPTChatResponse, Error>) -> ()) { | ||
|
||
let request = SwiftyGPTChatRequest(messages: messages, model: model, temperature: temperature, choices: choices, stream: false, maxTokens: maxTokens, presencePenalty: presencePenalty, frequencyPenalty: frequencyPenalty, user: user) | ||
SwiftyHTTP.request(with: SwiftyGPTRouter.chat(apiKey, request)) { result in | ||
switch result { | ||
case .success(let response): | ||
if response.statusCode == 200 { | ||
guard let body = try? JSONDecoder().decode(SwiftyGPTChatResponse.self, from: response.body) else { | ||
completion(.failure(URLError(.badServerResponse))) | ||
return | ||
} | ||
completion(.success(body)) | ||
} 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 chat(messages: [SwiftyGPTChatMessage], model: SwiftyGPTChatModel = .stable, @SwiftyOptionalRanged(0...2) temperature: Float? = nil, choices: Int? = nil, @SwiftyOptionalRanged(0...4096) maxTokens: Int? = nil, @SwiftyOptionalRanged(-2...2) presencePenalty: Float? = nil, @SwiftyOptionalRanged(-2...2) frequencyPenalty: Float? = nil, user: String? = nil) async -> Result<SwiftyGPTChatResponse, Error> { | ||
|
||
return await withCheckedContinuation { continuation in | ||
chat(messages: messages, model: model, temperature: temperature, choices: choices, maxTokens: maxTokens, presencePenalty: presencePenalty, frequencyPenalty: frequencyPenalty, user: user) { result in | ||
continuation.resume(returning: result) | ||
} | ||
} | ||
} | ||
|
||
public func chat(message: SwiftyGPTChatMessage, model: SwiftyGPTChatModel = .stable, @SwiftyOptionalRanged(0...2) temperature: Float? = nil, choices: Int? = nil, @SwiftyOptionalRanged(0...4096) maxTokens: Int? = nil, @SwiftyOptionalRanged(-2...2) presencePenalty: Float? = nil, @SwiftyOptionalRanged(-2...2) frequencyPenalty: Float? = nil, user: String? = nil, completion: @escaping (Result<SwiftyGPTChatResponse, Error>) -> ()) { | ||
chat(messages: [message], model: model, temperature: temperature, choices: choices, maxTokens: maxTokens, presencePenalty: presencePenalty, frequencyPenalty: frequencyPenalty, user: user, completion: completion) | ||
} | ||
|
||
public func chat(message: SwiftyGPTChatMessage, model: SwiftyGPTChatModel = .stable, @SwiftyOptionalRanged(0...2) temperature: Float? = nil, choices: Int? = nil, @SwiftyOptionalRanged(0...4096) maxTokens: Int? = nil, @SwiftyOptionalRanged(-2...2) presencePenalty: Float? = nil, @SwiftyOptionalRanged(-2...2) frequencyPenalty: Float? = nil, user: String? = nil) async -> Result<SwiftyGPTChatResponse, Error> { | ||
|
||
await chat(messages: [message], model: model, temperature: temperature, choices: choices, maxTokens: maxTokens, presencePenalty: presencePenalty, frequencyPenalty: frequencyPenalty, user: user) | ||
} | ||
|
||
public func chat(messages: [String], model: SwiftyGPTChatModel = .stable, user: String? = nil, completion: @escaping (Result<String, Error>) -> ()) { | ||
chat(messages: messages.map({SwiftyGPTChatMessage(content: $0)}), model: model, user: user) { result in | ||
switch result { | ||
case .success(let response): | ||
guard let message = response.choices.first?.message else { | ||
completion(.failure(URLError(.badServerResponse))) | ||
return | ||
} | ||
completion(.success(message.content)) | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
|
||
public func chat(messages: [String], model: SwiftyGPTChatModel = .stable, user: String? = nil) async -> Result<String, Error> { | ||
return await withCheckedContinuation { continuation in | ||
chat(messages: messages, model: model, user: user) { result in | ||
continuation.resume(returning: result) | ||
} | ||
} | ||
} | ||
|
||
public func chat(message: String, model: SwiftyGPTChatModel = .stable, user: String? = nil, completion: @escaping (Result<String, Error>) -> ()) { | ||
chat(messages: [message], model: model, user: user, completion: completion) | ||
} | ||
|
||
public func chat(message: String, model: SwiftyGPTChatModel = .stable, user: String? = nil) async -> Result<String, Error> { | ||
await chat(messages: [message], model: model, user: user) | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
Sources/SwiftyGPT/SwiftyGPTMessage.swift → ...SwiftyGPT/Chat/SwiftyGPTChatMessage.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
8 changes: 4 additions & 4 deletions
8
Sources/SwiftyGPT/SwiftyGPTModel.swift → ...s/SwiftyGPT/Chat/SwiftyGPTChatModel.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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
// | ||
// SwiftyGPTModel.swift | ||
// SwiftyGPTChatModel.swift | ||
// | ||
// | ||
// Created by Antonio Guerra on 27/03/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum SwiftyGPTModel: String, Codable { | ||
public enum SwiftyGPTChatModel: String, Codable { | ||
case gpt4 = "gpt-4" | ||
case gpt4_0314 = "gpt-4-0314" | ||
case gpt4_32k = "gpt-4-32k" | ||
case gpt4_32k_0314 = "gpt-4-32k-0314" | ||
case gpt3_5_turbo = "gpt-3.5-turbo" | ||
case gpt3_5_turbo_0301 = "gpt-3.5-turbo-0301" | ||
public static var stable: SwiftyGPTModel { | ||
|
||
public static var stable: SwiftyGPTChatModel { | ||
gpt3_5_turbo | ||
} | ||
} |
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
Oops, something went wrong.