@agentskit/rag

中文文档
0.7.1发布于 2026/10/24MITTypeScriptESM + CJS构建来源已验证近 30 天下载 5332

Plug-and-play retrieval-augmented generation for AgentsKit.

原始使用文档
文档目录16 节
  1. Verified proof
  2. How this fits the ecosystem
  3. Why rag
  4. Install
  5. Quick example
  6. With runtime (retriever)
  7. Features
  8. Markdown notes ( @agentskit/rag/markdown )
  9. S3 in Expo and React Native runtimes
  10. Ecosystem
  11. Contributors
  12. License
  13. Docs
  14. Maturity and compatibility
  15. Contributing
  16. Partition-bound retrieval

@agentskit/rag

AgentsKit

Plug-and-play retrieval-augmented generation: chunk documents, embed them, and retrieve the right context at query time.

npm version npm downloads bundle size license stability GitHub stars

Tags: ai · agents · llm · agentskit · rag · retrieval · vector-search · embeddings · ai-agents · semantic-search · knowledge-base

Verified proof

How this fits the ecosystem

@agentskit/rag is the retrieval layer: load documents, chunk them, embed them, rerank results, and feed precise context back to agents.

  • AgentsKit: compose it with the other packages in this repo to build agents from small, swappable parts.
  • Registry: look for ready agents and templates that already use this layer at registry.agentskit.io.
  • Playbook: learn the production patterns behind this layer at playbook.agentskit.io.

Docs: package guide · agent handoff

Why rag

  • Your data, your agent — no fine-tuning required; ingest plain text and query with natural language
  • Composable stack — uses any EmbedFn and any VectorMemory from @agentskit/adapters and @agentskit/memory; swap either layer without touching RAG logic
  • Retriever-ready — createRAG() returns a Retriever you pass to @agentskit/runtime or useChat so context is injected automatically
  • Tune chunking without a PhD — chunkSize, chunkOverlap, or a custom split function — three knobs that cover 95% of use cases

Install

npm install @agentskit/rag @agentskit/memory @agentskit/adapters

The file-backed example also needs the optional vectra peer. Add vectra to the install command when using fileVectorMemory; the runtime integration example additionally needs @agentskit/runtime.

Quick example

import { createRAG } from '@agentskit/rag'
import { openaiEmbedder } from '@agentskit/adapters'
import { fileVectorMemory } from '@agentskit/memory'

const rag = createRAG({
  embed: openaiEmbedder({ apiKey: process.env.OPENAI_API_KEY! }),
  store: fileVectorMemory({ path: './vectors' }),
})

await rag.ingest([
  { id: 'doc-1', content: 'AgentsKit is a JavaScript agent toolkit...' },
])

const docs = await rag.search('How does AgentsKit work?', { topK: 5 })
console.log(docs)

With runtime (retriever)

Pass the RAG instance as retriever so the runtime injects retrieved context into the task:

import { createRuntime } from '@agentskit/runtime'
import { openai } from '@agentskit/adapters'

const runtime = createRuntime({
  adapter: openai({ apiKey: process.env.OPENAI_API_KEY!, model: 'gpt-4o' }),
  retriever: rag,
})

const result = await runtime.run('Explain the AgentsKit architecture based on ingested docs')
console.log(result.content)

You can also call rag.retrieve({ query, messages }) to satisfy the core Retriever contract (for example from a custom controller).

Features

  • createRAG({ embed, store }) — single entry point for ingest + retrieve.
  • rag.ingest(docs) — chunk, embed, and store documents.
  • rag.search(query, { topK }) — semantic similarity search.
  • rag.retrieve({ query, messages }) — Retriever contract v1 for runtime/controller injection.
  • Configurable chunking: chunkSize, chunkOverlap, custom split.
  • Works with any EmbedFn and any VectorMemory.
  • Rerankers: createRerankedRetriever (Voyage, Jina, custom RerankFn, BM25 default), createHybridRetriever (vector + BM25 blend), standalone bm25Score. Recipe.
  • Document loaders: loadUrl, loadGitHubFile, loadGitHubTree, loadNotionPage, loadConfluencePage, loadGoogleDriveFile, loadPdf, loadS3, loadGcs, loadDropbox, and loadOneDrive. Recipe.
  • Loader resilience: HTTP/network and response-body read/parse failures surface as RagError (AK_RAG_LOAD_FAILED). Every remote request and body read has a finite timeout and byte limit by default; both are configurable through timeoutMs and maxResponseBytes. timeoutMs defaults to 15 seconds and accepts positive safe integers up to 2,147,483,647ms. Timeouts and optional signal aborts cancel fetches and HTTP body streams when supported. Caller aborts are never swallowed as a per-object skip; timed-out or other individual non-abort failures may be skipped where tree/list partial-success semantics allow it. S3 list/get calls receive the SDK abort signal, and async-iterable object bodies receive return() on cancellation; custom iterators that ignore it may continue in the background. Transform-only S3 bodies may also continue if their transform ignores cancellation. Tree/list loaders may return partial success when at least one eligible download succeeded; if every attempted eligible download failed, they throw. Missing/invalid S3 object bodies count as failed downloads. Pagination that reports more data without a new cursor/token throws (no silent truncation). loadNotionPage follows Notion has_more / next_cursor with start_cursor until complete (preserving block order; incomplete or repeated cursors throw). loadUrl requires an HTTPS origin in allowedOrigins; it does not follow redirects. Non-positive / non-finite maxFiles yields [].
  • Score contracts: scoreless search/rerank results keep order. When any score is present, every result must have a finite numeric score and is sorted descending — mixed or non-finite scores throw (never fabricate -Infinity). Malformed Voyage/Jina/custom reranker output throws AK_RAG_RERANK_FAILED. Optional signal on voyageReranker / jinaReranker is forwarded to fetch; request/body aborts remain AK_RAG_RERANK_FAILED. bm25Score sanitizes invalid k1/b to documented defaults and always emits finite scores. Hybrid relative weights are normalized to a finite pair that sums to 1 (both zero → 0.5/0.5).
  • Chunk/config safety: invalid chunkSize / chunkOverlap / topK values are sanitized so chunking always terminates and search never sends non-finite limits to the store.
  • Ingestion behavior: rag.ingest embeds chunks serially and sends one vector-store batch per call. For large corpora, batch documents in the caller and persist progress between calls; the package does not silently add concurrency or an unbounded background queue.

Markdown notes (@agentskit/rag/markdown)

Pure, synchronous helpers for knowledge-base style Markdown (YAML frontmatter, [[wikilinks]], heading sections, GitHub tables). The subpath is separate so the root entry and its size budget stay free of the yaml parser.

import { parseNote, findSection, parseTable } from '@agentskit/rag/markdown'

const note = parseNote('notes/plan.md', raw)
// { path, title, frontmatter, body, links, frontmatterError? }
const rows = parseTable(findSection(note.body, 'backlog')?.content ?? '')

Also exported: splitFrontmatter, extractWikilinks, stripWikilinks, sections, normalize, plain. Invalid frontmatter never throws; it is reported in frontmatterError.

S3 in Expo and React Native runtimes

Node consumers may install @aws-sdk/client-s3 and let loadS3 resolve it lazily. Browser, Expo/Metro, and React Native bundles keep that peer out of the universal entry; pass the command constructors explicitly when invoking the loader:

import { GetObjectCommand, ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3'
import { loadS3 } from '@agentskit/rag'

await loadS3({
  client: new S3Client({}),
  bucket: 'knowledge',
  commands: { GetObjectCommand, ListObjectsV2Command },
})

Ecosystem

PackageRole
@agentskit/coreRetriever, VectorMemory, types
@agentskit/memoryVector backends (fileVectorMemory, etc.)
@agentskit/adaptersopenaiEmbedder and other embedders
@agentskit/runtimeretriever integration for agents
@agentskit/reactuseChat + chat UI with the same core types

Contributors

AgentsKit contributors

License

MIT — see LICENSE.

Docs

Full documentation · GitHub

Maturity and compatibility

  • Stability: beta — see docs/STABILITY.md
  • Node.js 20+ and TypeScript strict mode
  • Published as @agentskit/rag

Contributing

See CONTRIBUTING.md and the monorepo LICENSE.

Partition-bound retrieval

createPartitionedRAG composes createRAG without changing the core Retriever contract or existing createRAG behavior. Bind each instance to a non-empty partition at construction:

import { createPartitionedRAG } from '@agentskit/rag'

const rag = createPartitionedRAG({
  store,
  embed,
  partition: 'team-a',
  topK: 5,
  overfetchFactor: 4,
  onDiagnostics: diagnostics => console.warn(diagnostics),
})
await rag.ingest([{ id: 'guide', content: 'Team A guide' }])
const hits = await rag.retrieve({ query: 'guide' })

Ingest overrides the reserved _akPartition metadata field and namespaces chunk IDs with a JSON tuple, so identical document IDs in different partitions do not overwrite or delete each other's chunks. Document metadata, including documentId, remains available. Returned chunk IDs are opaque namespaced IDs. Every search forwards a core VectorFilter equality predicate and verifies partition metadata locally, rejecting foreign or missing metadata even when a store ignores filters. The wrapper makes one store search, requests at most topK * overfetchFactor candidates, examines at most that many, and returns at most topK matches. The factor is an integer from 1 to 16 (default 4); unsafe candidate limits throw. Per-call search limits and thresholds still work.

The required onDiagnostics callback runs for every successful store search, including empty results, with partition, requestedTopK, candidateLimit, examined, rejected, returned, and recall: 'potentially-reduced'. This is conservative: bounded over-fetch can miss matching chunks, and a store can internally filter a bounded global candidate pool without revealing exclusions (as fileVectorMemory does). Short results never justify returning another partition's chunks. Callback exceptions reject the search.

Stores must faithfully preserve chunk metadata and IDs. Missing metadata fails closed, but fabricated or incorrectly reassigned partition metadata cannot be detected. This is retrieval isolation, not an authorization boundary against untrusted writers or stores. Use physically separate stores or a backend with trusted partition enforcement when that boundary or exhaustive recall is needed. Backend-internal over-fetch is outside the wrapper's candidate bound.

版本与发布记录

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

版本发布日期距今
0.7.1latest2026/10/2414 天前
0.7.02026/10/1028 天前
0.6.12026/10/031 个月前
0.6.02026/10/031 个月前
0.5.92026/09/291 个月前
0.5.82026/09/261 个月前

关于 @agentskit/rag 的常见问题

@agentskit/rag 是什么?

Plug-and-play retrieval-augmented generation for AgentsKit.

如何安装 @agentskit/rag?

在项目目录运行 npm install @agentskit/rag,或使用 pnpm add / yarn add / bun add。

@agentskit/rag 支持 TypeScript 吗?

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

@agentskit/rag 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@agentskit/rag 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@agentskit/rag 可以商用吗?

@agentskit/rag 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

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

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

@agentskit/rag 还在维护吗?

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