Class BailianKnowledge

java.lang.Object
io.agentscope.core.rag.integration.bailian.BailianKnowledge
All Implemented Interfaces:
io.agentscope.core.rag.Knowledge

public class BailianKnowledge extends Object implements io.agentscope.core.rag.Knowledge
Bailian Cloud Knowledge Base implementation.

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.

  • 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:
      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 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:
      retrieve in interface io.agentscope.core.rag.Knowledge
      Parameters:
      query - the search query text
      config - the retrieval configuration (limit, score threshold, conversation history)
      Returns:
      a Mono that emits a list of relevant Document objects, sorted by relevance
    • getIndexId

      public String getIndexId()
      Gets the knowledge base index ID.
      Returns:
      the index ID
    • getClient

      public BailianClient getClient()
      Gets the Bailian API client.
      Returns:
      the client
    • builder

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