Skip to content

Commit

Permalink
updated prompt history
Browse files Browse the repository at this point in the history
  • Loading branch information
Amruth-Vamshi committed Nov 8, 2023
1 parent e1b154b commit 8fa69d2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -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");
3 changes: 1 addition & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
86 changes: 43 additions & 43 deletions src/modules/prompt-history/prompt-history.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,52 +76,52 @@ export class PromptHistoryService {
// }

async create(queryId): Promise<PromptHistory> {
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)
}
}

Expand Down

0 comments on commit 8fa69d2

Please sign in to comment.