examples/cookbook_mind_map.ipynb
In this cookbook, we will demonstrate how to interact with files and create structured visual outputs like mind maps using an agent.
We will use Qwen-Agent in this book. For demonstration, we will need at least the mcp functionality of Qwen-Agent.
!pip3 install -U "qwen-agent[gui,rag,code_interpreter,mcp]"
# `pip install -U qwen-agent` will install the minimal requirements.
# The optional requirements, specified in double brackets, are:
# [gui] for Gradio-based GUI support;
# [rag] for RAG support;
# [code_interpreter] for Code Interpreter support;
# [mcp] for MCP support.
!pip3 install -U uv
# We will use mcp servers that run with the `uvx` command, so `uv` is required.
!npm --version
# We also use mcp servers that run with the `npx` command, so node.js should be installed.
Here, we create a sample document and save it as example_document.md for later use.
document = '''
# Document
## Introduction
- Brief overview of the document's purpose and main topics.
## Background
- Historical context and previous research.
- Key theories and models discussed.
## Methodology
- Detailed description of the methods used in the study.
- Explanation of data collection and analysis techniques.
## Results
- Presentation of the findings from the research.
- Statistical analysis and graphical representations.
## Discussion
- Interpretation of the results.
- Comparison with previous studies.
- Limitations and future research directions.
## Conclusion
- Summary of the main points covered in the document.
- Final thoughts and implications of the research.
'''.strip()
with open('example_document.md', 'w', encoding='utf-8') as f:
f.write(document)
Create an agent that is capable of
@modelcontextprotocol/server-filesystem).mindmap-mcp-server, source: Mindmap MCP Server).from qwen_agent.agents import Assistant
from qwen_agent.utils.output_beautify import typewriter_print
# `typewriter_print` prints streaming messages in a non-overlapping manner.
llm_cfg = {
'model': 'qwen3-32b',
'model_server': 'dashscope',
'api_key': '' # **fill your api key here**
# Use a model service compatible with the OpenAI API, such as vLLM or Ollama:
# 'model': 'Qwen3-8B',
# 'model_server': 'http://localhost:8000/v1', # base_url, also known as api_base
# 'api_key': 'EMPTY'
}
tools = [{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
'.',
]
},
"mindmap": {
"command": "uvx",
"args": ["mindmap-mcp-server", "--return-type", "filePath"]
}
}
}]
agent = Assistant(
llm=llm_cfg,
function_list=tools
)
We will add numbers to the Markdown headings to demonstrate how we can manipulate Markdown documents with the agent.
messages = []
messages += [{"role": "user", "content": "Read the example_document.md file in the current folder. In your reply, show me the content with the second-level headings numbered. (example: ## heading -> ## 1. heading)"}]
response_plain_text = ''
for ret_messages in agent.run(messages):
# `ret_messages` will contain all subsequent messages, consisting of interleaved assistant messages and tool responses
response_plain_text = typewriter_print(ret_messages, response_plain_text)
messages += ret_messages
We will create a mind map by prompting the agent, and it will call the corresponding tool.
messages += [{'role': 'user', 'content': 'Create a mind map with your previous markdown output. Show me the saved path.'}]
response_plain_text = ''
for ret_messages in agent.run(messages):
# `ret_messages` will contain all subsequent messages, consisting of interleaved assistant messages and tool responses
response_plain_text = typewriter_print(ret_messages, response_plain_text)
# We can preview the mind map in the browser!
We have explored the capabilities of the Qwen-Agent framework and Qwen models in processing files. We can also achieve this by using the GUI.
from qwen_agent.gui import WebUI
agent = Assistant(
name="Qwen File Processing Assistant",
description="I can help with your file processing needs, ask me anything!",
llm=llm_cfg,
function_list=tools
)
WebUI(agent).run()