Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change plan parts embeddings to 256 vectprs #157

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions packages/core/src/models/lessonPlans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
PrismaClientWithAccelerate,
Subject,
} from "@oakai/db";
import { Prisma } from "@prisma/client";

Check failure on line 12 in packages/core/src/models/lessonPlans.ts

View workflow job for this annotation

GitHub Actions / lint

'Prisma' is defined but never used
import { OpenAIEmbeddings } from "langchain/embeddings/openai";

Check failure on line 13 in packages/core/src/models/lessonPlans.ts

View workflow job for this annotation

GitHub Actions / lint

'OpenAIEmbeddings' is defined but never used
import { PrismaVectorStore } from "langchain/vectorstores/prisma";

Check failure on line 14 in packages/core/src/models/lessonPlans.ts

View workflow job for this annotation

GitHub Actions / lint

'PrismaVectorStore' is defined but never used
import yaml from "yaml";

import { LLMResponseJsonSchema } from "../../../aila/src/protocol/jsonPatchProtocol";
Expand Down Expand Up @@ -57,7 +57,7 @@
parts: LessonPlanPart[] | null;
};

interface FilterOptions {

Check failure on line 60 in packages/core/src/models/lessonPlans.ts

View workflow job for this annotation

GitHub Actions / lint

'FilterOptions' is defined but never used
key_stage_id?: object;
subject_id?: object;
}
Expand Down Expand Up @@ -335,75 +335,4 @@
WHERE id = ${id}`;
return result;
}

async search(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unused, and the table 'lesson_plans' no longer has embeddings in it, so this will not work.

query: string,
keyStage: string | undefined,
subject: string | undefined,
perPage: number,
): Promise<LessonPlanWithLesson[]> {
const filter: FilterOptions = {};

if (keyStage) {
const keyStageRecord = await this._rag.fetchKeyStage(keyStage);

if (keyStageRecord) {
filter["key_stage_id"] = {
equals: keyStageRecord.id,
};
}
}
if (subject) {
const subjectRecord = await this._rag.fetchSubject(subject);
if (subjectRecord) {
filter["subject_id"] = {
equals: subjectRecord.id,
};
}
}
const vectorStore = PrismaVectorStore.withModel<LessonPlan>(
this._prisma,
).create(new OpenAIEmbeddings(), {
prisma: Prisma,
tableName: "lesson_plans" as "LessonPlan",
vectorColumnName: "embedding",
columns: {
id: PrismaVectorStore.IdColumn,
content: PrismaVectorStore.ContentColumn,
},
// @ts-expect-error TODO Bug in PrismaVectorStore which doesn't allow mapped column names
filter,
});
const result = await vectorStore.similaritySearch(query, perPage);

const lessonPlans: LessonPlanWithLesson[] =
await this._prisma.lessonPlan.findMany({
where: {
id: { in: result.map((r) => r.metadata.id) },
},
include: {
lesson: {
select: {
id: true,
slug: true,
title: true,
subjectId: true,
keyStageId: true,
isNewLesson: true,
newLessonContent: true,
},
},
},
});

const hydrated: LessonPlanWithLesson[] = [];
for (const entry of result) {
const lessonPlan = lessonPlans.find((ls) => ls.id === entry.metadata.id);
if (!lessonPlan) {
throw new Error("Lesson summary not found");
}
hydrated.push(lessonPlan);
}
return hydrated;
}
}
32 changes: 19 additions & 13 deletions packages/core/src/rag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,20 +733,26 @@ Thank you and happy classifying!`;

const vectorStore = PrismaVectorStore.withModel<LessonPlanPart>(
this.prisma,
).create(new OpenAIEmbeddings(), {
prisma: Prisma,
tableName: "lesson_plan_parts" as "LessonPlanPart",
vectorColumnName: "embedding",
verbose: true,
openAIApiKey: process.env.OPENAI_API_KEY,
columns: {
id: PrismaVectorStore.IdColumn,
lesson_plan_id: PrismaVectorStore.IdColumn,
content: PrismaVectorStore.ContentColumn,
).create(
new OpenAIEmbeddings({
modelName: "text-embedding-3-large",
dimensions: 256,
}),
{
prisma: Prisma,
tableName: "lesson_plan_parts" as "LessonPlanPart",
vectorColumnName: "embedding",
verbose: true,
openAIApiKey: process.env.OPENAI_API_KEY,
columns: {
id: PrismaVectorStore.IdColumn,
lesson_plan_id: PrismaVectorStore.IdColumn,
content: PrismaVectorStore.ContentColumn,
},
// @ts-expect-error TODO Bug in PrismaVectorStore which doesn't allow mapped column names
filter,
},
// @ts-expect-error TODO Bug in PrismaVectorStore which doesn't allow mapped column names
filter,
});
);

const similaritySearchTerm = topic ? `${title}. ${topic}` : title;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- This changes the embedding column from a 1536 vector to a 256 vector
ALTER TABLE lesson_plan_parts ADD COLUMN embedding_temp vector(256);
ALTER TABLE lesson_plan_parts DROP COLUMN embedding;
ALTER TABLE lesson_plan_parts RENAME COLUMN embedding_temp TO embedding;
2 changes: 1 addition & 1 deletion packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ model LessonPlanPart {
lessonPlan LessonPlan @relation(fields: [lessonPlanId], references: [id], onDelete: Cascade)
subject Subject? @relation(fields: [subjectId], references: [id], onDelete: Cascade)
keyStage KeyStage? @relation(fields: [keyStageId], references: [id], onDelete: Cascade)
embedding Unsupported("vector(1536)")?
embedding Unsupported("vector(256)")?
status LessonPlanPartStatus @default(PENDING)

@@unique([lessonPlanId, key])
Expand Down
Loading