Class ChatClient

java.lang.Object
org.codelibs.fess.chat.ChatClient

public class ChatClient extends Object
Client class for RAG (Retrieval-Augmented Generation) chat functionality. Orchestrates the multi-phase RAG workflow including session management, document search, and delegation to LlmClientManager for LLM operations. Prompt construction and LLM-specific logic is handled by LlmClient implementations.
Author:
FessProject
  • Field Details

    • chatSessionManager

      protected ChatSessionManager chatSessionManager
      The session manager for managing chat sessions.
    • llmClientManager

      protected LlmClientManager llmClientManager
      The LLM client manager for language model interactions.
    • markdownRenderer

      protected MarkdownRenderer markdownRenderer
      The markdown renderer for converting markdown to safe HTML.
  • Constructor Details

    • ChatClient

      public ChatClient()
      Default constructor.
  • Method Details

    • isAvailable

      public boolean isAvailable()
      Checks if RAG chat is available.
      Returns:
      true if RAG chat is available
    • chat

      public ChatClient.ChatResult chat(String sessionId, String userMessage, String userId)
      Performs a chat request with RAG.
      Parameters:
      sessionId - the session ID (can be null for new sessions)
      userMessage - the user's message
      userId - the user ID (can be null for anonymous users)
      Returns:
      the chat response including session info and sources
    • streamChat

      public ChatClient.ChatResult streamChat(String sessionId, String userMessage, String userId, LlmStreamCallback callback)
      Performs a streaming chat request with RAG.
      Parameters:
      sessionId - the session ID (can be null for new sessions)
      userMessage - the user's message
      userId - the user ID (can be null for anonymous users)
      callback - the callback to receive streaming chunks
      Returns:
      the chat result with session info and sources
    • streamChatEnhanced

      public ChatClient.ChatResult streamChatEnhanced(String sessionId, String userMessage, String userId, ChatPhaseCallback callback)
      Performs an enhanced streaming chat request with multi-phase RAG flow. This flow includes: intent detection, keyword search, result evaluation, content retrieval, answer generation, and markdown rendering.
      Parameters:
      sessionId - the session ID (can be null for new sessions)
      userMessage - the user's message
      userId - the user ID (can be null for anonymous users)
      callback - the callback to receive phase notifications and streaming chunks
      Returns:
      the chat result with session info, sources, and HTML content
    • extractHistory

      protected List<LlmMessage> extractHistory(ChatSession session)
      Extracts conversation history from a chat session as LlmMessage list.
      Parameters:
      session - the chat session
      Returns:
      the list of LlmMessages representing the conversation history
    • searchWithQuery

      protected List<Map<String,Object>> searchWithQuery(String query)
      Searches documents using a Lucene query.
      Parameters:
      query - the Lucene query string
      Returns:
      the list of search result documents
    • fetchFullContent

      protected List<Map<String,Object>> fetchFullContent(List<String> docIds)
      Fetches full document content for the given document IDs.
      Parameters:
      docIds - the document IDs to fetch
      Returns:
      list of documents with full content
    • searchByUrl

      protected List<Map<String,Object>> searchByUrl(String url)
      Searches for documents by URL.
      Parameters:
      url - the URL to search for
      Returns:
      list of documents matching the URL
    • renderMarkdownToHtml

      protected String renderMarkdownToHtml(String markdown)
      Renders markdown text to sanitized HTML.
      Parameters:
      markdown - the markdown text
      Returns:
      sanitized HTML
    • escapeHtml

      protected String escapeHtml(String text)
      Escapes HTML special characters.
      Parameters:
      text - the text to escape
      Returns:
      the escaped text
    • getMaxHistoryMessages

      protected int getMaxHistoryMessages()
      Gets the maximum number of history messages to retain.
      Returns:
      the maximum number of history messages
    • searchDocuments

      protected List<Map<String,Object>> searchDocuments(String query)
      Searches for documents relevant to the user's query.
      Parameters:
      query - the search query
      Returns:
      a list of documents matching the query