- 新增 app/api、app/service 数据分析 Agent 全套服务与接口 - schemas.py 重构为 schemas 包(analyst schema) - 新增 SQL 防注入、guardrail、缓存、字典、LLM 等服务 - 新增 tests 测试套件与 scripts/dev、scripts/setup 脚本 - 补充需求规格、架构说明书、开发清单、表设计等文档
40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
"""演示:用真实 LLM + 真实 MySQL 跑数据分析 Agent 的典型问答。"""
|
||
import json
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||
|
||
from app.service.analyst_agent import AnalystAgent
|
||
from app.utils.auth import AuthContext
|
||
|
||
|
||
def run(question, roles, subject):
|
||
agent = AnalystAgent()
|
||
ctx = AuthContext(subject_id=subject, token_type="staff", roles=roles, staff_type=roles[0])
|
||
resp = agent.run(question, ctx)
|
||
print(f"\n=== 问题:{question}(角色 {roles[0]})===")
|
||
print(f"状态:{resp.status} 缓存命中:{resp.meta.cache_hit}")
|
||
print(f"解读:{resp.answer}")
|
||
print(f"表格:{json.dumps(resp.table.model_dump(), ensure_ascii=False, default=str)}")
|
||
print(f"SQL:{resp.sql}")
|
||
if resp.error_code:
|
||
print(f"错误码:{resp.error_code} 建议:{resp.suggestions}")
|
||
return resp
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 先拿到一个顾问和他的非名下客户
|
||
agent = AnalystAgent()
|
||
advisor = agent.repo.execute_readonly(
|
||
"SELECT staff_id FROM core_staff WHERE staff_type='advisor' AND is_active=1 LIMIT 1"
|
||
)["rows"][0][0]
|
||
scope = agent.repo.resolve_advisor_scope(advisor)
|
||
out_customer = next(
|
||
c for c in agent.repo.execute_readonly("SELECT customer_id FROM core_customer LIMIT 100")["rows"]
|
||
if c[0] not in scope
|
||
)[0]
|
||
|
||
run("按产品类型统计总持仓规模", ["analyst"], "STAFF-20001")
|
||
run(f"查 {out_customer} 的持仓", ["advisor"], advisor)
|