AgentScope provides built-in support for Retrieval-Augmented Generation (RAG). This page demonstrates how to use the RAG module, how to build multimodal knowledge bases, and how to integrate RAG with ReActAgent in both agentic and generic manners.
AgentScope does not require you to use the built-in RAG module. Integrating third-party RAG implementations, frameworks, or services is fully supported and encouraged.
TextReader reads and chunks plain text into paragraph-level (or character-level) Document objects.
import asyncioimport jsonfrom agentscope.rag import TextReader, Documentasync def example_text_reader() -> list[Document]: reader = TextReader(chunk_size=512, split_by="paragraph") documents = await reader( text=( "I'm John Doe, 28 years old.\n" "I live in San Francisco. I work at OpenAI as a " "software engineer. I love hiking and photography.\n" "My father is Michael Doe, a doctor. I'm very proud of him. " "My mother is Sarah Doe, a teacher. She is very kind and " "always helps me with my studies.\n" "I'm now a PhD student at Stanford University, majoring in " "Computer Science. My advisor is Prof. Jane Williams, who is " "a leading expert in artificial intelligence.\n" "My best friend is James Smith.\n" ), ) print("Number of chunks:", len(documents)) for idx, doc in enumerate(documents): print(f"Document #{idx}") print(" Score:", doc.score) print(" Metadata:", json.dumps(doc.metadata, indent=2)) return documentsdocs = asyncio.run(example_text_reader())
There is no universally optimal chunk size or splitting strategy. For PDF files and domain-specific content, implementing a custom reader tailored to your scenario is strongly recommended. To create one, inherit from ReaderBase and implement the __call__ method.
After chunking documents, create a knowledge base by providing an embedding model and an embedding store (vector database).AgentScope provides built-in support for Qdrant as the embedding store and SimpleKnowledge as the knowledge base implementation.
import asyncioimport osfrom agentscope.rag import TextReader, SimpleKnowledge, QdrantStorefrom agentscope.embedding import DashScopeTextEmbeddingasync def build_knowledge_base() -> SimpleKnowledge: # Read and chunk documents reader = TextReader(chunk_size=512, split_by="paragraph") documents = await reader( text=( "I'm John Doe, 28 years old.\n" "I live in San Francisco. I work at OpenAI as a " "software engineer. I love hiking and photography.\n" "My father is Michael Doe, a doctor. I'm very proud of him. " "My mother is Sarah Doe, a teacher. She is very kind and " "always helps me with my studies.\n" "I'm now a PhD student at Stanford University, majoring in " "Computer Science. My advisor is Prof. Jane Williams, who is " "a leading expert in artificial intelligence.\n" "My best friend is James Smith.\n" ), ) knowledge = SimpleKnowledge( embedding_model=DashScopeTextEmbedding( api_key=os.environ["DASHSCOPE_API_KEY"], model_name="text-embedding-v4", dimensions=1024, ), embedding_store=QdrantStore( location=":memory:", # Use in-memory storage; supports local files and remote servers collection_name="my_collection", dimensions=1024, ), ) # Add documents to the knowledge base await knowledge.add_documents(documents) # Retrieve relevant documents for a query results = await knowledge.retrieve( query="Who is John Doe's father?", limit=3, score_threshold=0.5, ) for doc in results: print(doc) return knowledgeknowledge = asyncio.run(build_knowledge_base())
The QdrantStorelocation parameter supports in-memory storage (:memory:), local file paths, and remote Qdrant server URLs. Refer to the Qdrant documentation for details.
SimpleKnowledge exposes a retrieve_knowledge method that wraps retrieve into a tool-compatible function. You can register it directly in an agent’s Toolkit:
toolkit.register_tool_function( knowledge.retrieve_knowledge, func_description=( "Retrieve documents relevant to the given query. " "Use this tool when you need to find information about John Doe." ),)
The get_client method in VDBStoreBase exposes the underlying vector database client directly, enabling advanced features such as index management and custom search configurations.
In agentic manner, retrieve_knowledge is registered as a tool in the agent’s Toolkit. The agent autonomously decides when to retrieve and rewrites the query using full conversation context.
import asyncioimport osfrom agentscope.agent import ReActAgentfrom agentscope.formatter import DashScopeChatFormatterfrom agentscope.model import DashScopeChatModelfrom agentscope.message import Msgfrom agentscope.tool import Toolkitasync def example_agentic_manner() -> None: toolkit = Toolkit() agent = ReActAgent( name="Friday", sys_prompt="You are a helpful assistant named Friday.", model=DashScopeChatModel( api_key=os.environ["DASHSCOPE_API_KEY"], model_name="qwen-max", ), formatter=DashScopeChatFormatter(), toolkit=toolkit, ) # First turn — introduce context without RAG await agent(Msg("user", "John Doe is my best friend.", "user")) # Register retrieve_knowledge after the agent knows who John Doe is toolkit.register_tool_function( knowledge.retrieve_knowledge, func_description=( "Retrieve documents relevant to the given query. " "Use this tool when you need to find information about John Doe." ), ) # Second turn — agent should rewrite the vague query using context await agent(Msg("user", "Do you know who his father is?", "user"))asyncio.run(example_agentic_manner())
In the second turn, the agent rewrites “his father” into a specific query such as “John Doe’s father” using the conversation history, then retrieves the relevant document.
In generic manner, pass the knowledge object directly to ReActAgent. The agent automatically retrieves relevant documents at the start of each reply and prepends them to the user message.
import asyncioimport osfrom agentscope.agent import ReActAgentfrom agentscope.formatter import DashScopeChatFormatterfrom agentscope.model import DashScopeChatModelfrom agentscope.message import Msgasync def example_generic_manner() -> None: agent = ReActAgent( name="Friday", sys_prompt="You are a helpful assistant named Friday.", model=DashScopeChatModel( api_key=os.environ["DASHSCOPE_API_KEY"], model_name="qwen-max", ), formatter=DashScopeChatFormatter(), knowledge=knowledge, # Pass the knowledge base here ) await agent(Msg("user", "Do you know who John Doe's father is?", "user"))asyncio.run(example_generic_manner())