agno_transfer_agent.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from logging import log
  2. from textwrap import dedent
  3. from agno.agent import Agent
  4. from agno.models.openai import OpenAILike
  5. from agno.playground import Playground
  6. from agno.storage.sqlite import SqliteStorage
  7. from agno.tools.yfinance import YFinanceTools
  8. from agno.tools import tool
  9. import os
  10. from agno.utils.log import log_info
  11. from dotenv import load_dotenv
  12. load_dotenv()
  13. #数据存储位置
  14. agent_storage: str = "./曹航/3/transfer/tmp/agents.db"
  15. #测试
  16. finance_agent = Agent(
  17. name="Finance Agent",
  18. model=OpenAILike(id="qwen3-32b",
  19. api_key=os.getenv("BAILIAN_API_KEY"),
  20. base_url=os.getenv("BAILIAN_API_BASE_URL"),
  21. request_params={"extra_body": {"enable_thinking": False}},),
  22. tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)],
  23. instructions=["Always use tables to display data"],
  24. storage=SqliteStorage(table_name="finance_agent", db_file=agent_storage),
  25. add_datetime_to_instructions=True,
  26. add_history_to_messages=True,
  27. num_history_responses=5,
  28. markdown=True,
  29. )
  30. # 从transfer/prompt目录读取prompt文件
  31. # with open("transfer/prompt/prompt.txt", "r", encoding="utf-8") as f:
  32. # prompt_text = f.read()
  33. prompt_text="""
  34. ## 背景与目标
  35. 你是一个金融助手 Agent,使用 Qwen3-32B 模型驱动,负责协助用户完成向他人账户转账的流程。/
  36. 当用户提问你其它与转账不相关的问题时,你应该时刻提醒并保持礼貌。/
  37. 你将通过自然语言与用户对话,收集必要信息,确认并调用转账接口完成操作。
  38. 请根据以下流程与规则进行对话与操作。
  39. ## 任务流程
  40. ### 进行信息收集
  41. 请通过对话与用户交互,依次确认并记录以下三项转账信息:
  42. 1. 通过对方姓名查询账户是否存在(`get_contact`)
  43. 2. 确认转账金额是否大于账户余额(`get_balance`)
  44. 3. 余额不足时,请重新向用户收集信息,继续转账(`replay_to_user`)
  45. > 请在用户表达不清或信息缺失时,请通过方法 `replay_to_user` 发起澄清。
  46. ### 执行转账
  47. 确认收集信息后,请总结信息,并请发起转账(`transfer`)
  48. """
  49. # 创建用户列表
  50. users = [
  51. {"name": "张三","phone": "13800138000","amount": 1000.0},
  52. {"name": "李四","phone": "13800138001","amount": 500.0},
  53. ]
  54. # 定义转账相关的工具函数
  55. def get_contact(user_name: str) -> bool:
  56. """
  57. 检查账户是否存在
  58. Args:
  59. account_number: 账号
  60. Returns:
  61. bool: 账户是否存在
  62. """
  63. # 这里模拟账户检查逻辑
  64. if user_name == "张三":
  65. return True
  66. else:
  67. return False
  68. def get_balance() -> float:
  69. """
  70. 查询账户余额
  71. Returns:
  72. float: 账户余额
  73. """
  74. balance=users[1]["amount"]
  75. return balance
  76. #TODO 做用户确认
  77. # @tool(requires_user_input=True,user_input_fields=["amount"])
  78. def transfer(user_name, phone: str, amount: float) -> bool:
  79. """
  80. 执行转账操作
  81. Args:
  82. to_account: 转入账号
  83. amount: 转账金额
  84. Returns:
  85. bool: 转账是否成功
  86. """
  87. # 这里模拟转账逻辑
  88. users[0]["amount"] += amount
  89. print(f"向{user_name}转账{amount}元成功")
  90. return True
  91. def replay_to_user(message: str)->str:
  92. """
  93. 当前步骤需要用户输入时,请通过此方法向用户提问
  94. 向用户提问获取信息,并记录到log
  95. Args:
  96. message: 提问内容
  97. """
  98. # 这里实现向用户提问的逻辑
  99. #只是记录log
  100. log_info(message)
  101. return "请等待用户回答"
  102. transfer_agent = Agent(
  103. name="Transfer Agent",
  104. model=OpenAILike(id="qwen3-32b",
  105. api_key=os.getenv("BAILIAN_API_KEY"),
  106. base_url=os.getenv("BAILIAN_API_BASE_URL"),
  107. request_params={"extra_body": {"enable_thinking": False}},),
  108. tools=[get_contact,get_balance,transfer,replay_to_user],
  109. #TODO memory记忆
  110. # memory=SqliteStorage(table_name="transfer_agent", db_file=agent_storage),
  111. instructions=dedent(prompt_text),
  112. # Store the agent sessions in a sqlite database
  113. storage=SqliteStorage(table_name="transfer_agent_session", db_file=agent_storage),
  114. show_tool_calls=True,
  115. # Adds the current date and time to the instructions
  116. add_datetime_to_instructions=True,
  117. # Adds the history of the conversation to the messages
  118. add_history_to_messages=True,
  119. # Number of history responses to add to the messages
  120. num_history_responses=5,
  121. # Adds markdown formatting to the messages
  122. markdown=True,
  123. )
  124. # 启动playground-server
  125. playground = Playground(agents=[transfer_agent, finance_agent])
  126. app = playground.get_app()
  127. if __name__ == "__main__":
  128. playground.serve("agno_transfer_agent:app", reload=True)