Support for changing BASE_PATH
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
https://github.com/TheoKanning/openai-java/pull/418
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.
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.