上下文路由教程
本教程将向你展示如何使用 上下文信号(Token 数量)根据请求长度进行路由。
这在以下场景中非常有用:
- 将短查询路由到更快、更小的模型
- 将长文档/长提示词路由到支持长上下文窗口的模型
- 对短任务使用更便宜的模型,从而优化成本
场景说明
我们的目标是:
- 将短请求(< 4K tokens)路由到快速模型(
llama-3-8b) - 将中等长度请求(4K - 32K tokens)路由到标准模型(
llama-3-70b) - 将长请求(32K - 128K tokens)路由到长上下文模型(
claude-3-opus)
第一步:定义上下文信号
在 signals 配置中添加 context_rules:
signals:
context:
- name: "short_context"
min_tokens: "0"
max_tokens: "4K"
description: "Short queries suitable for fast models"
- name: "medium_context"
min_tokens: "4K"
max_tokens: "32K"
description: "Medium length context"
- name: "long_context"
min_tokens: "32K"
max_tokens: "128K"
description: "Long context requiring specialized handling"
第二步:定义决策规则
创建基于这些上下文信号触发的路由决策:
decisions:
- name: "fast_route"
priority: 10
rules:
operator: "AND"
conditions:
- type: "context"
name: "short_context"
modelRefs:
- model: "llama-3-8b"
- name: "standard_route"
priority: 10
rules:
operator: "AND"
conditions:
- type: "context"
name: "medium_context"
modelRefs:
- model: "llama-3-70b"
- name: "long_context_route"
priority: 10
rules:
operator: "AND"
conditions:
- type: "context"
name: "long_context"
modelRefs:
- model: "claude-3-opus"