-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { publicProcedure } from '../../trpc'; | ||
import { getSimilarPacksService } from '../../services/pack/pack.service'; | ||
import { z } from 'zod'; | ||
|
||
/** | ||
* Retrieves packs that are similar to provided pack. | ||
* @param {Object} req - the request object | ||
* @param {Object} res - the response object | ||
* @return {Promise} - a promise that resolves with an array of similar packs | ||
*/ | ||
export function getSimilarPacksRoute() { | ||
return publicProcedure | ||
.input(z.object({ id: z.string(), limit: z.number() })) | ||
.query(async (opts) => { | ||
const { id, limit } = opts.input; | ||
const packs = await getSimilarPacksService(id, limit); | ||
return packs; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { DbClient } from '../../db/client'; | ||
import { Pack } from '../../drizzle/methods/pack'; | ||
import { VectorClient } from '../../vector/client'; | ||
import { pack as PacksTable } from '../../db/schema'; | ||
import { inArray } from 'drizzle-orm'; | ||
|
||
/** | ||
* Retrieves packs that are similar to the provided pack. | ||
* | ||
* @param {string} id - ID of the pack for which to retrive similar packs for. | ||
* @param {string} limit - Max number of similar packs to return. | ||
* @return {Promise<any[]>} An array of similar packs. | ||
*/ | ||
export async function getSimilarPacksService(id: string, limit: number = 5) { | ||
const packClass = new Pack(); | ||
let pack = await packClass.findPack({ | ||
id, | ||
}); | ||
|
||
if (!pack) { | ||
throw new Error(`Pack with id: ${id} not found`); | ||
} | ||
|
||
const { matches } = await VectorClient.instance.search( | ||
pack.name, | ||
'packs', | ||
limit, | ||
); | ||
|
||
const similarPacksResult = await DbClient.instance | ||
.select() | ||
.from(PacksTable) | ||
.where( | ||
inArray( | ||
PacksTable.id, | ||
matches.matches.map((m) => m.id), | ||
), | ||
); | ||
|
||
// add similarity score to packs result | ||
const similarPacks = matches.matches.map((match) => { | ||
return { | ||
...similarPacksResult.find((p) => p.id == match.id), | ||
similarityScore: match.score, | ||
}; | ||
}); | ||
|
||
return similarPacks; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters