Skip to main content

意图路由

机器翻译译自英文(docs.typesafe.ai)本页 11 段已全部译完

文档索引

在以下地址获取完整的文档索引:https://docs.typesafe.ai/llms.txt 使用此文件可以在进一步探索之前发现所有可用页面。

意图路由

对传入请求进行分类,并将每个请求路由到最佳处理程序:确定性逻辑、专家 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:分类意图和复杂度

{
  "intent": {
    "type": "choice",
    "instructions": "此客户消息的主要意图",
    "criteria": {
      "order_status": "询问现有订单",
      "product_question": "购买前询问产品信息",
      "return_exchange": "希望退货或换货",
      "complaint": "对体验不满意,希望解决"
    }
  },
  "complexity": {
    "type": "score",
    "instructions": "解决此请求的复杂度如何",
    "criteria": [
      "简单查询或标准流程",
      "需要一些判断或分步流程",
      "异常情况、边缘情况或需要升级处理"
    ]
  }
}

步骤 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 加载不同的上下文。另一个利用复杂度分数在 LLM 和人工之间做出决定。TypeSafe 在一次快速调用中完成所有分类;昂贵的资源仅在需要时才会被调用。

注意对复杂度分数的额外置信度检查。如 置信度 中所述,在系统上下文和决策风险的环境中考虑低置信度分数的含义始终很重要。