JavaScript: Vectors and embeddings
Turn text into embeddings, store vectors with metadata and run similarity search with @sovaria/sdk.
sovaria.vector stores embeddings in collections and finds the nearest ones to a query — the building block for semantic search and retrieval-augmented generation.
await sovaria.vector.upsert('articles', [
{ id: 'a1', text: 'How to reset your password', metadata: { lang: 'en' } },
{ id: 'a2', text: 'Exporting invoices as PDF', metadata: { lang: 'en' } },
])
const hits = await sovaria.vector.search('articles', 'forgot my login', { topK: 3, metadataFilter: { lang: 'en' } })#sovaria.vector
sovaria.vector.embed
Embed.
sovaria.vector.embed(
texts: string[],
): Promise<{ vectors: number[][]; dim: number; count: number; }>Parameters
textsstring[]required- Texts to embed.
Returns
Promise<{ vectors: number[][]; dim: number; count: number; }> — { vectors } — one array of numbers per text.
{
"vectors": [
[
0.012,
-0.044,
"…"
],
[
0.031,
0.007,
"…"
]
],
"dim": 1536,
"count": 2,
"model": "openai/text-embedding-3-small@1536"
}sovaria.vector.upsert
Upsert.
sovaria.vector.upsert(
collection: string,
docs: Array<{ id?: string; text: string; metadata?: JsonObject }>,
): Promise<JsonObject>Parameters
collectionstringrequired- Collection name.
docsArray<{ id?: string; text: string; metadata?: JsonObject }>required[{ id?, text, metadata? }]; texts are embedded for you.
Returns
Promise<JsonObject>
{
"inserted": 2,
"updated": 0,
"dim": 1536,
"size": 2
}sovaria.vector.search
Search a collection.
sovaria.vector.search(
collection: string,
query: string,
options?: { topK?: number; metadataFilter?: JsonObject; mode?: 'vector' | 'keyword' | 'hybrid' } = {},
): Promise<JsonObject>mode: vector (meaning), keyword (the words, BM25) or hybrid (both, fused); the default is vector with a real embedding model and hybrid with the local hash model. Apps (anon key, signed-in users) may only search collections opened with setCollection(name, { appSearch: true }).
Parameters
collectionstringrequired- Collection name.
querystringrequired- Text to search for.
options{ topK?: number; metadataFilter?: JsonObject; mode?: 'vector' | 'keyword' | 'hybrid' }optionaldefault{}topK(how many),metadataFilter(exact matches on metadata).
Returns
Promise<JsonObject>
{
"results": [
{
"id": "a1",
"text": "How to reset your password",
"metadata": {
"lang": "en"
},
"score": 0.83
}
],
"count": 1,
"mode": "vector",
"model": "openai/text-embedding-3-small@1536",
"semantic": true
}sovaria.vector.setCollection
Collection settings (team / service key): appSearch lets the project's apps search it.
sovaria.vector.setCollection(
collection: string,
settings: { appSearch?: boolean },
): Promise<JsonObject>Parameters
collectionstringrequiredsettings{ appSearch?: boolean }required
Returns
Promise<JsonObject>
{
"collection": "help-articles",
"settings": {
"app_search": true
}
}sovaria.vector.listCollections
List collections.
sovaria.vector.listCollections(): Promise<JsonObject>Returns
Promise<JsonObject>
{
"collections": [
{
"name": "help-articles",
"dim": 1536,
"size": 2,
"model": "openai/text-embedding-3-small@1536",
"app_search": false
}
],
"model": "openai/text-embedding-3-small@1536",
"semantic": true
}sovaria.vector.listRecords
List records.
sovaria.vector.listRecords(
collection: string,
options?: { limit?: number; offset?: number } = {},
): Promise<JsonObject>Parameters
collectionstringrequiredoptions{ limit?: number; offset?: number }optionaldefault{}
Returns
Promise<JsonObject>
{
"collection": "help-articles",
"dim": 1536,
"total": 2,
"records": [
{
"id": "a1",
"text": "How to reset your password",
"metadata": {
"lang": "en"
}
}
]
}sovaria.vector.deleteRecord
Delete record.
sovaria.vector.deleteRecord(
collection: string,
recordId: string,
): Promise<JsonObject>Parameters
collectionstringrequiredrecordIdstringrequired
Returns
Promise<JsonObject>
sovaria.vector.deleteCollection
Delete collection.
sovaria.vector.deleteCollection(collection: string): Promise<JsonObject>Parameters
collectionstringrequired
Returns
Promise<JsonObject>