Created
April 30, 2025 14:56
-
-
Save adeleke5140/b3647b0c8da821b88676dcaba832b26b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Storing Embeddings in A Vector Database | Mastra Docs" | |
description: Guide on vector storage options in Mastra, including embedded and dedicated vector databases for similarity search. | |
--- | |
import { Tabs } from "nextra/components"; | |
## Storing Embeddings in A Vector Database | |
After generating embeddings, you need to store them in a database that supports vector similarity search. Mastra provides a consistent interface for storing and querying embeddings across different vector databases. | |
## Supported Databases | |
<Tabs items={['Pg Vector', 'Pinecone', 'Qdrant', 'Chroma', 'Astra', 'LibSQL', 'Upstash', 'Cloudflare', 'MongoDB']}> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { PgVector } from '@mastra/pg'; | |
const store = new PgVector(process.env.POSTGRES_CONNECTION_STRING) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
### Using PostgreSQL with pgvector | |
PostgreSQL with the pgvector extension is a good solution for teams already using PostgreSQL who want to minimize infrastructure complexity. | |
For detailed setup instructions and best practices, see the [official pgvector repository](https://github.com/pgvector/pgvector). | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { PineconeVector } from '@mastra/pinecone' | |
const store = new PineconeVector(process.env.PINECONE_API_KEY) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { QdrantVector } from '@mastra/qdrant' | |
const store = new QdrantVector({ | |
url: process.env.QDRANT_URL, | |
apiKey: process.env.QDRANT_API_KEY | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { ChromaVector } from '@mastra/chroma' | |
const store = new ChromaVector() | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { AstraVector } from '@mastra/astra' | |
const store = new AstraVector({ | |
token: process.env.ASTRA_DB_TOKEN, | |
endpoint: process.env.ASTRA_DB_ENDPOINT, | |
keyspace: process.env.ASTRA_DB_KEYSPACE | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { LibSQLVector } from "@mastra/core/vector/libsql"; | |
const store = new LibSQLVector({ | |
connectionUrl: process.env.DATABASE_URL, | |
authToken: process.env.DATABASE_AUTH_TOKEN // Optional: for Turso cloud databases | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { UpstashVector } from '@mastra/upstash' | |
const store = new UpstashVector({ | |
url: process.env.UPSTASH_URL, | |
token: process.env.UPSTASH_TOKEN | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { CloudflareVector } from '@mastra/vectorize' | |
const store = new CloudflareVector({ | |
accountId: process.env.CF_ACCOUNT_ID, | |
apiToken: process.env.CF_API_TOKEN | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { MongoDBVector } from '@mastra/mongodb' | |
const store = new MongoDBVector({ | |
url: process.env.MONGODB_URL, | |
database: process.env.MONGODB_DATABASE | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
</Tabs> | |
## Using Vector Storage | |
Once initialized, all vector stores share the same interface for creating indexes, upserting embeddings, and querying. | |
### Creating Indexes | |
Before storing embeddings, you need to create an index with the appropriate dimension size for your embedding model: | |
```ts filename="store-embeddings.ts" showLineNumbers copy | |
// Create an index with dimension 1536 (for text-embedding-3-small) | |
await store.createIndex({ | |
indexName: 'myCollection', | |
dimension: 1536, | |
}); | |
``` | |
The dimension size must match the output dimension of your chosen embedding model. Common dimension sizes are: | |
- OpenAI text-embedding-3-small: 1536 dimensions (or custom, e.g., 256) | |
- Cohere embed-multilingual-v3: 1024 dimensions | |
- Google `text-embedding-004`: 768 dimensions (or custom) | |
> **Important**: Index dimensions cannot be changed after creation. To use a different model, delete and recreate the index with the new dimension size. | |
### Naming Rules for Databases | |
Each vector database enforces specific naming conventions for indexes and collections to ensure compatibility and prevent conflicts. | |
<Tabs items={['Pg Vector', 'Pinecone', 'Qdrant', 'Chroma', 'Astra', 'LibSQL', 'Upstash', 'Cloudflare', 'MongoDB']}> | |
<Tabs.Tab> | |
Index names must: | |
- Start with a letter or underscore | |
- Contain only letters, numbers, and underscores | |
- Example: `my_index_123` is valid | |
- Example: `my-index` is not valid (contains hyphen) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
Index names must: | |
- Use only lowercase letters, numbers, and dashes | |
- Not contain dots (used for DNS routing) | |
- Not use non-Latin characters or emojis | |
- Have a combined length (with project ID) under 52 characters | |
- Example: `my-index-123` is valid | |
- Example: `my.index` is not valid (contains dot) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
Collection names must: | |
- Be 1-255 characters long | |
- Not contain any of these special characters: | |
- `< > : " / \ | ? *` | |
- Null character (`\0`) | |
- Unit separator (`\u{1F}`) | |
- Example: `my_collection_123` is valid | |
- Example: `my/collection` is not valid (contains slash) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
Collection names must: | |
- Be 3-63 characters long | |
- Start and end with a letter or number | |
- Contain only letters, numbers, underscores, or hyphens | |
- Not contain consecutive periods (..) | |
- Not be a valid IPv4 address | |
- Example: `my-collection-123` is valid | |
- Example: `my..collection` is not valid (consecutive periods) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
Collection names must: | |
- Not be empty | |
- Be 48 characters or less | |
- Contain only letters, numbers, and underscores | |
- Example: `my_collection_123` is valid | |
- Example: `my-collection` is not valid (contains hyphen) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
Index names must: | |
- Start with a letter or underscore | |
- Contain only letters, numbers, and underscores | |
- Example: `my_index_123` is valid | |
- Example: `my-index` is not valid (contains hyphen) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
Namespace names must: | |
- Be 2-100 characters long | |
- Contain only: | |
- Alphanumeric characters (a-z, A-Z, 0-9) | |
- Underscores, hyphens, dots | |
- Not start or end with special characters (_, -, .) | |
- Can be case-sensitive | |
- Example: `MyNamespace123` is valid | |
- Example: `_namespace` is not valid (starts with underscore) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
Index names must: | |
- Start with a letter | |
- Be shorter than 32 characters | |
- Contain only lowercase ASCII letters, numbers, and dashes | |
- Use dashes instead of spaces | |
- Example: `my-index-123` is valid | |
- Example: `My_Index` is not valid (uppercase and underscore) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
Collection (index) names must: | |
- Start with a letter or underscore | |
- Be up to 120 bytes long | |
- Contain only letters, numbers, underscores, or dots | |
- Cannot contain `$` or the null character | |
- Example: `my_collection.123` is valid | |
- Example: `my-index` is not valid (contains hyphen) | |
- Example: `My$Collection` is not valid (contains `$`) | |
</Tabs.Tab> | |
</Tabs> | |
### Upserting Embeddings | |
After creating an index, you can store embeddings along with their basic metadata: | |
```ts filename="store-embeddings.ts" showLineNumbers copy | |
// Store embeddings with their corresponding metadata | |
await store.upsert({ | |
indexName: 'myCollection', // index name | |
vectors: embeddings, // array of embedding vectors | |
metadata: chunks.map(chunk => ({ | |
text: chunk.text, // The original text content | |
id: chunk.id // Optional unique identifier | |
})) | |
}); | |
``` | |
The upsert operation: | |
- Takes an array of embedding vectors and their corresponding metadata | |
- Updates existing vectors if they share the same ID | |
- Creates new vectors if they don't exist | |
- Automatically handles batching for large datasets | |
For complete examples of upserting embeddings in different vector stores, see the [Upsert Embeddings](../../examples/rag/upsert/upsert-embeddings.mdx) guide. | |
## Adding Metadata | |
Vector stores support rich metadata (any JSON-serializable fields) for filtering and organization. Since metadata is stored with no fixed schema, use consistent field naming to avoid unexpected query results. | |
**Important**: Metadata is crucial for vector storage - without it, you'd only have numerical embeddings with no way to return the original text or filter results. Always store at least the source text as metadata. | |
```ts showLineNumbers copy | |
// Store embeddings with rich metadata for better organization and filtering | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map((chunk) => ({ | |
// Basic content | |
text: chunk.text, | |
id: chunk.id, | |
// Document organization | |
source: chunk.source, | |
category: chunk.category, | |
// Temporal metadata | |
createdAt: new Date().toISOString(), | |
version: "1.0", | |
// Custom fields | |
language: chunk.language, | |
author: chunk.author, | |
confidenceScore: chunk.score, | |
})), | |
}); | |
``` | |
Key metadata considerations: | |
- Be strict with field naming - inconsistencies like 'category' vs 'Category' will affect queries | |
- Only include fields you plan to filter or sort by - extra fields add overhead | |
- Add timestamps (e.g., 'createdAt', 'lastUpdated') to track content freshness | |
## Best Practices | |
- Create indexes before bulk insertions | |
- Use batch operations for large insertions (the upsert method handles batching automatically) | |
- Only store metadata you'll query against | |
- Match embedding dimensions to your model (e.g., 1536 for `text-embedding-3-small`) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "ベクトルデータベースに埋め込みを保存する | Mastra ドキュメント" | |
description: Mastraにおけるベクトルストレージオプションのガイド。類似性検索のための埋め込みベクトルデータベースと専用ベクトルデータベースを含む。 | |
--- | |
import { Tabs } from "nextra/components"; | |
## ベクトルデータベースに埋め込みを保存する | |
埋め込みを生成した後、それらをベクトル類似性検索をサポートするデータベースに保存する必要があります。Mastraは、異なるベクトルデータベース間で埋め込みを保存およびクエリするための一貫したインターフェースを提供します。 | |
## サポートされているデータベース | |
<Tabs items={['Pg Vector', 'Pinecone', 'Qdrant', 'Chroma', 'Astra', 'LibSQL', 'Upstash', 'Cloudflare', 'MongoDB']}> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { PgVector } from '@mastra/pg'; | |
const store = new PgVector(process.env.POSTGRES_CONNECTION_STRING) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
### pgvectorを使用したPostgreSQLの利用 | |
pgvector拡張機能を備えたPostgreSQLは、すでにPostgreSQLを使用しているチームにとって、インフラの複雑さを最小限に抑えるための良い解決策です。 | |
詳細なセットアップ手順とベストプラクティスについては、[公式pgvectorリポジトリ](https://github.com/pgvector/pgvector)を参照してください。 | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { PineconeVector } from '@mastra/pinecone' | |
const store = new PineconeVector(process.env.PINECONE_API_KEY) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { QdrantVector } from '@mastra/qdrant' | |
const store = new QdrantVector({ | |
url: process.env.QDRANT_URL, | |
apiKey: process.env.QDRANT_API_KEY | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { ChromaVector } from '@mastra/chroma' | |
const store = new ChromaVector() | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { AstraVector } from '@mastra/astra' | |
const store = new AstraVector({ | |
token: process.env.ASTRA_DB_TOKEN, | |
endpoint: process.env.ASTRA_DB_ENDPOINT, | |
keyspace: process.env.ASTRA_DB_KEYSPACE | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { LibSQLVector } from "@mastra/core/vector/libsql"; | |
const store = new LibSQLVector({ | |
connectionUrl: process.env.DATABASE_URL, | |
authToken: process.env.DATABASE_AUTH_TOKEN // Optional: for Turso cloud databases | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { UpstashVector } from '@mastra/upstash' | |
const store = new UpstashVector({ | |
url: process.env.UPSTASH_URL, | |
token: process.env.UPSTASH_TOKEN | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { CloudflareVector } from '@mastra/vectorize' | |
const store = new CloudflareVector({ | |
accountId: process.env.CF_ACCOUNT_ID, | |
apiToken: process.env.CF_API_TOKEN | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
<Tabs.Tab> | |
```ts filename="vector-store.ts" showLineNumbers copy | |
import { MongoDBVector } from '@mastra/mongodb' | |
const store = new MongoDBVector({ | |
url: process.env.MONGODB_URL, | |
database: process.env.MONGODB_DATABASE | |
}) | |
await store.createIndex({ | |
indexName: "myCollection", | |
dimension: 1536, | |
}); | |
await store.upsert({ | |
indexName: "myCollection", | |
vectors: embeddings, | |
metadata: chunks.map(chunk => ({ text: chunk.text })), | |
}); | |
``` | |
</Tabs.Tab> | |
</Tabs> | |
## ベクトルストレージの使用 | |
初期化後、すべてのベクトルストアは、インデックスの作成、埋め込みのアップサート、およびクエリのための同じインターフェースを共有します。 | |
### インデックスの作成 | |
埋め込みを保存する前に、埋め込みモデルに適切な次元サイズでインデックスを作成する必要があります: | |
```ts filename="store-embeddings.ts" showLineNumbers copy | |
// Create an index with dimension 1536 (for text-embedding-3-small) | |
await store.createIndex({ | |
indexName: 'myCollection', | |
dimension: 1536, | |
}); | |
``` | |
次元サイズは、選択した埋め込みモデルの出力次元と一致する必要があります。一般的な次元サイズは以下の通りです: | |
- OpenAI text-embedding-3-small: 1536次元(またはカスタム、例えば256) | |
- Cohere embed-multilingual-v3: 1024次元 | |
- Google `text-embedding-004`: 768次元(またはカスタム) | |
> **重要**: インデックスの次元は作成後に変更できません。異なるモデルを使用するには、インデックスを削除して新しい次元サイズで再作成してください。 | |
### データベースの命名規則 | |
各ベクトルデータベースは、互換性を確保し競合を防ぐために、インデックスとコレクションに特定の命名規則を適用しています。 | |
<Tabs items={['Pg Vector', 'Pinecone', 'Qdrant', 'Chroma', 'Astra', 'LibSQL', 'Upstash', 'Cloudflare', 'MongoDB']}> | |
<Tabs.Tab> | |
インデックス名は以下の条件を満たす必要があります: | |
- 文字またはアンダースコアで始まる | |
- 文字、数字、アンダースコアのみを含む | |
- 例:`my_index_123` は有効 | |
- 例:`my-index` は無効(ハイフンを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
インデックス名は以下の条件を満たす必要があります: | |
- 小文字、数字、ダッシュのみを使用 | |
- ドット(DNS ルーティングに使用)を含まない | |
- 非ラテン文字や絵文字を使用しない | |
- プロジェクトIDと合わせた長さが52文字未満 | |
- 例:`my-index-123` は有効 | |
- 例:`my.index` は無効(ドットを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
コレクション名は以下の条件を満たす必要があります: | |
- 1〜255文字の長さ | |
- 以下の特殊文字を含まない: | |
- `< > : " / \ | ? *` | |
- Null文字(`\0`) | |
- Unit separator(`\u{1F}`) | |
- 例:`my_collection_123` は有効 | |
- 例:`my/collection` は無効(スラッシュを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
コレクション名は以下の条件を満たす必要があります: | |
- 3〜63文字の長さ | |
- 文字または数字で始まり、終わる | |
- 文字、数字、アンダースコア、ハイフンのみを含む | |
- 連続したピリオド(..)を含まない | |
- 有効なIPv4アドレスでない | |
- 例:`my-collection-123` は有効 | |
- 例:`my..collection` は無効(連続したピリオド) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
コレクション名は以下の条件を満たす必要があります: | |
- 空でない | |
- 48文字以下 | |
- 文字、数字、アンダースコアのみを含む | |
- 例:`my_collection_123` は有効 | |
- 例:`my-collection` は無効(ハイフンを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
インデックス名は以下の条件を満たす必要があります: | |
- 文字またはアンダースコアで始まる | |
- 文字、数字、アンダースコアのみを含む | |
- 例:`my_index_123` は有効 | |
- 例:`my-index` は無効(ハイフンを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
名前空間名は以下の条件を満たす必要があります: | |
- 2〜100文字の長さ | |
- 以下のみを含む: | |
- 英数字(a-z、A-Z、0-9) | |
- アンダースコア、ハイフン、ドット | |
- 特殊文字(_、-、.)で始まらない、または終わらない | |
- 大文字と小文字は区別される場合がある | |
- 例:`MyNamespace123` は有効 | |
- 例:`_namespace` は無効(アンダースコアで始まる) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
インデックス名は以下の条件を満たす必要があります: | |
- 文字で始まる | |
- 32文字未満 | |
- 小文字のASCII文字、数字、ダッシュのみを含む | |
- スペースの代わりにダッシュを使用 | |
- 例:`my-index-123` は有効 | |
- 例:`My_Index` は無効(大文字とアンダースコア) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
コレクション(インデックス)名は以下の条件を満たす必要があります: | |
- 文字またはアンダースコアで始まる | |
- 最大120バイトの長さ | |
- 文字、数字、アンダースコア、ドットのみを含む | |
- `$`やnull文字を含まない | |
- 例:`my_collection.123` は有効 | |
- 例:`my-index` は無効(ハイフンを含む) | |
- 例:`My$Collection` は無効(`$`を含む) | |
</Tabs.Tab> | |
</Tabs> | |
### エンベディングのアップサート | |
インデックスを作成した後、エンベディングとその基本的なメタデータを保存できます: | |
```ts filename="store-embeddings.ts" showLineNumbers copy | |
// Store embeddings with their corresponding metadata | |
await store.upsert({ | |
indexName: 'myCollection', // index name | |
vectors: embeddings, // array of embedding vectors | |
metadata: chunks.map(chunk => ({ | |
text: chunk.text, // The original text content | |
id: chunk.id // Optional unique identifier | |
})) | |
}); | |
``` | |
upsert操作: | |
- 埋め込みベクトルとそれに対応するメタデータの配列を取ります | |
- 同じIDを共有する既存のベクトルを更新します | |
- 存在しないベクトルを新規作成します | |
- 大規模なデータセットのバッチ処理を自動的に処理します | |
異なるベクトルストアへの埋め込みのupsertの完全な例については、[埋め込みのUpsert](../../examples/rag/upsert/upsert-embeddings.mdx)ガイドを参照してください。 | |
### 埋め込みのアップサート | |
インデックスを作成した後、埋め込みとその基本的なメタデータを保存できます: | |
<Tabs items={['Pg Vector', 'Pinecone', 'Qdrant', 'Chroma', 'Astra', 'LibSQL', 'Upstash', 'Cloudflare']}> | |
<Tabs.Tab> | |
インデックス名は次の条件を満たす必要があります: | |
- 文字またはアンダースコアで始まる | |
- 文字、数字、アンダースコアのみを含む | |
- 例: `my_index_123` は有効 | |
- 例: `my-index` は無効(ハイフンを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
インデックス名は次の条件を満たす必要があります: | |
- 小文字の文字、数字、ダッシュのみを使用 | |
- ドットを含まない(DNSルーティングに使用) | |
- 非ラテン文字や絵文字を使用しない | |
- プロジェクトIDと合わせて52文字未満 | |
- 例: `my-index-123` は有効 | |
- 例: `my.index` は無効(ドットを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
コレクション名は次の条件を満たす必要があります: | |
- 1〜255文字の長さ | |
- 以下の特殊文字を含まない: | |
- `< > : " / \ | ? *` | |
- Null文字 (`\0`) | |
- ユニットセパレータ (`\u{1F}`) | |
- 例: `my_collection_123` は有効 | |
- 例: `my/collection` は無効(スラッシュを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
コレクション名は次の条件を満たす必要があります: | |
- 3〜63文字の長さ | |
- 文字または数字で始まり、終わる | |
- 文字、数字、アンダースコア、ハイフンのみを含む | |
- 連続するピリオド(..)を含まない | |
- 有効なIPv4アドレスでない | |
- 例: `my-collection-123` は有効 | |
- 例: `my..collection` は無効(連続するピリオド) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
コレクション名は次の条件を満たす必要があります: | |
- 空でない | |
- 48文字以下 | |
- 文字、数字、アンダースコアのみを含む | |
- 例: `my_collection_123` は有効 | |
- 例: `my-collection` は無効(ハイフンを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
インデックス名は次の条件を満たす必要があります: | |
- 文字またはアンダースコアで始まる | |
- 文字、数字、アンダースコアのみを含む | |
- 例: `my_index_123` は有効 | |
- 例: `my-index` は無効(ハイフンを含む) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
名前空間名は次の条件を満たす必要があります: | |
- 2〜100文字の長さ | |
- 以下のみを含む: | |
- 英数字(a-z, A-Z, 0-9) | |
- アンダースコア、ハイフン、ドット | |
- 特殊文字(_, -, .)で始まったり終わったりしない | |
- 大文字小文字を区別できる | |
- 例: `MyNamespace123` は有効 | |
- 例: `_namespace` は無効(アンダースコアで始まる) | |
</Tabs.Tab> | |
<Tabs.Tab> | |
インデックス名は次の条件を満たす必要があります: | |
- 文字で始まる | |
- 32文字未満 | |
- 小文字のASCII文字、数字、ダッシュのみを含む | |
- スペースの代わりにダッシュを使用 | |
- 例: `my-index-123` は有効 | |
- 例: `My_Index` は無効(大文字とアンダースコアを含む) | |
</Tabs.Tab> | |
</Tabs>## ベストプラクティス | |
- 大量挿入の前にインデックスを作成する | |
- 大量挿入にはバッチ操作を使用する(upsertメソッドは自動的にバッチ処理を行います) | |
- クエリを実行する予定のメタデータのみを保存する | |
- 埋め込みの次元数をモデルに合わせる(例:`text-embedding-3-small`の場合は1536) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment