ドキュメント

意図ルーティング

意図ルーティング

受信したリクエストを分類し、決定論的なロジック・専門特化した LLM・人間の中から最適な処理先へ振り分けます。

すべてのユーザーリクエストが同じ種類の処理先を必要とするわけではありません。データベース検索だけで答えられるものもあれば、ドメイン固有のコンテキストを持つ LLM が必要なもの、人間が必要なものもあります。TypeSafe はその手前に立ち、どの処理先を呼び出すかを決める高速で安価な分類器として機能します。

例:カスタマーサポートのルーティング

カスタマーサポートのシステムを構築しているとします。メッセージが届き、それを適切な処理先へ振り分ける必要があります。どんな種類のリクエストかを判断するためにすべてのメッセージを高価な LLM に通すのではなく、まず分類してから振り分けます。

%%{init: {"fontFamily": "Inter, sans-serif", "flowchart": {"rankSpacing": 35, "wrappingWidth": 300, "subGraphTitleMargin": {"top": 12, "bottom": 36}}}}%%
flowchart LR
    message["customer message"]

    subgraph req["TypeSafe evaluates questions<br/>in parallel"]
        direction TB
        intent["<b>Choice:</b> intent"]
        complexity["<b>Score:</b> complexity"]
        %% Invisible links stack the questions; they are answered in parallel.
        intent ~~~ complexity
    end

    message -- "one request<br/>message + 2 questions" --> req
    req -- "one response<br/>2 answers with<br/>confidence" --> confidence{"<b>intent confidence<br/>≥ 0.5?</b><br/>your code"}
    confidence -- "no" --> human["human agent"]
    confidence -- "yes" --> route{"<b>which intent?</b><br/>"}
    route -- "order_status" --> order["order lookup<br/>deterministic code"]
    route -- "product_question" --> product["product specialist LLM"]
    route -- "return_exchange" --> returns["returns specialist LLM"]
    route -- "complaint" --> escalate{"<b>complexity > 1<br/>or its confidence < 0.5?</b><br/>"}
    escalate -- "yes" --> human
    escalate -- "no" --> complaint["complaint resolution LLM"]

ステップ 1:意図と複雑さを分類する

questions
{
  "intent": {
    "type": "choice",
    "instructions": "The primary intent of this customer message",
    "criteria": {
      "order_status": "Asking about an existing order",
      "product_question": "Asking about a product before buying",
      "return_exchange": "Wants to return or exchange something",
      "complaint": "Unhappy with experience, wants resolution"
    }
  },
  "complexity": {
    "type": "score",
    "instructions": "How complex is this request to resolve",
    "criteria": [
      "Simple lookup or standard procedure",
      "Requires some judgment or multi-step process",
      "Unusual situation, edge case, or escalation needed"
    ]
  }
}

ステップ 2:最適な処理先へ振り分ける

routing.py

def route_ticket(ticket_id, response):
    intent = response.answers["intent"]
    complexity = response.answers["complexity"]

    if intent.confidence < 0.5:
        # If we don't have enough confidence to classify, route to a human agent
        return route_to_human_agent(ticket_id)

    if intent.choice == "order_status":
        handle_order_status(ticket_id)

    elif intent.choice == "product_question":
        handle_with_llm(ticket_id, PRODUCT_SPECIALIST)

    elif intent.choice == "return_exchange":
        handle_with_llm(ticket_id, RETURNS_SPECIALIST)

    elif intent.choice == "complaint":
        low_confidence = complexity.confidence < 0.5
        # A higher complexity.score leans toward the "escalation needed" end of the scale.
        if complexity.score > 1 or low_confidence:
            # Too complex for safe automation, or we're not sure about the complexity; route to a human.
            route_to_human_agent(ticket_id)
        else:
            handle_with_llm(ticket_id, COMPLAINT_RESOLUTION)

1 つの意図は LLM を介さず決定論的なコードへ振り分けられます。2 つはそれぞれ別のコンテキストを読み込んだ専門特化 LLM へ振り分けられます。1 つは複雑さのスコアを使って LLM と人間のどちらにするかを決めます。TypeSafe は分類をすべて 1 回の素早い呼び出しで処理し、高価なリソースは本当に必要なリクエストにだけ呼び出されます。

複雑さのスコアに対する追加の信頼度チェックに注目してください。信頼度で述べたように、低い信頼度がシステムの中で何を意味し、その意思決定がどれだけ重要かを常に考えることが大切です。