Skip to content
This repository was archived by the owner on Jun 6, 2024. It is now read-only.
This repository was archived by the owner on Jun 6, 2024. It is now read-only.

Support for changing BASE_PATH #403

Open

Description

@chitalian

Can we change the constructor to allow a different base path for OpenAI? I want to be able to integrate tools like Helicone or LemonFox

Activity

  1. dhallatmaine commented on Nov 21, 2023

    @dhallatmaine
  2. ffrankan commented on Nov 30, 2023

    @ffrankan

    After going through multiple issues, I am still unclear about why the developers seem hesitant in supporting changes to the baseUrl. Is it because the repository is named "openai-java"? I am a bit confused and would appreciate some clarification.

  3. Dessix commented on Nov 30, 2023

    @Dessix

    If they won't change it, change it for them, like we did:

      class SlashInterceptor(trueBase: String) extends Interceptor:
        override def intercept(chain: Interceptor.Chain): Response =
          val originalRequest: Request = chain.request()
          val originalUrl: HttpUrl = originalRequest.url()
    
          // Check if the URL starts with an incorrect base
          if !originalUrl.encodedPath().startsWith(trueBase) then
            // Replace the mismatched leading section
            val newUrl: HttpUrl = originalUrl
              .newBuilder()
              .encodedPath(trueBase + "/" + originalUrl.encodedPath().substring(1))
              .build()
    
            // Create a new request with the modified URL
            val newRequest: Request = originalRequest
              .newBuilder()
              .url(newUrl)
              .build()
    
            // Proceed with the modified request
            chain.proceed(newRequest)
          else
            // Proceed with the original request
            chain.proceed(originalRequest)

    Usage:

      def buildAIService: OpenAiService =
        import okhttp3.{Interceptor, OkHttpClient, Request, Response}
        val mapper: ObjectMapper = OpenAiService.defaultObjectMapper();
        val client: OkHttpClient = OpenAiService
          .defaultClient("FAKETOKEN", Duration.ofSeconds(240))
          .newBuilder()
          .addInterceptor(new SlashInterceptor("/v2"))
          .build()
        val retrofit: Retrofit = OpenAiService
          .defaultRetrofit(client, mapper)
          .newBuilder()
          .baseUrl("http://myhost.local:3000/v2"))
          .build()
    
        val api: OpenAiApi = retrofit.create(classOf[OpenAiApi]);
        new OpenAiService(api)

    The above interceptor example can be trivially modified to change the .host(...) on the newRequest builder if you wish to alter the host section of the URI instead of simply prepending to the path. This is generally unnecessary, as the host redirection can occur at the retrofit level, as shown in the above Usage example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions