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.

  • Field Details

    • client

      protected final okhttp3.OkHttpClient client
    • baseUrl

      protected final String baseUrl
    • objectMapper

      protected final com.fasterxml.jackson.databind.ObjectMapper objectMapper
    • responseType

      protected final Class<O extends Mergeable<O>> responseType
    • resourceUri

      protected final String 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 settings
      objectMapper - configured ObjectMapper
      responseType - the type of the response object
      resourceUri - the main prefix for the resource, e.g. /images
      streamable - whether the whole API supports streaming
      currentType - 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 configured OkHttpClient client
      baseUrl - the base url such as https://api.openai.com/v1/
      objectMapper - configured ObjectMapper
      responseType - the type of the response object
      resourceUri - the main prefix for the resource, e.g. /images
      streamable - whether the whole API supports streaming
      currentType - 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 configured OkHttpClient client
      baseUrl - the base url such as https://api.openai.com/v1/
      objectMapper - configured ObjectMapper
      responseType - the type of the response object
      resourceUri - the main prefix for the resource, e.g. /images
      streamable - whether the whole API supports streaming
      log - Logger instance configured for the particular inheritor
  • Method Details

    • getField

      protected static <T> T getField(Field field, Object obj, Class<T> type)
    • execute

      public O execute(I request)
      Executes HTTP request synchronously
      Specified by:
      execute in interface OpenAIHttpExecutor<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:
      executeAsync in interface OpenAIHttpExecutor<I extends Streamable,O extends Mergeable<O>>
      Parameters:
      request - The request (Input) model
      callBack - 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:
      executeReactive in interface OpenAIHttpExecutor<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

      public boolean canStream(I input)

      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:
      canStream in interface OpenAIHttpExecutor<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

      protected String getEnv(String key)
    • toJson

      protected String toJson(I request)
    • prepareRequest

      @NotNull protected okhttp3.Request prepareRequest(I request)
    • toResponse

      protected O toResponse(okhttp3.Response response) throws IOException
      Throws:
      IOException
    • toResponse

      protected O toResponse(String response)
    • toError

      protected ErrorResponseHolder toError(String response)
    • log

      protected void log(String message, Object... args)
    • 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)
    • convertFormDataFieldName

      protected String convertFormDataFieldName(String originalName)