-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #996 from andrew-bierman/feat/cf-vectorize-initial
- Loading branch information
Showing
47 changed files
with
1,059 additions
and
31 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 @@ | ||
# shared-types |
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,20 @@ | ||
{ | ||
"name": "@packrat/shared-types", | ||
"version": "0.0.1", | ||
"source": "src/index.ts", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"private": true, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "NODE_OPTIONS=--max_old_space_size=4096 tsc", | ||
"build:and:install": "yarn build && yarn", | ||
"postinstall": "yarn build", | ||
"clean": "rm -rf dist && rm -rf node_modules" | ||
}, | ||
"keywords": [], | ||
"license": "ISC", | ||
"devDependencies": { | ||
"typescript": "^5.5.3" | ||
} | ||
} |
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 @@ | ||
export * from './packAndItemVisibilityFilter'; |
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,5 @@ | ||
export enum PackAndItemVisibilityFilter { | ||
PUBLIC = 'PUBLIC', | ||
PRIVATE = 'PRIVATE', | ||
ALL = 'ALL', | ||
} |
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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "ESNext", | ||
"lib": ["es6", "DOM"], | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"isolatedModules": true, | ||
"moduleResolution": "node", | ||
"preserveWatchOutput": true, | ||
"skipLibCheck": true, | ||
"allowJs": true, | ||
"strict": true, | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"declaration": true, | ||
"sourceMap": true, | ||
"declarationMap": true | ||
}, | ||
"exclude": ["node_modules"], | ||
"include": ["src"] | ||
} |
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
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
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,18 @@ | ||
import { getSimilarItemsService } from '../../services/item/getSimilarItemsService'; | ||
import { protectedProcedure } from '../../trpc'; | ||
import * as validator from '@packrat/validations'; | ||
|
||
export function getSimilarItemsRoute() { | ||
return protectedProcedure | ||
.input(validator.getSimilarItems) | ||
.query(async (opts) => { | ||
const { id, limit, visibility } = opts.input; | ||
|
||
if (limit < 1) { | ||
throw new Error('limit must be greater than 0'); | ||
} | ||
|
||
const items = await getSimilarItemsService(id, limit, visibility); | ||
return items; | ||
}); | ||
} |
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
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,16 @@ | ||
import { protectedProcedure } from '../../trpc'; | ||
import { getSimilarPacksService } from '../../services/pack/pack.service'; | ||
import { getSimilarPacks } from '@packrat/validations'; | ||
|
||
export function getSimilarPacksRoute() { | ||
return protectedProcedure.input(getSimilarPacks).query(async (opts) => { | ||
const { id, limit, visibility } = opts.input; | ||
|
||
if (limit < 1) { | ||
throw new Error('limit must be greater than 0'); | ||
} | ||
|
||
const packs = await getSimilarPacksService(id, limit, visibility); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Ai } from "@cloudflare/ai"; | ||
|
||
class AiClient { | ||
private static _instance: AiClient | null = null; | ||
private apiKey: string; | ||
private accountId: string; | ||
private readonly MODEL_NAME: string = '@cf/baai/bge-base-en-v1.5'; | ||
private readonly EXECUTE_AI_MODEL_URL: string; | ||
|
||
private constructor(apiKey: string, accountId: string) { | ||
this.apiKey = apiKey; | ||
this.accountId = accountId; | ||
this.EXECUTE_AI_MODEL_URL = `https://api.cloudflare.com/client/v4/accounts/${this.accountId}/ai/run/${this.MODEL_NAME}`; | ||
} | ||
|
||
public static get instance(): AiClient { | ||
if (!AiClient._instance) { | ||
throw new Error('AiClient instance not initialized.'); | ||
} | ||
return AiClient._instance; | ||
} | ||
|
||
public static async init({ | ||
apiKey, | ||
accountId, | ||
}: { | ||
apiKey: string; | ||
accountId: string; | ||
}): Promise<void> { | ||
if (!AiClient._instance) { | ||
AiClient._instance = new AiClient(apiKey, accountId); | ||
} | ||
} | ||
|
||
public async run(text: string) { | ||
const response = await fetch(this.EXECUTE_AI_MODEL_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.apiKey}`, | ||
}, | ||
body: JSON.stringify({ text }), | ||
}); | ||
|
||
if (!response.ok) { | ||
const errorText = await response.text(); | ||
throw new Error( | ||
`Failed to get embedding: ${response.statusText} - ${errorText}`, | ||
); | ||
} | ||
|
||
return await response.json(); | ||
} | ||
|
||
public static async getEmbedding(content: string): Promise<number[]> { | ||
const { | ||
result: { data }, | ||
} = await AiClient.instance.run(content); | ||
return data[0]; | ||
} | ||
|
||
// Commented out using AI client and temporalily use REST API instead | ||
// public static async init(cfAi): Promise<void> { | ||
// if (!AiClient._instance) { | ||
// AiClient._instance = new Ai(cfAi); | ||
// } | ||
// } | ||
|
||
// public static async getEmbedding(content: string): Promise<number[]> { | ||
// const { data } = await AiClient.instance.run('@cf/baai/bge-base-en-v1.5', { text: [content] }); | ||
// return data[0]; | ||
// } | ||
} | ||
|
||
export { AiClient }; | ||
|
||
export interface Env { | ||
AI: any; | ||
} |
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,25 @@ | ||
import PQueue from 'p-queue'; | ||
|
||
interface QueueTask<T> { | ||
(): Promise<T>; | ||
} | ||
|
||
export class Queue { | ||
private static instance: Queue; | ||
private queue: PQueue; | ||
|
||
private constructor(concurrency: number = 1) { | ||
this.queue = new PQueue({ concurrency }); | ||
} | ||
|
||
static getInstance(concurrency: number = 1): Queue { | ||
if (!Queue.instance) { | ||
Queue.instance = new Queue(concurrency); | ||
} | ||
return Queue.instance; | ||
} | ||
|
||
async addTask<T>(task: QueueTask<T>): Promise<T> { | ||
return this.queue.add(task); | ||
} | ||
} |
Oops, something went wrong.