Back to Qwen Agent

Cookbook: Drive Guide

examples/cookbook_drive_guide.ipynb

0.0.263.1 KB
Original Source

Cookbook: Drive Guide

In this cookbook, we will demonstrate how to query the distance between two locations and create a driving plan 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.

Step 1: Create an Agent

Create an agent that is capable of utilizing map functionalities provided by Amap.

An api key for Amap is needed, instructions are at https://lbs.amap.com/api/mcp-server/summary

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 for a clear view.
python
llm_cfg = {
    'model': 'qwen3-32b',
    'model_server': 'dashscope',
    'api_key': '' # **fill your dashscope 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": {
            # enumeration of mcp server configs
            "amap-amap-sse": {
                "url": "https://mcp.amap.com/sse?key=YOUR_KEY" # **fill your amap mcp key**
            }
        }
    }
]

agent = Assistant(
    llm=llm_cfg, 
    function_list=tools
)

Step 2: Calculate the Distance

We can query the distance between two locations by prompting the agent, which will then provide an answer utilizing the MCP server.

python
query = "How far is it between Zhejiang University (more specifically, the Zijingang Campus) and Hangzhou Xiaoshan airport?"
messages = [{'role': 'user', 'content': query}]
python
response_plain_text = ''

for ret_messages in agent.run(messages):
    response_plain_text = typewriter_print(ret_messages, response_plain_text)
python
messages += ret_messages # extending the context with new `ret_messages`.

Step 3: Make a Drive Plan

We can create a drive plan by asking follow-up questions, and the agent will generate the plan for us.

python
messages += [{'role': 'user', 'content': 'Okay, how can i drive?'}]
python
response_plain_text = ''

for ret_messages in agent.run(messages):
    response_plain_text = typewriter_print(ret_messages, response_plain_text)

Use the GUI

We have explored the capabilities of the Qwen-Agent framework and Qwen models in providing travel suggestions. We can also achieve this by using the GUI.

python
from qwen_agent.gui import WebUI

agent = Assistant(
    name="Qwen Guide",
    description="I can help with your travel needs, ask me anything!",
    llm=llm_cfg,
    function_list=tools
)

WebUI(agent).run()