Package com.hw.openai
Class OpenAiClient
java.lang.Object
com.hw.openai.OpenAiClient
- All Implemented Interfaces:
Closeable
,AutoCloseable
Represents a client for interacting with the OpenAI API.
- Author:
- HamaWhite
-
Field Summary
Modifier and TypeFieldDescriptionprotected long
Timeout for requests to OpenAI completion API. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionchatCompletion
(ChatCompletion chatCompletion) Creates a model response for the given chat conversation.void
close()
Closes the HttpClient connection pool.completion
(Completion completion) Creates a completion for the provided prompt and parameters.createChatCompletion
(ChatCompletion chatCompletion) Creates a model response for the given chat conversation.createCompletion
(Completion completion) Creates a completion for the provided prompt and parameters.createEmbedding
(Embedding embedding) Creates an embedding vector representing the input text.createImage
(CreateImageReq request) Creates an image given a prompt.static com.fasterxml.jackson.databind.ObjectMapper
<T> T
execute
(io.reactivex.Single<T> apiCall) Calls the Open AI api, returns the response, and parses error messages if the request failsinit()
Initializes the OpenAiClient instance.Lists the currently available models, and provides basic information about each one such as the owner and availability.retrieveModel
(String model) Retrieves a model instance, providing basic information about the model such as the owner and permissions.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.io.reactivex.Flowable<ChatCompletionChunk>
streamChatCompletion
(ChatCompletion chatCompletion) Creates a stream response for the given chat conversation.io.reactivex.Flowable<CompletionChunk>
streamCompletion
(Completion completion) Creates a stream completion for the provided prompt and parameters.
-
Field Details
-
requestTimeout
protected long requestTimeoutTimeout for requests to OpenAI completion API. Default is 16 seconds.
-
-
Constructor Details
-
OpenAiClient
public OpenAiClient()
-
-
Method Details
-
init
Initializes the OpenAiClient instance.- Returns:
- the initialized OpenAiClient instance
-
defaultObjectMapper
public static com.fasterxml.jackson.databind.ObjectMapper defaultObjectMapper() -
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
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
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
Creates a completion for the provided prompt and parameters.- Parameters:
completion
- the completion object containing the prompt and parameters- Returns:
- the completion response
-
streamCompletion
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
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
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
Creates an embedding vector representing the input text.- Parameters:
embedding
- The Embedding object containing the input text.- Returns:
- The embedding vector response.
-
createImage
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 callemitDone
- 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-