AS AgentScopeAPI 手册 / API Reference

SimpleKnowledge

public class SimpleKnowledge implements Knowledge
源码Source io/agentscope/core/rag/knowledge/SimpleKnowledge.java:71–265

SimpleKnowledge 是知识库接口的开箱即用实现,组合 EmbeddingModel 与向量库,提供文档写入与相似度检索。它可以直接传给 ReActAgent.构建器().知识库(...)。位于 io.agentscope.core.rag.knowledge 包,实现 Knowledge。核心成员包括 addDocuments、retrieve、getEmbeddingModel、getEmbeddingStore、builder。

Simple implementation of a knowledge base.

This implementation integrates an embedding model and a vector store to provide a complete RAG (Retrieval-Augmented Generation) knowledge base. It handles the full workflow: document embedding, storage, and retrieval.

Workflow:

  • addDocuments: Embed documents → Store documents (with metadata/payload) in vector store

  • retrieve: Embed query → Search documents → Filter by threshold → Return documents

Example usage:

`EmbeddingModel embeddingModel = DashScopeTextEmbedding.builder()
.apiKey(apiKey)
.modelName("text-embedding-v3")
.dimensions(1024)
.build();
VDBStoreBase vectorStore = InMemoryStore.builder().dimensions(1024).build();

SimpleKnowledge knowledge = SimpleKnowledge.builder()
.embeddingModel(embeddingModel)
.embeddingStore(vectorStore)
.build();

// Add documents
List<Document> documents = reader.read(input).block();
knowledge.addDocuments(documents).block();

// Retrieve documents
RetrieveConfig config = RetrieveConfig.builder().limit(5).scoreThreshold(0.5).build();
List<Document> results = knowledge.retrieve("query text", config).block();
`

核心精讲Deep dive

SimpleKnowledge:内存知识库SimpleKnowledge: an in-memory knowledge base

SimpleKnowledge 是 Knowledge 接口的开箱即用实现,组合 EmbeddingModel 与向量库,提供文档写入与相似度检索。它可以直接传给 ReActAgent.builder().knowledge(...)。

SimpleKnowledge is a ready-made implementation of the Knowledge interface, combining an EmbeddingModel with a vector store to provide document ingestion and similarity search. It can be passed to ReActAgent.builder().knowledge(...).

  • builder 只需要 embeddingModel 与 embeddingStore 两个部件。The builder only needs embeddingModel and embeddingStore.
  • addDocuments 写入文档,retrieve 按 RetrieveConfig 返回命中片段。addDocuments ingests documents, and retrieve returns hits according to RetrieveConfig.
  • InMemoryStore 适合测试;生产可换 Milvus、Qdrant 或 Elasticsearch。InMemoryStore suits tests; production can switch to Milvus, Qdrant or Elasticsearch.

内存知识库的写入与检索Ingest and retrieve with an in-memory store

import io.agentscope.core.embedding.openai.OpenAITextEmbedding;
import io.agentscope.core.message.TextBlock;
import io.agentscope.core.rag.knowledge.SimpleKnowledge;
import io.agentscope.core.rag.model.Document;
import io.agentscope.core.rag.model.DocumentMetadata;
import io.agentscope.core.rag.model.RetrieveConfig;
import io.agentscope.core.rag.store.InMemoryStore;
import java.util.List;

public class KnowledgeDemo {
    public static void main(String[] args) {
        SimpleKnowledge knowledge = SimpleKnowledge.builder()
                .embeddingModel(OpenAITextEmbedding.builder()
                        .apiKey(System.getenv("OPENAI_API_KEY"))
                        .modelName("text-embedding-3-small")
                        .dimensions(1536)
                        .build())
                .embeddingStore(InMemoryStore.builder()
                        .dimensions(1536)
                        .build())
                .build();

        knowledge.addDocuments(List.of(
                new Document(DocumentMetadata.builder()
                        .docId("doc-1")
                        .chunkId("chunk-1")
                        .content(TextBlock.builder()
                                .text("AgentScope uses a ReAct loop.")
                                .build())
                        .build())
        )).block();

        List<Document> hits = knowledge.retrieve(
                "How does AgentScope reason?",
                RetrieveConfig.builder().limit(3).build()
        ).block();
        System.out.println(hits);
    }
}

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
Builder类class
SimpleKnowledge的构建器。
Builder for SimpleKnowledge.

方法摘要Method Summary

方法Method说明Description
public Mono<Void> addDocuments(List<Document> documents)
写入一批文档并生成向量。
Adds the documents.
public Mono<List<Document>> retrieve(String query, RetrieveConfig config)
按查询检索相关文档片段。
Performs the retrieve operation.
public EmbeddingModel getEmbeddingModel()
返回使用的嵌入模型。
Gets the embedding model used by this knowledge base.
public VDBStoreBase getEmbeddingStore()
获取向量化存储。
Gets the vector store used by this knowledge base.
public static Builder builder()
static
创建知识库构建器。
Creates a new builder for SimpleKnowledge.

方法详情Method Detail

addDocuments

public Mono<Void> addDocuments(List<Document> documents)

添加文档。

Adds the documents.

参数Parameters
名称Name类型Type说明Description
documentsList<Document>文档列表(类型 `List`)。The documents.
返回Returns Mono<Void>
返回Mono。Returns mono.
源码Source io/agentscope/core/rag/knowledge/SimpleKnowledge.java:96–132

retrieve

public Mono<List<Document>> retrieve(String query, RetrieveConfig config)

按查询检索相关文档。

Performs the retrieve operation.

参数Parameters
名称Name类型Type说明Description
queryString查询文本(类型 `String`)。The query.
configRetrieveConfig配置对象(类型 `RetrieveConfig`)。The config.
返回Returns Mono<List<Document>>
返回Mono>。Returns mono.
源码Source io/agentscope/core/rag/knowledge/SimpleKnowledge.java:134–174

getEmbeddingModel

public EmbeddingModel getEmbeddingModel()

获取向量化模型。

Gets the embedding model used by this knowledge base.

返回Returns EmbeddingModel
向量化模型。the embedding model
源码Source io/agentscope/core/rag/knowledge/SimpleKnowledge.java:181–183

getEmbeddingStore

public VDBStoreBase getEmbeddingStore()

获取向量化存储。

Gets the vector store used by this knowledge base.

返回Returns VDBStoreBase
向量存储。the vector store
源码Source io/agentscope/core/rag/knowledge/SimpleKnowledge.java:190–192

builder

public static Builder builder()
static

创建新的构建器用于 SimpleKnowledge。

Creates a new builder for SimpleKnowledge.

返回Returns Builder
一种新的构建器实例。a new Builder instance
源码Source io/agentscope/core/rag/knowledge/SimpleKnowledge.java:214–216

嵌套类详情Nested Class Detail

Builder

public static class Builder

SimpleKnowledge的构建器。位于 io.agentscope.core.rag.knowledge 包。核心成员包括 embeddingModel、embeddingStore、build。

Builder for SimpleKnowledge.

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.