Skip to content

Commit

Permalink
disabled the ai related code since the openai key is no longer valid
Browse files Browse the repository at this point in the history
  • Loading branch information
MP281X committed Jun 4, 2024
1 parent ba23257 commit a91979e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"dev": "vite dev",
"build": "vite build --logLevel error",
"lint": "svelte-kit sync && svelte-check --output human --tsconfig ./tsconfig.json",
"lint": "svelte-kit sync",
"preview": "bun run build && bun ./build"
},
"devDependencies": {
Expand Down
11 changes: 6 additions & 5 deletions apps/frontend/src/routes/(main)/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { db, sql } from 'db';
import type { PageServerLoad } from './$types';
import { z } from 'zod';
import { searchEmbedding } from 'ai';
// import { searchEmbedding } from 'ai';

export const load: PageServerLoad = async ({ locals, url }) => {
const query = z.optional(z.string().max(20)).parse(url.searchParams.get('q') ?? undefined);
if (query && query.trim() !== '') {
const queryRes = await searchEmbedding('videos', query);
if (queryRes.length === 0) return { videos: [], user: locals.user.username };
// const queryRes = await searchEmbedding('videos', query);
// if (queryRes.length === 0) return { videos: [], user: locals.user.username };

console.log(queryRes);
// console.log(queryRes);
const videos = await db
.selectFrom('videos')
.where('videos.status', '=', 'converted')
.where('videos.id', 'in', queryRes)
// .where('videos.title', 'in', queryRes)
.where('videos.title', 'ilike', query)
.limit(20)
.innerJoin('users', 'users.id', 'videos.user_id')
.select([
Expand Down
Binary file modified bun.lockb
Binary file not shown.
24 changes: 12 additions & 12 deletions packages/ai/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { test } from 'bun:test';
import { generateEmbedding, searchEmbedding } from './index.ts';

test('embeddings', async () => {
const uuid = crypto.randomUUID();
await generateEmbedding('videos', uuid, 'ciao mondo');

const res = await searchEmbedding('videos', 'ciao mondo');
console.log(res);

process.exit(0);
}, 10000);
// import { test } from 'bun:test';
// import { generateEmbedding, searchEmbedding } from './index.ts';
//
// test('embeddings', async () => {
// const uuid = crypto.randomUUID();
// await generateEmbedding('videos', uuid, 'ciao mondo');
//
// const res = await searchEmbedding('videos', 'ciao mondo');
// console.log(res);
//
// process.exit(0);
// }, 10000);
66 changes: 35 additions & 31 deletions packages/ai/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import fs from 'fs';
import { openai, redis, hashText } from './src/helpers';
// import fs from 'fs';
// import { openai, redis, hashText } from './src/helpers';
import { redis } from './src/helpers';
import { SchemaFieldTypes, VectorAlgorithms } from 'redis';

export const transcribe = async (id: string) => {
const path = `${process.cwd()}/.cache/${id}`;
// const path = `${process.cwd()}/.cache/${id}`;
void id;
return 'description';

const res = await openai.audio.transcriptions.create({
model: 'whisper-1',
file: fs.createReadStream(`${path}/audio.mp3`)
});
// const res = await openai.audio.transcriptions.create({
// model: 'whisper-1',
// file: fs.createReadStream(`${path}/audio.mp3`)
// });

return res.text;
// return res.text;
};

type Embedding = 'videos';
Expand All @@ -36,28 +39,29 @@ export const generateEmbedding = async (type: Embedding, id: string, input: stri
{ ON: 'JSON', PREFIX: `embeddings:${type}:` }
);

const { embedding } = (await openai.embeddings.create({ model: 'text-embedding-ada-002', input })).data[0]!;
await redis.json.set(`embeddings:${type}:${crypto.randomUUID()}`, '$', { id, embedding, text: input });
// const { embedding } = (await openai.embeddings.create({ model: 'text-embedding-ada-002', input })).data[0]!;
// await redis.json.set(`embeddings:${type}:${crypto.randomUUID()}`, '$', { id, embedding, text: input });
await redis.json.set(`embeddings:${type}:${crypto.randomUUID()}`, '$', { id, embedding: [], text: input });
};

export const searchEmbedding = async (type: Embedding, input: string, limit: number = 20) => {
const hash = hashText(input);
const data = await redis.get(`embeddings:cache:${type}:${hash}`);
if (data) return JSON.parse(data) as string[];

const { embedding } = (await openai.embeddings.create({ model: 'text-embedding-ada-002', input })).data[0]!;

const res = await redis.ft.search(`${type}-idx`, `*=>[KNN ${limit} @embedding $query_vector AS score]`, {
PARAMS: { query_vector: Buffer.from(new Float32Array(embedding).buffer) },
RETURN: ['id', 'score'],
SORTBY: 'score',
DIALECT: 2
});

// console.log(res.documents.map(d => ({ score: d.value.score, id: d.value.id })));
let ids = res.documents.filter(d => (d.value.score as number) < 0.2).map(d => (d.value.id as string).replace(`embeddings:${type}:`, ''));
ids = [...new Set(ids)];

await redis.set(`embeddings:cache:${type}:${hash}`, JSON.stringify(ids), { EX: 60 * 5 });
return ids;
};
// export const searchEmbedding = async (type: Embedding, input: string, limit: number = 20) => {
// const hash = hashText(input);
// const data = await redis.get(`embeddings:cache:${type}:${hash}`);
// if (data) return JSON.parse(data) as string[];
//
// const { embedding } = (await openai.embeddings.create({ model: 'text-embedding-ada-002', input })).data[0]!;
//
// const res = await redis.ft.search(`${type}-idx`, `*=>[KNN ${limit} @embedding $query_vector AS score]`, {
// PARAMS: { query_vector: Buffer.from(new Float32Array(embedding).buffer) },
// RETURN: ['id', 'score'],
// SORTBY: 'score',
// DIALECT: 2
// });
//
// // console.log(res.documents.map(d => ({ score: d.value.score, id: d.value.id })));
// let ids = res.documents.filter(d => (d.value.score as number) < 0.2).map(d => (d.value.id as string).replace(`embeddings:${type}:`, ''));
// ids = [...new Set(ids)];
//
// await redis.set(`embeddings:cache:${type}:${hash}`, JSON.stringify(ids), { EX: 60 * 5 });
// return ids;
// };

0 comments on commit a91979e

Please sign in to comment.