Skip to content

Commit

Permalink
Add shared prompt slug methods, type annotations and remove comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stefl committed Aug 28, 2024
1 parent 0542bb6 commit 5a3c6b9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
12 changes: 9 additions & 3 deletions packages/aila/src/features/generation/AilaGeneration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { PromptVariants } from "@oakai/core/src/models/promptVariants";
import { ailaGenerate } from "@oakai/core/src/prompts/lesson-assistant/variants";
import {
ailaGenerate,
generateAilaPromptVersionVariantSlug,
} from "@oakai/core/src/prompts/lesson-assistant/variants";
import { prisma, Prompt } from "@oakai/db";
import { getEncoding } from "js-tiktoken";

Expand Down Expand Up @@ -150,8 +153,11 @@ export class AilaGeneration {
const basedOn = !!this._aila.lesson.plan.basedOn;
const useRag = this._aila.options.useRag ?? true;

// This key is defined in the setupPrompts script in core
const variantSlug = `${responseMode}-${basedOn ? "basedOn" : "notBasedOn"}-${useRag ? "rag" : "noRag"}`;
const variantSlug = generateAilaPromptVersionVariantSlug(
responseMode,
basedOn,
useRag,
);

let prompt: Prompt | null = null;
prompt = await prisma.prompt.findFirst({
Expand Down
46 changes: 34 additions & 12 deletions packages/core/src/models/promptVariants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@ export class PromptVariants {
this.variant = foundVariant;
}

async setCurrent(variant: string, raw = false) {
const template = this.format(raw);
async setCurrent(variant: string, storeAsJson = false) {
const template = this.format({ storeAsJson });
const { slug } = this.definition;
const hash = `${slug}-${variant}-${Md5.hashStr(template)}`;
console.log("*** setCurrent ***");
console.log("Hash", hash);
console.log("Slug", slug);
console.log("Variant", variant);
console.log("-----");
const hash = promptHash({ slug, variant, template });
const existing = await this.prisma.prompt.findFirst({
where: {
AND: [{ hash }, { slug }, { variant }],
Expand All @@ -41,9 +36,12 @@ export class PromptVariants {
if (existing) {
return;
}
console.log(
`Storing new prompt version for ${slug} / ${variant} with hash ${hash}`,
);

const { name, appId: appSlug, inputSchema, outputSchema } = this.definition;

console.log("Connecting to app", appSlug);
const app = await this.prisma.app.findFirstOrThrow({
where: { slug: appSlug },
});
Expand All @@ -66,7 +64,7 @@ export class PromptVariants {
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
variant,
identifier: `${slug}-${variant}-${version}`,
identifier: promptIdentifier({ slug, variant, version }),
version,
gitSha:
process.env.VERCEL_GIT_COMMIT_SHA ??
Expand Down Expand Up @@ -94,11 +92,11 @@ export class PromptVariants {
});
}

format(raw = false) {
format({ storeAsJson = false }: { storeAsJson?: boolean }) {
const { parts } = this.variant;
const { body, context, output, task } = parts;

if (raw) {
if (storeAsJson) {
return body;
}

Expand All @@ -121,3 +119,27 @@ export class PromptVariants {
${errorHandling}`;
}
}

export function promptHash({
slug,
variant,
template,
}: {
slug: string;
variant: string;
template: string;
}) {
return `${slug}-${variant}-${Md5.hashStr(template)}`;
}

export function promptIdentifier({
slug,
variant,
version,
}: {
slug: string;
variant: string;
version: number;
}): string {
return `${slug}-${variant}-${version}`;
}
8 changes: 6 additions & 2 deletions packages/core/src/prompts/lesson-assistant/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const generatePromptParts = (
};
};

const generateSlug = (
export const generateAilaPromptVersionVariantSlug = (
responseMode: string,
basedOn: boolean,
useRag: boolean,
Expand All @@ -41,7 +41,11 @@ const variants = [
{ responseMode: "generate", basedOn: false, useRag: true },
{ responseMode: "generate", basedOn: false, useRag: false },
].map(({ responseMode, basedOn, useRag }) => {
const slug = generateSlug(responseMode, basedOn, useRag);
const slug = generateAilaPromptVersionVariantSlug(
responseMode,
basedOn,
useRag,
);
return generatePromptParts(
{
responseMode: responseMode as "interactive" | "generate",
Expand Down

0 comments on commit 5a3c6b9

Please sign in to comment.