Class DifyKnowledge

java.lang.Object
io.agentscope.core.rag.integration.dify.DifyKnowledge
All Implemented Interfaces:
io.agentscope.core.rag.Knowledge

public class DifyKnowledge extends Object implements io.agentscope.core.rag.Knowledge
Dify Cloud Knowledge Base implementation.

This class provides integration with Dify Knowledge Base service, implementing the Knowledge interface to enable seamless RAG (Retrieval-Augmented Generation) capabilities with Dify-hosted or self-hosted knowledge bases.

Key features:

  • Cloud-hosted or self-hosted Dify deployment support
  • Multiple retrieval modes (keyword, semantic, hybrid, fulltext)
  • Advanced features like reranking and score filtering
  • Conversation history support for context-aware retrieval
  • Compatible with Bailian RAG plugin (same Knowledge interface)

Functional Scope: This plugin focuses on retrieval capabilities only, similar to Bailian RAG plugin. Document management (upload/delete/update) should be performed via Dify console.

Example usage:


 // 1. Configure Dify connection
 DifyRAGConfig config = DifyRAGConfig.builder()
     .apiKey(System.getenv("DIFY_RAG_API_KEY"))
     .datasetId("your-dataset-id")  // Configure in code
     .retrievalMode(RetrievalMode.HYBRID_SEARCH)
     .enableRerank(true)
     .build();

 // 2. Create knowledge base instance
 DifyKnowledge knowledge = DifyKnowledge.builder()
     .config(config)
     .build();

 // 3. Retrieve documents
 RetrieveConfig retrieveConfig = RetrieveConfig.builder()
     .limit(5)
     .scoreThreshold(0.5)
     .build();

 List<Document> results = knowledge.retrieve("query text", retrieveConfig).block();

 // 4. Use with agents via built-in RAG configuration
 ReActAgent agent = ReActAgent.builder()
     .name("Assistant")
     .model(chatModel)
     .knowledge(knowledge)
     .ragMode(RAGMode.AGENTIC)
     .build();
 

Note: Document management is not supported via this plugin. Please use Dify console to upload, delete, and manage documents. This design ensures compatibility with Bailian RAG plugin and keeps the plugin focused.

  • Method Details

    • addDocuments

      public reactor.core.publisher.Mono<Void> addDocuments(List<io.agentscope.core.rag.model.Document> documents)
      Adds documents to the knowledge base.

      Note: This operation is not supported, consistent with Bailian RAG plugin. Document management should be performed via Dify console:

      1. Log in to Dify console (https://cloud.dify.ai)
      2. Navigate to Knowledge > Select your dataset
      3. Click "Upload Document" to add files
      4. Wait for processing to complete
      5. Documents will be available for retrieval via this plugin
      Specified by:
      addDocuments in interface io.agentscope.core.rag.Knowledge
      Parameters:
      documents - the list of documents to add
      Returns:
      a Mono that completes with an error indicating the operation is not supported
    • retrieve

      public reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>> retrieve(String query, io.agentscope.core.rag.model.RetrieveConfig config)
      Retrieves relevant documents based on a query.

      This method searches the Dify knowledge base for documents relevant to the given query. The query is processed according to the configured retrieval mode (keyword/semantic/hybrid/fulltext), and results are filtered by score threshold and limited as specified.

      Dify's retrieve API features:

      • Multiple retrieval modes for different use cases
      • Automatic query embedding (for semantic/hybrid modes)
      • Vector similarity search with configurable algorithms
      • Optional reranking for improved relevance
      • Metadata filtering support

      The retrieval process:

      1. Query is sent to Dify API with configured parameters
      2. Dify performs search according to retrieval mode
      3. Results are optionally reranked if enabled
      4. Documents are filtered by score threshold
      5. Top-K documents are returned, sorted by relevance
      Specified by:
      retrieve in interface io.agentscope.core.rag.Knowledge
      Parameters:
      query - the search query text
      config - the retrieval configuration (limit, score threshold)
      Returns:
      a Mono that emits a list of relevant Document objects, sorted by relevance
    • getClient

      public DifyRAGClient getClient()
      Gets the Dify API client.
      Returns:
      the client
    • getConfig

      public DifyRAGConfig getConfig()
      Gets the Dify configuration.
      Returns:
      the configuration, or null if not available
    • builder

      public static DifyKnowledge.Builder builder()
      Creates a new builder for DifyKnowledge.
      Returns:
      a new Builder instance