main.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """
  2. 数据库Agent助手 - 主程序
  3. 演示如何使用DatabaseAgent进行自然语言查询
  4. """
  5. import os
  6. from dotenv import load_dotenv
  7. from hello_agents import HelloAgentsLLM
  8. from react_agent import DatabaseAgent, DatabaseConfig
  9. load_dotenv()
  10. def main():
  11. print("=" * 60)
  12. print("🤖 数据库Agent助手")
  13. print("=" * 60)
  14. llm = HelloAgentsLLM()
  15. db_config = DatabaseConfig()
  16. if not db_config.validate():
  17. print("❌ 数据库配置不完整,请检查.env文件")
  18. print("需要配置: DB_HOST, DB_PORT, DB_SERVICE_NAME, DB_USERNAME, DB_PASSWORD")
  19. return
  20. agent = DatabaseAgent(
  21. name="DatabaseAssistant",
  22. llm=llm,
  23. db_config=db_config,
  24. max_steps=5
  25. )
  26. print("\n📝 示例查询:")
  27. print("1. 查询所有员工信息")
  28. print("2. 查询工资大于5000的员工")
  29. print("3. 统计各部门的员工数量")
  30. print("4. 查询最近入职的5名员工")
  31. print("5. 退出")
  32. while True:
  33. print("\n" + "=" * 60)
  34. user_input = input("请输入您的查询 (或输入 '5' 退出): ").strip()
  35. if user_input.lower() in ['5', 'exit', 'quit', '退出']:
  36. print("👋 感谢使用数据库Agent助手!")
  37. break
  38. if not user_input:
  39. print("⚠️ 请输入有效的查询")
  40. continue
  41. try:
  42. result = agent.run(user_input)
  43. print("\n" + "=" * 60)
  44. print("📊 查询结果:")
  45. print("=" * 60)
  46. print(result)
  47. except Exception as e:
  48. print(f"❌ 执行查询时出错: {e}")
  49. if __name__ == "__main__":
  50. main()