Overview
Assistant is a widely applicable agent that integrates Retrieval-Augmented Generation (RAG) capabilities with function calling abilities. It automatically retrieves relevant information from documents and combines it with LLM responses.
Class Signature
from qwen_agent.agents import Assistant
class Assistant(FnCallAgent):
def __init__(
self,
function_list: Optional[List[Union[str, Dict, BaseTool]]] = None,
llm: Optional[Union[Dict, BaseChatModel]] = None,
system_message: Optional[str] = DEFAULT_SYSTEM_MESSAGE,
name: Optional[str] = None,
description: Optional[str] = None,
files: Optional[List[str]] = None,
rag_cfg: Optional[Dict] = None
)
Constructor Parameters
function_list
List[Union[str, Dict, BaseTool]]
List of tools for the agent:
- Tool names:
'code_interpreter', 'web_extractor', etc.
- Tool configs:
{'name': 'code_interpreter', 'timeout': 60}
- Tool objects:
CodeInterpreter()
llm
Union[Dict, BaseChatModel]
LLM configuration or instance:{
'model': 'qwen-max',
'api_key': 'your-api-key',
'model_type': 'qwen_dashscope'
}
system_message
str
default:"DEFAULT_SYSTEM_MESSAGE"
System prompt defining the agent’s behavior
Agent name for multi-agent scenarios
Agent description for routing
Initial files for RAG (local paths or URLs):files=['https://example.com/doc.pdf', '/path/to/local.txt']
RAG configuration:{
'max_ref_token': 4000, # Max tokens from retrieval
'parser_page_size': 500, # Chunk size for documents
'rag_searchers': ['hybrid_search'] # Search strategies
}
Methods
run
def run(
self,
messages: List[Union[Dict, Message]],
knowledge: str = '',
**kwargs
) -> Iterator[List[Message]]
Runs the agent with RAG and function calling.
messages
List[Union[Dict, Message]]
required
Conversation messages
External knowledge string. If provided, skips retrieval from files
Streaming response messages
Usage Examples
Basic Q&A with Documents
from qwen_agent.agents import Assistant
from qwen_agent.llm.schema import Message
# Create assistant with documents
assistant = Assistant(
llm={'model': 'qwen-max', 'api_key': 'your-api-key'},
files=[
'https://example.com/product_manual.pdf',
'/local/docs/faq.txt'
]
)
# Ask questions about the documents
messages = [Message(role='user', content='What are the key features?')]
for response in assistant.run(messages):
print(response[-1].content)
With Function Calling
# Assistant with tools and RAG
assistant = Assistant(
llm={'model': 'qwen-max'},
function_list=['code_interpreter', 'web_extractor'],
files=['research_paper.pdf']
)
messages = [
Message(
role='user',
content='Analyze the data in the paper and create a visualization'
)
]
for response in assistant.run(messages):
for msg in response:
if msg.content:
print(msg.content)
if msg.function_call:
print(f"Calling: {msg.function_call.name}")
Custom RAG Configuration
assistant = Assistant(
llm={'model': 'qwen-max'},
files=['large_document.pdf'],
rag_cfg={
'max_ref_token': 8000, # Retrieve more context
'parser_page_size': 300, # Smaller chunks
'rag_searchers': ['keyword_search', 'vector_search'] # Hybrid search
}
)
messages = [Message(role='user', content='Summarize the main findings')]
for response in assistant.run(messages):
print(response[-1].content)
Multi-turn Conversation
assistant = Assistant(
llm={'model': 'qwen-max'},
files=['customer_support.pdf'],
system_message='You are a helpful customer support agent.'
)
messages = []
# First question
messages.append(Message(role='user', content='How do I reset my password?'))
for response in assistant.run(messages):
pass
messages.extend(response)
print(f"Assistant: {messages[-1].content}")
# Follow-up question
messages.append(Message(role='user', content='What if I forgot my email?'))
for response in assistant.run(messages):
pass
messages.extend(response)
print(f"Assistant: {messages[-1].content}")
With External Knowledge
assistant = Assistant(
llm={'model': 'qwen-max'}
)
# Provide knowledge directly instead of from files
external_knowledge = """
Product: SuperWidget Pro
Features:
- Advanced AI processing
- Cloud synchronization
- Multi-platform support
Price: $99.99
"""
messages = [Message(role='user', content='Tell me about SuperWidget Pro')]
for response in assistant.run(messages, knowledge=external_knowledge):
print(response[-1].content)
Combining Multiple Sources
assistant = Assistant(
llm={'model': 'qwen-max'},
files=[
'company_policy.pdf',
'employee_handbook.docx',
'https://intranet.example.com/benefits.html'
],
function_list=['web_extractor'] # Can fetch additional web content
)
messages = [
Message(
role='user',
content='What are the vacation policies and health benefits?'
)
]
for response in assistant.run(messages):
print(response[-1].content)
Non-streaming Response
assistant = Assistant(
llm={'model': 'qwen-max'},
files=['technical_specs.pdf']
)
messages = [Message(role='user', content='What are the system requirements?')]
# Get complete response at once
response = assistant.run_nonstream(messages)
print(response[-1].content)
How RAG Works
The Assistant agent:
- Indexes documents: Automatically parses and chunks the provided files
- Retrieves context: When a query comes in, retrieves relevant chunks
- Augments prompt: Prepends retrieved knowledge to the system message
- Generates response: LLM responds with context-aware answers
# Example of retrieved knowledge format
"""
# Knowledge Base
## The content from product_manual.pdf:
Chapter 3: Installation
To install the software, follow these steps…
## The content from faq.txt:
Q: How do I update?
A: Go to Settings > Updates…
Advanced Configuration
Custom Memory Management
from qwen_agent.memory import Memory
# Use custom memory configuration
assistant = Assistant(
llm={'model': 'qwen-max'},
files=['docs.pdf']
)
# Access the memory system
print(f"Loaded files: {assistant.mem.system_files}")
Language-Specific Prompts
# The assistant auto-detects language and uses appropriate prompts
assistant_cn = Assistant(
llm={'model': 'qwen-max'},
files=['中文文档.pdf']
)
messages = [Message(role='user', content='这个产品有什么功能?')]
for response in assistant_cn.run(messages, lang='zh'):
print(response[-1].content)
See Also