Skip to content

Commit

Permalink
add distance to results
Browse files Browse the repository at this point in the history
add the store distance and remove the vector embedding from the items added.
  • Loading branch information
racebedo123 committed Nov 22, 2024
1 parent f645435 commit f0361a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
26 changes: 22 additions & 4 deletions routes/embeddedResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const averageEmbedding = (tensor) => {



function groupSimilarItems(data, threshold = 0.6) {
function groupSimilarItems(data, threshold = 0.5) {
const groupedItems = [];
const processedIndices = new Set();

Expand All @@ -66,7 +66,9 @@ function groupSimilarItems(data, threshold = 0.6) {
const compareItem = data[j];

if (currentItem.store !== compareItem.store) {
console.log(currentItem.name, compareItem.name)
const similarity = cosineSimilarity(currentItem.embedding, compareItem.embedding);
console.log(similarity);

if (similarity >= threshold) {
currentGroup.push(compareItem);
Expand Down Expand Up @@ -155,13 +157,29 @@ router.put(`/${parsed.name}`, async (req, res) => {

// Sort results based on similarity in descending order
results.sort((a, b) => b.similarity - a.similarity);
console.log(results[0]);

const groupedItems = groupSimilarItems(results);

const finalItems = groupedItems.map(group => group[0]);
const newItems = groupedItems.map(group => group[0]);

finalItems.sort((a, b) => b.similarity - a.similarity);
newItems.sort((a, b) => b.similarity - a.similarity);

const finalItems = await Promise.all(
newItems.map(async (item) => {
const store = item.store;

// Remove the `embedding` field
const { embedding, ...filteredItem } = item;

// Call the store API
const response = await axios.put('/getStores', { store });
const data = await response.json();

// Add store data if needed or return the filtered item
console.log(data);
return { ...filteredItem, storeData: data };
})
);

res.json(finalItems);

Expand Down
6 changes: 3 additions & 3 deletions routes/getStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function fetchNearbyStores(store, userLocation) {
}

// Iterates and finds all approved stores in parallel
async function searchNearbyStores(userLocation, stores) {
async function searchNearbyStores(userLocation, store) {
let storeArray = [];
const promises = stores.map(async (query, index) => {
const result = await fetchNearbyStores(query, userLocation);
Expand Down Expand Up @@ -115,10 +115,10 @@ router.put(`/${parsed.name}`, async (req, res) => {
return res.status(400).json({ error: 'Location is required' });
}

let stores = ['Target', 'Hannaford', 'CVS', 'ShopRite'];
let store = req.body.store;
const coordinates = await fetchCoordinates(location);

let storeArray = await searchNearbyStores(coordinates, stores);
let storeArray = await searchNearbyStores(coordinates, store);

res.json(storeArray);
});
Expand Down

0 comments on commit f0361a7

Please sign in to comment.