Qwen-Agent プロジェクト詳細
プロジェクト概要
Qwen-Agent は、千問大規模言語モデルをベースにしたエージェント開発フレームワークであり、指示に従う、ツールを使用する、計画を立てる、記憶する能力を備えた LLM アプリケーションの開発に特化しています。このプロジェクトは、Alibaba 千問チームによって開発・メンテナンスされており、現在は千問チャットサービス (Qwen Chat) のバックエンドをサポートしています。
コア機能
1. 多機能エージェントのサポート
- ブラウザアシスタント (Browser Assistant): ウェブページの閲覧と操作能力
- コードインタープリター (Code Interpreter): Python コードの実行と分析
- カスタムアシスタント (Custom Assistant): 個性的なエージェントのカスタマイズ
- RAG 検索拡張: ドキュメントの質問応答と知識検索
- Chrome 拡張: ブラウザプラグイン形式のスマートアシスタント
2. 先進技術の統合
- 関数呼び出し (Function Calling): ツールと API の統合をサポート
- MCP プロトコルサポート: モデルコンテキストプロトコル互換
- 並列ツール呼び出し: 複数ステップ、複数ラウンドのツール使用
- 推論能力: QwQ-32B などの推論モデルを統合
最新の更新情報
- 2025年3月18日:
reasoning_contentフィールドをサポート、デフォルトの関数呼び出しテンプレートを調整 - 2025年3月7日: QwQ-32B ツール呼び出しのデモを追加、並列および複数ステップの呼び出しをサポート
- 2024年12月3日: GUI を Gradio 5 にアップグレード、Python 3.10+ が必要
- 2024年9月18日: Qwen2.5-Math デモを追加、ツール統合による推論能力を展示
インストール方法
安定版のインストール
pip install -U "qwen-agent[gui,rag,code_interpreter,mcp]"
# または最小インストール
pip install -U qwen-agent
開発版のインストール
git clone https://github.com/QwenLM/Qwen-Agent.git
cd Qwen-Agent
pip install -e ./"[gui,rag,code_interpreter,mcp]"
オプションの依存関係の説明
[gui]: Gradio グラフィカルインターフェースのサポート[rag]: RAG 検索拡張機能[code_interpreter]: コードインタープリター機能[mcp]: MCP プロトコルサポート
モデルサービスの設定
方法1:DashScope サービスを使用する
llm_cfg = {
'model': 'qwen-max-latest',
'model_server': 'dashscope',
# 'api_key': 'YOUR_DASHSCOPE_API_KEY',
'generate_cfg': {
'top_p': 0.8
}
}
方法2:自己デプロイモデルサービス
llm_cfg = {
'model': 'Qwen2.5-7B-Instruct',
'model_server': 'http://localhost:8000/v1',
'api_key': 'EMPTY',
}
コアコンポーネントアーキテクチャ
基本コンポーネント
- BaseChatModel: LLM 基本クラス、関数呼び出しをサポート
- BaseTool: ツール基本クラス、カスタム機能を拡張可能
- Agent: エージェント基本クラス、継承によるカスタマイズをサポート
高度なコンポーネント
- Assistant: 汎用アシスタントエージェント
- FnCallAgent: 関数呼び出しエージェント
- ReActChat: 推論行動対話エージェント
実際のアプリケーション例
カスタムツールエージェントの作成
import pprint
import urllib.parse
import json5
from qwen_agent.agents import Assistant
from qwen_agent.tools.base import BaseTool, register_tool
from qwen_agent.utils.output_beautify import typewriter_print
# ステップ1: カスタムツールの追加
@register_tool('my_image_gen')
class MyImageGen(BaseTool):
description = 'AI painting (image generation) service, input text description, and return the image URL drawn based on text information.'
parameters = [{
'name': 'prompt',
'type': 'string',
'description': 'Detailed description of the desired image content, in English',
'required': True
}]
def call(self, params: str, **kwargs) -> str:
prompt = json5.loads(params)['prompt']
prompt = urllib.parse.quote(prompt)
return json5.dumps(
{'image_url': f'https://image.pollinations.ai/prompt/{prompt}'},
ensure_ascii=False)
# ステップ2: LLM の設定
llm_cfg = {
'model': 'qwen-max-latest',
'model_server': 'dashscope',
'generate_cfg': {
'top_p': 0.8
}
}
# ステップ3: エージェントの作成
system_instruction = '''After receiving the user's request, you should:
- first draw an image and obtain the image url,
- then run code `request.get(image_url)` to download the image,
- and finally select an image operation from the given document to process the image.
Please show the image using `plt.show()`.'''
tools = ['my_image_gen', 'code_interpreter']
files = ['./examples/resource/doc.pdf']
bot = Assistant(llm=llm_cfg,
system_message=system_instruction,
function_list=tools,
files=files)
# ステップ4: エージェントチャットの実行
messages = []
while True:
query = input('\nuser query: ')
messages.append({'role': 'user', 'content': query})
response = []
response_plain_text = ''
print('bot response:')
for response in bot.run(messages=messages):
response_plain_text = typewriter_print(response, response_plain_text)
messages.extend(response)
Web UI インターフェースの起動
from qwen_agent.gui import WebUI
WebUI(bot).run()
MCP プロトコルの統合
MCP サーバー設定の例
{
"mcpServers": {
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory"]
},
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/files"]
},
"sqlite": {
"command": "uvx",
"args": [
"mcp-server-sqlite",
"--db-path",
"test.db"
]
}
}
}
依存環境の要件
- Node.js (最新バージョン)
- uv 0.4.18 以降
- Git
- SQLite
RAG ドキュメント質問応答能力
プロジェクトは、高速な RAG ソリューションと、非常に長いドキュメントを対象とした競争力のあるエージェントを提供し、2つの困難なベンチマークテストでネイティブの長コンテキストモデルよりも優れたパフォーマンスを発揮し、100万トークンのコンテキストを含むシングルショットの「干し草の山から針を探す」ストレステストで完璧な結果を示しました。
BrowserQwen ブラウザアシスタント
BrowserQwen は、Qwen-Agent をベースに構築されたブラウザアシスタントであり、ウェブページの閲覧、操作、情報抽出機能を提供します。
技術的な特徴と利点
- モジュール設計: 原子レベルのコンポーネントで、拡張とカスタマイズが容易
- マルチモデルサポート: 千問シリーズの各バージョンモデルと互換性あり
- 豊富なツールエコシステム: 複数の実用的なツールを内蔵
- 柔軟なデプロイ: クラウドサービスとローカルデプロイをサポート
- 活発なメンテナンス: 継続的な更新と機能強化
関連リソースリンク
- GitHub リポジトリ: https://github.com/QwenLM/Qwen-Agent
- 公式ドキュメント: https://qwen.readthedocs.io/
- オンライン体験: https://chat.qwen.ai/
- 技術ブログ: https://qwenlm.github.io/blog/qwen-agent-2405/
- Hugging Face: https://huggingface.co/Qwen
- ModelScope: https://modelscope.cn/organization/qwen
まとめ
Qwen-Agent は、強力で使いやすいエージェント開発フレームワークであり、開発者に複雑な LLM アプリケーションを構築するための完全なツールチェーンを提供します。単純なチャットボットから複雑な多機能スマートアシスタントまで、このフレームワークを通じて迅速に実現およびデプロイできます。