Class LlmClientManager

java.lang.Object
org.codelibs.fess.llm.LlmClientManager

public class LlmClientManager extends Object
Manager class for coordinating LLM (Large Language Model) client operations. This class serves as the central coordinator for LLM operations in Fess. It manages registered LLM clients and provides access to the configured LLM provider based on the current configuration.
  • Field Details

    • clientList

      protected final List<LlmClient> clientList
      The list of registered LLM clients.
  • Constructor Details

    • LlmClientManager

      public LlmClientManager()
      Default constructor.
  • Method Details

    • available

      public boolean available()
      Checks whether LLM chat functionality is available and configured.
      Returns:
      true if LLM chat is configured and available, false otherwise
    • getClient

      public LlmClient getClient()
      Gets the LLM client instance for the configured LLM type.
      Returns:
      The LLM client instance, or null if not found
    • getLlmType

      protected String getLlmType()
      Gets the configured LLM type from the system configuration.
      Returns:
      The LLM type string from configuration (e.g., "ollama", "openai", "gemini")
    • isRagChatEnabled

      protected boolean isRagChatEnabled()
      Checks if RAG chat feature is enabled.
      Returns:
      true if RAG chat is enabled, false otherwise
    • getClients

      public LlmClient[] getClients()
      Gets all registered LLM clients.
      Returns:
      Array of all registered LLM clients
    • register

      public void register(LlmClient client)
      Registers an LLM client with this manager.
      Parameters:
      client - The LLM client to register
    • chat

      public LlmChatResponse chat(LlmChatRequest request)
      Performs a chat completion request using the configured LLM client.
      Parameters:
      request - the chat request
      Returns:
      the chat response
      Throws:
      LlmException - if LLM is not available or an error occurs
    • streamChat

      public void streamChat(LlmChatRequest request, LlmStreamCallback callback)
      Performs a streaming chat completion request using the configured LLM client.
      Parameters:
      request - the chat request
      callback - the callback to receive streaming chunks
      Throws:
      LlmException - if LLM is not available or an error occurs
    • getAvailableClient

      protected LlmClient getAvailableClient()
      Gets the available LLM client, performing a single lookup.
      Returns:
      the available LLM client
      Throws:
      LlmException - if LLM client is not available
    • detectIntent

      public IntentDetectionResult detectIntent(String userMessage)
      Detects the intent of a user message using the configured LLM client.
      Parameters:
      userMessage - the user's message
      Returns:
      the detected intent with extracted keywords
      Throws:
      LlmException - if LLM is not available
    • detectIntent

      public IntentDetectionResult detectIntent(String userMessage, List<LlmMessage> history)
      Detects the intent of a user message with conversation history context.
      Parameters:
      userMessage - the user's message
      history - the conversation history for context
      Returns:
      the detected intent with extracted keywords
      Throws:
      LlmException - if LLM is not available
    • evaluateResults

      public RelevanceEvaluationResult evaluateResults(String userMessage, String query, List<Map<String,Object>> searchResults)
      Evaluates search results for relevance using the configured LLM client.
      Parameters:
      userMessage - the original user message
      query - the search query used
      searchResults - the search results to evaluate
      Returns:
      evaluation result with relevant document IDs
      Throws:
      LlmException - if LLM is not available
    • generateAnswer

      public LlmChatResponse generateAnswer(String userMessage, List<Map<String,Object>> documents, List<LlmMessage> history)
      Generates an answer using document content (synchronous).
      Parameters:
      userMessage - the user's message
      documents - the documents with content
      history - the conversation history
      Returns:
      the chat response
      Throws:
      LlmException - if LLM is not available
    • regenerateQuery

      public String regenerateQuery(String userMessage, String failedQuery, String failureReason, List<LlmMessage> history)
      Regenerates a search query when the previous query failed.
      Parameters:
      userMessage - the user's original message
      failedQuery - the query that failed
      failureReason - the reason for failure
      history - the conversation history
      Returns:
      a new query string
      Throws:
      LlmException - if LLM is not available
    • streamGenerateAnswer

      public void streamGenerateAnswer(String userMessage, List<Map<String,Object>> documents, List<LlmMessage> history, LlmStreamCallback callback)
      Generates an answer using document content (streaming).
      Parameters:
      userMessage - the user's message
      documents - the documents with content
      history - the conversation history
      callback - the streaming callback
      Throws:
      LlmException - if LLM is not available
    • generateUnclearIntentResponse

      public void generateUnclearIntentResponse(String userMessage, List<LlmMessage> history, LlmStreamCallback callback)
      Generates a response asking user for clarification.
      Parameters:
      userMessage - the user's message
      history - the conversation history
      callback - the streaming callback
      Throws:
      LlmException - if LLM is not available
    • generateNoResultsResponse

      public void generateNoResultsResponse(String userMessage, List<LlmMessage> history, LlmStreamCallback callback)
      Generates a response when no relevant documents are found.
      Parameters:
      userMessage - the user's message
      history - the conversation history
      callback - the streaming callback
      Throws:
      LlmException - if LLM is not available
    • generateDocumentNotFoundResponse

      public void generateDocumentNotFoundResponse(String userMessage, String documentUrl, List<LlmMessage> history, LlmStreamCallback callback)
      Generates a response when the specified document URL is not found.
      Parameters:
      userMessage - the user's message
      documentUrl - the URL that was not found
      history - the conversation history
      callback - the streaming callback
      Throws:
      LlmException - if LLM is not available
    • generateSummaryResponse

      public void generateSummaryResponse(String userMessage, List<Map<String,Object>> documents, List<LlmMessage> history, LlmStreamCallback callback)
      Generates a summary of the specified documents.
      Parameters:
      userMessage - the user's message
      documents - the documents to summarize
      history - the conversation history
      callback - the streaming callback
      Throws:
      LlmException - if LLM is not available
    • generateFaqAnswerResponse

      public void generateFaqAnswerResponse(String userMessage, List<Map<String,Object>> documents, List<LlmMessage> history, LlmStreamCallback callback)
      Generates an FAQ answer using document content (streaming).
      Parameters:
      userMessage - the user's message
      documents - the documents with content
      history - the conversation history
      callback - the streaming callback
      Throws:
      LlmException - if LLM is not available
    • generateDirectAnswer

      public void generateDirectAnswer(String userMessage, List<LlmMessage> history, LlmStreamCallback callback)
      Generates a direct answer without document search.
      Parameters:
      userMessage - the user's message
      history - the conversation history
      callback - the streaming callback
      Throws:
      LlmException - if LLM is not available