Files
autogen/usage_examples.py
2026-03-12 13:27:03 +08:00

231 lines
6.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
使用示例 - 展示如何使用 AutoGen SDLC 系统
包含多个实际场景的演示
"""
import os
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from autogen_sdls_system import AutoGenSDLCSystem
def example_1_basic_workflow():
"""示例 1: 基本工作流 - 电池健康预测 API"""
print("\n" + "=" * 70)
print("示例 1: 基本 SDLC 工作流")
print("=" * 70)
api_key = os.getenv("DASHSCOPE_API_KEY")
if not api_key:
print("❌ 请设置 DASHSCOPE_API_KEY 环境变量")
return
system = AutoGenSDLCSystem(api_key=api_key)
requirement = """
我需要一个电池健康状态 (SOH) 预测 API要求
1. 接收电池的电压、电流、温度数据
2. 输出健康度百分比0-100%
3. 符合汽车行业标准
4. 包含错误处理和边界情况检测
"""
print(f"\n📋 需求:{requirement}")
result = system.run_workflow(requirement, max_round=15)
if result["success"]:
print("\n✅ 工作流完成!")
print(f"📄 摘要:{result['summary'][:300]}...")
else:
print(f"\n❌ 工作流失败:{result.get('error')}")
def example_2_custom_agents():
"""示例 2: 自定义 Agent 配置"""
print("\n" + "=" * 70)
print("示例 2: 自定义 Agent 配置")
print("=" * 70)
from config.llm_config import get_llm_config
from agents import create_pm_agent, create_qa_agent
api_key = os.getenv("DASHSCOPE_API_KEY")
if not api_key:
print("❌ 请设置 DASHSCOPE_API_KEY 环境变量")
return
# 使用不同的模型配置
llm_config = get_llm_config(
model="qwen3.5-flash",
api_key=api_key,
temperature=0.5, # 降低温度使输出更稳定
max_tokens=3000
)
# 创建自定义 Agent
pm_agent = create_pm_agent(llm_config=llm_config)
qa_agent = create_qa_agent(llm_config=llm_config)
print("✅ 已创建自定义配置的 Agent")
print(f" PM Agent: {pm_agent.name}")
print(f" QA Agent: {qa_agent.name}")
def example_3_direct_agent_call():
"""示例 3: 直接调用单个 Agent"""
print("\n" + "=" * 70)
print("示例 3: 直接调用 PM Agent 生成 SRS")
print("=" * 70)
from agents import ProductManagerAgent
from config.llm_config import get_agent_llm_config
api_key = os.getenv("DASHSCOPE_API_KEY")
if not api_key:
print("❌ 请设置 DASHSCOPE_API_KEY 环境变量")
return
# 创建 PM Agent
llm_config = get_agent_llm_config("PM_Agent", api_key=api_key)
pm_agent = ProductManagerAgent(llm_config=llm_config)
# 简单需求
requirement = "开发一个车载蓝牙连接管理模块"
print(f"\n📋 需求:{requirement}")
print("\n🤖 PM Agent 正在生成 SRS...\n")
try:
srs = pm_agent.generate_srs(requirement)
print("\n✅ SRS 生成成功!")
print(f"📄 前 500 字符预览:\n{srs[:500]}...")
except Exception as e:
print(f"\n❌ 生成失败:{e}")
def example_4_test_generation():
"""示例 4: QA Agent 生成测试用例"""
print("\n" + "=" * 70)
print("示例 4: QA Agent 生成测试用例")
print("=" * 70)
from agents import QAAgent
from config.llm_config import get_agent_llm_config
api_key = os.getenv("DASHSCOPE_API_KEY")
if not api_key:
print("❌ 请设置 DASHSCOPE_API_KEY 环境变量")
return
# 创建 QA Agent
llm_config = get_agent_llm_config("QA_Agent", api_key=api_key)
qa_agent = QAAgent(llm_config=llm_config)
# 简单的 SRS 片段
srs_sample = """
【功能性需求】
FR-001: 系统应能接收电池电压数据(范围 0-5V
FR-002: 系统应能计算 SOH 百分比(范围 0-100%
FR-003: 系统应能检测异常数据并报错
【非功能性需求】
NFR-001: 响应时间 < 100ms
NFR-002: 符合 MISRA-C 规范
"""
print("\n📋 SRS 样本:")
print(srs_sample)
print("\n🤖 QA Agent 正在生成测试用例...\n")
try:
test_cases = qa_agent.generate_test_cases(srs_sample)
print("\n✅ 测试用例生成成功!")
print(f"📄 预览:\n{test_cases[:500]}...")
except Exception as e:
print(f"\n❌ 生成失败:{e}")
def example_5_code_generation():
"""示例 5: Dev Agent 生成代码"""
print("\n" + "=" * 70)
print("示例 5: Dev Agent 生成代码")
print("=" * 70)
from agents import DevAgent
from config.llm_config import get_agent_llm_config
api_key = os.getenv("DASHSCOPE_API_KEY")
if not api_key:
print("❌ 请设置 DASHSCOPE_API_KEY 环境变量")
return
# 创建 Dev Agent
llm_config = get_agent_llm_config("Dev_Agent", api_key=api_key)
dev_agent = DevAgent(llm_config=llm_config)
# SRS 和测试样本
srs_sample = """
FR-001: 实现 SOH 计算函数
FR-002: 输入验证(电压 0-5V温度 -20~60°C
FR-003: 输出限制在 0-100%
"""
test_sample = """
def test_soh_calculation():
assert calculate_soh(3.7, 2.0, 25)["soh"] > 0
assert calculate_soh(0, 0, 25)["soh"] == 0
"""
print("\n📋 需求和测试样本已准备")
print("\n🤖 Dev Agent 正在生成代码...\n")
try:
code = dev_agent.generate_code(srs_sample, test_sample)
print("\n✅ 代码生成成功!")
print(f"📄 代码预览:\n{code[:500]}...")
except Exception as e:
print(f"\n❌ 生成失败:{e}")
def run_all_examples():
"""运行所有示例"""
print("\n" + "=" * 70)
print("🚀 AutoGen SDLC 系统 - 使用示例合集")
print("=" * 70)
examples = [
("直接调用 PM Agent", example_3_direct_agent_call),
("QA Agent 生成测试", example_4_test_generation),
("Dev Agent 生成代码", example_5_code_generation),
("自定义 Agent 配置", example_2_custom_agents),
("完整工作流", example_1_basic_workflow),
]
for name, func in examples:
print(f"\n\n{'='*70}")
print(f"▶️ 运行示例:{name}")
print('='*70)
try:
func()
except Exception as e:
print(f"\n❌ 示例执行失败:{e}")
input("\n按 Enter 键继续下一个示例...")
print("\n" + "=" * 70)
print("✅ 所有示例运行完成!")
print("=" * 70)
if __name__ == "__main__":
# 可以选择运行单个示例或全部示例
# example_3_direct_agent_call()
# example_4_test_generation()
# example_5_code_generation()
# 运行所有示例
run_all_examples()