Back to Qwen Agent

Cookbook: Database Manipulation

examples/cookbook_database_manipulation.ipynb

0.0.264.9 KB
Original Source

Cookbook: Database Manipulation

In this cookbook, we will explore a simple data processing example: reading a file, adding its data to a database, and querying it. It shows how we can efficiently manipulate data with agents.

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 Spreadsheet

We create a sample spreadsheet and save it as scores.csv for later use.

python
import csv

csv_file_name = 'scores.csv'
table = [
    {"id": 100, "score": 85},
    {"id": 101, "score": 90},
    {"id": 102, "score": 88},
    {"id": 103, "score": 95},
    {"id": 104, "score": 80},
    {"id": 105, "score": 92},
    {"id": 106, "score": 87},
    {"id": 107, "score": 100},
    {"id": 108, "score": 83},
    {"id": 109, "score": 98}
]

with open(csv_file_name, mode='w', newline='') as csv_file:
    writer = csv.DictWriter(csv_file, fieldnames=['id', 'score'])
    writer.writeheader()
    for record in table:
        writer.writerow(record)

Step 1: Create an Agent

Create an agent that is capable of

  • listing, reading and writing files (through the MCP server @modelcontextprotocol/server-filesystem).
  • performing SQLite database manipulations (through the MCP server mcp-server-sqlite).
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
            "filesystem": {
                "command": "npx",
                "args": [
                    "-y",
                    "@modelcontextprotocol/server-filesystem",
                    '.',
                ]
            },
            "sqlite" : {
                "command": "uvx",
                "args": [
                    "mcp-server-sqlite",
                    "--db-path",
                    "scores.db"
                ]
            }
        }
    }
]

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

Step 2: Read the Spreadsheet

The agent will read the csv table by calling the file reading tool.

python
messages = [
    {
        'role': 'user',
        'content': 'Show me the contents in scores.csv.'
    }
]
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 # extending the context with new `ret_messages`.

Step 3: Create a Database Table

Next, we create a database table and insert all the data that the agent just read.

python
messages.append({'role': 'user', 'content': "Create a table in the database, and fill it with the contents in the previous table."})
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

Step 4: Query the Highest Score

We query the highest score in the database. You can also perform other queries.

python
messages += [{'role': 'user', 'content': 'Query the highest score from the database.'}]
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 for data manipulations with databases.

Even better, we can easily create a web UI for an agent!

python
from qwen_agent.gui import WebUI

agent = Assistant(
    name="Qwen Assistant",
    description="I'm a digital assistant powered by Qwen-Agent, ask me anything!",
    llm=llm_cfg,
    function_list=tools
)

WebUI(agent).run()
# Have fun!