Package bg.codexio.ai.openai.api.http
Class DefaultOpenAIHttpExecutor<I extends Streamable,O extends Mergeable<O>>
- java.lang.Object
-
- bg.codexio.ai.openai.api.http.DefaultOpenAIHttpExecutor<I,O>
-
- All Implemented Interfaces:
OpenAIHttpExecutor<I,O>
- Direct Known Subclasses:
ChatHttpExecutor,CreateImageHttpExecutor,EditImageHttpExecutor,ImageVariationHttpExecutor,SpeechHttpExecutor,TranscriptionHttpExecutor,TranslationHttpExecutor,VisionHttpExecutor
public abstract class DefaultOpenAIHttpExecutor<I extends Streamable,O extends Mergeable<O>> extends Object implements OpenAIHttpExecutor<I,O>
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 Modifier and Type Field Description protected StringbaseUrlprotected okhttp3.OkHttpClientclientprotected com.fasterxml.jackson.databind.ObjectMapperobjectMapperprotected StringresourceUriprotected Class<O>responseTypeprotected booleanstreamable
-
Constructor Summary
Constructors Modifier Constructor Description protectedDefaultOpenAIHttpExecutor(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanStream(I input)Denotes whether the response can be streamed, mostly taking into account the value ofStreamable.stream()which usually is implemented like inputModel -> inputModel.stream() && specificApiLogic.protected voidconfigureObjectMapper()protected StringconvertFormDataFieldName(String originalName)Oexecute(I request)Executes HTTP request synchronouslyvoidexecuteAsync(I request, Consumer<String> callBack, Consumer<O> finalizer)Executes HTTP request asynchronously.OpenAIHttpExecutor.ReactiveExecution<O>executeReactive(I request)Executes HTTP request in reactive fashion.protected StringgetEnv(String key)protected static <T> TgetField(Field field, Object obj, Class<T> type)protected StringgetFormDataMimeType()protected voidhydrateFormData(okhttp3.MultipartBody.Builder builder, Object obj, Set<Object> visited)protected voidlog(String message, Object... args)protected okhttp3.RequestprepareRequest(I request)protected voidreinitializeExecutionIdentification()protected voidsetFormDataMimeType(String mimeType)protected voidsetMultipartBoundary(String boundary)protected ErrorResponseHoldertoError(String response)protected okhttp3.MultipartBodytoFormData(Object obj)protected StringtoJson(I request)protected OtoResponse(String response)protected OtoResponse(okhttp3.Response response)
-
-
-
Constructor Detail
-
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 Detail
-
execute
public O execute(I request)
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
public void executeAsync(I request, Consumer<String> callBack, Consumer<O> finalizer)
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
public OpenAIHttpExecutor.ReactiveExecution<O> executeReactive(I request)
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.ReactiveExecutionobject holding a single observable (Mono) to the whole response, and a multi-emit observable (Flux) to each response line.
-
canStream
public boolean canStream(I input)
Denotes whether the response can be streamed, mostly taking into account the value ofStreamable.stream()which usually is implemented like inputModel -> inputModel.stream() && 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()
-
prepareRequest
@NotNull protected okhttp3.Request prepareRequest(I request)
-
toResponse
protected O toResponse(okhttp3.Response response) throws IOException
- Throws:
IOException
-
toError
protected ErrorResponseHolder toError(String response)
-
reinitializeExecutionIdentification
protected void reinitializeExecutionIdentification()
-
toFormData
protected okhttp3.MultipartBody toFormData(Object obj)
-
hydrateFormData
protected void hydrateFormData(okhttp3.MultipartBody.Builder builder, Object obj, Set<Object> visited)
-
getFormDataMimeType
protected String getFormDataMimeType()
-
setFormDataMimeType
protected void setFormDataMimeType(String mimeType)
-
setMultipartBoundary
protected void setMultipartBoundary(String boundary)
-
-