Package xyz.felh.openai
Class OpenAiService
java.lang.Object
xyz.felh.openai.OpenAiService
OpenAi Service Class
-
Constructor Summary
ConstructorsConstructorDescriptionOpenAiService(String token) Creates a new OpenAiService that wraps OpenAiApiOpenAiService(String token, Duration timeout) Creates a new OpenAiService that wraps OpenAiApiOpenAiService(OpenAiApi api, okhttp3.OkHttpClient client) Creates a new OpenAiService that wraps OpenAiApi. -
Method Summary
Modifier and TypeMethodDescriptionstatic OpenAiApicancelFineTuningJob(String fineTuningJobId) Immediately cancel a fine-tune job.cancelThreadRun(String threadId, String runId) POST https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/cancelcreateAssistant(CreateAssistantRequest request) POST https://api.openai.com/v1/assistantscreateAssistantFile(String assistantId, CreateAssistantFileRequest request) POST https://api.openai.com/v1/assistants/{assistant_id}/fileswhisper-1whisper-1gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301 gpt-3.5-turbo-1006, gpt-4-1106-previewcreateEmbeddings(CreateEmbeddingRequest request) text-embedding-ada-002, text-search-ada-doc-001Creates a job that fine-tunes a specified model from a given dataset.createImage(CreateImageRequest request) createImageEdit(CreateEditRequest request) createModeration(CreateModerationRequest request) text-moderation-stable, text-moderation-latestbyte[]createSpeech(CreateSpeechRequest request) Generates audio from the input text.voidcreateSteamChatCompletion(String requestId, CreateChatCompletionRequest request, @NonNull StreamChatCompletionListener listener) create chat completion by streamcreateThread(CreateThreadRequest request) POST https://api.openai.com/v1/threadsPOST https://api.openai.com/v1/threads/runscreateThreadMessage(String threadId, CreateMessageRequest request) POST https://api.openai.com/v1/threads/{thread_id}/messagescreateThreadRun(String threadId, CreateRunRequest request) POST https://api.openai.com/v1/threads/{thread_id}/runsstatic okhttp3.OkHttpClientdefaultClient(String token, String orgId, Duration timeout) static okhttp3.OkHttpClientdefaultClient(String token, Duration timeout) static com.fasterxml.jackson.databind.ObjectMapperstatic retrofit2.RetrofitdefaultRetrofit(okhttp3.OkHttpClient client, com.fasterxml.jackson.databind.ObjectMapper mapper) deleteAssistant(String assistantId) DELETE https://api.openai.com/v1/assistants/{assistant_id}deleteAssistantFile(String assistantId, String fileId) DELETE https://api.openai.com/v1/assistants/{assistant_id}/files/{file_id}deleteFile(String fileId) deleteThread(String threadId) DELETE https://api.openai.com/v1/threads/{thread_id}static <T> Texecute(io.reactivex.rxjava3.core.Single<T> apiCall) Calls the Open AI api, returns the response, and parses error messages if the request failslistAssistantFiles(String assistantId) GET https://api.openai.com/v1/assistants/{assistant_id}/fileslistAssistants(Integer limit, String order, String after, String before) GET https://api.openai.com/v1/assistantslistFineTuningEvents(String fineTuningJobId, String after, Integer limit) Get status updates for a fine-tuning job.listThreadMessageFiles(String threadId, String messageId) listThreadMessageFiles(String threadId, String messageId, Integer limit, String order, String after, String before) GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}/fileslistThreadMessages(String threadId) GET https://api.openai.com/v1/threads/{thread_id}/messageslistThreadRuns(String threadId) GET https://api.openai.com/v1/threads/{thread_id}/runslistThreadRunSteps(String threadId, String runId) listThreadRunSteps(String threadId, String runId, Integer limit, String order, String after, String before) GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/stepsmodifyAssistant(String assistantId, ModifyAssistantRequest request) POST https://api.openai.com/v1/assistants/{assistant_id}modifyThread(String threadId, ModifyThreadRequest request) GET https://api.openai.com/v1/threads/{thread_id}modifyThreadMessage(String threadId, String messageId, ModifyMessageRequest request) GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}modifyThreadRun(String threadId, String runId, ModifyRunRequest request) GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}retrieveAssistant(String assistantId) Retrieves an assistant.retrieveAssistantFile(String assistantId, String fileId) GET https://api.openai.com/v1/assistants/{assistant_id}/files/{file_id}retrieveFile(String fileId) retrieveFileContent(String fileId) retrieveFineTuningJob(String fineTuningJobId) Get info about a fine-tuning job.retrieveThread(String threadId) GET https://api.openai.com/v1/threads/{thread_id}retrieveThreadMessage(String threadId, String messageId) GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}retrieveThreadMessageFile(String threadId, String messageId, String fileId) GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}/files/{file_id}retrieveThreadRun(String threadId, String runId) GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}retrieveThreadRunStep(String threadId, String runId, String stepId) GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/steps/{step_id}submitToolOutputs(String threadId, String runId, SubmitToolOutputsRequest request) POST https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/submit_tool_outputsuploadFile(File file, File.Purpose purpose) uploadFile(String filepath, File.Purpose purpose)
-
Constructor Details
-
OpenAiService
Creates a new OpenAiService that wraps OpenAiApi- Parameters:
token- OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
-
OpenAiService
Creates a new OpenAiService that wraps OpenAiApi- Parameters:
token- OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"timeout- http read timeout, Duration.ZERO means no timeout
-
OpenAiService
Creates a new OpenAiService that wraps OpenAiApi. Use this if you need more customization.- Parameters:
api- OpenAiApi instance to use for all methods
-
-
Method Details
-
execute
public static <T> T execute(io.reactivex.rxjava3.core.Single<T> apiCall) Calls the Open AI api, returns the response, and parses error messages if the request fails -
buildApi
-
defaultObjectMapper
public static com.fasterxml.jackson.databind.ObjectMapper defaultObjectMapper() -
defaultClient
-
defaultClient
-
defaultRetrofit
public static retrofit2.Retrofit defaultRetrofit(okhttp3.OkHttpClient client, com.fasterxml.jackson.databind.ObjectMapper mapper) -
listModels
-
getModel
-
createChatCompletion
gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301 gpt-3.5-turbo-1006, gpt-4-1106-preview- Parameters:
request- create chat completion request- Returns:
- chat completion
-
createSteamChatCompletion
public void createSteamChatCompletion(String requestId, CreateChatCompletionRequest request, @NonNull @NonNull StreamChatCompletionListener listener) create chat completion by stream- Parameters:
requestId- request ID, every observer is uniquerequest- detail of requestlistener- StreamChatCompletionListener
-
createImage
-
createImageEdit
-
createImageVariation
-
createEmbeddings
text-embedding-ada-002, text-search-ada-doc-001- Parameters:
request- create Embedding request- Returns:
- create embedding response
-
createSpeech
@POST("/v1/audio/speech") public byte[] createSpeech(CreateSpeechRequest request) throws IOException Generates audio from the input text.- Parameters:
request- create speech request- Returns:
- audio file
- Throws:
IOException
-
createAudioTranscription
whisper-1TODO only support mp3 so far
- Parameters:
request- create audio transcription request- Returns:
- audio response
-
createAudioTranslation
whisper-1TODO only support mp3 so far
- Parameters:
request- create audio traslation request- Returns:
- audio
-
listFiles
-
uploadFile
-
uploadFile
-
deleteFile
-
retrieveFile
-
retrieveFileContent
- Parameters:
fileId- file id- Returns:
- file content
-
createModeration
text-moderation-stable, text-moderation-latest- Parameters:
request- create moderation request- Returns:
- moderation response
-
createFineTuningJob
Creates a job that fine-tunes a specified model from a given dataset.- Parameters:
request- CreateFineTuningJobRequest- Returns:
- A fine-tuning.job object.
-
retrieveFineTuningJob
Get info about a fine-tuning job.- Parameters:
fineTuningJobId- fineTuningJobId- Returns:
- The fine-tuning object with the given ID.
-
cancelFineTuningJob
Immediately cancel a fine-tune job.- Parameters:
fineTuningJobId- fineTuningJobId- Returns:
- The cancelled fine-tuning object.
-
listFineTuningEvents
public List<FineTuningJobEvent> listFineTuningEvents(String fineTuningJobId, String after, Integer limit) Get status updates for a fine-tuning job.- Parameters:
fineTuningJobId- fineTuningJobIdafter- Identifier for the last event from the previous pagination request.limit- Number of events to retrieve.- Returns:
- A list of fine-tuning event objects.
-
createAssistant
POST https://api.openai.com/v1/assistantsCreate an assistant with a model and instructions.
- Parameters:
request- Request body- Returns:
- An
Assistantobject.
-
retrieveAssistant
Retrieves an assistant.- Parameters:
assistantId- The ID of the assistant to retrieve.- Returns:
- The
Assistantobject matching the specified ID.
-
modifyAssistant
POST https://api.openai.com/v1/assistants/{assistant_id}Modifies an assistant.
- Parameters:
assistantId- The ID of the assistant to modify.request- Request body- Returns:
- The modified
Assistantobject.
-
deleteAssistant
DELETE https://api.openai.com/v1/assistants/{assistant_id}Delete an assistant.
- Parameters:
assistantId- The ID of the assistant to delete.- Returns:
- Deletion status
-
listAssistants
public OpenAiApiListResponse<Assistant> listAssistants(Integer limit, String order, String after, String before) GET https://api.openai.com/v1/assistantsReturns a list of assistants.
- Parameters:
limit- A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.order- Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.after- A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.before- A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.- Returns:
- A list of
Assistantobjects.
-
listAssistants
-
createAssistantFile
POST https://api.openai.com/v1/assistants/{assistant_id}/filesCreate assistant file
- Parameters:
assistantId- The ID of the assistant for which to create a File.request- Request body- Returns:
- An
AssistantFileobject.
-
retrieveAssistantFile
GET https://api.openai.com/v1/assistants/{assistant_id}/files/{file_id}Retrieve assistant file
- Parameters:
assistantId- The ID of the assistant who the file belongs to.fileId- The ID of the file we're getting.- Returns:
- The
AssistantFileobject matching the specified ID.
-
deleteAssistantFile
DELETE https://api.openai.com/v1/assistants/{assistant_id}/files/{file_id}Delete an assistant file.
- Parameters:
assistantId- The ID of the assistant who the file belongs to.fileId- The ID of the file to delete.- Returns:
- Deletion status
-
listAssistantFiles
public OpenAiApiListResponse<AssistantFile> listAssistantFiles(String assistantId, Integer limit, String order, String after, String before) GET https://api.openai.com/v1/assistants/{assistant_id}/filesReturns a list of assistant files.
- Parameters:
assistantId- The ID of the assistant the file belongs to.limit- A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.order- Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.after- A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.before- A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.- Returns:
- A list of
AssistantFileobjects.
-
listAssistantFiles
-
createThread
POST https://api.openai.com/v1/threadsCreate thread
- Parameters:
request- Request body- Returns:
- An
Threadobject.
-
retrieveThread
GET https://api.openai.com/v1/threads/{thread_id}Retrieve thread
- Parameters:
threadId- The ID of the thread to retrieve.- Returns:
- The
Threadobject matching the specified ID.
-
modifyThread
GET https://api.openai.com/v1/threads/{thread_id}Modify thread
- Parameters:
threadId- The ID of the thread to modify. Only the metadata can be modified.request- Request body- Returns:
- The modified
Threadobject matching the specified ID.
-
deleteThread
DELETE https://api.openai.com/v1/threads/{thread_id}Delete thread
- Parameters:
threadId- The ID of the thread to delete.- Returns:
- Deletion status
-
createThreadMessage
POST https://api.openai.com/v1/threads/{thread_id}/messagesCreate message
-
retrieveThreadMessage
GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}Retrieve message
-
modifyThreadMessage
GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}Modify message
- Parameters:
threadId- The ID of the thread to which this message belongs.messageId- The ID of the message to modify.request- Request body- Returns:
- The modified
Messageobject.
-
listThreadMessages
public OpenAiApiListResponse<Message> listThreadMessages(String threadId, Integer limit, String order, String after, String before) GET https://api.openai.com/v1/threads/{thread_id}/messagesList messages
- Parameters:
threadId- The ID of theThreadthe messages belong to.limit- A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.order- Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.after- A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.before- A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.- Returns:
- A list of
Messageobjects.
-
listThreadMessages
-
retrieveThreadMessageFile
GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}/files/{file_id}Retrieves a message file.
- Parameters:
threadId- The ID of the thread to which the message and File belong.messageId- The ID of the message the file belongs to.fileId- The ID of the file being retrieved.- Returns:
- The
MessageFileobject.
-
listThreadMessageFiles
public OpenAiApiListResponse<MessageFile> listThreadMessageFiles(String threadId, String messageId, Integer limit, String order, String after, String before) GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}/filesReturns a list of message files.
- Parameters:
threadId- The ID of the thread that the message and files belong to.messageId- The ID of the message that the files belongs to.limit- A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.order- Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.after- A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.before- A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.- Returns:
- A list of
MessageFileobjects.
-
listThreadMessageFiles
-
createThreadRun
POST https://api.openai.com/v1/threads/{thread_id}/runsCreate a run.
- Parameters:
threadId- The ID of the thread to run.request- Request body- Returns:
- An
Runobject.
-
retrieveThreadRun
GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}Retrieves a run.
-
modifyThreadRun
GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}Modify message
-
listThreadRuns
public OpenAiApiListResponse<Run> listThreadRuns(String threadId, Integer limit, String order, String after, String before) GET https://api.openai.com/v1/threads/{thread_id}/runsList messages
- Parameters:
threadId- The ID of theThreadthe messages belong to.limit- A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.order- Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.after- A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.before- A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.- Returns:
- A list of
Runobjects.
-
listThreadRuns
-
submitToolOutputs
POST https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/submit_tool_outputs -
cancelThreadRun
POST https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/cancelCancels a run that is in_progress.
- Parameters:
threadId- The ID of the thread to which this run belongs.runId- The ID of the run to cancel.- Returns:
- The modified
Runobject matching the specified ID.
-
createThreadAndRun
POST https://api.openai.com/v1/threads/runsCreate a thread and run it in one request.
- Parameters:
request- Request body- Returns:
- A
Runobject.
-
retrieveThreadRunStep
GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/steps/{step_id}Retrieves a run step.
- Parameters:
threadId- The ID of the thread to which the run and run step belongs.runId- The ID of the run to which the run step belongs.stepId- The ID of the run step to retrieve.- Returns:
- The
RunStepobject matching the specified ID.
-
listThreadRunSteps
public OpenAiApiListResponse<RunStep> listThreadRunSteps(String threadId, String runId, Integer limit, String order, String after, String before) GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/stepsList run steps
- Parameters:
threadId- The ID of theThreadthe messages belong to.runId- The ID of the run the run steps belong to.limit- A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.order- Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.after- A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.before- A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.- Returns:
- A list of
RunStepobjects.
-
listThreadRunSteps
-