AI PostgreVectorStore
Vector storage based on PostgreSQL with the pgvector extension.
Allows storing, searching and managing documents with their vector embeddings in provider-isolated collections. Supports cosine similarity search, metadata filtering and batch insertion with atomic transaction.
The pgvector extension is verified and installed automatically if not present. Support tables are created automatically on first initialization.
const vector = _ai.vector('default')
const client = _ai.client()
const chunker = _ai.contextRetrievalChunker()
// Create the collection if it does not yet exist
if (!vector.collectionExists('netuno')) {
vector.createCollection('netuno', 768)
}
// Recursively collect all Markdown files
const files = collectFiles(_app.folder(_app.pathStorage() + '/netuno_docs'))
for (const path of files) {
const file = _app.file(path)
if (file.exists()) {
const content = file.input().readAllAndClose()
const chunks = chunker.markdown(content, 1500 * 3, 400)
for (const chunk of chunks) {
const options = _val.init()
.set('encoding_format', 'float')
.set('dimensions', 768)
const embeddingResponse = client.embeddings(
'embeddinggemma:latest',
chunk.get('text'),
options
)
const embedding = embeddingResponse.get('data').get(0).get('embedding')
vector.add('netuno', embedding, chunk.get('text'), null)
}
}
}
function collectFiles(folder) {
const list = _val.list()
folder.list().forEach(item => {
if (item.isDirectory()) {
collectFiles(item).forEach(f => list.add(f))
} else {
const path = item.fullPath()
if (path.endsWith('.md') || path.endsWith('.mdx')) {
list.add(path)
}
}
})
return list
}