|
@@ -1,258 +0,0 @@
|
|
|
-{
|
|
|
- "cells": [
|
|
|
- {
|
|
|
- "cell_type": "code",
|
|
|
- "execution_count": 4,
|
|
|
- "id": "38787c0b",
|
|
|
- "metadata": {},
|
|
|
- "outputs": [],
|
|
|
- "source": [
|
|
|
- "from textwrap import dedent\n",
|
|
|
- "\n",
|
|
|
- "from agno.agent import Agent\n",
|
|
|
- "from agno.models.openai import OpenAILike\n",
|
|
|
- "from agno.playground import Playground\n",
|
|
|
- "from agno.storage.sqlite import SqliteStorage\n",
|
|
|
- "from agno.tools.yfinance import YFinanceTools\n",
|
|
|
- "from agno.tools import tool\n",
|
|
|
- "\n",
|
|
|
- "import os\n",
|
|
|
- "from dotenv import load_dotenv\n",
|
|
|
- "load_dotenv()\n",
|
|
|
- "#数据存储位置\n",
|
|
|
- "agent_storage: str = \"transfer/tmp/agents.db\"\n",
|
|
|
- "\n",
|
|
|
- "#测试\n",
|
|
|
- "finance_agent = Agent(\n",
|
|
|
- " name=\"Finance Agent\",\n",
|
|
|
- " model=OpenAILike(id=\"qwen3-32b\",\n",
|
|
|
- " api_key=os.getenv(\"BAILIAN_API_KEY\"), \n",
|
|
|
- " base_url=os.getenv(\"BAILIAN_API_BASE_URL\"),\n",
|
|
|
- " request_params={\"extra_body\": {\"enable_thinking\": False}},),\n",
|
|
|
- " tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True)],\n",
|
|
|
- " instructions=[\"Always use tables to display data\"],\n",
|
|
|
- " storage=SqliteStorage(table_name=\"finance_agent\", db_file=agent_storage),\n",
|
|
|
- " add_datetime_to_instructions=True,\n",
|
|
|
- " add_history_to_messages=True,\n",
|
|
|
- " num_history_responses=5,\n",
|
|
|
- " markdown=True,\n",
|
|
|
- ")"
|
|
|
- ]
|
|
|
- },
|
|
|
- {
|
|
|
- "cell_type": "code",
|
|
|
- "execution_count": 5,
|
|
|
- "id": "ee6a0633",
|
|
|
- "metadata": {},
|
|
|
- "outputs": [],
|
|
|
- "source": [
|
|
|
- "# 从transfer/prompt目录读取prompt文件\n",
|
|
|
- "# with open(\"transfer/prompt/prompt.txt\", \"r\", encoding=\"utf-8\") as f:\n",
|
|
|
- "# prompt_text = f.read()\n",
|
|
|
- "\n",
|
|
|
- "prompt_text=\"\"\"\n",
|
|
|
- " ## 背景与目标\n",
|
|
|
- " 你是一个金融助手 Agent,使用 Qwen3-32B 模型驱动,负责协助用户完成向他人账户转账的流程。/\n",
|
|
|
- " 当用户提问你其它与转账不相关的问题时,你应该时刻提醒并保持礼貌。/\n",
|
|
|
- " 你将通过自然语言与用户对话,收集必要信息,确认并调用转账接口完成操作。\n",
|
|
|
- " 请根据以下流程与规则进行对话与操作。\n",
|
|
|
- "\n",
|
|
|
- " ## 任务流程\n",
|
|
|
- "\n",
|
|
|
- " ### 第一步:信息收集\n",
|
|
|
- " 请通过对话与用户交互,依次确认并记录以下三项转账信息:\n",
|
|
|
- "\n",
|
|
|
- " 1. 通过对方姓名查询账户是否存在(`get_contact`)\n",
|
|
|
- " 2. 确认转账金额是否大于账户余额(`get_balance`)\n",
|
|
|
- " 3. 余额不足时,请重新向用户收集信息,继续转账(`replay_to_user`)\n",
|
|
|
- "\n",
|
|
|
- " > 请在用户表达不清或信息缺失时,请通过方法 `replay_to_user` 发起澄清。\n",
|
|
|
- "\n",
|
|
|
- " ### 第二步:执行转账\n",
|
|
|
- " 请使用适当方法发起转账(`transfer`)\n",
|
|
|
- "\n",
|
|
|
- "\"\"\"\n",
|
|
|
- "# 创建用户列表\n",
|
|
|
- "users = [\n",
|
|
|
- " {\"name\": \"张三\",\"phone\": \"13800138000\",\"amount\": 1000.0},\n",
|
|
|
- " {\"name\": \"李四\",\"phone\": \"13800138001\",\"amount\": 500.0},\n",
|
|
|
- "]\n",
|
|
|
- "\n",
|
|
|
- "# 定义转账相关的工具函数\n",
|
|
|
- "def get_contact(user_name: str) -> bool:\n",
|
|
|
- " \"\"\"\n",
|
|
|
- " 检查账户是否存在\n",
|
|
|
- " Args:\n",
|
|
|
- " account_number: 账号\n",
|
|
|
- " Returns:\n",
|
|
|
- " bool: 账户是否存在\n",
|
|
|
- " \"\"\"\n",
|
|
|
- " # 这里模拟账户检查逻辑\n",
|
|
|
- " if user_name == \"张三\":\n",
|
|
|
- " return True\n",
|
|
|
- " else:\n",
|
|
|
- " return False\n",
|
|
|
- "\n",
|
|
|
- "def get_balance() -> float:\n",
|
|
|
- " \"\"\"\n",
|
|
|
- " 查询账户余额\n",
|
|
|
- " Returns:\n",
|
|
|
- " float: 账户余额\n",
|
|
|
- " \"\"\"\n",
|
|
|
- " balance=users[1][\"amount\"]\n",
|
|
|
- " return balance\n",
|
|
|
- "\n",
|
|
|
- "#TODO 做用户确认\n",
|
|
|
- "# @tool(requires_user_input=True,user_input_fields=[\"amount\"])\n",
|
|
|
- "def transfer(user_name, phone: str, amount: float) -> bool:\n",
|
|
|
- " \"\"\"\n",
|
|
|
- " 执行转账操作\n",
|
|
|
- " Args:\n",
|
|
|
- " to_account: 转入账号 \n",
|
|
|
- " amount: 转账金额\n",
|
|
|
- " Returns:\n",
|
|
|
- " bool: 转账是否成功\n",
|
|
|
- " \"\"\"\n",
|
|
|
- " # 这里模拟转账逻辑\n",
|
|
|
- " users[0][\"amount\"] += amount\n",
|
|
|
- " print(f\"向{user_name}转账{amount}元成功\")\n",
|
|
|
- " return True\n",
|
|
|
- "\n",
|
|
|
- "def replay_to_user(message: str) -> str:\n",
|
|
|
- " \"\"\"\n",
|
|
|
- " 当当前步骤需要用户输入时,请通过此方法向用户提问\n",
|
|
|
- " 向用户提问获取信息\n",
|
|
|
- " Args:\n",
|
|
|
- " Returns:\n",
|
|
|
- " str: 用户回答\n",
|
|
|
- " \"\"\"\n",
|
|
|
- " # 这里实现向用户提问的逻辑\n",
|
|
|
- " return input(message)\n",
|
|
|
- "\n",
|
|
|
- "\n",
|
|
|
- "transfer_agent = Agent(\n",
|
|
|
- " name=\"Transfer Agent\",\n",
|
|
|
- " model=OpenAILike(id=\"qwen3-32b\",\n",
|
|
|
- " api_key=os.getenv(\"BAILIAN_API_KEY\"), \n",
|
|
|
- " base_url=os.getenv(\"BAILIAN_API_BASE_URL\"),\n",
|
|
|
- " request_params={\"extra_body\": {\"enable_thinking\": False}},),\n",
|
|
|
- " tools=[get_contact,get_balance,transfer,replay_to_user],\n",
|
|
|
- " #TODO memory记忆\n",
|
|
|
- " # memory=SqliteStorage(table_name=\"transfer_agent\", db_file=agent_storage),\n",
|
|
|
- " instructions=dedent(prompt_text),\n",
|
|
|
- " # Store the agent sessions in a sqlite database\n",
|
|
|
- " storage=SqliteStorage(table_name=\"transfer_agent\", db_file=agent_storage),\n",
|
|
|
- " show_tool_calls=True,\n",
|
|
|
- " # Adds the current date and time to the instructions\n",
|
|
|
- " add_datetime_to_instructions=True,\n",
|
|
|
- " # Adds the history of the conversation to the messages\n",
|
|
|
- " add_history_to_messages=True,\n",
|
|
|
- " # Number of history responses to add to the messages\n",
|
|
|
- " num_history_responses=5,\n",
|
|
|
- " # Adds markdown formatting to the messages\n",
|
|
|
- " markdown=True,\n",
|
|
|
- ")"
|
|
|
- ]
|
|
|
- },
|
|
|
- {
|
|
|
- "cell_type": "code",
|
|
|
- "execution_count": null,
|
|
|
- "id": "df67e40a",
|
|
|
- "metadata": {},
|
|
|
- "outputs": [],
|
|
|
- "source": []
|
|
|
- },
|
|
|
- {
|
|
|
- "cell_type": "code",
|
|
|
- "execution_count": null,
|
|
|
- "id": "c5c13c01",
|
|
|
- "metadata": {},
|
|
|
- "outputs": [
|
|
|
- {
|
|
|
- "data": {
|
|
|
- "text/html": [
|
|
|
- "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; text-decoration-color: #000080\">INFO</span> Starting playground on <span style=\"color: #0000ff; text-decoration-color: #0000ff; text-decoration: underline\">http://localhost:7777</span> \n",
|
|
|
- "</pre>\n"
|
|
|
- ],
|
|
|
- "text/plain": [
|
|
|
- "\u001b[34mINFO\u001b[0m Starting playground on \u001b[4;94mhttp://localhost:7777\u001b[0m \n"
|
|
|
- ]
|
|
|
- },
|
|
|
- "metadata": {},
|
|
|
- "output_type": "display_data"
|
|
|
- },
|
|
|
- {
|
|
|
- "data": {
|
|
|
- "text/html": [
|
|
|
- "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #008080; text-decoration-color: #008080\">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Agent Playground ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓</span>\n",
|
|
|
- "<span style=\"color: #008080; text-decoration-color: #008080\">┃</span> <span style=\"color: #008080; text-decoration-color: #008080\">┃</span>\n",
|
|
|
- "<span style=\"color: #008080; text-decoration-color: #008080\">┃</span> <span style=\"color: #008080; text-decoration-color: #008080\">┃</span>\n",
|
|
|
- "<span style=\"color: #008080; text-decoration-color: #008080\">┃</span> <span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">Playground URL:</span> <a href=\"https://app.agno.com/playground?endpoint=localhost%3A7777/v1\" target=\"_blank\">https://app.agno.com/playground?endpoint=localhost%3A7777/v1</a> <span style=\"color: #008080; text-decoration-color: #008080\">┃</span>\n",
|
|
|
- "<span style=\"color: #008080; text-decoration-color: #008080\">┃</span> <span style=\"color: #008080; text-decoration-color: #008080\">┃</span>\n",
|
|
|
- "<span style=\"color: #008080; text-decoration-color: #008080\">┃</span> <span style=\"color: #008080; text-decoration-color: #008080\">┃</span>\n",
|
|
|
- "<span style=\"color: #008080; text-decoration-color: #008080\">┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛</span>\n",
|
|
|
- "</pre>\n"
|
|
|
- ],
|
|
|
- "text/plain": [
|
|
|
- "\u001b[36m┏━\u001b[0m\u001b[36m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[36m Agent Playground \u001b[0m\u001b[36m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[36m━┓\u001b[0m\n",
|
|
|
- "\u001b[36m┃\u001b[0m \u001b[36m┃\u001b[0m\n",
|
|
|
- "\u001b[36m┃\u001b[0m \u001b[36m┃\u001b[0m\n",
|
|
|
- "\u001b[36m┃\u001b[0m \u001b[1;32mPlayground URL:\u001b[0m \u001b]8;id=527383;https://app.agno.com/playground?endpoint=localhost%3A7777/v1\u001b\\https://app.agno.com/playground?endpoint=localhost%3A7777/v1\u001b]8;;\u001b\\ \u001b[36m┃\u001b[0m\n",
|
|
|
- "\u001b[36m┃\u001b[0m \u001b[36m┃\u001b[0m\n",
|
|
|
- "\u001b[36m┃\u001b[0m \u001b[36m┃\u001b[0m\n",
|
|
|
- "\u001b[36m┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\u001b[0m\n"
|
|
|
- ]
|
|
|
- },
|
|
|
- "metadata": {},
|
|
|
- "output_type": "display_data"
|
|
|
- },
|
|
|
- {
|
|
|
- "name": "stderr",
|
|
|
- "output_type": "stream",
|
|
|
- "text": [
|
|
|
- "INFO: Will watch for changes in these directories: ['d:\\\\yusys\\\\ai_learning\\\\曹航\\\\3']\n",
|
|
|
- "INFO: Uvicorn running on http://localhost:7777 (Press CTRL+C to quit)\n",
|
|
|
- "INFO: Started reloader process [21576] using WatchFiles\n",
|
|
|
- "INFO: Stopping reloader process [21576]\n"
|
|
|
- ]
|
|
|
- }
|
|
|
- ],
|
|
|
- "source": [
|
|
|
- "# 启动playground-server\n",
|
|
|
- "playground = Playground(agents=[transfer_agent, finance_agent])\n",
|
|
|
- "app = playground.get_app()\n",
|
|
|
- "\n",
|
|
|
- "playground.serve(\"agno_transfer_agent:app\", reload=True)"
|
|
|
- ]
|
|
|
- },
|
|
|
- {
|
|
|
- "cell_type": "code",
|
|
|
- "execution_count": null,
|
|
|
- "id": "bc32bba1",
|
|
|
- "metadata": {},
|
|
|
- "outputs": [],
|
|
|
- "source": []
|
|
|
- }
|
|
|
- ],
|
|
|
- "metadata": {
|
|
|
- "kernelspec": {
|
|
|
- "display_name": ".venv",
|
|
|
- "language": "python",
|
|
|
- "name": "python3"
|
|
|
- },
|
|
|
- "language_info": {
|
|
|
- "codemirror_mode": {
|
|
|
- "name": "ipython",
|
|
|
- "version": 3
|
|
|
- },
|
|
|
- "file_extension": ".py",
|
|
|
- "mimetype": "text/x-python",
|
|
|
- "name": "python",
|
|
|
- "nbconvert_exporter": "python",
|
|
|
- "pygments_lexer": "ipython3",
|
|
|
- "version": "3.11.13"
|
|
|
- }
|
|
|
- },
|
|
|
- "nbformat": 4,
|
|
|
- "nbformat_minor": 5
|
|
|
-}
|