39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
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)
|
||
def terminate(status: str, reason: str = "") -> str:
|
||
"""
|
||
當任務完成、報告已生成,或無法繼續進行時,呼叫此工具來結束本次互動。
|
||
|
||
使用時機:
|
||
- 已經成功產生最終報告(report_generator 已完成)
|
||
- 遇到無法解決的錯誤或缺少關鍵資訊
|
||
- 用戶需求已完全滿足
|
||
|
||
請在呼叫前確保所有必要步驟已完成,並在 reason 中簡單說明結束原因。
|
||
"""
|
||
if status not in ("success", "failure"):
|
||
status = "failure" # 防呆
|
||
|
||
msg = f"互動已終止,狀態:{status.upper()}"
|
||
if reason:
|
||
msg += f"\n原因:{reason}"
|
||
|
||
return msg
|