Skip to content

Enable basePath when proxy openai with own host with base path #68

@metrue

Description

@metrue

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions