examples/cookbook_database_manipulation.ipynb
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.
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.
We create a sample spreadsheet and save it as scores.csv for later use.
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)
Create an agent that is capable of
@modelcontextprotocol/server-filesystem).mcp-server-sqlite).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.
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
)
The agent will read the csv table by calling the file reading tool.
messages = [
{
'role': 'user',
'content': 'Show me the contents in scores.csv.'
}
]
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 # extending the context with new `ret_messages`.
Next, we create a database table and insert all the data that the agent just read.
messages.append({'role': 'user', 'content': "Create a table in the database, and fill it with the contents in the previous table."})
response_plain_text = ''
for ret_messages in agent.run(messages):
response_plain_text = typewriter_print(ret_messages, response_plain_text)
messages += ret_messages
We query the highest score in the database. You can also perform other queries.
messages += [{'role': 'user', 'content': 'Query the highest score from the database.'}]
response_plain_text = ''
for ret_messages in agent.run(messages):
response_plain_text = typewriter_print(ret_messages, response_plain_text)
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!
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!