コンテンツにスキップ

fast2flow

fast2flow は高性能な routing 拡張で、決定論的な token ベースのスコアリング(BM25)を使い、必要に応じて LLM fallback を行いながら、受信メッセージを適切な flow に routing します。

中核となる考え方: ユーザーが "refund please" のようなメッセージを送る → fast2flow が tenant ごとの indexes を確認する → routing directive(DispatchRespondDenyContinue)を返す。

主な原則:

  • まず決定論的に - 予測可能で説明可能な routing のために token ベースの BM25 スコアリングを使う
  • Fail-open - エラー、タイムアウト、indexes の欠如時には Continue directive を返す
  • 時間制約付き - time_budget_ms によって厳格にタイムアウトを適用する
  • ポリシー駆動 - コード変更なしで runtime の挙動を変更できる
Incoming Message
┌──────────────────────────────────────────────────┐
│ fast2flow Pipeline │
│ │
│ ┌────────────────────────────────────────────┐ │
│ │ 1. Hook Filter │ │
│ │ Allow/deny lists, respond rules, policy │ │
│ └────────────────────────────────────────────┘ │
│ │ │
│ ┌────────────────────────────────────────────┐ │
│ │ 2. Index Lookup │ │
│ │ Load TF-IDF index for tenant scope │ │
│ └────────────────────────────────────────────┘ │
│ │ │
│ ┌────────────────────────────────────────────┐ │
│ │ 3. Deterministic Strategy (BM25) │ │
│ │ Token scoring with title boosting (2x) │ │
│ └────────────────────────────────────────────┘ │
│ │ │
│ ┌────────────────────────────────────────────┐ │
│ │ 4. Confidence Gate │ │
│ │ min_confidence threshold check │ │
│ └────────────────────────────────────────────┘ │
│ │ │
│ ┌────────────────────────────────────────────┐ │
│ │ 5. LLM Fallback (optional) │ │
│ │ OpenAI or Ollama for ambiguous cases │ │
│ └────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────┘
Routing Directive (Dispatch / Respond / Deny / Continue)

各 routing 判断では、4 つの directives のいずれかが生成されます:

Directive用途Fields
dispatch特定の flow に route するtarget, confidence, reason
respond即時応答を返すmessage
denyリクエストをブロックするreason
continue判断しない。呼び出し元に処理を任せる
// Dispatch to a flow
{"type": "dispatch", "target": "support-pack:refund_request", "confidence": 0.92, "reason": "BM25 match"}
// Auto-respond without routing
{"type": "respond", "message": "Use the self-service refund form at /refund."}
// Block the request
{"type": "deny", "reason": "Denied by scope policy"}
// Pass through (fail-open default)
{"type": "continue"}

fast2flow は GHCR 経由の .gtpack artifact として配布されます:

Terminal window
# Pull from GHCR
oras pull ghcr.io/greentic-biz/providers/routing-hook/fast2flow.gtpack:latest
# Or reference a specific version
oras pull ghcr.io/greentic-biz/providers/routing-hook/fast2flow.gtpack:v0.4.6

この pack は、メッセージがどの flow にも届く前に横取りする post_ingress hook を登録します。

fast2flow には 3 つの WASM components(wasm32-wasip2 向け)が含まれます:

Component用途Operation
Indexerflow metadata から検索可能な TF-IDF index を構築するbuild, update
Matcherindex に対して高速な BM25 ベースの intent matching を行うmatch
Router完全な routing pipeline を制御するroute

これらの components は、pack 内で定義された次の 3 つの flows によって協調動作します:

# flows/index.ygtc — Runs at deploy time to build indexes
# flows/match.ygtc — Runtime BM25 intent matching
# flows/route.ygtc — Full routing pipeline with LLM fallback

fast2flow は bundle 内の .ygtc ファイルから flows を index 化します。indexer は bundle ディレクトリを走査し、metadata(title、description、tags)を抽出し、BM25 スコアリングを持つ TF-IDF index を構築します。

my-bundle/
├── packs/
│ ├── support-pack/
│ │ └── flows/
│ │ ├── refund.ygtc
│ │ ├── shipping.ygtc
│ │ └── faq.ygtc
│ └── hr-pack/
│ └── flows/
│ ├── leave.ygtc
│ └── booking.ygtc

各 flow ファイルは、intent matching に使われる metadata を提供します:

refund.ygtc
id: refund_request
title: Process Refund Request
description: Handle customer refund requests for orders and payments
type: messaging
tags:
- refund
- payment
- billing
- return
start: collect_info
nodes:
collect_info:
templating.handlebars:
text: "Please provide your order number for the refund."
routing:
- out: true

CLI を使って bundle から index をビルドします:

Terminal window
greentic-fast2flow bundle index \
--bundle ./my-bundle \
--output ./state/indexes \
--tenant demo \
--team default \
--verbose

これにより次が生成されます:

  • index.json - term frequency と document frequency を持つ TF-IDF index
  • intents.md - 人間が読める intent ドキュメント
Terminal window
greentic-fast2flow bundle validate --bundle ./my-bundle

policies は、コード変更なしで runtime の routing 挙動を制御します。JSON ファイルとして /mnt/registry/fast2flow-policy.json または任意のカスタムパスから読み込まれます。

fast2flow-policy.json
{
"stage_order": ["scope", "channel", "provider"],
"default": {
"min_confidence": 0.5,
"llm_min_confidence": 0.5,
"candidate_limit": 20
},
"scope_overrides": [],
"channel_overrides": [],
"provider_overrides": []
}

すべての rule fields は任意です。指定した fields のみが適用されます:

FieldType説明
min_confidencef32dispatch に必要な最小 BM25 スコア(0.0–1.0)
llm_min_confidencef32dispatch に必要な最小 LLM confidence(0.0–1.0)
candidate_limitusize評価する候補の最大数
allow_channelsstring[]channels のホワイトリスト(null = すべて許可)
deny_channelsstring[]channels のブラックリスト
allow_providersstring[]providers のホワイトリスト(null = すべて許可)
deny_providersstring[]providers のブラックリスト
allow_scopesstring[]scopes のホワイトリスト(null = すべて許可)
deny_scopesstring[]scopes のブラックリスト
respond_rulesobject[]自動応答ルール(keyword matching)

overrides は、各 stage 内の priority ソートとともに stage order(scope → channel → provider)で適用されます。

Scope override - VIP tenant 向けに confidence を厳しくする:

{
"id": "vip-tenant",
"priority": 10,
"scope": "tenant-vip",
"rules": {
"min_confidence": 0.8,
"candidate_limit": 10
}
}

Channel override - email channel で自動応答する:

{
"id": "email-autorespond",
"priority": 20,
"channel": "email",
"rules": {
"respond_rules": [
{
"needle": "refund",
"message": "Refund requests via email take 3–5 business days. Use chat for instant support.",
"mode": "contains"
}
]
}
}

Provider override - 特定の provider のみに制限する:

{
"id": "slack-only",
"priority": 30,
"provider": "slack",
"rules": {
"deny_providers": ["telegram"]
}
}

自動応答ルールは、routing pipeline が実行される前にテキストを照合します:

{
"needle": "business hours",
"message": "Our business hours are Mon–Fri 9AM–5PM UTC.",
"mode": "contains"
}

対応する modes: exactcontains(デフォルト)、regex

Terminal window
# Print default policy
greentic-fast2flow policy print-default
# Validate a policy file
greentic-fast2flow policy validate --file ./my-policy.json

決定論的な BM25 戦略で低い confidence スコアしか得られない場合、fast2flow は分類のために LLM へ fallback できます。

ProviderEnvironment Variables
OpenAIFAST2FLOW_OPENAI_API_KEY_PATH, FAST2FLOW_OPENAI_MODEL_PATH
OllamaFAST2FLOW_OLLAMA_ENDPOINT_PATH, FAST2FLOW_OLLAMA_MODEL_PATH
DisabledFAST2FLOW_LLM_PROVIDER=disabled (default)
Terminal window
# Enable OpenAI fallback
FAST2FLOW_LLM_PROVIDER=openai \
FAST2FLOW_OPENAI_API_KEY_PATH=/run/secrets/openai-key \
greentic-fast2flow-routing-host < request.json
Terminal window
# Build TF-IDF index from bundle
greentic-fast2flow bundle index \
--bundle ./my-bundle \
--output ./indexes \
--tenant demo \
--team default \
--generate-docs \
--verbose
# Validate bundle has indexable flows
greentic-fast2flow bundle validate --bundle ./my-bundle
Terminal window
# Build index from flow definitions JSON
greentic-fast2flow index build \
--scope tenant-a \
--flows flows.json \
--output /tmp/indexes
# Inspect a built index
greentic-fast2flow index inspect \
--scope tenant-a \
--input /tmp/indexes
Terminal window
# Simulate a routing decision
greentic-fast2flow route simulate \
--scope tenant-a \
--text "I need a refund" \
--indexes-path /tmp/indexes
Terminal window
# Print default policy template
greentic-fast2flow policy print-default
# Validate policy file
greentic-fast2flow policy validate --file policy.json
VariableDefault説明
FAST2FLOW_LLM_PROVIDERdisabledLLM provider: disabled, openai, ollama
FAST2FLOW_POLICY_PATH/mnt/registry/fast2flow-policy.jsonPolicy ファイルパス
FAST2FLOW_TRACE_POLICY1 に設定すると policy trace を stderr に出力
FAST2FLOW_MIN_CONFIDENCE0.5デフォルトの最小 confidence しきい値
FAST2FLOW_LLM_MIN_CONFIDENCE0.5デフォルトの LLM 最小 confidence
FAST2FLOW_CANDIDATE_LIMIT20デフォルトの最大候補数

fast2flow は低レイテンシの routing 向けに最適化されています:

StageTypical Latency
Hook filter (allow/deny)< 0.1ms
BM25 index lookup< 1ms
Policy resolution< 0.1ms
LLM fallback (if enabled)200–500ms
  1. 説明的な titles を書く - title の単語は 2 倍の TF-IDF boost を受け、スコアが向上する
  2. 具体的な tags を使う - tags は BM25 matching の主要なシグナル
  3. 適切なしきい値を設定する - min_confidence: 0.5 から始めて調整する
  4. overrides には policies を使う - 再デプロイなしで scope/channel/provider ごとに挙動を変える
  5. Continue 率を監視する - Continue が多い場合、flow coverage にギャップがある
  6. LLM は fallback に留める - 決定論的な routing の方が高速で予測しやすい