Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
23 changes: 20 additions & 3 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ final public class OpenAI: OpenAIProtocol {

/// 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

/// Optional base path if you set up OpenAI API proxy on a custom path on your own host. Default is ""
public let basePath: String

public let port: Int
public let scheme: String

/// 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) {
public init(token: String, organizationIdentifier: String? = nil, host: String = "api.openai.com", port: Int = 443, scheme: String = "https", basePath: String = "", timeoutInterval: TimeInterval = 60.0) {
self.token = token
self.organizationIdentifier = organizationIdentifier
self.host = host
self.port = port
self.scheme = scheme
self.basePath = basePath
self.timeoutInterval = timeoutInterval
}
}
Expand Down Expand Up @@ -202,8 +208,19 @@ extension OpenAI {
components.scheme = configuration.scheme
components.host = configuration.host
components.port = configuration.port
components.path = path
return components.url!

components.path = NSString.path(withComponents: [
"/", configuration.basePath, path
])

if let url = components.url {
return url
} else {
// We're expecting components.url to be not nil
// But if it isn't, let's just use some URL api that returns non-nil url
// Let all requests fail, but so that we don't crash on explicit unwrapping
return URL(fileURLWithPath: "")
}
}
}

Expand Down
37 changes: 36 additions & 1 deletion Tests/OpenAITests/OpenAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,47 @@ class OpenAITests: XCTestCase {
XCTAssertEqual(chatsURL, URL(string: "https://api.openai.com:443/v1/chat/completions"))
}

func testCustomURLBuilt() {
func testCustomURLBuiltWithPredefinedPath() {
let configuration = OpenAI.Configuration(token: "foo", organizationIdentifier: "bar", host: "my.host.com", timeoutInterval: 14)
let openAI = OpenAI(configuration: configuration, session: self.urlSession)
let chatsURL = openAI.buildURL(path: .chats)
XCTAssertEqual(chatsURL, URL(string: "https://my.host.com:443/v1/chat/completions"))
}

func testCustomURLBuiltWithCustomPath() {
let configuration = OpenAI.Configuration(
token: "foo",
organizationIdentifier: "bar",
host: "bizbaz.com",
timeoutInterval: 14
)
let openAI = OpenAI(configuration: configuration, session: URLSessionMock())
XCTAssertEqual(openAI.buildURL(path: "foo"), URL(string: "https://bizbaz.com:443/foo"))
}

func testCustomURLBuiltWithCustomBasePath() {
let configuration = OpenAI.Configuration(
token: "foo",
organizationIdentifier: "bar",
host: "bizbaz.com",
basePath: "/openai",
timeoutInterval: 14
)
let openAI = OpenAI(configuration: configuration, session: URLSessionMock())
XCTAssertEqual(openAI.buildURL(path: "foo"), URL(string:"https://bizbaz.com:443/openai/foo"))
}

func testCustomURLBuiltWithCustomBasePathWithTrailingSlash() {
let configuration = OpenAI.Configuration(
token: "foo",
organizationIdentifier: "bar",
host: "bizbaz.com",
basePath: "/openai/",
timeoutInterval: 14
)
let openAI = OpenAI(configuration: configuration, session: URLSessionMock())
XCTAssertEqual(openAI.buildURL(path: "/foo"), URL(string: "https://bizbaz.com:443/openai/foo"))
}
}

@available(tvOS 13.0, *)
Expand Down
Loading