Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add configurable endpoint option #222

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,32 @@ final public class OpenAI: OpenAIProtocol {
public let organizationIdentifier: String?

/// API host. Set this property if you use some kind of proxy or your own server. Default is api.openai.com
public let host: String
public let port: Int
public let scheme: String
public let endpoint : URL
var scheme : String {endpoint.scheme ?? "https"}
var host : String {endpoint.host ?? "api.openai.com"}
var port : Int {endpoint.port ?? 443}

/// Default request timeout
public let timeoutInterval: TimeInterval

public init(token: String, organizationIdentifier: String? = nil, host: String = "api.openai.com", port: Int = 443, scheme: String = "https", timeoutInterval: TimeInterval = 60.0) {
self.token = token
self.organizationIdentifier = organizationIdentifier
self.host = host
self.port = port
self.scheme = scheme

var components = URLComponents()
components.scheme = scheme
components.host = host
components.port = port
components.path = "/v1"

self.endpoint = components.url!
self.timeoutInterval = timeoutInterval
}

public init(token: String, organizationIdentifier: String? = nil, endpoint: URL, timeoutInterval: TimeInterval = 60.0) {
self.token = token
self.organizationIdentifier = organizationIdentifier
self.endpoint = endpoint
self.timeoutInterval = timeoutInterval
}
}
Expand Down Expand Up @@ -198,32 +212,27 @@ extension OpenAI {
extension OpenAI {

func buildURL(path: String) -> URL {
var components = URLComponents()
components.scheme = configuration.scheme
components.host = configuration.host
components.port = configuration.port
components.path = path
return components.url!
return configuration.endpoint.appendingPathComponent(path)
}
}

typealias APIPath = String
extension APIPath {

static let completions = "/v1/completions"
static let embeddings = "/v1/embeddings"
static let chats = "/v1/chat/completions"
static let edits = "/v1/edits"
static let models = "/v1/models"
static let moderations = "/v1/moderations"
static let completions = "/completions"
static let embeddings = "/embeddings"
static let chats = "/chat/completions"
static let edits = "/edits"
static let models = "/models"
static let moderations = "/moderations"

static let audioSpeech = "/v1/audio/speech"
static let audioTranscriptions = "/v1/audio/transcriptions"
static let audioTranslations = "/v1/audio/translations"
static let audioSpeech = "/audio/speech"
static let audioTranscriptions = "/audio/transcriptions"
static let audioTranslations = "/audio/translations"

static let images = "/v1/images/generations"
static let imageEdits = "/v1/images/edits"
static let imageVariations = "/v1/images/variations"
static let images = "/images/generations"
static let imageEdits = "/images/edits"
static let imageVariations = "/images/variations"

func withPath(_ path: String) -> String {
self + "/" + path
Expand Down
7 changes: 7 additions & 0 deletions Tests/OpenAITests/OpenAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ class OpenAITests: XCTestCase {
let chatsURL = openAI.buildURL(path: .chats)
XCTAssertEqual(chatsURL, URL(string: "https://my.host.com:443/v1/chat/completions"))
}

func testCustomEndpointURLBuilt() {
let configuration = OpenAI.Configuration(token: "foo", organizationIdentifier: "bar", endpoint: URL(string: "https://api.internal.company.com:443/prod/aiendpoint/v1")!, timeoutInterval: 14)
let openAI = OpenAI(configuration: configuration, session: self.urlSession)
let chatsURL = openAI.buildURL(path: .chats)
XCTAssertEqual(chatsURL, URL(string: "https://api.internal.company.com:443/prod/aiendpoint/v1/chat/completions"))
}
}

@available(tvOS 13.0, *)
Expand Down