Class DefaultOpenAIHttpExecutor<I extends Streamable,​O extends Mergeable<O>>

    • Field Detail

      • client

        protected final okhttp3.OkHttpClient client
      • baseUrl

        protected final String baseUrl
      • objectMapper

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

        protected final String resourceUri
      • streamable

        protected final boolean streamable
    • 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 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 Detail

      • 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)
      • 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() && 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()
      • toJson

        protected String toJson​(I request)
      • prepareRequest

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

        protected O toResponse​(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)