Files
2026-02-26 13:43:44 +08:00

98 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""
LangChain + LangGraph 脚手架项目主入口
"""
import sys
import os
from workflows.workflow_manager import WorkflowManager, WorkflowType
def interactive_cli():
"""交互式命令行界面"""
print("🚀 LangChain + LangGraph Scaffolding")
print("=" * 50)
# 允许通过命令行指定模型配置段,例如 python main.py gpt-3.5-turbo
model_section = sys.argv[1] if len(sys.argv) > 1 else None
manager = WorkflowManager(default_model_section=model_section)
while True:
print("\nAvailable workflows:")
for i, workflow in enumerate(manager.get_available_workflows(), 1):
print(f"{i}. {workflow}")
print("0. Exit")
try:
choice = input("\nSelect workflow (0-2): ").strip()
if choice == "0":
print("Goodbye!")
break
elif choice == "1":
workflow_type = WorkflowType.CONVERSATION
print("\n💬 Conversation Mode - Type 'quit' to return to menu")
elif choice == "2":
workflow_type = WorkflowType.TOOL_USING
print("\n🔧 Tool Mode - Type 'quit' to return to menu")
else:
print("Invalid choice")
continue
# 交互会话
session_id = None
while True:
user_input = input("\nYou: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
break
if not user_input:
continue
try:
result = manager.execute_workflow(
workflow_type,
user_input,
session_id=session_id
)
session_id = result['session_id']
# 提取并显示回复
last_message = result['result']['messages'][-1]
if hasattr(last_message, 'content') and last_message.content:
print(f"AI: {last_message.content}")
# 如有工具调用则显示
if hasattr(last_message, 'tool_calls') and last_message.tool_calls:
print(f"🔧 Tools used: {[tc['name'] for tc in last_message.tool_calls]}")
except Exception as e:
print(f"❌ Error: {e}")
except KeyboardInterrupt:
print("\n\nGoodbye!")
break
except Exception as e:
print(f"Error: {e}")
def main():
"""主函数"""
try:
from config import Config
Config.validate_config()
interactive_cli()
except Exception as e:
print(f"❌ Configuration error: {e}")
print("\nPlease make sure to:")
print("1. Copy 'config/config.ini.example' to 'config/config.ini'")
print("2. Set your API key in the 'config/config.ini' file")
print("3. Install dependencies: pip install -r requirements.txt")
sys.exit(1)
if __name__ == "__main__":
main()