-
Notifications
You must be signed in to change notification settings - Fork 220
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 #310 from briefercloud/rag2
Rag SQL AI
- Loading branch information
Showing
15 changed files
with
308 additions
and
16 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
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,55 @@ | ||
import { createHash } from 'crypto' | ||
import prisma from '@briefer/database' | ||
import { OpenAI } from 'openai' | ||
import { head } from 'ramda' | ||
import { z } from 'zod' | ||
import { logger } from './logger.js' | ||
import { jsonString } from '@briefer/types' | ||
|
||
export async function createEmbedding(input: string, openAiApiKey: string) { | ||
const model = 'text-embedding-3-small' | ||
const inputChecksum = createHash('sha256').update(input).digest('hex') | ||
const rawExistingEmbeddings = await prisma() | ||
.$queryRaw`SELECT embedding::text FROM "EmbeddingCache" WHERE "inputChecksum" = ${inputChecksum} AND model = ${model}` | ||
|
||
const parsedExistingEmbeddings = z | ||
.array(z.object({ embedding: jsonString.pipe(z.array(z.number())) })) | ||
.safeParse(rawExistingEmbeddings) | ||
if (parsedExistingEmbeddings.success) { | ||
const embedding = head(parsedExistingEmbeddings.data)?.embedding | ||
if (embedding) { | ||
return embedding | ||
} | ||
} else { | ||
logger().error( | ||
{ | ||
err: parsedExistingEmbeddings.error, | ||
}, | ||
'Failed to parse existing embeddings' | ||
) | ||
} | ||
|
||
const openai = new OpenAI({ apiKey: openAiApiKey }) | ||
const embeddingResponse = await openai.embeddings.create({ | ||
model, | ||
input, | ||
}) | ||
const embedding = head(embeddingResponse.data)?.embedding | ||
if (!embedding) { | ||
throw new Error('OpenAI did not return any embeddings') | ||
} | ||
|
||
try { | ||
await prisma() | ||
.$queryRaw`INSERT INTO "EmbeddingCache" ("inputChecksum", model, embedding) VALUES (${inputChecksum}, ${model}, ${embedding}::vector) ON CONFLICT DO NOTHING` | ||
} catch (err) { | ||
logger().error( | ||
{ | ||
err, | ||
}, | ||
'Failed to insert embedding into cache' | ||
) | ||
} | ||
|
||
return embedding | ||
} |
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,7 @@ | ||
FROM postgres:16 | ||
|
||
# Install pgvector | ||
RUN apt-get update && apt-get install -y postgresql-16-pgvector | ||
|
||
# Clean up | ||
RUN apt-get clean && rm -rf /var/lib/apt/lists/* |
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
4 changes: 4 additions & 0 deletions
4
...e/prisma/migrations/20250105002014_add_embedding_to_datasource_schema_table/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,4 @@ | ||
CREATE EXTENSION IF NOT EXISTS vector; | ||
|
||
-- AlterTable | ||
ALTER TABLE "DataSourceSchemaTable" ADD COLUMN "embedding" vector; |
14 changes: 14 additions & 0 deletions
14
...s/database/prisma/migrations/20250105011201_introduce_embedding_cache_table/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,14 @@ | ||
-- CreateTable | ||
CREATE TABLE "EmbeddingCache" ( | ||
"id" UUID NOT NULL DEFAULT gen_random_uuid(), | ||
"inputChecksum" TEXT NOT NULL, | ||
"model" TEXT NOT NULL, | ||
"embedding" vector NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "EmbeddingCache_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "unique_inputChecksum_model" ON "EmbeddingCache"("inputChecksum", "model"); |
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
Oops, something went wrong.