Package com.hw.openai

Class OpenAiClient

java.lang.Object
com.hw.openai.OpenAiClient
All Implemented Interfaces:
Closeable, AutoCloseable

public class OpenAiClient extends Object implements Closeable
Represents a client for interacting with the OpenAI API.
Author:
HamaWhite
  • Field Details

    • requestTimeout

      protected long requestTimeout
      Timeout for requests to OpenAI completion API. Default is 16 seconds.
  • Constructor Details

    • OpenAiClient

      public OpenAiClient()
  • Method Details

    • init

      public OpenAiClient init()
      Initializes the OpenAiClient instance.
      Returns:
      the initialized OpenAiClient instance
    • defaultObjectMapper

      public static com.fasterxml.jackson.databind.ObjectMapper defaultObjectMapper()
    • listModels

      public ModelResp listModels()
      Lists the currently available models, and provides basic information about each one such as the owner and availability.
      Returns:
      the response containing the list of available models
    • retrieveModel

      public Model retrieveModel(String model)
      Retrieves a model instance, providing basic information about the model such as the owner and permissions.
      Parameters:
      model - the ID of the model to retrieve
      Returns:
      the retrieved model
    • completion

      public String completion(Completion completion)
      Creates a completion for the provided prompt and parameters.
      Parameters:
      completion - the completion object containing the prompt and parameters
      Returns:
      the generated completion text
    • createCompletion

      public CompletionResp createCompletion(Completion completion)
      Creates a completion for the provided prompt and parameters.
      Parameters:
      completion - the completion object containing the prompt and parameters
      Returns:
      the completion response
    • streamCompletion

      public io.reactivex.Flowable<CompletionChunk> streamCompletion(Completion completion)
      Creates a stream completion for the provided prompt and parameters.
      Parameters:
      completion - the completion request object containing the prompt and parameters
      Returns:
      a stream of generated completions
    • chatCompletion

      public String chatCompletion(ChatCompletion chatCompletion)
      Creates a model response for the given chat conversation.
      Parameters:
      chatCompletion - the chat completion object containing the conversation
      Returns:
      the generated model response text
    • createChatCompletion

      public ChatCompletionResp createChatCompletion(ChatCompletion chatCompletion)
      Creates a model response for the given chat conversation.
      Parameters:
      chatCompletion - the chat completion object containing the conversation
      Returns:
      the chat completion response
    • streamChatCompletion

      public io.reactivex.Flowable<ChatCompletionChunk> streamChatCompletion(ChatCompletion chatCompletion)
      Creates a stream response for the given chat conversation.
      Parameters:
      chatCompletion - the chat completion request object
      Returns:
      a stream of generated chat completions
    • createEmbedding

      public EmbeddingResp createEmbedding(Embedding embedding)
      Creates an embedding vector representing the input text.
      Parameters:
      embedding - The Embedding object containing the input text.
      Returns:
      The embedding vector response.
    • createImage

      public ImageResp createImage(CreateImageReq request)
      Creates an image given a prompt.
      Parameters:
      request - the create image request
      Returns:
      the image result response
    • stream

      public io.reactivex.Flowable<SSE> stream(retrofit2.Call<okhttp3.ResponseBody> apiCall, boolean emitDone)
      Calls the Open AI api and returns a Flowable of SSE for streaming.
      Parameters:
      apiCall - The api call
      emitDone - If true the last message ([DONE]) is emitted
    • execute

      public <T> T execute(io.reactivex.Single<T> apiCall)
      Calls the Open AI api, returns the response, and parses error messages if the request fails
    • close

      public void close()
      Closes the HttpClient connection pool.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable