OpenAI icon indicating copy to clipboard operation
OpenAI copied to clipboard

Enable basePath when proxy openai with own host with base path

Open metrue opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe. Right, it's pretty nice we can init a OpenAI with custom host, like

 let openAI = OpenAI(configuration: OpenAI.Configuration(
        token: "sk-xxx",
        organizationIdentifier: "org-xxx",
        host: "api.myhost",
        timeoutInterval: 60.0)
    )

It would even move better enabling a basePath like this,

let openAI = OpenAI(configuration: OpenAI.Configuration(
        token: "sk-xxx",
        organizationIdentifier: "org-xxx",
        host: "api.myhost",
        basePath: "/openai/api",
        timeoutInterval: 60.0)
)

Since most of the time we have multiple endpoints for a host.

Describe the solution you'd like

It will be a simple changes on the current implementation actually.

 public init(
            token: String,
            organizationIdentifier: String? = nil,
            host: String = "api.openai.com",
            basePath: String? = "",
            timeoutInterval: TimeInterval = 60.0
        ) {
            self.token = token
            self.organizationIdentifier = organizationIdentifier
            self.host = host
            self.basePath = basePath ?? ""
            self.timeoutInterval = timeoutInterval
        }
....

extension OpenAI {
    
    func buildURL(path: String) -> URL {
        var components = URLComponents()
        components.scheme = "https"
        components.host = configuration.host
        components.path = self.configuration.basePath + path
        return components.url!
    }
}

If it's an acceptable changes, I can draft a PR.

Describe alternatives you've considered None.

Additional context None.

metrue avatar May 15 '23 15:05 metrue

It makes sense to me; would the basePath solution also solve #61?

SunburstEnzo avatar May 15 '23 17:05 SunburstEnzo

Cool, a PR drafted, please help review it.

metrue avatar May 16 '23 02:05 metrue

Hey @SunburstEnzo , could you help review it? thanks a lot.

metrue avatar May 29 '23 04:05 metrue