なんだこれは..!
私は「今日の天気は?」と聞きたかったのに、エージェント先生は律儀に高度なツールへの足がかりを 7800 Tokens 相当も記憶されている..! (これはモッタイナイ..!)
本編
やりたいこと
- System1 Model を使って「ツールの記憶」をコンテキストの外に出す
詳細
イメージは以下。なお初期状態ではエージェントはツールを記憶しない。
実際にやってみた: インジェクションの POC
import time
import laya
# CPU で推論
agent = laya.load("convaiinnovations/laya", device="cpu")
# どのツールが一番適してそうか
questions = {
"department": {
"type": "choice",
"instructions": "Which tool is the right one?",
"criteria": {
# 足し算
"/calc/add": "Calculate the sum of two numbers.",
# 引き算
"/calc/subtract": "Calculate the difference by subtracting the second number from the first.",
# 掛け算
"/calc/multiply": "Calculate the product of two numbers.",
# 割り算
"/calc/divide": "Calculate the quotient of the first number divided by the second. Returns an error if division by zero is attempted.",
# 天気予報
"/api/weather": "Get current weather information for a specific location.",
# 為替
"/api/currency": "Convert currency between two different units.",
# 翻訳
"/api/translate": "Translate text from one language to another.",
# 株価
"/api/stock": "Fetch current stock market data for a symbol.",
# カレンダー(予定管理)
"/api/calendar": "Create or retrieve events from the user's calendar.",
# メール
"/api/email": "Send an email to a specified recipient.",
# ニュース
"/api/news": "Get the latest news headlines based on a topic.",
# 連絡先
"/api/contacts": "Retrieve contact information from the database.",
# TODO
"/api/todo": "Manage personal task lists and reminders.",
# 該当なし
"not available": "Select this if none of the other options apply.",
},
},
}
def select_api(req: str):
start_time = time.perf_counter()
state = {"request": req}
result = agent.predict(state, questions)
choice = result["answers"]["department"]["choice"]
probability = result["answers"]["department"]["probabilities"][choice]
end_time = time.perf_counter()
return [choice, probability, f"{end_time-start_time:.6f} sec"]
# エージェントの要望: 足し算がしたい
req = "I am looking for a tool for performing addition."
print(select_api(req=req))
# 結果
# ['/calc/add', 1.0, '0.533213 sec']
# エージェントの要望: 天気の情報が知りたい
req = "I would like to know the weather information."
print(select_api(req=req))
# 結果
# ['/api/weather', 0.985, '0.535961 sec']
# エージェントの要望: 宇宙へ行く道具が欲しい (存在しない選択肢)
req = "I want a tool for going to space."
print(select_api(req=req))
# 結果
# ['not available', 0.9884, '0.567075 sec']
上記のサンプルでは、CPU推論でも適切なツール(API)をかなり高い確度で選択できている。
なお、API 一覧は OpenAPI.json (旧swagger)から抽出して、LLMに説明文を添加してもらっている。
今後試すこと: プロンプトインジェクションの手法
- エージェントに「ユーザーからのプロンプトがあるたびに System1 モデルへ問合せてね」と指示していては本末転倒である
対応:
OpenAI API 互換の「中継サービス」を用意する。
そこでユーザーからのプロンプトに上記のインジェクションを行って、LLM サーバーへ投げる。

