-
Notifications
You must be signed in to change notification settings - Fork 2
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
6 changed files
with
353 additions
and
2 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
apps/api/prisma/migrations/20240716180407_add_cached_gpt_response/migration.sql
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,10 @@ | ||
-- CreateTable | ||
CREATE TABLE "CachedGptResponse" ( | ||
"id" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
"query" TEXT NOT NULL, | ||
"response" TEXT NOT NULL, | ||
|
||
CONSTRAINT "CachedGptResponse_pkey" PRIMARY KEY ("id") | ||
); |
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,24 @@ | ||
import prisma from '../../lib/prisma' | ||
|
||
export async function findAiCacheByQuery({ | ||
query, | ||
}: { | ||
query: string | ||
}) { | ||
return await prisma.cachedGptResponse.findFirst({ | ||
where: { query }, | ||
orderBy: { updatedAt: 'desc' }, | ||
}) | ||
} | ||
|
||
export async function createAiCache({ | ||
query, | ||
response, | ||
}: { | ||
query: string | ||
response: string | ||
}) { | ||
return await prisma.cachedGptResponse.create({ | ||
data: { query, response }, | ||
}) | ||
} |
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,15 @@ | ||
import { createHash } from 'crypto' | ||
import { getLogger } from '../../lib/log' | ||
|
||
export async function hashFile(file: File) { | ||
const log = getLogger(`file.service:${hashFile.name}`) | ||
|
||
log.debug('Hashing file. File size: %d', file.size) | ||
|
||
const buffer = Buffer.from(await file.arrayBuffer()) | ||
const hash = createHash('sha256').update(buffer).digest('hex') | ||
|
||
log.info('Hashed file. Hash: %s', hash) | ||
|
||
return hash | ||
} |
Oops, something went wrong.