Files
FiDA_Python/src/server/agent/tools/terminate_tool.py

39 lines
1.3 KiB
Python
Raw Normal View History

2026-03-03 17:33:51 +08:00
from typing import Literal
from langchain_core.tools import tool
from pydantic import BaseModel, Field
class TerminateInput(BaseModel):
"""終止對話的輸入參數"""
status: Literal["success", "failure"] = Field(
description="互動結束的狀態:'success' 表示任務完成,'failure' 表示無法繼續",
examples=["success", "failure"]
)
reason: str = Field(
default="",
description="可選:簡單說明為什麼結束(例如 '報告已生成''缺少關鍵資訊'",
examples=["報告已成功生成", "無法取得足夠資料"]
)
@tool(args_schema=TerminateInput)
2026-03-12 13:13:52 +08:00
def terminate(status: str, reason: str = "") -> str:
2026-03-03 17:33:51 +08:00
"""
2026-03-12 13:13:52 +08:00
當任務完成報告已生成或無法繼續進行時呼叫此工具來結束本次互動
使用時機
- 已經成功產生最終報告report_generator 已完成
- 遇到無法解決的錯誤或缺少關鍵資訊
- 用戶需求已完全滿足
請在呼叫前確保所有必要步驟已完成並在 reason 中簡單說明結束原因
2026-03-03 17:33:51 +08:00
"""
2026-03-12 13:13:52 +08:00
if status not in ("success", "failure"):
status = "failure" # 防呆
msg = f"互動已終止,狀態:{status.upper()}"
if reason:
msg += f"\n原因:{reason}"
return msg