minor constants refactoring
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
const {getDB} = require('../db');
|
const {getDB} = require('../db');
|
||||||
|
const { EPISODIC } = require('@nexusai/shared');
|
||||||
|
|
||||||
// --Sessions --------------------------------------------------
|
// --Sessions --------------------------------------------------
|
||||||
|
|
||||||
@@ -73,7 +74,7 @@ function getEpisode(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Retrieves episodes for a given session, ordered by creation time descending, with pagination
|
// Retrieves episodes for a given session, ordered by creation time descending, with pagination
|
||||||
function getEpisodesBySession(sessionId, limit = 20, offset = 0) {
|
function getEpisodesBySession(sessionId, limit = EPISODIC.DEFAULT_PAGE_SIZE, offset = 0) {
|
||||||
const db = getDB();
|
const db = getDB();
|
||||||
const stmt = db.prepare(`
|
const stmt = db.prepare(`
|
||||||
SELECT * FROM episodes
|
SELECT * FROM episodes
|
||||||
@@ -85,7 +86,7 @@ function getEpisodesBySession(sessionId, limit = 20, offset = 0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Retrieves recent episodes across all sessions, ordered by creation time descending, with a limit
|
// Retrieves recent episodes across all sessions, ordered by creation time descending, with a limit
|
||||||
function getRecentEpisodes(limit = 10) {
|
function getRecentEpisodes(limit = EPISODIC.DEFAULT_RECENT_LIMIT) {
|
||||||
// Cross-session recent episodes — useful for recency-based retrieval
|
// Cross-session recent episodes — useful for recency-based retrieval
|
||||||
const db = getDB();
|
const db = getDB();
|
||||||
const stmt = db.prepare(`
|
const stmt = db.prepare(`
|
||||||
@@ -98,7 +99,7 @@ function getRecentEpisodes(limit = 10) {
|
|||||||
|
|
||||||
|
|
||||||
// Searches episodes using FTS5 full-text search, ordered by relevance, with a limit
|
// Searches episodes using FTS5 full-text search, ordered by relevance, with a limit
|
||||||
function searchEpisodes(query, limit = 10) {
|
function searchEpisodes(query, limit = EPISODIC.DEFAULT_SEARCH_LIMIT) {
|
||||||
// FTS5 full-text search across all episodes
|
// FTS5 full-text search across all episodes
|
||||||
const db = getDB();
|
const db = getDB();
|
||||||
const stmt = db.prepare(`
|
const stmt = db.prepare(`
|
||||||
|
|||||||
@@ -1,27 +1,17 @@
|
|||||||
const {QdrantClient} = require('@qdrant/js-client-rest');
|
const {QdrantClient} = require('@qdrant/js-client-rest');
|
||||||
const {getEnv} = require('@nexusai/shared');
|
const {QDRANT, COLLECTIONS, getEnv} = require('@nexusai/shared');
|
||||||
|
|
||||||
let client;
|
let client;
|
||||||
|
|
||||||
// ********** QDrant Client **********
|
// ********** QDrant Client **********
|
||||||
function getClient(){
|
function getClient(){
|
||||||
if(!client){
|
if(!client){
|
||||||
const url = getEnv('QDrant_URL', 'http://localhost:6333');
|
client = new QdrantClient({url: getEnv('QDRANT_URL', QDRANT.DEFAULT_URL)});
|
||||||
client = new QdrantClient({url});
|
|
||||||
}
|
}
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ********** Collections **********
|
// ********** Collections **********
|
||||||
const COLLECTIONS = {
|
|
||||||
EPISODES: 'episodes',
|
|
||||||
ENTITIES: 'entities',
|
|
||||||
SUMMARIES: 'summaries'
|
|
||||||
};
|
|
||||||
|
|
||||||
// Vectoir size much match the embedding model output dimension
|
|
||||||
// nomic-embed-text outputs 768 dimensions
|
|
||||||
const VECTOR_SIZE = 768;
|
|
||||||
|
|
||||||
async function initCollections() {
|
async function initCollections() {
|
||||||
const client = getClient();
|
const client = getClient();
|
||||||
@@ -29,9 +19,9 @@ async function initCollections() {
|
|||||||
const exists = await collectionExists(name);
|
const exists = await collectionExists(name);
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
await client.createCollection(name, {
|
await client.createCollection(name, {
|
||||||
vectors: {
|
vectors: {
|
||||||
size: VECTOR_SIZE,
|
size: QDRANT.VECTOR_SIZE,
|
||||||
distance: 'Cosine' // Cosine similarity — best for text embeddings
|
distance: QDRANT.DISTANCE_METRIC
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log(`Created Qdrant collection: ${name}`);
|
console.log(`Created Qdrant collection: ${name}`);
|
||||||
@@ -77,7 +67,7 @@ async function upsertSummary(id, vector, payload={}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//********** Search Vectors ********** */
|
//********** Search Vectors ********** */
|
||||||
async function searchCollection(collection, vector, limit = 10, filter = null){
|
async function searchCollection(collection, vector, limit = QDRANT.DEFAULT_LIMIT, filter = null){
|
||||||
const client = getClient();
|
const client = getClient();
|
||||||
const params = {vector,limit, with_payload: true};
|
const params = {vector,limit, with_payload: true};
|
||||||
|
|
||||||
@@ -85,13 +75,13 @@ async function searchCollection(collection, vector, limit = 10, filter = null){
|
|||||||
|
|
||||||
return client.search(collection, params);
|
return client.search(collection, params);
|
||||||
}
|
}
|
||||||
async function searchEpisodes(vector, limit = 10, filter = null) {
|
async function searchEpisodes(vector, limit = QDRANT.DEFAULT_LIMIT, filter = null) {
|
||||||
return searchCollection(COLLECTIONS.EPISODES, vector, limit, filter);
|
return searchCollection(COLLECTIONS.EPISODES, vector, limit, filter);
|
||||||
}
|
}
|
||||||
async function searchEntities(vector, limit = 10, filter = null) {
|
async function searchEntities(vector, limit = QDRANT.DEFAULT_LIMIT, filter = null) {
|
||||||
return searchCollection(COLLECTIONS.ENTITIES, vector, limit, filter);
|
return searchCollection(COLLECTIONS.ENTITIES, vector, limit, filter);
|
||||||
}
|
}
|
||||||
async function searchSummaries(vector, limit = 10, filter = null) {
|
async function searchSummaries(vector, limit = QDRANT.DEFAULT_LIMIT, filter = null) {
|
||||||
return searchCollection(COLLECTIONS.SUMMARIES, vector, limit, filter);
|
return searchCollection(COLLECTIONS.SUMMARIES, vector, limit, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +96,6 @@ async function deleteVector(collection, id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
COLLECTIONS,
|
|
||||||
initCollections,
|
initCollections,
|
||||||
upsertEpisode,
|
upsertEpisode,
|
||||||
upsertEntity,
|
upsertEntity,
|
||||||
|
|||||||
26
packages/shared/src/config/constants.js
Normal file
26
packages/shared/src/config/constants.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
//A store for tunables and constants used across the codebase, to avoid magic numbers and hardcoded values
|
||||||
|
|
||||||
|
const QDRANT = {
|
||||||
|
DEFAULT_URL: 'http://localhost:6333',
|
||||||
|
VECTOR_SIZE: 768, // Must match the output dimension of the embedding model (e.g. nomic-embed-text)
|
||||||
|
DISTANCE_METRIC: 'Cosine', // Best for normalized embeddings like text vectors
|
||||||
|
DEFAULT_LIMIT: 10, //Default top-=k for vector searches
|
||||||
|
};
|
||||||
|
|
||||||
|
const COLLECTIONS = {
|
||||||
|
EPISODES: 'episodes',
|
||||||
|
ENTITIES: 'entities',
|
||||||
|
SUMMARIES: 'summaries'
|
||||||
|
};
|
||||||
|
|
||||||
|
const EPISODIC = {
|
||||||
|
DEFAULT_RECENT_LIMIT: 10, // Default number of recent episodes to retrieve
|
||||||
|
DEFAULT_PAGE_SIZE: 20, // Default number of episodes per page for pagination
|
||||||
|
DEFAULT_SEARCH_LIMIT: 10, // Default number of search results to return
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
QDRANT,
|
||||||
|
COLLECTIONS,
|
||||||
|
EPISODIC
|
||||||
|
};
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
const {getEnv} = require('./config/env');
|
const {getEnv} = require('./config/env');
|
||||||
|
const {QDRANT, COLLECTIONS, EPISODIC } = require('./config/constants');
|
||||||
|
|
||||||
module.exports = {getEnv};
|
module.exports = {getEnv, QDRANT, COLLECTIONS, EPISODIC};
|
||||||
Reference in New Issue
Block a user