openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Support for changing BASE_PATH

Open chitalian opened this issue 2 years ago • 3 comments

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

chitalian avatar Nov 15 '23 00:11 chitalian

https://github.com/TheoKanning/openai-java/pull/418

dhallatmaine avatar Nov 21 '23 05:11 dhallatmaine

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.

ffrankan avatar Nov 30 '23 02:11 ffrankan

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.

Dessix avatar Nov 30 '23 13:11 Dessix