Class OpenaiClient

java.lang.Object
edu.tufts.hrilab.llm.openai.OpenaiClient
All Implemented Interfaces:
LLMClient

public class OpenaiClient extends Object implements LLMClient
  • Field Details

  • Constructor Details

    • OpenaiClient

      public OpenaiClient()
  • Method Details

    • setEndpoint

      public void setEndpoint(String endpoint)
      Specified by:
      setEndpoint in interface LLMClient
    • setModel

      public void setModel(String modelStr)
      Specified by:
      setModel in interface LLMClient
    • setStopWords

      public void setStopWords(String[] stopWords)
      Specified by:
      setStopWords in interface LLMClient
    • setTemperature

      public void setTemperature(float temperature)
      Specified by:
      setTemperature in interface LLMClient
    • completion

      public Completion completion(String userPrompt)
      Calls the OpenAI API to generate a completion based on the given prompt.
      Specified by:
      completion in interface LLMClient
      Parameters:
      userPrompt - the prompt used to generate the completion
      Returns:
      an OpenaiCompletionResponse object representing the response from the OpenAI completion endpoint
    • completion

      public Completion completion(String userPrompt, String model)
      Specified by:
      completion in interface LLMClient
    • completion

      public Completion completion(String userPrompt, String model, String systemPrompt)
      Specified by:
      completion in interface LLMClient
    • 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 Completion chatCompletion(String userPrompt)
      Generates a chat completion using the OpenAI service and a prompt string.
      Specified by:
      chatCompletion in interface LLMClient
      Parameters:
      userPrompt - the prompt string provided by the user
      Returns:
      the OpenaiChatCompletionResponse containing the completed chat.
    • chatCompletion

      public Completion chatCompletion(String userPrompt, String model)
      Specified by:
      chatCompletion in interface LLMClient
    • chatCompletion

      public Completion chatCompletion(String userPrompt, String model, String systemPrompt)
      Specified by:
      chatCompletion in interface LLMClient
    • chatCompletion

      public Completion 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.
      Specified by:
      chatCompletion in interface LLMClient
      Parameters:
      messages - a list of messages to be sent for chat completion
      Returns:
      an OpenaiChatCompletionResponse object representing the response from OpenAI.
    • chatCompletion

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

      public Completion chatCompletion(Chat chat, List<ToolDefinition> tools)
    • chatCompletion

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

      public Completion chatCompletion(List<Message> messages, String model)
      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 Completion chatCompletion(List<Message> messages, String model, List<ToolDefinition> tools)
    • visionCompletion

      public OpenaiChatCompletionResponse visionCompletion(List<VisionMessage> vmessages)
      Performs an OpenAI chat completion using VisionMessage
      Parameters:
      vmessages - a list of messages to be sent for chat completion
      Returns:
      an OpenaiChatCompletionResponse object representing the response from OpenAI
    • streamingChatCompletion

      public Completion streamingChatCompletion(Chat chat, List<ToolDefinition> tools, ai.thinkingrobots.trade.TRADEServiceInfo chunkHandlerService)