Class OpenAiLlmClient

java.lang.Object
org.codelibs.fess.llm.AbstractLlmClient
org.codelibs.fess.llm.openai.OpenAiLlmClient
All Implemented Interfaces:
LlmClient

public class OpenAiLlmClient extends AbstractLlmClient
LLM client implementation for OpenAI API. OpenAI provides cloud-based LLM services including GPT-4 and other models. This client supports both synchronous and streaming chat completions.
Author:
FessProject
See Also:
  • Field Details

  • Constructor Details

    • OpenAiLlmClient

      public OpenAiLlmClient()
      Default constructor.
  • Method Details

    • getName

      public String getName()
      Description copied from interface: LlmClient
      Returns the name of this LLM client.
      Returns:
      the client name (e.g., "ollama", "openai", "gemini")
    • checkAvailabilityNow

      protected boolean checkAvailabilityNow()
      Description copied from class: AbstractLlmClient
      Performs the actual availability check against the LLM provider.
      Specified by:
      checkAvailabilityNow in class AbstractLlmClient
      Returns:
      true if the provider is available
    • chat

      public LlmChatResponse chat(LlmChatRequest request)
      Description copied from interface: LlmClient
      Performs a chat completion request.
      Parameters:
      request - the chat request containing messages and parameters
      Returns:
      the chat response from the LLM
    • streamChat

      public void streamChat(LlmChatRequest request, LlmStreamCallback callback)
      Description copied from interface: LlmClient
      Performs a streaming chat completion request. The callback is invoked for each chunk of the response.
      Parameters:
      request - the chat request containing messages and parameters
      callback - the callback to receive streaming chunks
    • buildRequestBody

      protected Map<String,Object> buildRequestBody(LlmChatRequest request, boolean stream)
      Builds the request body for the OpenAI API.
      Parameters:
      request - the chat request
      stream - whether to enable streaming
      Returns:
      the request body as a map
    • useMaxCompletionTokens

      protected boolean useMaxCompletionTokens(String model)
      Determines whether the given model requires the "max_completion_tokens" parameter instead of the legacy "max_tokens" parameter.
      Parameters:
      model - the model name
      Returns:
      true if the model uses max_completion_tokens
    • supportsTemperature

      protected boolean supportsTemperature(String model)
      Determines whether the given model supports the "temperature" parameter. Some newer models (e.g., gpt-5-mini) only support the default temperature value.
      Parameters:
      model - the model name
      Returns:
      true if the model supports custom temperature values
    • isReasoningModel

      protected boolean isReasoningModel(String model)
      Determines whether the given model is a reasoning model that uses internal reasoning tokens (e.g., o1, o3, o4, gpt-5 series).
      Parameters:
      model - the model name
      Returns:
      true if the model is a reasoning model
    • convertMessage

      protected Map<String,String> convertMessage(LlmMessage message)
      Converts an LlmMessage to a map for the API request.
      Parameters:
      message - the message to convert
      Returns:
      the message as a map
    • getApiKey

      protected String getApiKey()
      Gets the OpenAI API key.
      Returns:
      the API key
    • getApiUrl

      protected String getApiUrl()
      Gets the OpenAI API URL.
      Returns:
      the API URL
    • getModel

      protected String getModel()
      Description copied from class: AbstractLlmClient
      Gets the model name.
      Specified by:
      getModel in class AbstractLlmClient
      Returns:
      the model name
    • getTimeout

      protected int getTimeout()
      Description copied from class: AbstractLlmClient
      Gets the request timeout in milliseconds.
      Specified by:
      getTimeout in class AbstractLlmClient
      Returns:
      the timeout in milliseconds
    • getIntentDetectionMaxTokens

      protected int getIntentDetectionMaxTokens()
      Description copied from class: AbstractLlmClient
      Gets the maximum tokens for intent detection.
      Overrides:
      getIntentDetectionMaxTokens in class AbstractLlmClient
      Returns:
      the maximum tokens for intent detection
    • getEvaluationMaxTokens

      protected int getEvaluationMaxTokens()
      Description copied from class: AbstractLlmClient
      Gets the maximum tokens for result evaluation.
      Overrides:
      getEvaluationMaxTokens in class AbstractLlmClient
      Returns:
      the maximum tokens for result evaluation