feat: 多智能体竞标调度系统 v1.0.0
核心组件: - Orchestrator: 意图理解、任务拆分、竞标管理、结果验证 - Worker: 竞标任务、执行交付 - TaskBoard: 状态管理、信息存储 - BidEvaluator: 竞标评估算法 - ExecutionMonitor: 执行监控、超时处理 - LLMClient: 大模型接口调用 功能特性: - 竞标机制:Agent主动竞争任务 - 动态调度:串行/并行任务智能调度 - 智能容错:超时切换、验证重试 - 质量保证:结果验证、历史追踪 Web界面:首页、请求列表、任务列表、Agent管理 API接口:请求/任务/Agent管理、测试接口 端口:19015
This commit is contained in:
+260
@@ -0,0 +1,260 @@
|
||||
"""
|
||||
多智能体竞标调度系统 - Web应用
|
||||
"""
|
||||
|
||||
from flask import Flask, render_template, request, jsonify
|
||||
from flask_cors import CORS
|
||||
import json
|
||||
import time
|
||||
|
||||
from .models import AgentProfile, TaskStatus
|
||||
from .task_board import TaskBoard
|
||||
from .orchestrator import Orchestrator
|
||||
from .worker import WorkerPool, create_default_workers
|
||||
from .llm_client import LLMClient
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
# 初始化组件
|
||||
task_board = TaskBoard()
|
||||
llm_client = LLMClient(
|
||||
base_url="http://192.168.2.17:19007/v1",
|
||||
api_key="xxxx",
|
||||
model="auto"
|
||||
)
|
||||
orchestrator = Orchestrator(task_board, llm_client)
|
||||
worker_pool = create_default_workers(task_board, llm_client)
|
||||
|
||||
|
||||
# === Web页面 ===
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
"""首页"""
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
@app.route('/requests')
|
||||
def requests_page():
|
||||
"""请求列表页"""
|
||||
return render_template('requests.html')
|
||||
|
||||
|
||||
@app.route('/tasks')
|
||||
def tasks_page():
|
||||
"""任务列表页"""
|
||||
return render_template('tasks.html')
|
||||
|
||||
|
||||
@app.route('/agents')
|
||||
def agents_page():
|
||||
"""Agent列表页"""
|
||||
return render_template('agents.html')
|
||||
|
||||
|
||||
# === API ===
|
||||
|
||||
@app.route('/api/request', methods=['POST'])
|
||||
def create_request():
|
||||
"""
|
||||
创建用户请求
|
||||
|
||||
Body: {"content": "用户请求内容"}
|
||||
"""
|
||||
data = request.get_json()
|
||||
content = data.get('content', '')
|
||||
|
||||
if not content:
|
||||
return jsonify({'error': '请提供请求内容'}), 400
|
||||
|
||||
# 处理请求
|
||||
result = orchestrator.process_request(content)
|
||||
|
||||
return jsonify(result.to_dict())
|
||||
|
||||
|
||||
@app.route('/api/request/<request_id>')
|
||||
def get_request(request_id):
|
||||
"""获取请求详情"""
|
||||
req = task_board.get_request(request_id)
|
||||
if req:
|
||||
return jsonify(req.to_dict())
|
||||
return jsonify({'error': '请求不存在'}), 404
|
||||
|
||||
|
||||
@app.route('/api/requests')
|
||||
def list_requests():
|
||||
"""列出请求"""
|
||||
limit = int(request.args.get('limit', 50))
|
||||
requests = task_board.list_requests(limit)
|
||||
return jsonify([r.to_dict() for r in requests])
|
||||
|
||||
|
||||
@app.route('/api/tasks')
|
||||
def list_tasks():
|
||||
"""列出任务"""
|
||||
limit = int(request.args.get('limit', 100))
|
||||
tasks = task_board.list_tasks(limit)
|
||||
return jsonify([t.to_dict() for t in tasks])
|
||||
|
||||
|
||||
@app.route('/api/task/<task_id>')
|
||||
def get_task(task_id):
|
||||
"""获取任务详情"""
|
||||
task = task_board.get_task(task_id)
|
||||
if task:
|
||||
task_dict = task.to_dict()
|
||||
# 添加竞标和尝试记录
|
||||
task_dict['bids'] = [b.to_dict() for b in task_board.get_bids(task_id)]
|
||||
task_dict['attempts'] = [a.to_dict() for a in task_board.get_attempts(task_id)]
|
||||
return jsonify(task_dict)
|
||||
return jsonify({'error': '任务不存在'}), 404
|
||||
|
||||
|
||||
@app.route('/api/agents')
|
||||
def list_agents():
|
||||
"""列出Agent"""
|
||||
agents = task_board.list_agents()
|
||||
return jsonify([a.to_dict() for a in agents])
|
||||
|
||||
|
||||
@app.route('/api/agent', methods=['POST'])
|
||||
def register_agent():
|
||||
"""
|
||||
注册Agent
|
||||
|
||||
Body: {"id": "...", "name": "...", "capabilities": [...]}
|
||||
"""
|
||||
data = request.get_json()
|
||||
|
||||
agent = AgentProfile(
|
||||
id=data.get('id', ''),
|
||||
name=data.get('name', ''),
|
||||
description=data.get('description', ''),
|
||||
capabilities=data.get('capabilities', []),
|
||||
max_concurrent_tasks=data.get('max_concurrent_tasks', 1),
|
||||
preferred_task_types=data.get('preferred_task_types', [])
|
||||
)
|
||||
|
||||
if not agent.id:
|
||||
return jsonify({'error': '请提供Agent ID'}), 400
|
||||
|
||||
task_board.register_agent(agent)
|
||||
|
||||
return jsonify(agent.to_dict())
|
||||
|
||||
|
||||
@app.route('/api/agent/<agent_id>', methods=['DELETE'])
|
||||
def unregister_agent(agent_id):
|
||||
"""注销Agent"""
|
||||
task_board.unregister_agent(agent_id)
|
||||
return jsonify({'success': True})
|
||||
|
||||
|
||||
@app.route('/api/stats')
|
||||
def get_stats():
|
||||
"""获取统计信息"""
|
||||
stats = task_board.get_stats()
|
||||
stats['active_executions'] = orchestrator.execution_manager.get_active_count()
|
||||
return jsonify(stats)
|
||||
|
||||
|
||||
# === 快速测试 ===
|
||||
|
||||
@app.route('/api/test', methods=['POST'])
|
||||
def test_execution():
|
||||
"""
|
||||
快速测试
|
||||
|
||||
Body: {"prompt": "测试内容"}
|
||||
"""
|
||||
data = request.get_json()
|
||||
prompt = data.get('prompt', '你好,请介绍一下你自己')
|
||||
|
||||
try:
|
||||
response = llm_client.simple_chat(prompt)
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'response': response
|
||||
})
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
@app.route('/api/test_intent', methods=['POST'])
|
||||
def test_intent():
|
||||
"""
|
||||
测试意图分析
|
||||
|
||||
Body: {"prompt": "帮我写一个Python爬虫"}
|
||||
"""
|
||||
data = request.get_json()
|
||||
prompt = data.get('prompt', '')
|
||||
|
||||
try:
|
||||
intent = llm_client.analyze_intent(prompt)
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'intent': intent
|
||||
})
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
@app.route('/api/test_split', methods=['POST'])
|
||||
def test_split():
|
||||
"""
|
||||
测试任务拆分
|
||||
|
||||
Body: {"prompt": "..."}
|
||||
"""
|
||||
data = request.get_json()
|
||||
prompt = data.get('prompt', '')
|
||||
|
||||
try:
|
||||
intent = llm_client.analyze_intent(prompt)
|
||||
tasks = llm_client.split_tasks(intent, prompt)
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'intent': intent,
|
||||
'tasks': tasks
|
||||
})
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
def main():
|
||||
"""启动应用"""
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='多智能体竞标调度系统')
|
||||
parser.add_argument('--port', type=int, default=19015, help='服务端口')
|
||||
parser.add_argument('--host', type=str, default='0.0.0.0', help='服务地址')
|
||||
parser.add_argument('--debug', action='store_true', help='调试模式')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
print(f"启动多智能体竞标调度系统...")
|
||||
print(f"端口: {args.port}")
|
||||
print(f"LLM接口: {llm_client.base_url}")
|
||||
print(f"默认Agent数量: {len(worker_pool.list_workers())}")
|
||||
|
||||
app.run(
|
||||
host=args.host,
|
||||
port=args.port,
|
||||
debug=args.debug
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user