GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||
GeezerVison is showing doc.agentscope.io — RAG - AgentScope Note Go to the end to download the full example code. RAG¶AgentScope provides built-in support for Retrieval-Augmented Generation (RAG) tasks. This tutorial demonstrates
Note As an open-source project, AgentScope doesn’t insist that developers use the built-in RAG module. Our target is make the development easier and more enjoyable, so integrating other RAG implementations, frameworks, or services are welcome and encouraged! import asyncio import json import os from matplotlib import pyplot as plt import agentscope from agentscope.agent import ReActAgent from agentscope.embedding import ( DashScopeTextEmbedding, DashScopeMultiModalEmbedding, ) from agentscope.formatter import DashScopeChatFormatter from agentscope.message import Msg from agentscope.model import DashScopeChatModel from agentscope.rag import ( TextReader, SimpleKnowledge, QdrantStore, Document, ImageReader, ) from agentscope.tool import Toolkit Using RAG Module¶The RAG module in AgentScope is composed of two core components:
Note We’re integrating more vector databases and readers into AgentScope. Contributions are welcome! The currently built-in readers include: for _ in agentscope.rag.__all__: if _.endswith("Reader"): print(f"- {_}") - TextReader - PDFReader - ImageReader - WordReader - ExcelReader - PowerPointReader they are responsible for reading data and chunking them into
Taking the async def example_text_reader(print_docs: bool) -> list[Document]: """The example of using TextReader.""" # Create a text reader with chunk size of 512 characters, split by characters reader = TextReader(chunk_size=512, split_by="paragraph") # Read documents from a text string documents = await reader( text=( # Fake personal profile for demonstration "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. I have published " "several papers in top conferences, such as NeurIPS and ICML.\n" "My best friend is James Smith.\n" ), ) if print_docs: print("The length of the documents:", len(documents)) for idx, doc in enumerate(documents): print("Document #", idx) print("\tScore: ", doc.score) print("\tMetadata: ", json.dumps(doc.metadata, indent=2), "\n") return documents docs = asyncio.run(example_text_reader(print_docs=True)) The length of the documents: 5
Document # 0
Score: None
Metadata: {
"content": {
"type": "text",
"text": "I'm John Doe, 28 years old."
},
"doc_id": "84d9740607155242357481c1cd8eb8001a986cda3665aaac33329ab235458168",
"chunk_id": 0,
"total_chunks": 5
}
Document # 1
Score: None
Metadata: {
"content": {
"type": "text",
"text": "I live in San Francisco. I work at OpenAI as a software engineer. I love hiking and photography."
},
"doc_id": "84d9740607155242357481c1cd8eb8001a986cda3665aaac33329ab235458168",
"chunk_id": 1,
"total_chunks": 5
}
Document # 2
Score: None
Metadata: {
"content": {
"type": "text",
"text": "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."
},
"doc_id": "84d9740607155242357481c1cd8eb8001a986cda3665aaac33329ab235458168",
"chunk_id": 2,
"total_chunks": 5
}
Document # 3
Score: None
Metadata: {
"content": {
"type": "text",
"text": "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. I have published several papers in top conferences, such as NeurIPS and ICML."
},
"doc_id": "84d9740607155242357481c1cd8eb8001a986cda3665aaac33329ab235458168",
"chunk_id": 3,
"total_chunks": 5
}
Document # 4
Score: None
Metadata: {
"content": {
"type": "text",
"text": "My best friend is James Smith."
},
"doc_id": "84d9740607155242357481c1cd8eb8001a986cda3665aaac33329ab235458168",
"chunk_id": 4,
"total_chunks": 5
}
Note there doesn’t exist a universally best chunk size and splitting method, especially for PDF files, we highly
encourage developers to implement or contribute their own readers according to their specific scenarios.
To create a custom reader, you only need to inherit the After chunking the documents, we can create a knowledge base to store the documents and perform retrieval.
Such a knowledge base is initialized by providing an embedding model and an embedding store (also known as a vector database).
Agentscope provides built-in support for Qdrant as the embedding store and a simple knowledge base implementation Note
async def build_knowledge_base() -> SimpleKnowledge: """Build a knowledge base with sample documents.""" # Read documents using the text reader documents = await example_text_reader(print_docs=False) # Create an in-memory knowledge base instance knowledge = SimpleKnowledge( # Choose an embedding model to convert text to embedding vectors embedding_model=DashScopeTextEmbedding( api_key=os.environ["DASHSCOPE_API_KEY"], model_name="text-embedding-v4", dimensions=1024, ), # Choose Qdrant as the embedding store embedding_store=QdrantStore( location=":memory:", # Use in-memory storage for demonstration collection_name="test_collection", dimensions=1024, # The dimension of the embedding vectors ), ) # Insert documents into the knowledge base await knowledge.add_documents(documents) # Retrieve relevant documents based on a given query docs = await knowledge.retrieve( query="Who is John Doe's father?", limit=3, score_threshold=0.5, ) print("Retrieved Documents:") for doc in docs: print(doc, "\n") return knowledge knowledge = asyncio.run(build_knowledge_base()) Retrieved Documents:
Document(metadata=DocMetadata(content={'type': 'text', 'text': "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."}, doc_id='84d9740607155242357481c1cd8eb8001a986cda3665aaac33329ab235458168', chunk_id=2, total_chunks=5), id='VHZwAjb5nDqJwbipSDXs9i', embedding=None, score=0.5657136536003619)
Document(metadata=DocMetadata(content={'type': 'text', 'text': "I'm John Doe, 28 years old."}, doc_id='84d9740607155242357481c1cd8eb8001a986cda3665aaac33329ab235458168', chunk_id=0, total_chunks=5), id='9ESnwFeiRRqdMHjTU7too4', embedding=None, score=0.5062624400117309)
The knowledge base class provides two main methods: In addition, the knowledge base class also provides a convenient method
Customizing RAG Components¶AgentScope supports and encourages developers to customize their own RAG components, including readers, knowledge bases and embedding stores. Specifically, we provide the following base classes for customization:
The get_client method in the Integrating with ReActAgent¶Next we demonstrate how to integrate the RAG module with the Agentic Manner¶In agentic manner, the ReAct agent is empowered with the ability to decide when to retrieve knowledge and the queries to be retrieved.
It’s very easy to integrate the RAG module with the async def example_agentic_manner() -> None: """The example of integrating RAG module with ReActAgent in agentic manner.""" # Create a ReAct agent toolkit = Toolkit() # Create the ReAct agent with DashScope as the model 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, ) print("The first response: ") # Ask some questions about Tony Stank await agent( Msg( "user", "John Doe is my best friend.", "user", ), ) # Register the retrieve_knowledge method as a tool function in the toolkit toolkit.register_tool_function( knowledge.retrieve_knowledge, func_description=( # Provide a clear description for the tool "The tool used to retrieve documents relevant to the given query. " "Use this tool when you need to find some information about John Doe." ), ) print("\n\nThe second response: ") # We hope the agent can rewrite the query to be more specific, e.g. # "Who is Tony Stank's father?" or "Tony Stank's father" await agent( Msg( "user", "Do you know who his father is?", "user", ), ) asyncio.run(example_agentic_manner()) The first response:
Friday: That's great! It's wonderful to have a best friend. Is there anything specific you'd like to share about John or any special memories you have together? Or is there something in particular you need help with related to your friendship?
The second response:
Friday: {
"type": "tool_use",
"id": "call_8bbc26a262a641eab87d20",
"name": "retrieve_knowledge",
"input": {
"query": "John Doe father",
"limit": 5,
"score_threshold": null
},
"raw_input": "{\"query\": \"John Doe father\", \"limit\": 5, \"score_threshold\": null}"
}
system: {
"type": "tool_result",
"id": "call_8bbc26a262a641eab87d20",
"name": "retrieve_knowledge",
"output": [
{
"type": "text",
"text": "Score: 0.5653893900693138, Content: 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."
},
{
"type": "text",
"text": "Score: 0.4790107771624694, Content: I'm John Doe, 28 years old."
},
{
"type": "text",
"text": "Score: 0.2473851245249162, Content: My best friend is James Smith."
},
{
"type": "text",
"text": "Score: 0.15979023413058244, Content: I live in San Francisco. I work at OpenAI as a software engineer. I love hiking and photography."
},
{
"type": "text",
"text": "Score: 0.15195164753879928, Content: 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. I have published several papers in top conferences, such as NeurIPS and ICML."
}
]
}
Friday: According to the information I found, John Doe's father is Michael Doe, and he is a doctor. It seems that John is very proud of his father. If you need more details or if there's anything else you would like to know, feel free to ask!
In the above example, our question is “Do you know who his father is?”. We hope the agent can rewrite the query with the historical information, and rewrite it to be more specific, e.g. “Who is John Doe’s father?” or “John Doe’s father”. Generic Manner¶The Just set the async def example_generic_manner() -> None: """The example of integrating RAG module with ReActAgent in generic manner.""" # Create a ReAct agent 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, ) await agent( Msg( "user", "Do you know who John Doe's father is?", "user", ), ) print("Take a look at the agent's memory:") content = (await agent.memory.get_memory())[1].content print(json.dumps(content, indent=2, ensure_ascii=False)) asyncio.run(example_generic_manner()) Friday: According to the information provided, John Doe's father is Michael Doe, who is a doctor.
Take a look at the agent's memory:
[
{
"type": "text",
"text": "<retrieved_knowledge>Use the following content from the knowledge base(s) if it's helpful:\n"
},
{
"type": "text",
"text": "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."
},
{
"type": "text",
"text": "I'm John Doe, 28 years old."
},
{
"type": "text",
"text": "My best friend is James Smith."
},
{
"type": "text",
"text": "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. I have published several papers in top conferences, such as NeurIPS and ICML."
},
{
"type": "text",
"text": "I live in San Francisco. I work at OpenAI as a software engineer. I love hiking and photography."
},
{
"type": "text",
"text": "</retrieved_knowledge>"
}
]
Multimodal RAG¶The RAG module in AgentScope supports multimodal RAG natively, as
Thus, we can directly use the multimodal reader and embedding model to build a multimodal knowledge base as follows. First we prepare an image with some text about my name. # Prepare an image with the text "John Doe's father is Michael Doe." path_image = "./example.jpg" plt.figure(figsize=(8, 3)) plt.text( 0.5, 0.5, "My name is Tony Stank", ha="center", va="center", fontsize=30, ) plt.axis("off") plt.savefig(path_image, bbox_inches="tight", pad_inches=0.1) plt.close() Then we can build a multimodal knowledge base with the image document.
The example is the same as before, just using the async def example_multimodal_rag() -> None: """The example of using multimodal RAG.""" # Read the image using the ImageReader reader = ImageReader() docs = await reader(image_url=path_image) # Create a knowledge base with the new image document knowledge = SimpleKnowledge( embedding_model=DashScopeMultiModalEmbedding( api_key=os.environ["DASHSCOPE_API_KEY"], model_name="multimodal-embedding-v1", dimensions=1024, ), embedding_store=QdrantStore( location=":memory:", collection_name="test_collection", dimensions=1024, ), ) await knowledge.add_documents(docs) 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-vl-max", ), formatter=DashScopeChatFormatter(), knowledge=knowledge, ) await agent( Msg( "user", "What's my name?", "user", ), ) # Let's see the last message from the agent print("\nThe image is attached in the agent's memory:") print((await agent.memory.get_memory())[1]) asyncio.run(example_multimodal_rag()) Friday: Based on the information provided, your name is Tony Stank.
The image is attached in the agent's memory:
Msg(id='ZXB2FKBPJQvRioRjzwvWEZ', name='user', content=[{'type': 'text', 'text': "<retrieved_knowledge>Use the following content from the knowledge base(s) if it's helpful:\n"}, {'type': 'image', 'source': {'type': 'url', 'url': './example.jpg'}}, {'type': 'text', 'text': '</retrieved_knowledge>'}], role='user', metadata={}, timestamp='2026-05-25 08:46:43.284', invocation_id='None')
We can see that the agent can answer the question based on the retrieved image. Total running time of the script: (0 minutes 28.560 seconds) | ||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |