Back to Qwen Agent

Cookbook: Mind Map

examples/cookbook_mind_map.ipynb

0.0.264.8 KB
Original Source

Cookbook: Mind Map

In this cookbook, we will demonstrate how to interact with files and create structured visual outputs like mind maps using an agent.

Install Requirements

We will use Qwen-Agent in this book. For demonstration, we will need at least the mcp functionality of Qwen-Agent.

python
!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.
python
!pip3 install -U uv
# We will use mcp servers that run with the `uvx` command, so `uv` is required.
python
!npm --version
# We also use mcp servers that run with the `npx` command, so node.js should be installed.

Create a Document

Here, we create a sample document and save it as example_document.md for later use.

python
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)

Step 1: Create an Agent

Create an agent that is capable of

  • listing, reading and writing files (through the MCP server @modelcontextprotocol/server-filesystem).
  • making mind maps (through the MCP server mindmap-mcp-server, source: Mindmap MCP Server).
python
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.
python
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
)

Step 2: Number the Headings

We will add numbers to the Markdown headings to demonstrate how we can manipulate Markdown documents with the agent.

python
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)"}]
python
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)
python
messages += ret_messages

Step 3: Create a Mind Map

We will create a mind map by prompting the agent, and it will call the corresponding tool.

python
messages += [{'role': 'user', 'content': 'Create a mind map with your previous markdown output. Show me the saved path.'}]
python
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)
python
# We can preview the mind map in the browser!

Use the GUI

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.

python
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()