Skip to content

Commit

Permalink
Refactor DefaultEntitiesVectorAdapter for improved vector filtering a…
Browse files Browse the repository at this point in the history
…nd result accumulation

- Updated nearest() and furthest() methods to utilize collection filtering and reduce for more concise logic.
- Enhanced error handling by skipping items without vectors during calculations.
- Maintained default limit configuration in filter, ensuring consistency with future settings.
- Improved overall code readability and maintainability by streamlining result accumulation processes.
  • Loading branch information
Brian Joseph Petro committed Dec 15, 2024
1 parent 543661f commit 4a383f3
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions smart-entities/adapters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ export class DefaultEntitiesVectorAdapter extends EntitiesVectorAdapter {
if (!vec || !Array.isArray(vec)) {
throw new Error("Invalid vector input to nearest()");
}
const { limit = 50 } = filter;
const items = Object.values(this.collection.items).filter(item => item.vec);
const acc = { min: 0, results: new Set(), minResult: null };
for (let i = 0; i < items.length; i++) {
const item = items[i];
const score = cos_sim(vec, item.vec);
const result = { item, score };
results_acc(acc, result, limit);
}
return Array.from(acc.results).sort((a,b) => b.score - a.score);
const {
limit = 50, // TODO: default configured in settings
} = filter;
const nearest = this.collection.filter(filter)
.reduce((acc, item) => {
if (!item.vec) return acc; // skip if no vec
const result = { item, score: cos_sim(vec, item.vec) };
results_acc(acc, result, limit); // update acc
return acc;
}, { min: 0, results: new Set() });
return Array.from(nearest.results);
}

/**
Expand All @@ -56,18 +57,17 @@ export class DefaultEntitiesVectorAdapter extends EntitiesVectorAdapter {
if (!vec || !Array.isArray(vec)) {
throw new Error("Invalid vector input to furthest()");
}
const { limit = 50 } = filter;
const items = Object.values(this.collection.items).filter(item => item.vec);
const acc = { max: 1, results: new Set(), maxResult: null };
for (let i = 0; i < items.length; i++) {
const item = items[i];
const score = cos_sim(vec, item.vec);
const result = { item, score };
furthest_acc(acc, result, limit);
}
const results = Array.from(acc.results);
// sort ascending by score for furthest
return results.sort((a,b) => a.score - b.score);
const {
limit = 50, // TODO: default configured in settings
} = filter;
const furthest = this.collection.filter(filter)
.reduce((acc, item) => {
if (!item.vec) return acc; // skip if no vec
const result = { item, score: cos_sim(vec, item.vec) };
furthest_acc(acc, result, limit); // update acc
return acc;
}, { max: 0, results: new Set() });
return Array.from(furthest.results);
}

/**
Expand Down

0 comments on commit 4a383f3

Please sign in to comment.