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

140 lines
3.7 KiB
Python
Raw Permalink 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.

"""
PM Agent - 产品经理智能体
负责需求消歧与规格生成
"""
from autogen import AssistantAgent
from typing import Dict, Any, Optional
import os
from pathlib import Path
from config.llm_config import get_agent_llm_config, PM_PROMPT
class ProductManagerAgent:
"""产品经理 Agent负责生成软件需求规格说明书"""
def __init__(self, llm_config: Optional[Dict] = None):
"""
初始化 PM Agent
Args:
llm_config: LLM 配置,为 None 时使用默认配置
"""
self.llm_config = llm_config or get_agent_llm_config("PM_Agent")
# 创建 AutoGen AssistantAgent
self.agent = AssistantAgent(
name="PM_Agent",
system_message=PM_PROMPT,
llm_config=self.llm_config,
description="资深软件产品经理,专注于汽车嵌入式系统领域",
human_input_mode="NEVER" # 全自动模式
)
self.workspace_dir = Path("workspace")
self.workspace_dir.mkdir(exist_ok=True)
def generate_srs(self, user_requirement: str) -> str:
"""
根据用户需求生成 SRS 文档
Args:
user_requirement: 用户输入的原始需求
Returns:
生成的 SRS 文档内容
"""
prompt = f"""
请根据以下用户需求生成完整的《软件需求规格说明书 (SRS)》:
用户需求:{user_requirement}
请确保输出包含:
1. 文档标题和版本信息
2. 功能性需求列表FR-001, FR-002...
3. 非功能性需求NFR-001, NFR-002...
4. 验收标准AC-001, AC-002...
5. 潜在风险与边缘情况
请以 Markdown 格式输出完整文档。
"""
# 调用 Agent 生成 SRS
response = self.agent.generate_reply(
messages=[{"role": "user", "content": prompt}]
)
srs_content = response if isinstance(response, str) else str(response)
# 保存到文件
srs_file = self.workspace_dir / "SRS.md"
with open(srs_file, 'w', encoding='utf-8') as f:
f.write(srs_content)
print(f"✅ SRS 文档已生成:{srs_file}")
return srs_content
def refine_requirements(
self,
original_srs: str,
feedback: str
) -> str:
"""
根据反馈优化需求
Args:
original_srs: 原始 SRS 文档
feedback: 反馈意见
Returns:
优化后的 SRS 文档
"""
prompt = f"""
请根据以下反馈优化现有的 SRS 文档:
原始 SRS:
{original_srs[:2000]}... # 限制长度避免超出上下文
反馈意见:
{feedback}
请输出优化后的完整 SRS 文档。
"""
response = self.agent.generate_reply(
messages=[{"role": "user", "content": prompt}]
)
refined_srs = response if isinstance(response, str) else str(response)
# 更新文件
srs_file = self.workspace_dir / "SRS.md"
with open(srs_file, 'w', encoding='utf-8') as f:
f.write(refined_srs)
print(f"✅ SRS 文档已更新:{srs_file}")
return refined_srs
def create_pm_agent(llm_config: Optional[Dict] = None) -> AssistantAgent:
"""
创建 PM AgentAutoGen 原生格式)
Args:
llm_config: LLM 配置
Returns:
AutoGen AssistantAgent 实例
"""
config = llm_config or get_agent_llm_config("PM_Agent")
agent = AssistantAgent(
name="PM_Agent",
system_message=PM_PROMPT,
llm_config=config,
description="资深软件产品经理",
human_input_mode="NEVER"
)
return agent