自动布局
This commit is contained in:
394
backend/scripts/generate_android_agent.py
Executable file
394
backend/scripts/generate_android_agent.py
Executable file
@@ -0,0 +1,394 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
生成Android应用开发助手Agent
|
||||
这是一个专门用于Android应用开发的Agent,能够帮助开发者快速生成、优化和调试Android应用
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import SessionLocal
|
||||
from app.models.agent import Agent
|
||||
from app.models.user import User
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
|
||||
def generate_android_agent(db: Session, username: str = "admin"):
|
||||
"""生成Android应用开发助手Agent"""
|
||||
print("=" * 60)
|
||||
print("生成Android应用开发助手Agent")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
# 查找用户
|
||||
user = db.query(User).filter(User.username == username).first()
|
||||
if not user:
|
||||
print(f"❌ 未找到用户 '{username}',请先创建该用户")
|
||||
return
|
||||
print(f"✅ 找到用户: {user.username} (ID: {user.id})")
|
||||
print()
|
||||
|
||||
# 生成Android应用开发助手工作流配置
|
||||
# 工作流结构:
|
||||
# 开始 -> 需求分析 -> 需求分类 -> [代码生成|架构设计|问题诊断|性能优化] -> 结果整合 -> 格式化输出 -> 结束
|
||||
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
# 1. 开始节点
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": 400},
|
||||
"data": {
|
||||
"label": "开始",
|
||||
"output_format": "json"
|
||||
}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
# 2. 需求理解与分析节点(LLM节点)
|
||||
requirement_analysis_node = {
|
||||
"id": "llm-requirement-analysis",
|
||||
"type": "llm",
|
||||
"position": {"x": 250, "y": 400},
|
||||
"data": {
|
||||
"label": "需求理解与分析",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "2000",
|
||||
"prompt": """你是一个专业的Android开发顾问。请分析用户的需求,提取以下信息:
|
||||
|
||||
用户需求:{{query}}
|
||||
|
||||
请以JSON格式输出分析结果:
|
||||
{
|
||||
"app_type": "应用类型(工具类、社交类、电商类、游戏类等)",
|
||||
"core_modules": ["模块1", "模块2", ...],
|
||||
"target_users": "目标用户群体",
|
||||
"complexity": "简单|中等|复杂",
|
||||
"estimated_weeks": 数字,
|
||||
"tech_stack": ["技术1", "技术2", ...],
|
||||
"summary": "需求摘要"
|
||||
}
|
||||
|
||||
请确保输出是有效的JSON格式。"""
|
||||
}
|
||||
}
|
||||
nodes.append(requirement_analysis_node)
|
||||
edges.append({
|
||||
"id": "e1",
|
||||
"source": "start-1",
|
||||
"target": "llm-requirement-analysis"
|
||||
})
|
||||
|
||||
# 3. 数据准备节点(Transform节点)- 用于传递数据
|
||||
data_prepare_node = {
|
||||
"id": "transform-prepare",
|
||||
"type": "transform",
|
||||
"position": {"x": 450, "y": 400},
|
||||
"data": {
|
||||
"label": "准备数据",
|
||||
"mode": "merge",
|
||||
"mapping": {
|
||||
"requirement_analysis": "{{output}}",
|
||||
"user_query": "{{query}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
nodes.append(data_prepare_node)
|
||||
edges.append({
|
||||
"id": "e2",
|
||||
"source": "llm-requirement-analysis",
|
||||
"target": "transform-prepare"
|
||||
})
|
||||
|
||||
# 4. 需求分类节点(条件节点)
|
||||
classify_condition = {
|
||||
"id": "condition-classify",
|
||||
"type": "condition",
|
||||
"position": {"x": 650, "y": 400},
|
||||
"data": {
|
||||
"label": "需求分类",
|
||||
"condition": "{user_query} contains '生成' or {user_query} contains '创建' or {user_query} contains '写代码' or {user_query} contains '代码'"
|
||||
}
|
||||
}
|
||||
nodes.append(classify_condition)
|
||||
edges.append({
|
||||
"id": "e3",
|
||||
"source": "transform-prepare",
|
||||
"target": "condition-classify"
|
||||
})
|
||||
|
||||
# 5. 代码生成节点(LLM节点)- true分支
|
||||
code_generation_node = {
|
||||
"id": "llm-code-generation",
|
||||
"type": "llm",
|
||||
"position": {"x": 850, "y": 300},
|
||||
"data": {
|
||||
"label": "代码生成",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.2",
|
||||
"max_tokens": "4000",
|
||||
"prompt": """你是一个专业的Android开发工程师。根据以下需求分析,生成完整的Android代码。
|
||||
|
||||
用户原始需求:{{user_query}}
|
||||
需求分析结果:{{requirement_analysis}}
|
||||
|
||||
请生成以下内容:
|
||||
1. Java/Kotlin代码(包含必要的注释)
|
||||
2. 布局XML文件
|
||||
3. 资源文件配置(如需要)
|
||||
4. 依赖配置(build.gradle,如需要)
|
||||
5. 使用说明
|
||||
|
||||
代码要求:
|
||||
- 遵循Android开发最佳实践
|
||||
- 使用现代Android架构(推荐MVVM)
|
||||
- 包含错误处理
|
||||
- 代码注释清晰
|
||||
- 支持Android API 24+
|
||||
|
||||
请以Markdown格式输出,包含代码块,使用正确的语言标识(java、kotlin、xml等)。"""
|
||||
}
|
||||
}
|
||||
nodes.append(code_generation_node)
|
||||
edges.append({
|
||||
"id": "e4-true",
|
||||
"source": "condition-classify",
|
||||
"target": "llm-code-generation",
|
||||
"sourceHandle": "true"
|
||||
})
|
||||
|
||||
# 6. 架构设计节点(LLM节点)- false分支(当不是代码生成时)
|
||||
architecture_node = {
|
||||
"id": "llm-architecture",
|
||||
"type": "llm",
|
||||
"position": {"x": 850, "y": 500},
|
||||
"data": {
|
||||
"label": "架构设计/问题诊断/性能优化",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "3000",
|
||||
"prompt": """你是一个Android开发专家。根据用户需求,提供专业的开发建议。
|
||||
|
||||
用户原始需求:{{user_query}}
|
||||
需求分析结果:{{requirement_analysis}}
|
||||
|
||||
请根据需求类型提供相应的帮助:
|
||||
|
||||
**如果是架构设计需求**,请提供:
|
||||
1. 架构模式选择(MVP/MVVM/MVI)及理由
|
||||
2. 模块划分方案
|
||||
3. 技术栈选型(框架、库、工具)
|
||||
4. 目录结构设计
|
||||
5. 数据流设计
|
||||
6. 扩展性考虑
|
||||
|
||||
**如果是问题诊断需求**(包含错误、崩溃、问题、bug等关键词),请提供:
|
||||
1. 问题类型(崩溃/ANR/内存泄漏/网络错误等)
|
||||
2. 根本原因分析
|
||||
3. 解决方案(步骤清晰)
|
||||
4. 修复代码示例
|
||||
5. 预防措施
|
||||
|
||||
**如果是性能优化需求**(包含优化、性能、速度、卡顿等关键词),请提供:
|
||||
1. 性能瓶颈分析
|
||||
2. 优化方案(按优先级排序)
|
||||
3. 具体优化代码
|
||||
4. 优化前后对比
|
||||
5. 性能测试建议
|
||||
|
||||
请以Markdown格式输出,确保内容专业、清晰、实用。"""
|
||||
}
|
||||
}
|
||||
nodes.append(architecture_node)
|
||||
edges.append({
|
||||
"id": "e4-false",
|
||||
"source": "condition-classify",
|
||||
"target": "llm-architecture",
|
||||
"sourceHandle": "false"
|
||||
})
|
||||
|
||||
# 7. 结果整合节点(Transform节点)
|
||||
integration_node = {
|
||||
"id": "transform-integration",
|
||||
"type": "transform",
|
||||
"position": {"x": 1050, "y": 400},
|
||||
"data": {
|
||||
"label": "结果整合",
|
||||
"mode": "merge",
|
||||
"mapping": {
|
||||
"requirement_analysis": "{{requirement_analysis}}",
|
||||
"user_query": "{{user_query}}",
|
||||
"solution": "{{output}}",
|
||||
"agent_type": "Android应用开发助手",
|
||||
"timestamp": "{{$timestamp}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
nodes.append(integration_node)
|
||||
|
||||
# 连接代码生成节点到整合节点
|
||||
edges.append({
|
||||
"id": "e5-code",
|
||||
"source": "llm-code-generation",
|
||||
"target": "transform-integration"
|
||||
})
|
||||
|
||||
# 连接架构设计节点到整合节点
|
||||
edges.append({
|
||||
"id": "e5-arch",
|
||||
"source": "llm-architecture",
|
||||
"target": "transform-integration"
|
||||
})
|
||||
|
||||
# 8. 格式化输出节点(LLM节点)
|
||||
format_node = {
|
||||
"id": "llm-format",
|
||||
"type": "llm",
|
||||
"position": {"x": 1250, "y": 400},
|
||||
"data": {
|
||||
"label": "格式化输出",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.1",
|
||||
"max_tokens": "4000",
|
||||
"prompt": """请将以下Android开发方案整理成专业的Markdown文档格式。
|
||||
|
||||
方案内容:{{input}}
|
||||
|
||||
文档结构:
|
||||
1. **需求分析摘要**
|
||||
- 应用类型
|
||||
- 核心模块
|
||||
- 技术复杂度
|
||||
- 开发周期估算
|
||||
|
||||
2. **解决方案**
|
||||
- 详细说明
|
||||
- 代码示例(如有)
|
||||
- 架构设计(如有)
|
||||
|
||||
3. **实施步骤**
|
||||
- 步骤清晰的实施指南
|
||||
|
||||
4. **注意事项**
|
||||
- 重要提醒
|
||||
- 常见问题
|
||||
|
||||
5. **参考资料**
|
||||
- 相关文档链接
|
||||
|
||||
请确保格式清晰、代码高亮正确、结构完整。如果输入中已经包含格式良好的内容,请保持原有格式并适当优化。"""
|
||||
}
|
||||
}
|
||||
nodes.append(format_node)
|
||||
edges.append({
|
||||
"id": "e6",
|
||||
"source": "transform-integration",
|
||||
"target": "llm-format"
|
||||
})
|
||||
|
||||
# 9. 结束节点
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 1450, "y": 400},
|
||||
"data": {
|
||||
"label": "结束",
|
||||
"description": "返回最终结果"
|
||||
}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
edges.append({
|
||||
"id": "e7",
|
||||
"source": "llm-format",
|
||||
"target": "end-1"
|
||||
})
|
||||
|
||||
# 创建或更新Agent
|
||||
workflow_config = {
|
||||
"nodes": nodes,
|
||||
"edges": edges
|
||||
}
|
||||
|
||||
agent = db.query(Agent).filter(
|
||||
Agent.name == "Android应用开发助手",
|
||||
Agent.user_id == user.id
|
||||
).first()
|
||||
|
||||
if agent:
|
||||
agent.workflow_config = workflow_config
|
||||
agent.description = "帮助开发者快速生成、优化和调试Android应用。支持代码生成、架构设计、问题诊断、性能优化等功能。"
|
||||
agent.updated_at = datetime.now()
|
||||
agent.status = "published" # 设置为已发布状态,可直接使用
|
||||
print("⚠️ Agent 'Android应用开发助手' 已存在,将更新它...")
|
||||
else:
|
||||
agent = Agent(
|
||||
id=str(uuid.uuid4()),
|
||||
name="Android应用开发助手",
|
||||
description="帮助开发者快速生成、优化和调试Android应用。支持代码生成、架构设计、问题诊断、性能优化等功能。",
|
||||
workflow_config=workflow_config,
|
||||
status="published", # 直接设置为已发布状态,可立即使用
|
||||
user_id=user.id,
|
||||
version=1
|
||||
)
|
||||
db.add(agent)
|
||||
|
||||
try:
|
||||
db.commit()
|
||||
db.refresh(agent)
|
||||
print()
|
||||
print("✅ Agent创建/更新成功!")
|
||||
print()
|
||||
print(f"📋 Agent信息:")
|
||||
print(f" - ID: {agent.id}")
|
||||
print(f" - 名称: {agent.name}")
|
||||
print(f" - 状态: {agent.status} (已发布,可直接使用)")
|
||||
print(f" - 版本: {agent.version}")
|
||||
print(f" - 节点数: {len(nodes)}")
|
||||
print(f" - 连接数: {len(edges)}")
|
||||
print()
|
||||
print("🎯 功能特性:")
|
||||
print(" ✅ 需求分析与理解")
|
||||
print(" ✅ 代码生成(Activity、ViewModel、Repository等)")
|
||||
print(" ✅ 架构设计建议(MVP/MVVM/MVI)")
|
||||
print(" ✅ 问题诊断与修复")
|
||||
print(" ✅ 性能优化建议")
|
||||
print(" ✅ 格式化输出")
|
||||
print()
|
||||
print("💡 使用提示:")
|
||||
print(" 1. 在Agent管理页面找到 'Android应用开发助手'")
|
||||
print(" 2. 点击 '使用' 按钮开始使用")
|
||||
print(" 3. 输入你的Android开发需求,例如:")
|
||||
print(" - '帮我生成一个登录功能的代码'")
|
||||
print(" - '设计一个电商应用的架构'")
|
||||
print(" - '我的应用崩溃了,错误信息是...'")
|
||||
print(" - '如何优化列表页面的滚动性能?'")
|
||||
print()
|
||||
return agent
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(f"❌ 创建Agent失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
generate_android_agent(db, username="admin")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
626
backend/scripts/generate_batch_agents.py
Executable file
626
backend/scripts/generate_batch_agents.py
Executable file
@@ -0,0 +1,626 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
批量生成多个Agent
|
||||
生成一批不同类型的Agent,展示各种工作流模式
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import SessionLocal
|
||||
from app.models.agent import Agent
|
||||
from app.models.user import User
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
|
||||
def generate_text_summary_agent(db: Session, user: User):
|
||||
"""生成文本摘要Agent"""
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": 300},
|
||||
"data": {"label": "开始", "output_format": "json"}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
summary_node = {
|
||||
"id": "llm-summary",
|
||||
"type": "llm",
|
||||
"position": {"x": 250, "y": 300},
|
||||
"data": {
|
||||
"label": "文本摘要",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "2000",
|
||||
"prompt": """你是一个专业的文本摘要专家。请对以下文本进行摘要。
|
||||
|
||||
文本内容:{{query}}
|
||||
|
||||
请生成:
|
||||
1. 核心要点(3-5条)
|
||||
2. 简要摘要(100-200字)
|
||||
3. 关键词(5-10个)
|
||||
|
||||
请以Markdown格式输出。"""
|
||||
}
|
||||
}
|
||||
nodes.append(summary_node)
|
||||
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 450, "y": 300},
|
||||
"data": {"label": "结束"}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
|
||||
edges.append({"id": "e1", "source": "start-1", "target": "llm-summary", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e2", "source": "llm-summary", "target": "end-1", "sourceHandle": "right", "targetHandle": "left"})
|
||||
|
||||
return {
|
||||
"name": "文本摘要Agent",
|
||||
"description": "智能文本摘要工具,能够提取文本核心要点、生成简要摘要和关键词。",
|
||||
"workflow_config": {"nodes": nodes, "edges": edges}
|
||||
}
|
||||
|
||||
|
||||
def generate_code_review_agent(db: Session, user: User):
|
||||
"""生成代码审查Agent"""
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": 300},
|
||||
"data": {"label": "开始", "output_format": "json"}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
analysis_node = {
|
||||
"id": "llm-analysis",
|
||||
"type": "llm",
|
||||
"position": {"x": 250, "y": 300},
|
||||
"data": {
|
||||
"label": "代码分析",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.2",
|
||||
"max_tokens": "3000",
|
||||
"prompt": """你是一个资深的代码审查专家。请审查以下代码。
|
||||
|
||||
代码:{{query}}
|
||||
|
||||
请检查:
|
||||
1. 代码规范(命名、格式、注释)
|
||||
2. 潜在bug和错误
|
||||
3. 性能问题
|
||||
4. 安全性问题
|
||||
5. 最佳实践建议
|
||||
|
||||
请以Markdown格式输出,包含问题列表和改进建议。"""
|
||||
}
|
||||
}
|
||||
nodes.append(analysis_node)
|
||||
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 450, "y": 300},
|
||||
"data": {"label": "结束"}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
|
||||
edges.append({"id": "e1", "source": "start-1", "target": "llm-analysis", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e2", "source": "llm-analysis", "target": "end-1", "sourceHandle": "right", "targetHandle": "left"})
|
||||
|
||||
return {
|
||||
"name": "代码审查Agent",
|
||||
"description": "专业的代码审查工具,能够检查代码规范、潜在bug、性能问题和安全性。",
|
||||
"workflow_config": {"nodes": nodes, "edges": edges}
|
||||
}
|
||||
|
||||
|
||||
def generate_translation_agent(db: Session, user: User):
|
||||
"""生成翻译Agent"""
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": 300},
|
||||
"data": {"label": "开始", "output_format": "json"}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
detect_node = {
|
||||
"id": "llm-detect",
|
||||
"type": "llm",
|
||||
"position": {"x": 250, "y": 300},
|
||||
"data": {
|
||||
"label": "语言检测",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.1",
|
||||
"max_tokens": "500",
|
||||
"prompt": """请检测以下文本的语言类型。
|
||||
|
||||
文本:{{query}}
|
||||
|
||||
请输出JSON格式:
|
||||
{
|
||||
"language": "检测到的语言(中文/英文/日文等)",
|
||||
"confidence": "置信度(高/中/低)"
|
||||
}"""
|
||||
}
|
||||
}
|
||||
nodes.append(detect_node)
|
||||
|
||||
translate_node = {
|
||||
"id": "llm-translate",
|
||||
"type": "llm",
|
||||
"position": {"x": 450, "y": 300},
|
||||
"data": {
|
||||
"label": "翻译",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "2000",
|
||||
"prompt": """你是一个专业的翻译专家。请翻译以下文本。
|
||||
|
||||
原文:{{query}}
|
||||
语言信息:{{output}}
|
||||
|
||||
请提供:
|
||||
1. 翻译结果
|
||||
2. 翻译说明(如有特殊处理)
|
||||
|
||||
请以Markdown格式输出。"""
|
||||
}
|
||||
}
|
||||
nodes.append(translate_node)
|
||||
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 650, "y": 300},
|
||||
"data": {"label": "结束"}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
|
||||
edges.append({"id": "e1", "source": "start-1", "target": "llm-detect", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e2", "source": "llm-detect", "target": "llm-translate", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e3", "source": "llm-translate", "target": "end-1", "sourceHandle": "right", "targetHandle": "left"})
|
||||
|
||||
return {
|
||||
"name": "智能翻译Agent",
|
||||
"description": "多语言翻译工具,支持语言自动检测和高质量翻译。",
|
||||
"workflow_config": {"nodes": nodes, "edges": edges}
|
||||
}
|
||||
|
||||
|
||||
def generate_qa_agent(db: Session, user: User):
|
||||
"""生成问答助手Agent"""
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": 300},
|
||||
"data": {"label": "开始", "output_format": "json"}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
understand_node = {
|
||||
"id": "llm-understand",
|
||||
"type": "llm",
|
||||
"position": {"x": 250, "y": 300},
|
||||
"data": {
|
||||
"label": "问题理解",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "1000",
|
||||
"prompt": """请分析用户的问题,提取关键信息。
|
||||
|
||||
用户问题:{{query}}
|
||||
|
||||
请输出JSON格式:
|
||||
{
|
||||
"question_type": "问题类型(技术/生活/学习等)",
|
||||
"keywords": ["关键词1", "关键词2"],
|
||||
"intent": "用户意图"
|
||||
}"""
|
||||
}
|
||||
}
|
||||
nodes.append(understand_node)
|
||||
|
||||
answer_node = {
|
||||
"id": "llm-answer",
|
||||
"type": "llm",
|
||||
"position": {"x": 450, "y": 300},
|
||||
"data": {
|
||||
"label": "生成答案",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.7",
|
||||
"max_tokens": "2000",
|
||||
"prompt": """你是一个知识渊博的助手。请回答用户的问题。
|
||||
|
||||
用户问题:{{query}}
|
||||
问题分析:{{output}}
|
||||
|
||||
请提供:
|
||||
1. 直接答案
|
||||
2. 详细解释
|
||||
3. 相关建议
|
||||
|
||||
请以Markdown格式输出,确保答案准确、清晰、有用。"""
|
||||
}
|
||||
}
|
||||
nodes.append(answer_node)
|
||||
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 650, "y": 300},
|
||||
"data": {"label": "结束"}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
|
||||
edges.append({"id": "e1", "source": "start-1", "target": "llm-understand", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e2", "source": "llm-understand", "target": "llm-answer", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e3", "source": "llm-answer", "target": "end-1", "sourceHandle": "right", "targetHandle": "left"})
|
||||
|
||||
return {
|
||||
"name": "智能问答助手",
|
||||
"description": "智能问答系统,能够理解问题意图并提供详细准确的答案。",
|
||||
"workflow_config": {"nodes": nodes, "edges": edges}
|
||||
}
|
||||
|
||||
|
||||
def generate_document_agent(db: Session, user: User):
|
||||
"""生成文档生成Agent"""
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": 300},
|
||||
"data": {"label": "开始", "output_format": "json"}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
plan_node = {
|
||||
"id": "llm-plan",
|
||||
"type": "llm",
|
||||
"position": {"x": 250, "y": 300},
|
||||
"data": {
|
||||
"label": "文档规划",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "1500",
|
||||
"prompt": """请根据用户需求规划文档结构。
|
||||
|
||||
用户需求:{{query}}
|
||||
|
||||
请输出JSON格式的文档大纲:
|
||||
{
|
||||
"title": "文档标题",
|
||||
"sections": [
|
||||
{"name": "章节1", "content": "内容描述"},
|
||||
{"name": "章节2", "content": "内容描述"}
|
||||
]
|
||||
}"""
|
||||
}
|
||||
}
|
||||
nodes.append(plan_node)
|
||||
|
||||
generate_node = {
|
||||
"id": "llm-generate",
|
||||
"type": "llm",
|
||||
"position": {"x": 450, "y": 300},
|
||||
"data": {
|
||||
"label": "生成文档",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.5",
|
||||
"max_tokens": "4000",
|
||||
"prompt": """请根据文档规划生成完整的文档内容。
|
||||
|
||||
用户需求:{{query}}
|
||||
文档规划:{{output}}
|
||||
|
||||
请生成结构完整、内容详实的Markdown文档。"""
|
||||
}
|
||||
}
|
||||
nodes.append(generate_node)
|
||||
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 650, "y": 300},
|
||||
"data": {"label": "结束"}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
|
||||
edges.append({"id": "e1", "source": "start-1", "target": "llm-plan", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e2", "source": "llm-plan", "target": "llm-generate", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e3", "source": "llm-generate", "target": "end-1", "sourceHandle": "right", "targetHandle": "left"})
|
||||
|
||||
return {
|
||||
"name": "文档生成Agent",
|
||||
"description": "智能文档生成工具,能够根据需求规划文档结构并生成完整内容。",
|
||||
"workflow_config": {"nodes": nodes, "edges": edges}
|
||||
}
|
||||
|
||||
|
||||
def generate_data_analysis_agent(db: Session, user: User):
|
||||
"""生成数据分析Agent"""
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": 300},
|
||||
"data": {"label": "开始", "output_format": "json"}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
parse_node = {
|
||||
"id": "llm-parse",
|
||||
"type": "llm",
|
||||
"position": {"x": 250, "y": 300},
|
||||
"data": {
|
||||
"label": "数据解析",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.2",
|
||||
"max_tokens": "2000",
|
||||
"prompt": """请解析用户提供的数据。
|
||||
|
||||
数据内容:{{query}}
|
||||
|
||||
请输出JSON格式:
|
||||
{
|
||||
"data_type": "数据类型(表格/列表/文本等)",
|
||||
"structure": "数据结构描述",
|
||||
"key_fields": ["字段1", "字段2"]
|
||||
}"""
|
||||
}
|
||||
}
|
||||
nodes.append(parse_node)
|
||||
|
||||
analysis_node = {
|
||||
"id": "llm-analysis",
|
||||
"type": "llm",
|
||||
"position": {"x": 450, "y": 300},
|
||||
"data": {
|
||||
"label": "数据分析",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "3000",
|
||||
"prompt": """请对数据进行深入分析。
|
||||
|
||||
原始数据:{{query}}
|
||||
数据解析:{{output}}
|
||||
|
||||
请提供:
|
||||
1. 数据概览
|
||||
2. 关键指标
|
||||
3. 趋势分析
|
||||
4. 洞察建议
|
||||
|
||||
请以Markdown格式输出,包含数据表格和图表描述。"""
|
||||
}
|
||||
}
|
||||
nodes.append(analysis_node)
|
||||
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 650, "y": 300},
|
||||
"data": {"label": "结束"}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
|
||||
edges.append({"id": "e1", "source": "start-1", "target": "llm-parse", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e2", "source": "llm-parse", "target": "llm-analysis", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e3", "source": "llm-analysis", "target": "end-1", "sourceHandle": "right", "targetHandle": "left"})
|
||||
|
||||
return {
|
||||
"name": "数据分析Agent",
|
||||
"description": "智能数据分析工具,能够解析数据、提取关键指标并提供深度洞察。",
|
||||
"workflow_config": {"nodes": nodes, "edges": edges}
|
||||
}
|
||||
|
||||
|
||||
def generate_creative_writing_agent(db: Session, user: User):
|
||||
"""生成创意写作Agent"""
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": 300},
|
||||
"data": {"label": "开始", "output_format": "json"}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
brainstorm_node = {
|
||||
"id": "llm-brainstorm",
|
||||
"type": "llm",
|
||||
"position": {"x": 250, "y": 300},
|
||||
"data": {
|
||||
"label": "头脑风暴",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.8",
|
||||
"max_tokens": "1500",
|
||||
"prompt": """你是一个创意写作专家。请根据用户需求进行头脑风暴。
|
||||
|
||||
用户需求:{{query}}
|
||||
|
||||
请提供:
|
||||
1. 创意主题(3-5个)
|
||||
2. 故事大纲
|
||||
3. 角色设定
|
||||
4. 写作风格建议
|
||||
|
||||
请以Markdown格式输出。"""
|
||||
}
|
||||
}
|
||||
nodes.append(brainstorm_node)
|
||||
|
||||
write_node = {
|
||||
"id": "llm-write",
|
||||
"type": "llm",
|
||||
"position": {"x": 450, "y": 300},
|
||||
"data": {
|
||||
"label": "创作内容",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.9",
|
||||
"max_tokens": "4000",
|
||||
"prompt": """请根据创意方案进行创作。
|
||||
|
||||
用户需求:{{query}}
|
||||
创意方案:{{output}}
|
||||
|
||||
请创作一篇完整的作品(文章、故事、诗歌等),确保内容生动、有趣、有创意。"""
|
||||
}
|
||||
}
|
||||
nodes.append(write_node)
|
||||
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 650, "y": 300},
|
||||
"data": {"label": "结束"}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
|
||||
edges.append({"id": "e1", "source": "start-1", "target": "llm-brainstorm", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e2", "source": "llm-brainstorm", "target": "llm-write", "sourceHandle": "right", "targetHandle": "left"})
|
||||
edges.append({"id": "e3", "source": "llm-write", "target": "end-1", "sourceHandle": "right", "targetHandle": "left"})
|
||||
|
||||
return {
|
||||
"name": "创意写作Agent",
|
||||
"description": "创意写作助手,能够进行头脑风暴并创作各种类型的创意内容。",
|
||||
"workflow_config": {"nodes": nodes, "edges": edges}
|
||||
}
|
||||
|
||||
|
||||
def generate_batch_agents(db: Session, username: str = "admin"):
|
||||
"""批量生成Agent"""
|
||||
print("=" * 60)
|
||||
print("批量生成Agent")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
# 查找用户
|
||||
user = db.query(User).filter(User.username == username).first()
|
||||
if not user:
|
||||
print(f"❌ 未找到用户 '{username}',请先创建该用户")
|
||||
return
|
||||
print(f"✅ 找到用户: {user.username} (ID: {user.id})")
|
||||
print()
|
||||
|
||||
# 定义要生成的Agent列表
|
||||
agent_generators = [
|
||||
generate_text_summary_agent,
|
||||
generate_code_review_agent,
|
||||
generate_translation_agent,
|
||||
generate_qa_agent,
|
||||
generate_document_agent,
|
||||
generate_data_analysis_agent,
|
||||
generate_creative_writing_agent,
|
||||
]
|
||||
|
||||
created_count = 0
|
||||
updated_count = 0
|
||||
failed_count = 0
|
||||
|
||||
for generator in agent_generators:
|
||||
try:
|
||||
agent_data = generator(db, user)
|
||||
agent_name = agent_data["name"]
|
||||
|
||||
# 检查Agent是否已存在
|
||||
existing_agent = db.query(Agent).filter(
|
||||
Agent.name == agent_name,
|
||||
Agent.user_id == user.id
|
||||
).first()
|
||||
|
||||
if existing_agent:
|
||||
existing_agent.workflow_config = agent_data["workflow_config"]
|
||||
existing_agent.description = agent_data["description"]
|
||||
existing_agent.updated_at = datetime.now()
|
||||
existing_agent.status = "published"
|
||||
updated_count += 1
|
||||
print(f"⚠️ 更新Agent: {agent_name}")
|
||||
else:
|
||||
agent = Agent(
|
||||
id=str(uuid.uuid4()),
|
||||
name=agent_name,
|
||||
description=agent_data["description"],
|
||||
workflow_config=agent_data["workflow_config"],
|
||||
status="published",
|
||||
user_id=user.id,
|
||||
version=1
|
||||
)
|
||||
db.add(agent)
|
||||
created_count += 1
|
||||
print(f"✅ 创建Agent: {agent_name}")
|
||||
except Exception as e:
|
||||
failed_count += 1
|
||||
print(f"❌ 生成Agent失败: {generator.__name__} - {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
try:
|
||||
db.commit()
|
||||
print()
|
||||
print("=" * 60)
|
||||
print("✅ 批量生成完成!")
|
||||
print("=" * 60)
|
||||
print(f" - 新建: {created_count} 个")
|
||||
print(f" - 更新: {updated_count} 个")
|
||||
print(f" - 失败: {failed_count} 个")
|
||||
print()
|
||||
print("📋 生成的Agent列表:")
|
||||
for generator in agent_generators:
|
||||
agent_data = generator(db, user)
|
||||
print(f" • {agent_data['name']}")
|
||||
print()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(f"❌ 提交失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
generate_batch_agents(db, username="admin")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
233
backend/scripts/generate_test_agent.py
Executable file
233
backend/scripts/generate_test_agent.py
Executable file
@@ -0,0 +1,233 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
生成测试Agent - 演示左右连接
|
||||
这是一个测试Agent,用于演示节点左右连接的功能
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from app.core.database import SessionLocal
|
||||
from app.models.agent import Agent
|
||||
from app.models.user import User
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
|
||||
def generate_test_agent(db: Session, username: str = "admin"):
|
||||
"""生成测试Agent(左右连接)"""
|
||||
print("=" * 60)
|
||||
print("生成测试Agent(左右连接演示)")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
# 查找用户
|
||||
user = db.query(User).filter(User.username == username).first()
|
||||
if not user:
|
||||
print(f"❌ 未找到用户 '{username}',请先创建该用户")
|
||||
return
|
||||
print(f"✅ 找到用户: {user.username} (ID: {user.id})")
|
||||
print()
|
||||
|
||||
# 生成测试工作流配置
|
||||
# 工作流结构(横向排列,使用左右连接):
|
||||
# 开始 → 处理1 → 处理2 → 处理3 → 结束
|
||||
|
||||
nodes = []
|
||||
edges = []
|
||||
|
||||
# 节点横向排列,Y坐标相同,X坐标递增
|
||||
base_y = 300
|
||||
x_spacing = 250
|
||||
|
||||
# 1. 开始节点
|
||||
start_node = {
|
||||
"id": "start-1",
|
||||
"type": "start",
|
||||
"position": {"x": 50, "y": base_y},
|
||||
"data": {
|
||||
"label": "开始",
|
||||
"output_format": "json"
|
||||
}
|
||||
}
|
||||
nodes.append(start_node)
|
||||
|
||||
# 2. 处理节点1(LLM)
|
||||
process1_node = {
|
||||
"id": "llm-process1",
|
||||
"type": "llm",
|
||||
"position": {"x": 50 + x_spacing, "y": base_y},
|
||||
"data": {
|
||||
"label": "处理1",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "1000",
|
||||
"prompt": """你是一个测试助手。请简单处理用户输入。
|
||||
|
||||
用户输入:{{query}}
|
||||
|
||||
请输出:已处理用户输入:"{{query}}"
|
||||
"""
|
||||
}
|
||||
}
|
||||
nodes.append(process1_node)
|
||||
|
||||
# 3. 处理节点2(Transform)
|
||||
process2_node = {
|
||||
"id": "transform-process2",
|
||||
"type": "transform",
|
||||
"position": {"x": 50 + x_spacing * 2, "y": base_y},
|
||||
"data": {
|
||||
"label": "处理2",
|
||||
"mode": "merge",
|
||||
"mapping": {
|
||||
"original_input": "{{query}}",
|
||||
"processed_result": "{{output}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
nodes.append(process2_node)
|
||||
|
||||
# 4. 处理节点3(LLM)
|
||||
process3_node = {
|
||||
"id": "llm-process3",
|
||||
"type": "llm",
|
||||
"position": {"x": 50 + x_spacing * 3, "y": base_y},
|
||||
"data": {
|
||||
"label": "处理3",
|
||||
"provider": "deepseek",
|
||||
"model": "deepseek-chat",
|
||||
"temperature": "0.3",
|
||||
"max_tokens": "1000",
|
||||
"prompt": """请对处理结果进行总结。
|
||||
|
||||
原始输入:{{original_input}}
|
||||
处理结果:{{processed_result}}
|
||||
|
||||
请输出一个简洁的总结。"""
|
||||
}
|
||||
}
|
||||
nodes.append(process3_node)
|
||||
|
||||
# 5. 结束节点
|
||||
end_node = {
|
||||
"id": "end-1",
|
||||
"type": "end",
|
||||
"position": {"x": 50 + x_spacing * 4, "y": base_y},
|
||||
"data": {
|
||||
"label": "结束",
|
||||
"description": "返回最终结果"
|
||||
}
|
||||
}
|
||||
nodes.append(end_node)
|
||||
|
||||
# 创建连接(使用左右连接)
|
||||
# 注意:使用 sourceHandle='right' 和 targetHandle='left' 来指定左右连接
|
||||
edges.append({
|
||||
"id": "e1",
|
||||
"source": "start-1",
|
||||
"target": "llm-process1",
|
||||
"sourceHandle": "right", # 从开始节点的右侧连接
|
||||
"targetHandle": "left" # 连接到处理1节点的左侧
|
||||
})
|
||||
|
||||
edges.append({
|
||||
"id": "e2",
|
||||
"source": "llm-process1",
|
||||
"target": "transform-process2",
|
||||
"sourceHandle": "right", # 从处理1节点的右侧连接
|
||||
"targetHandle": "left" # 连接到处理2节点的左侧
|
||||
})
|
||||
|
||||
edges.append({
|
||||
"id": "e3",
|
||||
"source": "transform-process2",
|
||||
"target": "llm-process3",
|
||||
"sourceHandle": "right", # 从处理2节点的右侧连接
|
||||
"targetHandle": "left" # 连接到处理3节点的左侧
|
||||
})
|
||||
|
||||
edges.append({
|
||||
"id": "e4",
|
||||
"source": "llm-process3",
|
||||
"target": "end-1",
|
||||
"sourceHandle": "right", # 从处理3节点的右侧连接
|
||||
"targetHandle": "left" # 连接到结束节点的左侧
|
||||
})
|
||||
|
||||
# 创建或更新Agent
|
||||
workflow_config = {
|
||||
"nodes": nodes,
|
||||
"edges": edges
|
||||
}
|
||||
|
||||
agent = db.query(Agent).filter(
|
||||
Agent.name == "测试Agent(左右连接)",
|
||||
Agent.user_id == user.id
|
||||
).first()
|
||||
|
||||
if agent:
|
||||
agent.workflow_config = workflow_config
|
||||
agent.description = "测试Agent,演示节点左右连接功能。工作流横向排列,使用左右连接点。"
|
||||
agent.updated_at = datetime.now()
|
||||
agent.status = "published"
|
||||
print("⚠️ Agent '测试Agent(左右连接)' 已存在,将更新它...")
|
||||
else:
|
||||
agent = Agent(
|
||||
id=str(uuid.uuid4()),
|
||||
name="测试Agent(左右连接)",
|
||||
description="测试Agent,演示节点左右连接功能。工作流横向排列,使用左右连接点。",
|
||||
workflow_config=workflow_config,
|
||||
status="published",
|
||||
user_id=user.id,
|
||||
version=1
|
||||
)
|
||||
db.add(agent)
|
||||
|
||||
try:
|
||||
db.commit()
|
||||
db.refresh(agent)
|
||||
print()
|
||||
print("✅ Agent创建/更新成功!")
|
||||
print()
|
||||
print(f"📋 Agent信息:")
|
||||
print(f" - ID: {agent.id}")
|
||||
print(f" - 名称: {agent.name}")
|
||||
print(f" - 状态: {agent.status} (已发布,可直接使用)")
|
||||
print(f" - 版本: {agent.version}")
|
||||
print(f" - 节点数: {len(nodes)}")
|
||||
print(f" - 连接数: {len(edges)}")
|
||||
print()
|
||||
print("🎯 工作流特点:")
|
||||
print(" ✅ 节点横向排列")
|
||||
print(" ✅ 使用左右连接点(sourceHandle='right', targetHandle='left')")
|
||||
print(" ✅ 工作流:开始 → 处理1 → 处理2 → 处理3 → 结束")
|
||||
print()
|
||||
print("💡 使用提示:")
|
||||
print(" 1. 在Agent管理页面找到 '测试Agent(左右连接)'")
|
||||
print(" 2. 点击 '使用' 按钮开始使用")
|
||||
print(" 3. 输入任意文本进行测试")
|
||||
print(" 4. 在工作流设计器中查看左右连接的视觉效果")
|
||||
print()
|
||||
return agent
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(f"❌ 创建Agent失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
generate_test_agent(db, username="admin")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user