読み方のヒント

矢印ボタン(または ← → キー)でページを移動できます。

第5節 偽のLLMでテストする

5.2 誤った呼び出しと、窓口のテスト

LLMは、ない道具の名前や、形の合わない引数を返すことがあります。call_tool は、例外を出さずに、誤りの内容を結果として返します。LLMはその結果を読み、呼び直すことができます。

def test_unknown_tool_is_reported_to_llm(ctx):
    output, ok = call_tool("get_stock", {"product_id": "p012"}, ctx)
    assert not ok
    assert "get_stock という道具はありません" in output["error"]


def test_bad_arguments_are_reported_to_llm(ctx):
    output, ok = call_tool("find_products", {"keyword": "", "quantity": 0}, ctx)
    assert not ok
    assert output["error"].startswith("引数が正しくありません")

窓口 /agent は、/ask と同じ検査(長さ、伏せ字、店員につなぐ質問)を通してから、ループを呼びます。

テスト確かめること
test_answer_without_tools_is_asked_again道具を呼ばない答えのあとに、頼み直しのメッセージが入る
test_stops_at_step_limitMAX_STEPS 回で止まり、answer が None になる
test_agent_endpoint_gives_up上限で止まったとき、窓口が gave_up を返す
test_agent_endpoint_hands_off_allergy「卵」を含む質問で、LLMを呼ばずに handoff を返す
test_agent_endpoint_llm_downLLMが止まっているとき、503を返す
python -m pytest
32 passed
解答例

tool_call() は呼び出しを1つしか作らないため、返事の辞書を直接書きます。

both = {"role": "assistant", "content": "", "tool_calls": [
    {"function": {"name": "find_products", "arguments": {"keyword": "クロワッサン"}}},
    {"function": {"name": "find_products", "arguments": {"keyword": "メロンパン"}}},
]}
fake_llm.tool_replies += [both, say("どちらもあります。")]

result.steps が2つになること、2回目の呼び出しの messages に tool のメッセージが2つ入ることを確かめます。

21 / 22