Class OpenAiService

java.lang.Object
xyz.felh.openai.OpenAiService

public class OpenAiService extends Object
OpenAi Service Class
  • Constructor Details

    • OpenAiService

      public OpenAiService(String token)
      Creates a new OpenAiService that wraps OpenAiApi
      Parameters:
      token - OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    • OpenAiService

      public OpenAiService(String token, Duration timeout)
      Creates a new OpenAiService that wraps OpenAiApi
      Parameters:
      token - OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
      timeout - http read timeout, Duration.ZERO means no timeout
    • OpenAiService

      public OpenAiService(OpenAiApi api, okhttp3.OkHttpClient client)
      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

      public static OpenAiApi buildApi(String token, Duration timeout)
    • defaultObjectMapper

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

      public static okhttp3.OkHttpClient defaultClient(String token, Duration timeout)
    • defaultClient

      public static okhttp3.OkHttpClient defaultClient(String token, String orgId, Duration timeout)
    • defaultRetrofit

      public static retrofit2.Retrofit defaultRetrofit(okhttp3.OkHttpClient client, com.fasterxml.jackson.databind.ObjectMapper mapper)
    • listModels

      public List<Model> listModels()
    • getModel

      public Model getModel(String modelId)
    • createChatCompletion

      public ChatCompletion createChatCompletion(CreateChatCompletionRequest request)
      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 unique
      request - detail of request
      listener - StreamChatCompletionListener
    • createImage

      public ImageResponse createImage(CreateImageRequest request)
    • createImageEdit

      public ImageResponse createImageEdit(CreateEditRequest request)
    • createImageVariation

      public ImageResponse createImageVariation(CreateVariationRequest request)
    • createEmbeddings

      public CreateEmbeddingResponse createEmbeddings(CreateEmbeddingRequest request)
      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

      public AudioResponse createAudioTranscription(CreateAudioTranscriptionRequest request)
      whisper-1

      TODO only support mp3 so far

      Parameters:
      request - create audio transcription request
      Returns:
      audio response
    • createAudioTranslation

      public AudioResponse createAudioTranslation(CreateAudioTranslationRequest request)
      whisper-1

      TODO only support mp3 so far

      Parameters:
      request - create audio traslation request
      Returns:
      audio
    • listFiles

      public List<File> listFiles()
    • uploadFile

      public File uploadFile(File file, File.Purpose purpose)
    • uploadFile

      public File uploadFile(String filepath, File.Purpose purpose)
    • deleteFile

      public DeleteResponse deleteFile(String fileId)
    • retrieveFile

      public File retrieveFile(String fileId)
    • retrieveFileContent

      public String retrieveFileContent(String fileId)
      Parameters:
      fileId - file id
      Returns:
      file content
    • createModeration

      public CreateModerationResponse createModeration(CreateModerationRequest request)
      text-moderation-stable, text-moderation-latest
      Parameters:
      request - create moderation request
      Returns:
      moderation response
    • createFineTuningJob

      public FineTuningJob createFineTuningJob(CreateFineTuningJobRequest request)
      Creates a job that fine-tunes a specified model from a given dataset.
      Parameters:
      request - CreateFineTuningJobRequest
      Returns:
      A fine-tuning.job object.
    • retrieveFineTuningJob

      public FineTuningJob retrieveFineTuningJob(String fineTuningJobId)
      Get info about a fine-tuning job.
      Parameters:
      fineTuningJobId - fineTuningJobId
      Returns:
      The fine-tuning object with the given ID.
    • cancelFineTuningJob

      public FineTuningJob cancelFineTuningJob(String fineTuningJobId)
      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 - fineTuningJobId
      after - 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

      public Assistant createAssistant(CreateAssistantRequest request)
      POST https://api.openai.com/v1/assistants

      Create an assistant with a model and instructions.

      Parameters:
      request - Request body
      Returns:
      An Assistant object.
    • retrieveAssistant

      public Assistant retrieveAssistant(String assistantId)
      Retrieves an assistant.
      Parameters:
      assistantId - The ID of the assistant to retrieve.
      Returns:
      The Assistant object matching the specified ID.
    • modifyAssistant

      public Assistant modifyAssistant(String assistantId, ModifyAssistantRequest request)
      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 Assistant object.
    • deleteAssistant

      public DeleteResponse deleteAssistant(String assistantId)
      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/assistants

      Returns 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 Assistant objects.
    • listAssistants

      public OpenAiApiListResponse<Assistant> listAssistants()
    • createAssistantFile

      public AssistantFile createAssistantFile(String assistantId, CreateAssistantFileRequest request)
      POST https://api.openai.com/v1/assistants/{assistant_id}/files

      Create assistant file

      Parameters:
      assistantId - The ID of the assistant for which to create a File.
      request - Request body
      Returns:
      An AssistantFile object.
    • retrieveAssistantFile

      public AssistantFile retrieveAssistantFile(String assistantId, String fileId)
      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 AssistantFile object matching the specified ID.
    • deleteAssistantFile

      public DeleteResponse deleteAssistantFile(String assistantId, String fileId)
      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}/files

      Returns 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 AssistantFile objects.
    • listAssistantFiles

      public OpenAiApiListResponse<AssistantFile> listAssistantFiles(String assistantId)
    • createThread

      public Thread createThread(CreateThreadRequest request)
      POST https://api.openai.com/v1/threads

      Create thread

      Parameters:
      request - Request body
      Returns:
      An Thread object.
    • retrieveThread

      public Thread retrieveThread(String threadId)
      GET https://api.openai.com/v1/threads/{thread_id}

      Retrieve thread

      Parameters:
      threadId - The ID of the thread to retrieve.
      Returns:
      The Thread object matching the specified ID.
    • modifyThread

      public Thread modifyThread(String threadId, ModifyThreadRequest request)
      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 Thread object matching the specified ID.
    • deleteThread

      public DeleteResponse deleteThread(String threadId)
      DELETE https://api.openai.com/v1/threads/{thread_id}

      Delete thread

      Parameters:
      threadId - The ID of the thread to delete.
      Returns:
      Deletion status
    • createThreadMessage

      public Message createThreadMessage(String threadId, CreateMessageRequest request)
      POST https://api.openai.com/v1/threads/{thread_id}/messages

      Create message

      Parameters:
      threadId - The ID of the Thread to create a message for.
      request - Request body
      Returns:
      An Message object.
    • retrieveThreadMessage

      public Message retrieveThreadMessage(String threadId, String messageId)
      GET https://api.openai.com/v1/threads/{thread_id}/messages/{message_id}

      Retrieve message

      Parameters:
      threadId - The ID of the Thread to which this message belongs.
      messageId - The ID of the message to retrieve.
      Returns:
      The Message object matching the specified ID.
    • modifyThreadMessage

      public Message modifyThreadMessage(String threadId, String messageId, ModifyMessageRequest request)
      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 Message object.
    • listThreadMessages

      public OpenAiApiListResponse<Message> listThreadMessages(String threadId, Integer limit, String order, String after, String before)
      GET https://api.openai.com/v1/threads/{thread_id}/messages

      List messages

      Parameters:
      threadId - The ID of the Thread the 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 Message objects.
    • listThreadMessages

      public OpenAiApiListResponse<Message> listThreadMessages(String threadId)
    • retrieveThreadMessageFile

      public MessageFile retrieveThreadMessageFile(String threadId, String messageId, String fileId)
      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 MessageFile object.
    • 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}/files

      Returns 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 MessageFile objects.
    • listThreadMessageFiles

      public OpenAiApiListResponse<MessageFile> listThreadMessageFiles(String threadId, String messageId)
    • createThreadRun

      public Run createThreadRun(String threadId, CreateRunRequest request)
      POST https://api.openai.com/v1/threads/{thread_id}/runs

      Create a run.

      Parameters:
      threadId - The ID of the thread to run.
      request - Request body
      Returns:
      An Run object.
    • retrieveThreadRun

      public Run retrieveThreadRun(String threadId, String runId)
      GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}

      Retrieves a run.

      Parameters:
      threadId - The ID of the Thread that was run.
      runId - The ID of the run to retrieve.
      Returns:
      The Run object matching the specified ID.
    • modifyThreadRun

      public Run modifyThreadRun(String threadId, String runId, ModifyRunRequest request)
      GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}

      Modify message

      Parameters:
      threadId - The ID of the Thread that was run.
      runId - The ID of the run to modify.
      request - Request body
      Returns:
      The modified Run object.
    • listThreadRuns

      public OpenAiApiListResponse<Run> listThreadRuns(String threadId, Integer limit, String order, String after, String before)
      GET https://api.openai.com/v1/threads/{thread_id}/runs

      List messages

      Parameters:
      threadId - The ID of the Thread the 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 Run objects.
    • listThreadRuns

      public OpenAiApiListResponse<Run> listThreadRuns(String threadId)
    • submitToolOutputs

      public Run submitToolOutputs(String threadId, String runId, SubmitToolOutputsRequest request)
      POST https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/submit_tool_outputs
      Parameters:
      threadId - The ID of the Thread to which this run belongs.
      runId - The ID of the run that requires the tool output submission.
      request - Request body
      Returns:
      The modified Run object matching the specified ID.
    • cancelThreadRun

      public Run cancelThreadRun(String threadId, String runId)
      POST https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}/cancel

      Cancels 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 Run object matching the specified ID.
    • createThreadAndRun

      public Run createThreadAndRun(CreateThreadAndRunRequest request)
      POST https://api.openai.com/v1/threads/runs

      Create a thread and run it in one request.

      Parameters:
      request - Request body
      Returns:
      A Run object.
    • retrieveThreadRunStep

      public RunStep retrieveThreadRunStep(String threadId, String runId, String stepId)
      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 RunStep object 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}/steps

      List run steps

      Parameters:
      threadId - The ID of the Thread the 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 RunStep objects.
    • listThreadRunSteps

      public OpenAiApiListResponse<RunStep> listThreadRunSteps(String threadId, String runId)