Retrieval pipeline

RAG Passage Filtering with Laya

Score retrieved passages and filter irrelevant context before a generator sees it.

Use case

Use Laya after retrieval and before generation to keep obviously irrelevant passages out of the context window.

State

Question: What is the refund period? Passage: Annual subscriptions can be refunded within 30 days of the initial purchase.

Typed questions

scorerelevance

How relevant is this passage to the question?

irrelevantweakusefuldirect answer
noulkeep

Should this passage be kept for the downstream answer?

Expected direction

  • relevance should rank near direct answer
  • keep should return a high P(true)

Copy the implementation

Python
from laya import Router
router = Router(preload=True)
state = {"question": "What is the refund period?", "passage": "Annual subscriptions can be refunded within 30 days."}
questions = {
    "relevance": {"type": "score", "instructions": "How relevant is this passage?", "criteria": ["irrelevant", "weak", "useful", "direct answer"]},
    "keep": {"type": "noul", "instructions": "Should this passage be kept?"}
}
print(router.predict(state, questions))
TypeScript / Node.js
import { Laya } from "@receptron/laya";
const laya = await Laya.load();
const result = await laya.systemOne(
  { question: "What is the refund period?", passage: "Annual subscriptions can be refunded within 30 days." },
  {
    relevance: { type: "score", instructions: "How relevant is this passage?", criteria: ["irrelevant", "weak", "useful", "direct answer"] },
    keep: { type: "noul", instructions: "Should this passage be kept?" },
  },
);
console.log(result.answers);
await laya.close();

Related resources