|
1 | 1 | import { models } from "@hypermode/functions-as"; |
2 | 2 | import { EmbeddingsModel } from "@hypermode/models-as/models/experimental/embeddings"; |
3 | 3 | import { collections } from "@hypermode/functions-as"; |
4 | | -import { getProduct } from "./crud"; |
| 4 | +import { getProduct, getCart } from "./crud"; |
5 | 5 | import { ProductSearchResult, ProductSearchObject, consts } from "./types"; |
6 | 6 |
|
| 7 | +export function recommendProductByCart( |
| 8 | + cartId: string, |
| 9 | + maxItems: i32, |
| 10 | +): ProductSearchResult { |
| 11 | + const productSearchRes = new ProductSearchResult( |
| 12 | + consts.productNameCollection, |
| 13 | + consts.searchMethod, |
| 14 | + "success", |
| 15 | + "", |
| 16 | + ); |
| 17 | + |
| 18 | + const cart = getCart(cartId); |
| 19 | + if (cart === null) { |
| 20 | + productSearchRes.status = "error"; |
| 21 | + productSearchRes.error = "Cart not found"; |
| 22 | + return productSearchRes; |
| 23 | + } |
| 24 | + |
| 25 | + const cartVecs: f32[][] = []; |
| 26 | + |
| 27 | + for (let i = 0; i < cart.items.length; i++) { |
| 28 | + const vec = collections.getVector( |
| 29 | + consts.productNameCollection, |
| 30 | + consts.searchMethod, |
| 31 | + cart.items[i].Product.id, |
| 32 | + ); |
| 33 | + |
| 34 | + cartVecs.push(vec); |
| 35 | + } |
| 36 | + |
| 37 | + const sumVec: f32[] = []; |
| 38 | + |
| 39 | + for (let i = 0; i < cartVecs[0].length; i++) { |
| 40 | + sumVec[i] = 0; |
| 41 | + for (let j = 0; j < cartVecs.length; j++) { |
| 42 | + sumVec[i] += cartVecs[j][i]; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const normalizedVec = normalize(sumVec); |
| 47 | + |
| 48 | + const cartProductIds = cart.items.map((item) => item.Product.id); |
| 49 | + |
| 50 | + const semanticSearchRes = collections.searchByVector( |
| 51 | + consts.productNameCollection, |
| 52 | + consts.searchMethod, |
| 53 | + normalizedVec, |
| 54 | + maxItems + cart.items.length, |
| 55 | + ); |
| 56 | + |
| 57 | + if (!semanticSearchRes.isSuccessful) { |
| 58 | + productSearchRes.status = semanticSearchRes.status; |
| 59 | + productSearchRes.error = semanticSearchRes.error; |
| 60 | + |
| 61 | + return productSearchRes; |
| 62 | + } |
| 63 | + |
| 64 | + for (let i = 0; i < semanticSearchRes.objects.length; i++) { |
| 65 | + if (cartProductIds.includes(semanticSearchRes.objects[i].key)) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + const searchObj = getSearchObject( |
| 69 | + semanticSearchRes.objects[i].key, |
| 70 | + semanticSearchRes.objects[i].score, |
| 71 | + semanticSearchRes.objects[i].distance, |
| 72 | + ); |
| 73 | + productSearchRes.searchObjs.push(searchObj); |
| 74 | + } |
| 75 | + |
| 76 | + productSearchRes.searchObjs = productSearchRes.searchObjs.slice(0, maxItems); |
| 77 | + |
| 78 | + return productSearchRes; |
| 79 | +} |
| 80 | + |
7 | 81 | export function searchProducts( |
8 | 82 | query: string, |
9 | 83 | maxItems: i32, |
@@ -125,3 +199,27 @@ export function miniLMEmbed(texts: string[]): f32[][] { |
125 | 199 |
|
126 | 200 | return output.predictions; |
127 | 201 | } |
| 202 | + |
| 203 | +// Function to calculate the magnitude of a vector |
| 204 | +function magnitude(vec: f32[]): f32 { |
| 205 | + let sum: f32 = 0.0; |
| 206 | + for (let i = 0; i < vec.length; i++) { |
| 207 | + sum += vec[i] * vec[i]; |
| 208 | + } |
| 209 | + return f32(Math.sqrt(sum)); |
| 210 | +} |
| 211 | + |
| 212 | +// Function to normalize a vector |
| 213 | +function normalize(vec: f32[]): f32[] { |
| 214 | + const mag = magnitude(vec); |
| 215 | + if (mag == 0) { |
| 216 | + throw new Error("Cannot normalize a zero vector"); |
| 217 | + } |
| 218 | + |
| 219 | + const normalizedVec: f32[] = []; |
| 220 | + for (let i = 0; i < vec.length; i++) { |
| 221 | + normalizedVec.push(vec[i] / mag); |
| 222 | + } |
| 223 | + |
| 224 | + return normalizedVec; |
| 225 | +} |
0 commit comments