120 行
5.1 KiB
Python
120 行
5.1 KiB
Python
"""Hydro 工具集:暴露给 AI 的 function-calling 工具 schema + 执行器。
|
||
|
||
数据源:直连 Hydro MongoDB(hydro_db),不依赖网页解析或 API 权限。
|
||
- 学生说"P57"→ 按显示题号 pid="P57" 查
|
||
- 学生说训练名"Scanner"→ 模糊匹配训练 docType=40
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from app.adapters import hydro_db
|
||
|
||
logger = logging.getLogger("fluent.services.hydro_tools")
|
||
|
||
TOOLS = [
|
||
{
|
||
"type": "function",
|
||
"function": {
|
||
"name": "hydro_get_problem",
|
||
"description": "按题号拉取 Hydro 题面。学生说 P57、P142、练23.4、或直接给题号时调用;不要凭记忆编造题面。数字会自动匹配显示题号或 Hydro 内部 ID。",
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {
|
||
"pid": {"type": "string", "description": "题号,如 P57、57、P142"}
|
||
},
|
||
"required": ["pid"],
|
||
},
|
||
},
|
||
},
|
||
{
|
||
"type": "function",
|
||
"function": {
|
||
"name": "hydro_find_training",
|
||
"description": "按名称模糊搜索训练/题单(如 Scanner、if、循环、A级、for、数组)。学生说'XX训练有哪些题'或'第X题'但没给训练 ID 时,先用这个找训练 ID。",
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {
|
||
"keyword": {"type": "string", "description": "训练名称关键词"}
|
||
},
|
||
"required": ["keyword"],
|
||
},
|
||
},
|
||
},
|
||
{
|
||
"type": "function",
|
||
"function": {
|
||
"name": "hydro_list_training",
|
||
"description": "列出某个训练包含的所有题目(按节分组)。先调 hydro_find_training 拿训练 ID,再调这个列题目。",
|
||
"parameters": {
|
||
"type": "object",
|
||
"properties": {
|
||
"training_id": {"type": "string", "description": "训练 ID(来自 hydro_find_training 结果)"}
|
||
},
|
||
"required": ["training_id"],
|
||
},
|
||
},
|
||
},
|
||
{
|
||
"type": "function",
|
||
"function": {
|
||
"name": "hydro_list_my_trainings",
|
||
"description": "列出所有训练/题单。学生问'我有哪些训练'或需要浏览时调用。",
|
||
"parameters": {"type": "object", "properties": {}},
|
||
},
|
||
},
|
||
]
|
||
|
||
|
||
async def execute(name: str, args: dict, adapter=None) -> str:
|
||
try:
|
||
if name == "hydro_get_problem":
|
||
pid = str(args.get("pid", "")).strip()
|
||
p = hydro_db.get_problem(pid)
|
||
if not p:
|
||
return f"没找到题号 {pid}。请让学生确认题号,或用 hydro_find_training 按训练名找题。不要编题面。"
|
||
# 自动同步到本地题库(提交时需要)
|
||
try:
|
||
from app.core.db import SessionLocal
|
||
from app.repositories.problem_repo import ProblemRepo
|
||
db = SessionLocal()
|
||
try:
|
||
ProblemRepo(db).upsert(
|
||
str(p["numeric_id"]), p["title"], p["content"],
|
||
course="ap-csa", lang="java",
|
||
)
|
||
finally:
|
||
db.close()
|
||
except Exception as e:
|
||
logger.warning("同步本地题库失败(不影响读题): %s", e)
|
||
content = p["content"][:4000]
|
||
return (f"题目 {p['pid']}:{p['title']}\n"
|
||
f"(Hydro 数字 ID: {p['numeric_id']},学生提交代码时用此 ID)\n\n"
|
||
f"{content}")
|
||
if name == "hydro_find_training":
|
||
kw = str(args.get("keyword", "")).strip()
|
||
matches = hydro_db.find_training(kw)
|
||
if not matches:
|
||
all_t = hydro_db.list_trainings()
|
||
return f"没找到名称含'{kw}'的训练。所有训练:\n" + "\n".join(f"- ID {t['id']} {t['title']}" for t in all_t[:30])
|
||
rows = "\n".join(f"- ID {t['id']} {t['title']}" for t in matches[:15])
|
||
return f"匹配到 {len(matches)} 个训练:\n{rows}"
|
||
if name == "hydro_list_training":
|
||
tid = str(args.get("training_id", "")).strip()
|
||
t = hydro_db.get_training_problems(tid)
|
||
if not t:
|
||
return f"没找到训练 {tid}。"
|
||
lines = [f"训练:{t['title']}"]
|
||
for sec in t.get("sections", []):
|
||
lines.append(f"\n【{sec['title']}】")
|
||
for x in sec.get("problems", []):
|
||
lines.append(f" {x['pid']} {x['title']}")
|
||
return "\n".join(lines)
|
||
if name == "hydro_list_my_trainings":
|
||
items = hydro_db.list_trainings()
|
||
rows = "\n".join(f"- ID {x['id']} {x['title']}" for x in items[:50])
|
||
return f"所有训练({len(items)} 个):\n{rows}"
|
||
return f"未知工具:{name}"
|
||
except Exception as e:
|
||
logger.warning("Hydro 工具 %s 失败: %s", name, e)
|
||
return f"工具 {name} 调用失败:{type(e).__name__}: {e}。请如实告诉学生暂时读不到,不要编题面。"
|