🧠 自主思维图生成器
一个由DSPy驱动的系统,能够自主生成思维图,并通过Pydantic基础模型进行LLM驱动的状态管理。具有带有GraphVis可视化的web UI和用于集成的MCP服务器。
🌟 主要特点
自主处理
- 单DSP签名:
ReactSignature通过推理处理所有操作 - 单个模块:
AutonomousThoughtReactor自主管理自己的状态 - LLM状态管理:系统通过基本模型决定自己的处理流程
- Try/Except/Finally循环:具有恢复机制的强大错误处理
灵活的输入/输出
- 扩展输入:灵活的输入模型,可接受任何其他字段
- 统一输出:将所有可能的输出与嵌套的基本模型结合起来
- 粒度基础模型:思维、连接、思维图、处理上下文、验证结果
Web UI和可视化
- 要求掌握交互图:HTML5+GraphVis可视化
- 实时处理:显示自主决策过程
- 漂亮的UI:具有悬停效果的现代渐变设计
MCP服务器
- 4工具:生成、验证、扩展想法并获取统计数据
- 应用程序接口:与MCP协议完全集成
- 异步支持:非阻塞操作
🚀 快速开始
安装
cd thought_graph
pip install -r requirements.txt运行模式
1.演示模式(自主处理)
python main.py demo显示自治系统处理不同输入的完整日志记录。
2.Web UI模式
python main.py web在以下位置启动web服务器http://localhost:52336
3.MCP服务器模式
python main.py mcp启动MCP服务器以与其他工具集成。
🏗️ 建筑
核心组件
1.自主反应堆(AutonomousThoughtReactor)
class AutonomousThoughtReactor(dspy.Module):
def forward(self, expanded_input: ExpandedInput, use_fake_data: bool = True) -> UnifiedOutput:
# Main autonomous loop with try/except/finally
while context.should_continue and context.iteration_count < context.max_iterations:
try:
# LLM decides next action based on current state
result = self._process_with_llm(expanded_input, context, graph)
# Update state based on LLM decision
except Exception as e:
# Error handling and recovery
finally:
# Always update output with current state2.单一签名(ReactSignature)
class ReactSignature(dspy.Signature):
"""React to input and context, managing own state autonomously through reasoning."""
input_data = dspy.InputField(desc="Input data as JSON string containing ExpandedInput model")
context = dspy.InputField(desc="Current processing context as JSON string")
reasoning = dspy.OutputField(desc="Step-by-step reasoning about what to do next")
action = dspy.OutputField(desc="Next action to take")
output_data = dspy.OutputField(desc="Output data as JSON string containing UnifiedOutput model")
updated_context = dspy.OutputField(desc="Updated processing context as JSON string")3.带嵌套的基础模型
class ProcessingContext(BaseModel):
"""Context for LLM to manage its own state"""
current_state: SystemState = SystemState.INITIALIZING
iteration_count: int = 0
should_continue: bool = True
reasoning: str = ""
next_action: str = ""
class UnifiedOutput(BaseModel):
"""Union of all possible outputs with nesting"""
graph: Optional[ThoughtGraph] = None
validation: Optional[ValidationResult] = None
context: Optional[ProcessingContext] = None
thoughts: Optional[List[Thought]] = Field(default_factory=list)
connections: Optional[List[Connection]] = Field(default_factory=list)
class Config:
extra = "allow" # Allow additional fields状态流
- 初始化 → 设置初始上下文
- 处理 → 产生初步想法
- 扩大 → 建立联系和更深入的思考
- 确认 → 检查质量和完整性
- 完成 → 最终确定输出
- 错误 → 处理错误并尝试恢复
🎨 Web UI功能
- 交互式图形可视化:单击节点查看详细信息
- 实时生成:观察自主过程的展开
- 漂亮的设计:带有渐变和动画的现代UI
- 响应式:适用于台式机和移动设备
- 输入示例:从预定义的示例快速开始
🔧 MCP服务器工具
1. generate_thought_graph
根据输入文本生成完整的思维图。
2. validate_thought_graph
验证现有图表并提供反馈。
3. expand_thought
用相关概念扩展特定的思想。
4. get_graph_statistics
获取关于思维图的详细统计数据。
📊 输出示例
{
"success": true,
"input_text": "artificial intelligence",
"graph": {
"thoughts": [
{
"id": "abc123",
"content": "Machine learning algorithms that can adapt and learn",
"thought_type": "concept",
"confidence": 0.85,
"depth": 1
}
],
"connections": [
{
"source_id": "abc123",
"target_id": "def456",
"relationship": "leads to",
"strength": 0.7
}
]
},
"processing_log": {
"iterations": 5,
"final_state": "completed",
"reasoning": "Generated comprehensive thought network with balanced coverage"
}
}🧪 测试
# Test MCP tools
python test_mcp.py
# Test web API
curl http://localhost:52336/api/generate/creativity
# Test autonomous processing
python main.py demo🔮 高级用法
自定义上下文
expanded_input = ExpandedInput(
original_text="quantum computing",
context={
"focus": "technical_depth",
"style": "academic"
},
preferences={
"max_depth": 4,
"creative_mode": False
},
domain_knowledge="physics" # Extra field allowed
)真正的LLM集成
更换模拟LM dspy_program.py:
import dspy
lm = dspy.OpenAI(model="gpt-4", max_tokens=500)
dspy.settings.configure(lm=lm)🤝 贡献
- 分叉存储库
- 创建要素分支
- 添加新功能的测试
- 提交拉取请求
📝 许可证
MIT许可证-有关详细信息,请参阅许可证文件。
______________________________________________________________________
内置于❤️ 使用DSPy、Pydantic、FastAPI和GraphVis
