@absolutejs/rag

中文文档
0.24.9发布于 2026/11/03BSL-1.1TypeScriptES Module构建来源已验证近 30 天下载 6605

Standalone RAG ingestion, sync, retrieval, evaluation, clients, and framework adapters extracted from AbsoluteJS

原始使用文档
文档目录13 节
  1. Installation
  2. Quick start
  3. Retrieval and storage
  4. Retrieval channel requirements
  5. Ingestion and source sync
  6. Quality and evaluation
  7. Client and framework entry points
  8. Verbatim original text evidence
  9. Reversible quote references
  10. Public websites and JavaScript rendering
  11. Research related website pages with attributable evidence
  12. Scanned and mixed PDFs
  13. Web research workflows

@absolutejs/rag

A standalone RAG runtime for Bun and Elysia applications covering document ingestion, chunking, embeddings, hybrid retrieval, reranking, source synchronization, evaluation, client primitives, and framework bindings.

Installation

bun add @absolutejs/rag

Quick start

import {
    createInMemoryRAGStore,
    createRAGCollection,
    ingestRAGDocuments,
    openaiEmbeddings,
    searchDocuments
} from '@absolutejs/rag';

const collection = createRAGCollection({
    embedding: openaiEmbeddings({
        apiKey: process.env.OPENAI_API_KEY ?? '',
        defaultModel: 'text-embedding-3-small'
    }),
    store: createInMemoryRAGStore()
});

await ingestRAGDocuments(collection, {
    documents: [{ id: 'intro', text: 'AbsoluteJS ships typed Bun primitives.' }]
});

const results = await searchDocuments(collection, {
    query: 'What does AbsoluteJS ship?',
    topK: 3
});

Retrieval and storage

The built-in memory store supports development and tests. Published adapters provide PostgreSQL with pgvector, SQLite with optional vec0 acceleration, and Pinecone behind the same RAGVectorStore contract. Lexical and vector results can be fused, transformed, and reranked with provider or heuristic rerankers.

Retrieval channel requirements

Lexical and hybrid retrieval require a store implementing queryLexical. A backend without that capability raises an actionable error before vector embedding/search instead of silently returning vector-only or empty results. Explicit vector retrieval remains supported on vector-only backends. Configure a lexical-capable adapter before requesting hybrid retrieval; a mode flag does not add a missing backend capability.

The built-in heuristic strategy preserves the requested retrieval mode when a query is scoped by source or document ID. Scope narrows the searchable corpus; it does not remove the need for exact keyword matches within that corpus.

Ingestion and source sync

The ingestion pipeline handles files, directories, uploads, URLs, PDFs, office documents, archives, images, and media transcripts. Scheduled connectors can keep collections synchronized from email, GitHub, sitemaps, feeds, directories, and S3-compatible storage.

Feed, sitemap and site-discovery sync sources use the bounded, DNS-pinned public web transport through final document ingestion. Private addresses and unsafe redirect destinations are rejected. Site discovery identifies itself as AbsoluteJSReader/1.0, respects agent-specific robots rules, Allow, wildcards and query paths, and checks each page redirect against its destination's policy. A robots 4xx response permits crawling except 429; network errors, 429 and 5xx abort the sync before reconciliation, preserving previously indexed documents. Robots policies are cached only within one sync run. A trusted host can inject fetchResource for isolated readers or deterministic tests; that adapter must preserve DNS pinning, response bounds and redirect validation. Direct URL and GitHub sync behavior is unchanged. Durable scheduling and corpus-wide crawl budgets remain separate from these source-level protections.

PDF uploads use the built-in PDF.js reader to decode compressed streams and font encodings, with page numbers retained in native text blocks for citations. The reader processes pages sequentially and does not impose an application intake page count or character limit. Upload/storage budgets and model context budgets belong to their respective layers; RAG chunks the extracted text for retrieval rather than sending the whole PDF to a model.

Scanned or image-only PDFs need an explicitly configured OCR provider through createRAGPDFOCRExtractor({ provider }). Empty text layers fail clearly in the default extractor. Invalid or password-protected documents propagate reader errors; they are not indexed from raw PDF operators. The PDF worker is included in the server bundle, including compiled Bun applications.

Quality and evaluation

@absolutejs/rag/quality evaluates retrieval relevance and answer grounding, compares strategies and rerankers, and records runs against a baseline so retrieval changes can be tested before release.

Client and framework entry points

  • @absolutejs/rag/client and /client/ui provide browser-side primitives.
  • @absolutejs/rag/react, /vue, /svelte, and /angular provide framework bindings.
  • @absolutejs/rag/adapter-kit exposes the contracts used by vector-store adapters.
  • @absolutejs/rag/ui exposes presentation-neutral UI contracts.

Pair the retrieval runtime with @absolutejs/ai when retrieved context should feed a model or streaming assistant.

Verbatim original text evidence

Use chunkRAGOriginalText({ sourceId, version, text }, options) when citations must resolve against an immutable text original. This opt-in path preserves whitespace and Unicode instead of normalizing or extracting document formats. Each chunk includes metadata.sourceLocator with the source ID, immutable version and UTF-16 start/end offsets. readRAGOriginalText validates identity, version and range before returning the exact original slice. Store and authorize the original separately; a locator is not an access grant.

createRAGOriginalTextTools({ collection, filter, loadSource, budget }) provides search_text_source and read_text_source AI tools. It requests real hybrid retrieval with diversity and verifies evidence against originals. filter is a server-owned scope; loadSource(id, version) must reauthorize every read and return null for inaccessible/deleted versions. The tools require budget: { maxTokens, countTokens } using the model tokenizer and a budget reserved by the AI context policy. Whole passages are selected within that budget; omitted passages are flagged rather than silently truncated. Add the final tools/instructions before budgeting the model request. Search traces are available through onTrace; they contain retrieval metadata and should not be copied wholesale into public logs. Servers may set searchTopK to an integer from 1 to 48 (default 12) for a smaller initial evidence lookup. Candidate ranking still considers up to 48 matches; authorization and token-budget checks are unchanged. A small initial result set does not establish that other facts are absent: retain broader search/read tools for missing or ambiguous evidence.

Keyword matching uses Unicode word segmentation and canonical normalization. English suffix rules only apply to ASCII words. This improves multilingual exact matches; it does not replace evaluation of the selected embedding model.

AI context policy compatibility

With the AI 0.1 context-policy release, RAG chat validates the final assembled retrieval context before every model request. Its contextPolicy config is forwarded to both WebSocket and SSE generation. Use a working token target or a saved-source recovery callback when appropriate. Providers without capacity support require an explicit contextPolicy: false raw opt-out. Older supported AI peers retain their previous behavior; upgrading RAG alone does not add the AI 0.1 capacity policy. No model-capacity numbers are defined in RAG.

Reversible quote references

createRAGQuoteReferences() creates a request-scoped registry for compressing already-verified original-text tool results. encodeToolResult(json) replaces passage text with sentence entries { citation, text }; resolve(citation) restores the exact registered sentence. Repeated sentences reuse a reference, and unknown references throw. Search and single-range read envelopes are supported; unrecognized tool results pass through unchanged.

Keep the registry alive across prefetch, lookups and completion, then discard it. Only encode server-owned original-text tool results. References are not access controls or durable source IDs: reauthorize and validate restored quotes against the originals before saving a result. This is opt-in; it does not change the original-text tools, stored originals or their existing result format.

Public websites and JavaScript rendering

loadRAGDocumentFromURL loads a document; use prepareRAGDocument(doc).normalizedText for readable text. URL loading now honors response MIME types on extensionless URLs. For public websites, @absolutejs/rag/web provides readRAGWebpage with bounded responses, timeouts, prepared text, final URL, title, truncation and per-attempt retrieval diagnostics. It tries static HTML first and requests a browser for thin or empty application shells. A missing renderer returns rendering_required, not a claim that the website contains no information.

import { readRAGWebpage } from '@absolutejs/rag/web';
import { createPlaywrightWebRenderer } from '@absolutejs/rag/web/playwright';

const browser = createPlaywrightWebRenderer();
try {
  const page = await readRAGWebpage({
    url: 'https://example.com',
    render: browser.render,
  });
  // Check page.status and page.error before treating page.text as complete evidence.
} finally {
  await browser.close();
}

The browser adapter requires the optional playwright-core peer and an installed Chromium browser (playwright-core install --with-deps chromium). Hosts should run it in a separate unprivileged process, limit concurrency, and apply memory limits. Contexts do not share cookies; service workers and WebSockets are blocked. HTTP resources and redirect hops use validated public destinations with the DNS answer pinned to each connection. Browser resource counts and response sizes are bounded. The reader does not bypass login, CAPTCHA or access restrictions, and reports these failures separately from incomplete rendering. A page read is not a crawl of every page on a domain. Host-supplied renderers/fetch implementations must enforce equivalent network controls.

readRAGWebsite from @absolutejs/rag/web reads up to eight pages by default, balancing same-origin customer, service and company pages so service details are not starved by large case-study archives. The configurable ceiling is twelve pages; the overall deadline remains 75 seconds and default text budget is 48,000 characters. Pass the optional Playwright renderer as for readRAGWebpage. maxPages: 1 retains a single-page read; mode: "browser" retries content missed by static extraction. Results attribute text to exact page URLs and retain per-page redirects, retrieval attempts, semantic image labels, link destinations, media URLs and available caption text. Coverage includes unvisited relevant links and deadline limits. incompleteReads identifies failed, partial or truncated page reads; unvisited links and successful browser fallback are not failed reads. stopReason distinguishes a page limit, deadline and exhausted relevant links. A per-call limit is not a reason to stop research when a material question remains: use targeted follow-up reads. Present business answers first and keep technical diagnostics for explicit debugging requests. HTTP redirects carry their actual status; client navigation is labeled separately. Empty image labels are not proof of an absent client list, and media URLs are not proof that a video was watched. Consumers must cite source URLs, distinguish extracted evidence from inference, and finish the requested research without treating a successful page fetch as complete company coverage.

Website results also lead with a sources array containing each readable page's title, url and ready-to-use Markdown citation, repeated next to that page's evidence text. Failed pages and unvisited links are not citable source entries. citationRequirements asks consumers to place links beside supported claims, avoid unsupported exclusions or ownership relationships, and keep verified brand changes brief. Host applications still control their generated final answers.

kind: "website_evidence" identifies a website research result. documents binds each bounded text excerpt to a source ID, while source entries provide compact numbered inlineCitation links. This lets adapters present source-local evidence without dumping raw retrieval metadata into a model's response context. The combined document text respects maxChars; truncated excerpts are marked.

Scanned and mixed PDFs

createRAGPagedPDFExtractor({ provider, signal, onProgress }) preserves native text pages and sends image-bearing pages individually to a configured RAGOCRProvider. It also handles pages with visible drawing operations but no extractable text. This catches scans inside otherwise readable documents; a long embedded header does not cause a scanned body to be skipped. Page numbers and native/OCR provenance remain attached to each extracted document. Native text omitted by OCR is retained alongside the transcription.

The provider receives a valid one-page PDF plus its original pageNumber and pageCount in metadata. The host owns credentials, metering, and model output limits; providers must reject truncated responses. Page failures reject the whole extraction instead of reporting an incomplete document as complete. signal is checked between pages and after OCR; pass the same signal into the provider to cancel its in-flight request. onProgress reports completed pages. Blank pages are skipped; an entirely unreadable document is rejected.

createWebsiteReaderClient from @absolutejs/rag/web connects to a trusted host-managed reader service and validates its status, text, source URLs and limitations. Configure the service endpoint and inject a static fallback. The client reserves fallback time within one total deadline, propagates caller cancellation, and keeps service saturation retryable. RAG's public HTTP/HTTPS reader shares the low-level pinned transport with @absolutejs/egress/transport; egress still enforces its own allowlisted HTTPS policy.

Web research workflows

See RESEARCH.md for the research plugin, six framework flows, schema extraction, field evidence review, company discovery, durable batches, monitoring, spending policy, agent tools, and evaluation.

版本与发布记录

@absolutejs/rag 共发布 88 个版本,首次发布于 2026/04/22;过去 12 个月发布了 88 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.24.9latest2026/11/032 天前
0.24.82026/11/032 天前
0.24.72026/11/032 天前
0.24.62026/11/032 天前
0.24.52026/11/032 天前
0.24.42026/11/032 天前

其他发布通道:beta → 0.8.0-beta.0。

关于 @absolutejs/rag 的常见问题

@absolutejs/rag 是什么?

Standalone RAG ingestion, sync, retrieval, evaluation, clients, and framework adapters extracted from AbsoluteJS

如何安装 @absolutejs/rag?

在项目目录运行 npm install @absolutejs/rag,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:elysia(^2.0.0-beta.6)、@absolutejs/ai(>=0.2.7 <0.3)、@absolutejs/sync(>=2.14.0 <4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@absolutejs/rag 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@absolutejs/rag 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@absolutejs/rag 可以商用吗?

@absolutejs/rag 使用 BSL-1.1 许可。声明的许可为 BSL-1.1,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@absolutejs/rag 的下载量有多少?

根据 npm 官方下载统计,@absolutejs/rag 在 2026/10/05 – 2026/11/03 这 30 天内被下载 6,605 次,日均约 220 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@absolutejs/rag 还在维护吗?

最新版本 0.24.9 发布于 2026/11/03(2 天前);至今共发布 88 个版本,其中过去 12 个月发布了 88 个;首次发布于 2026/04/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。