文档目录

意图路由

对传入的请求进行分类,并将每条请求路由到最优处理方:确定性的逻辑、专业的大语言模型(LLM),或人工。

非官方中文翻译 · 内容以 英文原文 ↗ 为准 · 同步于 2026-10-15

并非每条用户请求都需要同一种处理方。有些可以通过数据库查询来回答,有些需要带有领域专属上下文的大语言模型(LLM),有些则需要人工。TypeSafe 可以作为所有这些处理方的前置层,作为一个快速且低成本的分类器来决定调用哪一个处理方。

示例:客服路由

假设你正在构建一个客服系统。消息进来后需要被路由到正确的处理方。与其把每条消息都丢给昂贵的 LLM 去判断这是什么类型的请求,不如先分类再路由。

mermaid
%%{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 blue_low@-- "no" --> human["human agent"]
    confidence -- "yes" --> route{"<b>which intent?</b><br/>"}
    route green_order@-- "order_status" --> order["order lookup<br/>deterministic code"]
    route green_product@-- "product_question" --> product["product specialist LLM"]
    route green_returns@-- "return_exchange" --> returns["returns specialist LLM"]
    route -- "complaint" --> escalate{"<b>complexity > 1<br/>or its confidence < 0.5?</b><br/>"}
    escalate blue_complex@-- "yes" --> human
    escalate green_complaint@-- "no" --> complaint["complaint resolution LLM"]
    %% Branch colours (styles.css): node classes and green_/blue_/purple_/orange_ edge ids.
    class order,product,returns,complaint neonGreen
    class human neonBlue

第 1 步:分类意图与复杂度

问题

json
{
  "model": "jev-latest",
  "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 步:路由到最优处理方

python
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)

其中一个意图路由到不涉及 LLM 的确定性的代码;另外两个意图路由到不同的专业 LLM,各自加载不同的上下文;还有一个意图则使用复杂度评分来决定走 LLM 还是人工。TypeSafe 在一次快速调用中完成分类,昂贵的资源只会被用于真正需要它们的请求。

注意对 complexity 评分也做了额外的置信度检查。正如在 置信度 中讨论的,始终重要的是结合系统背景和决策代价来理解低置信度评分所代表的意义。

本文档由 Mintlify(开发者文档平台)构建并托管。