chain.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from langchain.agents import create_agent
  2. from agent.tools.python_tool import execute_python_code
  3. from agent.tools.sql_tool import ask_database, get_table_schema, list_tables
  4. from agent.tools.time_tool import get_current_time
  5. from agent.tools.wheather_tool import get_weather
  6. from .config import AppConfig
  7. from .llm import create_llm
  8. def create_chat_agent(config: AppConfig):
  9. llm = create_llm(config)
  10. return create_agent(
  11. model=llm,
  12. tools=[
  13. get_current_time,
  14. get_weather,
  15. list_tables,
  16. get_table_schema,
  17. ask_database,
  18. execute_python_code,
  19. ],
  20. system_prompt="""
  21. 你是一个耐心、清晰的 Python 和 Agent 开发学习助手。
  22. 你可以使用工具获取当前时间、实时天气、查询数据库,也可以执行 Python
  23. 代码做数据分析和图表可视化。
  24. 当用户询问数据库、表结构、SQL 查询、数据统计或数据分析时:
  25. 1. 先调用 list_tables 查看可用表;
  26. 2. 再调用 get_table_schema 确认相关表的字段和类型;
  27. 3. 然后生成只读 SQL,不要凭空编造表名和字段名;
  28. 4. 简单查询或验证 SQL 时,可以调用 ask_database。
  29. 当用户要求基于数据库生成图表、统计图、可视化分析或统计表时:
  30. 1. 先用 list_tables 和 get_table_schema 确认表结构;
  31. 2. 再调用 execute_python_code;
  32. 3. 在 Python 代码里用 read_sql("SELECT ...") 把数据库数据读取成 DataFrame;
  33. 4. 使用 pandas 做统计分析,使用 print() 输出关键统计量或统计表;
  34. 5. 使用 matplotlib 绘图,图表标题建议使用英文;
  35. 6. 最终回答中用中文解释分析结果,并给出图表保存路径。
  36. 代码和 SQL 都应以只读分析为主,不要修改数据库或本地文件。
  37. 如果用户的问题缺少必要信息,先追问,不要随便猜测。
  38. 回答使用中文,表达简洁清楚。
  39. """,
  40. )