TS TypeSafe 文档中文版 原文 ↗

意图路由

对传入的请求进行分类,并把每一个路由到最合适的处理方:确定性逻辑、专用 LLM,或人工。

并非每个用户请求都需要同一种处理方。有些可以通过一次数据库查询来回答。有些需要一个具备领域特定上下文的 LLM。有些需要人工。TypeSafe 可以作为快速、廉价的分类器置于所有这些处理方之前,判定应当调用哪一个。

示例:客户服务路由

设想你正在构建一个客户服务系统。消息不断涌入,需要被路由到正确的处理方。与其把每条消息都送进一个昂贵的 LLM 来判断它属于哪类请求,不如先做分类,再据此路由。

%%{init: {"fontFamily": "Inter, sans-serif", "flowchart": {"rankSpacing": 35, "wrappingWidth": 300, "subGraphTitleMargin": {"top": 12, "bottom": 36}}}}%% flowchart LR message["客户消息"] subgraph req["TypeSafe 并行<br/>评估各个问题"] direction TB intent["<b>Choice:</b> 意图"] complexity["<b>Score:</b> 复杂度"] %% Invisible links stack the questions; they are answered in parallel. intent ~~~ complexity end message -- "一次请求<br/>消息 + 2 个问题" --> req req -- "一次响应<br/>2 个答案<br/>及置信度" --> confidence{"<b>意图置信度<br/>≥ 0.5?</b><br/>你的代码"} confidence -- "否" --> human["人工客服"] confidence -- "是" --> route{"<b>是哪个意图?</b><br/>"} route -- "order_status" --> order["订单查询<br/>确定性代码"] route -- "product_question" --> product["产品专家 LLM"] route -- "return_exchange" --> returns["退货专家 LLM"] route -- "complaint" --> escalate{"<b>复杂度 > 1<br/>或其置信度 < 0.5?</b><br/>"} escalate -- "是" --> human escalate -- "否" --> complaint["投诉解决 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"
      ]
    }
  }
}

这个示例是可交互的;到官网原页面可以直接在 Playground 里运行。

第 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 在单次快速调用中完成全部分类;昂贵的资源只会为真正需要它们的请求而启用。

注意对复杂度分数所做的额外置信度检查。正如置信度中所讨论的,在系统所处的语境与决策的代价这一背景下,考虑低置信度分数的含义始终很重要。