RAG
RAGとは?
Section titled “RAGとは?”RAG(Retrieval-Augmented Generation)は、エージェントがドキュメントを使用して質問に答えることを可能にします。Schiftはパイプライン全体を処理します:
PDF/DOCX/TXTのアップロード -> OCR -> チャンキング -> 埋め込み -> ベクトルインデックス | クエリ -> 埋め込み -> 検索 -> リランク -> コンテキストドキュメントをアップロードするだけです。Schiftが処理し、エージェントが検索して回答します。
セットアップ
Section titled “セットアップ”import { Schift, RAG } from "@schift-io/sdk";
const schift = new Schift({ apiKey: "sch_..." });const rag = new RAG({ bucket: "my-docs", topK: 5 }, schift.transport);| オプション | 型 | デフォルト | 説明 |
|---|---|---|---|
bucket | string | 必須 | SchiftバケットID |
topK | number | 7 | 検索あたりの結果数 |
const results = await rag.search("How do I reset my password?");
for (const r of results) { console.log(r.text); // 一致した箇所 console.log(r.score); // 関連性スコア(0-1) console.log(r.metadata); // ドキュメントメタデータ}チャット(検索 + LLM回答)
Section titled “チャット(検索 + LLM回答)”const response = await rag.chat("How do I reset my password?");
console.log(response.answer); // LLM生成の回答console.log(response.sources); // 使用されたソース箇所エージェントでのRAGの使用
Section titled “エージェントでのRAGの使用”RAGインスタンスをエージェントに渡します。rag_searchという名前のツールとして自動登録されます。
const agent = new Agent({ name: "Support Bot", instructions: "Answer questions using the knowledge base.", rag, model: "gpt-4o-mini", transport: schift.transport,});
const result = await agent.run("How do I reset my password?");エージェントは、ドキュメントから情報が必要なときにrag_searchを呼び出します。
ドキュメントのアップロード
Section titled “ドキュメントのアップロード”SchiftダッシュボードまたはAPIを介してドキュメントをアップロードします:
// Schiftクライアント経由await schift.db.upload("my-docs", { files: [new File([pdfBytes], "manual.pdf")],});対応形式:PDF、DOCX、TXT、MD、HTML。Schiftはスキャン文書に対して自動的にOCRを実行します。
パブリックバケット
Section titled “パブリックバケット”Schiftは、何もアップロードしなくても検索できる、共有の事前インデックス済みデータセットを提供しています:
const rag = new RAG({ bucket: "public--korean-law" }, schift.transport);const results = await rag.search("개인정볼보호법 동의 철회");完全なリストと詳細はパブリックバケットを参照してください。
カスタムツール名
Section titled “カスタムツール名”別のツール名が必要な場合(例:複数のRAGソース):
const legalDocs = new RAG({ bucket: "legal" }, schift.transport);const supportDocs = new RAG({ bucket: "support" }, schift.transport);
const agent = new Agent({ tools: [ legalDocs.asTool("search_legal_docs"), supportDocs.asTool("search_support_docs"), ], ...});