515 lines
13 KiB
JavaScript
515 lines
13 KiB
JavaScript
|
|
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class Memory_embeddingsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const memory_embeddings = await db.memory_embeddings.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
provider: data.provider
|
|
||
|
|
null
|
|
,
|
|
|
|
model_name: data.model_name
|
|
||
|
|
null
|
|
,
|
|
|
|
dimensions: data.dimensions
|
|
||
|
|
null
|
|
,
|
|
|
|
embedded_at: data.embedded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await memory_embeddings.setMemory_item( data.memory_item || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.memory_embeddings.getTableName(),
|
|
belongsToColumn: 'vector_blob',
|
|
belongsToId: memory_embeddings.id,
|
|
},
|
|
data.vector_blob,
|
|
options,
|
|
);
|
|
|
|
|
|
return memory_embeddings;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const memory_embeddingsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
provider: item.provider
|
|
||
|
|
null
|
|
,
|
|
|
|
model_name: item.model_name
|
|
||
|
|
null
|
|
,
|
|
|
|
dimensions: item.dimensions
|
|
||
|
|
null
|
|
,
|
|
|
|
embedded_at: item.embedded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const memory_embeddings = await db.memory_embeddings.bulkCreate(memory_embeddingsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < memory_embeddings.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.memory_embeddings.getTableName(),
|
|
belongsToColumn: 'vector_blob',
|
|
belongsToId: memory_embeddings[i].id,
|
|
},
|
|
data[i].vector_blob,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return memory_embeddings;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const memory_embeddings = await db.memory_embeddings.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.provider !== undefined) updatePayload.provider = data.provider;
|
|
|
|
|
|
if (data.model_name !== undefined) updatePayload.model_name = data.model_name;
|
|
|
|
|
|
if (data.dimensions !== undefined) updatePayload.dimensions = data.dimensions;
|
|
|
|
|
|
if (data.embedded_at !== undefined) updatePayload.embedded_at = data.embedded_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await memory_embeddings.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.memory_item !== undefined) {
|
|
await memory_embeddings.setMemory_item(
|
|
|
|
data.memory_item,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.memory_embeddings.getTableName(),
|
|
belongsToColumn: 'vector_blob',
|
|
belongsToId: memory_embeddings.id,
|
|
},
|
|
data.vector_blob,
|
|
options,
|
|
);
|
|
|
|
|
|
return memory_embeddings;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const memory_embeddings = await db.memory_embeddings.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of memory_embeddings) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of memory_embeddings) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return memory_embeddings;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const memory_embeddings = await db.memory_embeddings.findByPk(id, options);
|
|
|
|
await memory_embeddings.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await memory_embeddings.destroy({
|
|
transaction
|
|
});
|
|
|
|
return memory_embeddings;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const memory_embeddings = await db.memory_embeddings.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!memory_embeddings) {
|
|
return memory_embeddings;
|
|
}
|
|
|
|
const output = memory_embeddings.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.memory_item = await memory_embeddings.getMemory_item({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.vector_blob = await memory_embeddings.getVector_blob({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.memory_items,
|
|
as: 'memory_item',
|
|
|
|
where: filter.memory_item ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.memory_item.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.memory_item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'vector_blob',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.model_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'memory_embeddings',
|
|
'model_name',
|
|
filter.model_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.dimensionsRange) {
|
|
const [start, end] = filter.dimensionsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
dimensions: {
|
|
...where.dimensions,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
dimensions: {
|
|
...where.dimensions,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.embedded_atRange) {
|
|
const [start, end] = filter.embedded_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
embedded_at: {
|
|
...where.embedded_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
embedded_at: {
|
|
...where.embedded_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.provider) {
|
|
where = {
|
|
...where,
|
|
provider: filter.provider,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.memory_embeddings.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'memory_embeddings',
|
|
'model_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.memory_embeddings.findAll({
|
|
attributes: [ 'id', 'model_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['model_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.model_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|