Class DifyKnowledge
- All Implemented Interfaces:
io.agentscope.core.rag.Knowledge
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.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<Void>addDocuments(List<io.agentscope.core.rag.model.Document> documents) Adds documents to the knowledge base.static DifyKnowledge.Builderbuilder()Creates a new builder for DifyKnowledge.Gets the Dify API client.Gets the Dify configuration.reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>>Retrieves relevant documents based on a query.
-
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:
- Log in to Dify console (https://cloud.dify.ai)
- Navigate to Knowledge > Select your dataset
- Click "Upload Document" to add files
- Wait for processing to complete
- Documents will be available for retrieval via this plugin
- Specified by:
addDocumentsin interfaceio.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:
- Query is sent to Dify API with configured parameters
- Dify performs search according to retrieval mode
- Results are optionally reranked if enabled
- Documents are filtered by score threshold
- Top-K documents are returned, sorted by relevance
- Specified by:
retrievein interfaceio.agentscope.core.rag.Knowledge- Parameters:
query- the search query textconfig- the retrieval configuration (limit, score threshold)- Returns:
- a Mono that emits a list of relevant Document objects, sorted by relevance
-
getClient
Gets the Dify API client.- Returns:
- the client
-
getConfig
Gets the Dify configuration.- Returns:
- the configuration, or null if not available
-
builder
Creates a new builder for DifyKnowledge.- Returns:
- a new Builder instance
-