类 GenericRAGHook

java.lang.Object
io.agentscope.core.rag.GenericRAGHook
所有已实现的接口:
Hook

public class GenericRAGHook extends Object implements Hook
Generic RAG Hook for automatic knowledge retrieval before reasoning.

This hook implements the Generic RAG mode, where knowledge is automatically retrieved and injected into the prompt before each reasoning step. Unlike Agentic mode (where agents decide when to retrieve), Generic mode always retrieves relevant knowledge for user queries.

This hook intercepts PreReasoningEvent and:

  1. Extracts the query from user messages
  2. Retrieves relevant documents from the knowledge base
  3. Injects the retrieved knowledge as a user message
  4. Modifies the input messages to include the knowledge context

Example usage:


 KnowledgeBase knowledgeBase = new SimpleKnowledge(embeddingModel, vectorStore);
 GenericRAGHook ragHook = new GenericRAGHook(knowledgeBase);

 ReActAgent agent = ReActAgent.builder()
     .name("Assistant")
     .model(chatModel)
     .hook(ragHook)
     .build();
 

Configuration options:

  • defaultConfig - Retrieval configuration (limit, score threshold)
  • 构造器详细资料

    • GenericRAGHook

      public GenericRAGHook(Knowledge knowledge)
      Creates a GenericRAGHook with default configuration.

      Default configuration:

      • Limit: 5 documents
      • Score threshold: 0.5
      参数:
      knowledge - the knowledge base to retrieve from
      抛出:
      IllegalArgumentException - if knowledgeBase is null
    • GenericRAGHook

      public GenericRAGHook(Knowledge knowledge, RetrieveConfig defaultConfig)
      Creates a GenericRAGHook with custom configuration.
      参数:
      knowledge - the knowledge base to retrieve from
      defaultConfig - the default retrieval configuration
      抛出:
      IllegalArgumentException - if knowledgeBase is null
  • 方法详细资料

    • onEvent

      public <T extends HookEvent> reactor.core.publisher.Mono<T> onEvent(T event)
      从接口复制的说明: Hook
      Handle a hook event.

      This method is called for all agent execution events. Use pattern matching to handle specific event types.

      Modifiable Events: For events with setters, you can modify the context and the changes will affect agent execution:

      Notification Events: Events without setters are read-only:

      指定者:
      onEvent 在接口中 Hook
      类型参数:
      T - The concrete event type
      参数:
      event - The hook event
      返回:
      Mono containing the potentially modified event
    • priority

      public int priority()
      从接口复制的说明: Hook
      The priority of this hook (lower value = higher priority).

      Hooks are executed in ascending priority order. Hooks with the same priority execute in their registration order.

      Common Priority Ranges:

      • 0-50: Critical system hooks (auth, security)
      • 51-100: High priority hooks (validation, preprocessing)
      • 101-500: Normal priority hooks (business logic)
      • 501-1000: Low priority hooks (logging, metrics)
      指定者:
      priority 在接口中 Hook
      返回:
      The priority value (default: 100)
    • getKnowledgeBase

      public Knowledge getKnowledgeBase()
      Gets the knowledge base used by this hook.
      返回:
      the knowledge base
    • getDefaultConfig

      public RetrieveConfig getDefaultConfig()
      Gets the default retrieval configuration.
      返回:
      the default config