Class BailianKnowledge
- All Implemented Interfaces:
io.agentscope.core.rag.Knowledge
This class provides integration with Alibaba Cloud Bailian Knowledge Base service, implementing the Knowledge interface to enable seamless RAG (Retrieval-Augmented Generation) capabilities with cloud-hosted knowledge bases.
Key features:
- Cloud-hosted knowledge base (no local infrastructure needed)
- Automatic document parsing and embedding by Bailian service
- Enterprise-grade search with reranking and filtering
- Supports structured, unstructured, and image knowledge bases
Example usage:
// 1. Configure Bailian connection
BailianConfig config = BailianConfig.builder()
.accessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.accessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
.workspaceId("llm-xxx")
.indexId("mymxbdxxxx")
.build();
// 2. Create knowledge base instance
BailianKnowledge knowledge = BailianKnowledge.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 KnowledgeRetrievalTools
KnowledgeRetrievalTools tools = new KnowledgeRetrievalTools(knowledge);
Toolkit toolkit = new Toolkit();
toolkit.registerObject(tools);
ReActAgent agent = ReActAgent.builder()
.name("Assistant")
.model(chatModel)
.toolkit(toolkit)
.build();
Note: Currently, only the retrieve operation is supported via API. Document upload and management must be performed through the Bailian console or will be supported in future releases.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for BailianKnowledge. -
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 BailianKnowledge.Builderbuilder()Creates a new builder for BailianKnowledge.Gets the Bailian API client.Gets the knowledge base index ID.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 yet supported via API. Please use the Bailian console to upload documents to your knowledge base, or wait for future API support.
- 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 Bailian knowledge base for documents relevant to the given query. The query is automatically embedded by Bailian service, and results are filtered by score threshold and limited as specified in the config.
Bailian's retrieve API includes:
- Automatic query embedding
- Vector similarity search
- Optional reranking for improved relevance
- Metadata filtering support
If conversation history is provided in RetrieveConfig and query rewrite is enabled in BailianConfig, the query will be rewritten based on the conversation context for better retrieval.
- Specified by:
retrievein interfaceio.agentscope.core.rag.Knowledge- Parameters:
query- the search query textconfig- the retrieval configuration (limit, score threshold, conversation history)- Returns:
- a Mono that emits a list of relevant Document objects, sorted by relevance
-
getIndexId
Gets the knowledge base index ID.- Returns:
- the index ID
-
getClient
Gets the Bailian API client.- Returns:
- the client
-
builder
Creates a new builder for BailianKnowledge.- Returns:
- a new Builder instance
-