Class OpenaiClient

java.lang.Object
edu.tufts.hrilab.llm.openai.OpenaiClient

public class OpenaiClient extends Object
  • Field Details

  • Constructor Details

    • OpenaiClient

      public OpenaiClient()
  • Method Details

    • setEndpoint

      public void setEndpoint(String endpoint)
    • setModel

      public void setModel(String modelStr)
    • completion

      public OpenaiCompletionResponse completion(Prompt prompt)
      Calls the OpenAI API to generate a completion based on the given prompt.
      Parameters:
      prompt - the prompt used to generate the completion
      Returns:
      an OpenaiCompletionResponse object representing the response from the OpenAI completion endpoint
    • completion

      public OpenaiCompletionResponse completion(Symbol prompt)
    • completion

      public OpenaiCompletionResponse completion(String prompt)
    • completion

      public OpenaiCompletionResponse completion(Symbol modelSym, Prompt prompt)
      Calls the OpenAI API to generate a completion based on the given model and prompt.
      Parameters:
      modelSym - the model to use when generating a completion
      prompt - the prompt used to generate the completion
      Returns:
      an OpenaiCompletionResponse object representing the response from the OpenAI completion endpoint
    • completion

      public OpenaiCompletionResponse completion(Symbol modelSym, Symbol prompt)
    • completion

      public OpenaiCompletionResponse completion(String modelStr, String prompt)
    • completion

      public OpenaiCompletionResponse completion(OpenaiCompletionRequestBody requestBody)
      Sends a POST request to the OpenAI completion endpoint with the given request body and API key. https://platform.openai.com/docs/api-reference/completions
      Parameters:
      requestBody - the request body to send to the OpenAI completion endpoint
      Returns:
      an OpenaiCompletionResponse object representing the response from the OpenAI completion endpoint
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(Symbol prompt)
      Generates a chat completion using the OpenAI service and a prompt string.
      Parameters:
      prompt - the prompt string provided by the user
      Returns:
      the OpenaiChatCompletionResponse containing the completed chat.
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(String prompt)
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(Symbol model, Symbol prompt)
      Generates a chat completion response using OpenAI's API with the specified model and prompt. Creates a list of two messages, one from the system and one from the user, to use as input for the chat completion.
      Parameters:
      model - the model to use for the chat completion
      prompt - the text prompt to use for the chat completion
      Returns:
      an OpenaiChatCompletionResponse object representing the response from the OpenAI API
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(String model, String prompt)
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(List<Message> messages)
      Performs an OpenAI chat completion using the default model and the given list of messages. This method creates a new OpenaiChatRequestBody object with the default model and the provided messages.
      Parameters:
      messages - a list of messages to be sent for chat completion
      Returns:
      an OpenaiChatCompletionResponse object representing the response from OpenAI.
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(Chat chat)
      Performs a chat completion using OpenAI with the set model and the given chat messages.
      Parameters:
      chat - the chat containing the messages to use for the completion
      Returns:
      an OpenaiChatCompletionResponse object representing the response from OpenAI
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(Symbol model, Chat chat)
      Performs a chat completion using OpenAI with a provided model and the messages in the given chat.
      Parameters:
      model - the model to use for the completion
      chat - the chat containing the messages to use for the completion
      Returns:
      an OpenaiChatCompletionResponse object representing the response from OpenAI
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(String model, Chat chat)
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(Symbol model, List<Message> messages)
      Performs an OpenAI chat completion using the specified model and the given list of messages. This method creates a new OpenaiChatRequestBody object with the provided model and messages.
      Parameters:
      model - the model to use for the chat completion
      messages - a list of messages to be sent for chat completion
      Returns:
      an OpenaiChatCompletionResponse object representing the response from OpenAI
    • chatCompletion

      public OpenaiChatCompletionResponse chatCompletion(String model, List<Message> messages)