Skip to Content
RAGアップサート埋め込みのアップサート

埋め込みのアップサート

埋め込みを生成した後、それらをベクトル類似検索をサポートするデータベースに保存する必要があります。この例では、後で取得するために埋め込みをさまざまなベクトルデータベースに保存する方法を示します。

PgVectorクラスは、pgvector拡張機能を使用してPostgreSQLにインデックスを作成し、埋め込みを挿入するためのメソッドを提供します。

import { openai } from "@ai-sdk/openai"; import { PgVector } from "@mastra/pg"; import { MDocument } from "@mastra/rag"; import { embedMany } from "ai"; const doc = MDocument.fromText("Your text content..."); const chunks = await doc.chunk(); const { embeddings } = await embedMany({ values: chunks.map(chunk => chunk.text), model: openai.embedding("text-embedding-3-small"), }); const pgVector = new PgVector(process.env.POSTGRES_CONNECTION_STRING!); await pgVector.createIndex({ indexName: "test_index", dimension: 1536, }); await pgVector.upsert({ indexName: "test_index", vectors: embeddings, metadata: chunks?.map((chunk: any) => ({ text: chunk.text })), });



GitHubで例を見る