Class DefaultOpenAIHttpExecutor<I extends Streamable,O extends Mergeable<O>>
- All Implemented Interfaces:
OpenAIHttpExecutor<I,O>
- Direct Known Subclasses:
ChatHttpExecutor,CreateImageHttpExecutor,EditImageHttpExecutor,ImageVariationHttpExecutor,SpeechHttpExecutor,TranscriptionHttpExecutor,TranslationHttpExecutor,VisionHttpExecutor
Common implementation that should work out of the box when inherited with the correct input and output models.
-
Nested Class Summary
Nested classes/interfaces inherited from interface bg.codexio.ai.openai.api.http.OpenAIHttpExecutor
OpenAIHttpExecutor.ReactiveExecution<O> -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDefaultOpenAIHttpExecutor(HttpExecutorContext context, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<O> responseType, String resourceUri, boolean streamable, Class<? extends DefaultOpenAIHttpExecutor<I, O>> currentType) protectedDefaultOpenAIHttpExecutor(okhttp3.OkHttpClient client, String baseUrl, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<O> responseType, String resourceUri, boolean streamable, Class<? extends DefaultOpenAIHttpExecutor<I, O>> currentType) protectedDefaultOpenAIHttpExecutor(okhttp3.OkHttpClient client, String baseUrl, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<O> responseType, String resourceUri, boolean streamable, org.slf4j.Logger log) -
Method Summary
Modifier and TypeMethodDescriptionbooleanDenotes whether the response can be streamed, mostly taking into account the value ofStreamable.stream()which usually is implemented like inputModel -> inputModel.stream() invalid input: '&'invalid input: '&' specificApiLogic.protected voidprotected StringconvertFormDataFieldName(String originalName) Executes HTTP request synchronouslyvoidExecutes HTTP request asynchronously.executeReactive(I request) Executes HTTP request in reactive fashion.protected Stringprotected static <T> Tprotected Stringprotected voidhydrateFormData(okhttp3.MultipartBody.Builder builder, Object obj, Set<Object> visited) protected voidprotected okhttp3.RequestprepareRequest(I request) protected voidprotected voidsetFormDataMimeType(String mimeType) protected voidsetMultipartBoundary(String boundary) protected ErrorResponseHolderprotected okhttp3.MultipartBodytoFormData(Object obj) protected Stringprotected OtoResponse(String response) protected OtoResponse(okhttp3.Response response)
-
Field Details
-
client
protected final okhttp3.OkHttpClient client -
baseUrl
-
objectMapper
protected final com.fasterxml.jackson.databind.ObjectMapper objectMapper -
responseType
-
resourceUri
-
streamable
protected final boolean streamable
-
-
Constructor Details
-
DefaultOpenAIHttpExecutor
protected DefaultOpenAIHttpExecutor(HttpExecutorContext context, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<O> responseType, String resourceUri, boolean streamable, Class<? extends DefaultOpenAIHttpExecutor<I, O>> currentType) - Parameters:
context- timeouts and other HTTP settingsobjectMapper- configuredObjectMapperresponseType- the type of the response objectresourceUri- the main prefix for the resource, e.g. /imagesstreamable- whether the whole API supports streamingcurrentType- the type of the inheritor to initialize logger with
-
DefaultOpenAIHttpExecutor
protected DefaultOpenAIHttpExecutor(okhttp3.OkHttpClient client, String baseUrl, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<O> responseType, String resourceUri, boolean streamable, Class<? extends DefaultOpenAIHttpExecutor<I, O>> currentType) - Parameters:
client- already configuredOkHttpClientclientbaseUrl- the base url such as https://api.openai.com/v1/objectMapper- configuredObjectMapperresponseType- the type of the response objectresourceUri- the main prefix for the resource, e.g. /imagesstreamable- whether the whole API supports streamingcurrentType- the type of the inheritor to initialize logger with
-
DefaultOpenAIHttpExecutor
protected DefaultOpenAIHttpExecutor(okhttp3.OkHttpClient client, String baseUrl, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<O> responseType, String resourceUri, boolean streamable, org.slf4j.Logger log) - Parameters:
client- already configuredOkHttpClientclientbaseUrl- the base url such as https://api.openai.com/v1/objectMapper- configuredObjectMapperresponseType- the type of the response objectresourceUri- the main prefix for the resource, e.g. /imagesstreamable- whether the whole API supports streaminglog-Loggerinstance configured for the particular inheritor
-
-
Method Details
-
getField
-
execute
Executes HTTP request synchronously- Specified by:
executein interfaceOpenAIHttpExecutor<I extends Streamable,O extends Mergeable<O>> - Parameters:
request- The request (Input) model- Returns:
- deserialized HTTP Response in the Output model
-
executeAsync
Executes HTTP request asynchronously. Since response can be streamed, it can be potentially beneficial for the developer, to subscribe to each line, hence the callback parameter.
It makes a little bit more sense to subscribe, to the whole response, using the finalizer parameter.
- Specified by:
executeAsyncin interfaceOpenAIHttpExecutor<I extends Streamable,O extends Mergeable<O>> - Parameters:
request- The request (Input) modelcallBack- A callback of type stringLine -> consume(stringLine)finalizer- A callback of type outputModel -> consume(outputModel)
-
executeReactive
Executes HTTP request in reactive fashion. We strongly recommend to use this only if a real reactive runtime is present, such as Reactor netty.
- Specified by:
executeReactivein interfaceOpenAIHttpExecutor<I extends Streamable,O extends Mergeable<O>> - Parameters:
request- The request (Input) model- Returns:
OpenAIHttpExecutor.ReactiveExecution<O extends Mergeable<O>>object holding a single observable (reactor.core.publisher.Mono<O extends Mergeable<O>>) to the whole response, and a multi-emit observable (reactor.core.publisher.Flux<String>) to each response line.
-
canStream
Denotes whether the response can be streamed, mostly taking into account the value of
Streamable.stream()which usually is implemented like inputModel -> inputModel.stream() invalid input: '&'invalid input: '&' specificApiLogic.- Specified by:
canStreamin interfaceOpenAIHttpExecutor<I extends Streamable,O extends Mergeable<O>> - Parameters:
input- The request (Input) model- Returns:
- boolean saying if you can stream the response or not.
Usually beneficial for internal calls like
OpenAIHttpExecutor.execute(Streamable)
-
configureObjectMapper
protected void configureObjectMapper() -
getEnv
-
toJson
-
prepareRequest
-
toResponse
- Throws:
IOException
-
toResponse
-
toError
-
log
-
reinitializeExecutionIdentification
protected void reinitializeExecutionIdentification() -
toFormData
-
hydrateFormData
-
getFormDataMimeType
-
setFormDataMimeType
-
setMultipartBoundary
-
convertFormDataFieldName
-