🛡️TypeSafe 中文文档
原文档 ↗

意图路由

对传入请求进行分类,并将每个请求路由到最优的处理器:确定性逻辑、专家 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
{
  "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)

一个意图路由到完全不涉及 LLM 的确定性代码。两个意图分别路由到加载了不同上下文的不同专家 LLM。还有一个意图利用复杂度分数在 LLM 与人工之间做出抉择。TypeSafe 用一次快速调用完成全部分类;昂贵的资源只会被真正需要的请求调用。

注意针对复杂度分数的额外置信度检查。正如置信度中所讨论的,结合系统所处的情境和决策的风险来理解低置信度分数的含义,始终十分重要。

本站为 docs.typesafe.ai 的中文翻译,仅供学习参考;内容版权归原作者所有。