From 8fa69d2f7ec9266d1c583bb6ccf4b60a29b290f3 Mon Sep 17 00:00:00 2001 From: Amruth-Vamshi Date: Wed, 8 Nov 2023 13:49:36 +0530 Subject: [PATCH] updated prompt history --- .../migration.sql | 15 ++++ prisma/schema.prisma | 3 +- .../prompt-history/prompt-history.service.ts | 86 +++++++++---------- 3 files changed, 59 insertions(+), 45 deletions(-) create mode 100644 prisma/migrations/20231108081755_update_prompt_history/migration.sql diff --git a/prisma/migrations/20231108081755_update_prompt_history/migration.sql b/prisma/migrations/20231108081755_update_prompt_history/migration.sql new file mode 100644 index 0000000..4d142aa --- /dev/null +++ b/prisma/migrations/20231108081755_update_prompt_history/migration.sql @@ -0,0 +1,15 @@ +/* + Warnings: + + - You are about to drop the column `pdfId` on the `prompt_history` table. All the data in the column will be lost. + - A unique constraint covering the columns `[queryInEnglish]` on the table `prompt_history` will be added. If there are existing duplicate values, this will fail. + +*/ +-- DropIndex +DROP INDEX "prompt_history_queryInEnglish_pdfId_key"; + +-- AlterTable +ALTER TABLE "prompt_history" DROP COLUMN "pdfId"; + +-- CreateIndex +CREATE UNIQUE INDEX "prompt_history_queryInEnglish_key" ON "prompt_history"("queryInEnglish"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b44a72f..b829a60 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -75,10 +75,9 @@ model prompt_history { timesUsed Int metadata Json? embedding Unsupported("vector")? - pdfId String? @db.Uuid queryId String? @db.Uuid - @@unique([queryInEnglish,pdfId]) + @@unique([queryInEnglish]) } model config { diff --git a/src/modules/prompt-history/prompt-history.service.ts b/src/modules/prompt-history/prompt-history.service.ts index 3fe449e..95bbda6 100644 --- a/src/modules/prompt-history/prompt-history.service.ts +++ b/src/modules/prompt-history/prompt-history.service.ts @@ -76,52 +76,52 @@ export class PromptHistoryService { // } async create(queryId): Promise { - let query = await this.prisma.query.findFirst({ - where: { - id: queryId - } - }) - if(!query) return null - let similarDocs = await this.prisma.similarity_search_response.findFirst({ - where: { - queryId: queryId - } - }) - if(!similarDocs) return null - let doc = await this.prisma.document.findFirst({ - where: { - id: similarDocs.documentId - } - }) - if(!doc) return null - let promptHistory = await this.prisma.prompt_history.findUnique({ - where:{ - queryInEnglish_pdfId: { - queryInEnglish: query.query, - pdfId: doc.pdfId + try{ + let query = await this.prisma.query.findFirst({ + where: { + id: queryId + } + }) + if(!query) return null + let similarDocs = await this.prisma.similarity_search_response.findFirst({ + where: { + queryId: queryId + } + }) + if(!similarDocs) return null + let doc = await this.prisma.document.findFirst({ + where: { + id: similarDocs.documentId } - } - }) - if(promptHistory) return null - let embedding = (await this.aiToolsService.getEmbedding(query.query))[0]; - if(embedding){ - promptHistory = await this.prisma.prompt_history.create({ - data:{ - queryInEnglish: query.query, - responseInEnglish: query.response, - timesUsed: 0, - responseTime: query.responseTime, - metadata: {}, - queryId: query.id, - pdfId: doc.pdfId + }) + if(!doc) return null + let promptHistory = await this.prisma.prompt_history.findUnique({ + where:{ + queryInEnglish: query.query } }) - await this.prisma.$queryRawUnsafe( - `UPDATE prompt_history SET embedding = '[${embedding - .map((x) => `${x}`) - .join(",")}]' WHERE id = ${promptHistory.id}` - ); - return promptHistory + if(promptHistory) return null + let embedding = (await this.aiToolsService.getEmbedding(query.query))[0]; + if(embedding){ + promptHistory = await this.prisma.prompt_history.create({ + data:{ + queryInEnglish: query.query, + responseInEnglish: query.response, + timesUsed: 0, + responseTime: query.responseTime, + metadata: {}, + queryId: query.id + } + }) + await this.prisma.$queryRawUnsafe( + `UPDATE prompt_history SET embedding = '[${embedding + .map((x) => `${x}`) + .join(",")}]' WHERE id = ${promptHistory.id}` + ); + return promptHistory + } + } catch (error){ + console.log(error) } }