Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat: add prompt config (#1147)

Richard Krümmel3b84af8cbfa5
diff --git a/fern/apis/server/definition/prompts.yml b/fern/apis/server/definition/prompts.yml
index 324e0452eb..2ebfeb4470 100644
--- a/fern/apis/server/definition/prompts.yml
+++ b/fern/apis/server/definition/prompts.yml
@@ -29,8 +29,11 @@ types:
name: string
isActive: boolean
prompt: string
+ config: optional<unknown>
+
Prompt:
properties:
name: string
version: integer
prompt: string
+ config: unknown
diff --git a/generated/openapi-server/openapi.yml b/generated/openapi-server/openapi.yml
index 0ecc519254..93221f66ad 100644
--- a/generated/openapi-server/openapi.yml
+++ b/generated/openapi-server/openapi.yml
@@ -2195,6 +2195,8 @@ components:
type: boolean
prompt:
type: string
+ config:
+ nullable: true
required:
- name
- isActive
@@ -2209,10 +2211,12 @@ components:
type: integer
prompt:
type: string
+ config: {}
required:
- name
- version
- prompt
+ - config
CreateScoreRequest:
title: CreateScoreRequest
type: object
diff --git a/generated/postman/collection.json b/generated/postman/collection.json
index ce87cf6561..fe3e2020da 100644
--- a/generated/postman/collection.json
+++ b/generated/postman/collection.json
@@ -587,7 +587,7 @@
"auth": null,
"body": {
"mode": "raw",
- "raw": "{\n \"name\": \"example\",\n \"isActive\": true,\n \"prompt\": \"example\"\n}",
+ "raw": "{\n \"name\": \"example\",\n \"isActive\": true,\n \"prompt\": \"example\",\n \"config\": \"UNKNOWN\"\n}",
"options": {
"raw": {
"language": "json"
diff --git a/prisma/migrations/20240219162415_add_prompt_config/migration.sql b/prisma/migrations/20240219162415_add_prompt_config/migration.sql
new file mode 100644
index 0000000000..c068cab7b9
--- /dev/null
+++ b/prisma/migrations/20240219162415_add_prompt_config/migration.sql
@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE "prompts" ADD COLUMN "config" JSONB NOT NULL DEFAULT '{}';
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 89b071f08f..5fabf4cb6b 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -421,6 +421,7 @@ model Prompt {
name String
version Int
isActive Boolean @map("is_active")
+ config Json @default("{}")
Observation Observation[]
@@unique([projectId, name, version])
diff --git a/prisma/seed.ts b/prisma/seed.ts
index 4a5e9ba45f..0a863505e5 100755
--- a/prisma/seed.ts
+++ b/prisma/seed.ts
@@ -176,7 +176,10 @@ async function main() {
projectId: project2.id,
createdBy: "user-1",
prompt: "Prompt 4 version 1 content with {{variable}}",
- name: "Prompt 4 with variable",
+ name: "Prompt 4 with variable and config",
+ config: {
+ temperature: 0.7,
+ },
version: 1,
isActive: false,
},
@@ -185,7 +188,11 @@ async function main() {
projectId: project2.id,
createdBy: "user-1",
prompt: "Prompt 4 version 2 content with {{variable}}",
- name: "Prompt 4 with variable",
+ name: "Prompt 4 with variable and config",
+ config: {
+ temperature: 0.7,
+ topP: 0.9,
+ },
version: 2,
isActive: true,
},
@@ -194,7 +201,12 @@ async function main() {
projectId: project2.id,
createdBy: "user-1",
prompt: "Prompt 4 version 3 content with {{variable}}",
- name: "Prompt 4 with variable",
+ name: "Prompt 4 with variable and config",
+ config: {
+ temperature: 0.7,
+ topP: 0.9,
+ frequencyPenalty: 0.5,
+ },
version: 3,
isActive: false,
},
@@ -208,13 +220,14 @@ async function main() {
createdBy: version.createdBy,
prompt: version.prompt,
name: version.name,
+ config: version.config,
version: version.version,
isActive: version.isActive,
},
});
promptIds.push(version.id);
}
- const promptName = "Prompt with Longer Name";
+ const promptName = "Prompt with many versions";
const projectId = project2.id;
const createdBy = "user-1";
diff --git a/src/__tests__/prompts.servertest.ts b/src/__tests__/prompts.servertest.ts
index b00f891542..6b159546b2 100644
--- a/src/__tests__/prompts.servertest.ts
+++ b/src/__tests__/prompts.servertest.ts
@@ -18,6 +18,9 @@ describe("/api/public/prompts API Endpoint", () => {
prompt: "prompt",
isActive: true,
version: 1,
+ config: {
+ temperature: 0.1,
+ },
project: {
connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" },
},
@@ -43,6 +46,7 @@ describe("/api/public/prompts API Endpoint", () => {
expect(fetchedObservations.body.version).toBe(1);
expect(fetchedObservations.body.isActive).toBe(true);
expect(fetchedObservations.body.createdBy).toBe("user-1");
+ expect(fetchedObservations.body.config).toEqual({ temperature: 0.1 });
});
it("should fetch active prompt only if no prompt version is given", async () => {
@@ -55,6 +59,9 @@ describe("/api/public/prompts API Endpoint", () => {
prompt: "prompt",
isActive: false,
version: 1,
+ config: {
+ temperature: 0.1,
+ },
project: {
connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" },
},
@@ -82,6 +89,9 @@ describe("/api/public/prompts API Endpoint", () => {
prompt: "prompt-one",
isActive: false,
version: 1,
+ config: {
+ temperature: 0.1,
+ },
project: {
connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" },
},
@@ -96,6 +106,9 @@ describe("/api/public/prompts API Endpoint", () => {
prompt: "prompt",
isActive: true,
version: 2,
+ config: {
+ temperature: 0.2,
+ },
project: {
connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" },
},
@@ -121,6 +134,7 @@ describe("/api/public/prompts API Endpoint", () => {
expect(fetchedObservations.body.version).toBe(1);
expect(fetchedObservations.body.isActive).toBe(false);
expect(fetchedObservations.body.createdBy).toBe("user-1");
+ expect(fetchedObservations.body.config).toEqual({ temperature: 0.1 });
});
it("should fetch active prompt when multiple exist", async () => {
@@ -134,6 +148,9 @@ describe("/api/public/prompts API Endpoint", () => {
prompt: "prompt",
isActive: false,
version: 1,
+ config: {
+ temperature: 0.1,
+ },
project: {
connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" },
},
@@ -148,6 +165,9 @@ describe("/api/public/prompts API Endpoint", () => {
prompt: "prompt",
isActive: true,
version: 2,
+ config: {
+ temperature: 0.2,
+ },
project: {
connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" },
},
@@ -173,6 +193,7 @@ describe("/api/public/prompts API Endpoint", () => {
expect(fetchedObservations.body.version).toBe(2);
expect(fetchedObservations.body.isActive).toBe(true);
expect(fetchedObservations.body.createdBy).toBe("user-1");
+ expect(fetchedObservations.body.config).toEqual({ temperature: 0.2 });
});
it("should create and fetch a prompt", async () => {
@@ -181,6 +202,9 @@ describe("/api/public/prompts API Endpoint", () => {
prompt: "prompt",
isActive: true,
projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ config: {
+ temperature: 0.1,
+ },
});
const fetchedObservations = await makeAPICall(
@@ -200,6 +224,7 @@ describe("/api/public/prompts API Endpoint", () => {
expect(fetchedObservations.body.version).toBe(1);
expect(fetchedObservations.body.isActive).toBe(true);
expect(fetchedObservations.body.createdBy).toBe("API");
+ expect(fetchedObservations.body.config).toEqual({ temperature: 0.1 });
});
it("should relate generation to prompt", async () => {
@@ -327,6 +352,34 @@ describe("/api/public/prompts API Endpoint", () => {
expect(dbGeneration).toBeNull();
});
+
+ it("should create empty object if no config is provided", async () => {
+ await makeAPICall("POST", "/api/public/prompts", {
+ name: "prompt-name",
+ prompt: "prompt",
+ isActive: true,
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ });
+
+ const fetchedObservations = await makeAPICall(
+ "GET",
+ "/api/public/prompts?name=prompt-name&version=1",
+ undefined,
+ );
+
+ expect(fetchedObservations.status).toBe(200);
+
+ if (!isPrompt(fetchedObservations.body)) {
+ throw new Error("Expected body to be an array of observations");
+ }
+
+ expect(fetchedObservations.body.name).toBe("prompt-name");
+ expect(fetchedObservations.body.prompt).toBe("prompt");
+ expect(fetchedObservations.body.version).toBe(1);
+ expect(fetchedObservations.body.isActive).toBe(true);
+ expect(fetchedObservations.body.createdBy).toBe("API");
+ expect(fetchedObservations.body.config).toEqual({});
+ });
});
const isPrompt = (x: unknown): x is Prompt => {
@@ -339,6 +392,7 @@ const isPrompt = (x: unknown): x is Prompt => {
typeof prompt.prompt === "string" &&
typeof prompt.isActive === "boolean" &&
typeof prompt.projectId === "string" &&
- typeof prompt.createdBy === "string"
+ typeof prompt.createdBy === "string" &&
+ typeof prompt.config === "object"
);
};
diff --git a/src/features/prompts/components/new-prompt-button.tsx b/src/features/prompts/components/new-prompt-button.tsx
index 84460143a5..b3ff9ffdfb 100644
--- a/src/features/prompts/components/new-prompt-button.tsx
+++ b/src/features/prompts/components/new-prompt-button.tsx
@@ -15,6 +15,7 @@ import {
FormControl,
FormMessage,
Form,
+ FormDescription,
} from "@/src/components/ui/form";
import { api } from "@/src/utils/api";
import { zodResolver } from "@hookform/resolvers/zod";
@@ -29,6 +30,8 @@ import { Badge } from "@/src/components/ui/badge";
import router from "next/router";
import { AutoComplete } from "@/src/features/prompts/components/auto-complete";
import { type AutoCompleteOption } from "@/src/features/prompts/components/auto-complete";
+import JsonView from "react18-json-view";
+import { jsonSchema } from "@/src/utils/zod";
export const CreatePromptDialog = (props: {
projectId: string;
@@ -36,6 +39,7 @@ export const CreatePromptDialog = (props: {
promptName?: string;
promptText?: string;
subtitle?: string;
+ promptConfig?: z.infer<typeof jsonSchema>;
children?: React.ReactNode;
}) => {
const [open, setOpen] = useState(false);
@@ -48,7 +52,7 @@ export const CreatePromptDialog = (props: {
return (
<Dialog open={hasAccess && open} onOpenChange={setOpen}>
<DialogTrigger asChild>{props.children}</DialogTrigger>
- <DialogContent className="sm:max-w-3xl">
+ <DialogContent className="max-h-screen overflow-auto sm:max-w-3xl">
<DialogHeader>
<DialogTitle className="mb-5">
{props.title}
@@ -61,6 +65,7 @@ export const CreatePromptDialog = (props: {
projectId={props.projectId}
promptName={props.promptName}
promptText={props.promptText}
+ promptConfig={props.promptConfig}
onFormSuccess={() => setOpen(false)}
/>
</DialogContent>
@@ -87,6 +92,20 @@ const formSchema = z.object({
isActive: z.boolean({
required_error: "Enter whether the prompt should go live",
}),
+ // string as we keep the state in string to avoid recursive zod parsing issues
+ config: z.string().refine(
+ (value) => {
+ try {
+ JSON.parse(value);
+ return true;
+ } catch (e) {
+ return false;
+ }
+ },
+ {
+ message: "Config needs to be valid JSON",
+ },
+ ),
});
export const NewPromptForm = (props: {
@@ -94,6 +113,7 @@ export const NewPromptForm = (props: {
onFormSuccess?: () => void;
promptName?: string;
promptText?: string;
+ promptConfig?: z.infer<typeof jsonSchema>;
}) => {
const [formError, setFormError] = useState<string | null>(null);
@@ -105,6 +125,7 @@ export const NewPromptForm = (props: {
isActive: false,
name: props.promptName ?? "",
prompt: props.promptText ?? "",
+ config: props.promptConfig ? JSON.stringify(props.promptConfig) : "{}",
},
});
@@ -148,6 +169,9 @@ export const NewPromptForm = (props: {
name: values.name,
prompt: values.prompt,
isActive: values.isActive,
+ // we keep the config in state as string. need to convert it to JSON before sending it to the API
+ // zod parsing necessary to align with TRPC schema
+ config: jsonSchema.parse(JSON.parse(values.config)),
})
.then((newPrompt) => {
props.onFormSuccess?.();
@@ -229,6 +253,30 @@ export const NewPromptForm = (props: {
</>
)}
/>
+ <FormField
+ control={form.control}
+ name="config"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Config</FormLabel>
+ <JsonView
+ // need to convert string in state to JSON for the JSONView component
+ src={jsonSchema.parse(JSON.parse(field.value))}
+ onEdit={(edit) => {
+ // need to put string back into the state
+ field.onChange(JSON.stringify(edit.src));
+ }}
+ editable
+ className="rounded-md border border-gray-200 p-2 text-sm"
+ />
+ <FormDescription>
+ Track configs for LLM API calls such as function definitions or
+ LLM parameters.
+ </FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
<FormField
control={form.control}
name="isActive"
diff --git a/src/features/prompts/components/prompt-detail.tsx b/src/features/prompts/components/prompt-detail.tsx
index 7f4924a82f..8f081a9d07 100644
--- a/src/features/prompts/components/prompt-detail.tsx
+++ b/src/features/prompts/components/prompt-detail.tsx
@@ -14,7 +14,9 @@ import { PromotePrompt } from "@/src/features/prompts/components/promote-prompt"
import { ScrollArea } from "@radix-ui/react-scroll-area";
import { useQueryParam, NumberParam } from "use-query-params";
import router from "next/router";
+import { JSONView } from "@/src/components/ui/code";
import { DeletePromptVersion } from "@/src/features/prompts/components/delete-prompt-version";
+import { jsonSchema } from "@/src/utils/zod";
export type PromptDetailProps = {
projectId: string;
@@ -73,6 +75,7 @@ export const PromptDetail = (props: PromptDetailProps) => {
subtitle="We do not update prompts, instead we create a new version of the prompt."
promptName={prompt.name}
promptText={prompt.prompt}
+ promptConfig={jsonSchema.parse(prompt.config)}
>
<Button variant="outline" size="icon">
<Pencil className="h-5 w-5" />
@@ -96,7 +99,7 @@ export const PromptDetail = (props: PromptDetailProps) => {
</div>
<div className="col-span-2 md:h-full">
<CodeView content={prompt.prompt} title="Prompt" />
- <div className="mx-auto mt-5 w-full rounded-lg border text-base leading-7 text-gray-700">
+ <div className="mx-auto mt-5 w-full rounded-lg border text-base leading-7">
<div className="border-b px-3 py-1 text-xs font-medium">
Variables
</div>
@@ -112,6 +115,10 @@ export const PromptDetail = (props: PromptDetailProps) => {
)}
</div>
</div>
+
+ {prompt.config && Object.keys(prompt.config).length > 0 && (
+ <JSONView className="mt-5" json={prompt.config} title="Config" />
+ )}
</div>
<div className="flex h-screen flex-col">
<div className="text-m px-3 font-medium">
diff --git a/src/features/prompts/server/prompt-router.ts b/src/features/prompts/server/prompt-router.ts
index d9a293d149..9c094bc2e3 100644
--- a/src/features/prompts/server/prompt-router.ts
+++ b/src/features/prompts/server/prompt-router.ts
@@ -6,6 +6,7 @@ import {
} from "@/src/server/api/trpc";
import { throwIfNoAccess } from "@/src/features/rbac/utils/checkAccess";
import { type Prompt, type PrismaClient } from "@prisma/client";
+import { jsonSchema } from "@/src/utils/zod";
import { auditLog } from "@/src/features/audit-logs/auditLog";
export const CreatePrompt = z.object({
@@ -13,6 +14,7 @@ export const CreatePrompt = z.object({
name: z.string(),
isActive: z.boolean(),
prompt: z.string(),
+ config: jsonSchema,
});
export const promptRouter = createTRPCRouter({
@@ -77,6 +79,7 @@ export const promptRouter = createTRPCRouter({
prompt: input.prompt,
isActive: input.isActive,
createdBy: ctx.session.user.id,
+ config: jsonSchema.parse(input.config),
prisma: ctx.prisma,
});
@@ -315,6 +318,7 @@ export const createPrompt = async ({
prompt,
isActive = true,
createdBy,
+ config,
prisma,
}: {
projectId: string;
@@ -322,6 +326,7 @@ export const createPrompt = async ({
prompt: string;
isActive?: boolean;
createdBy: string;
+ config: z.infer<typeof jsonSchema>;
prisma: PrismaClient;
}) => {
const latestPrompt = await prisma.prompt.findFirst({
@@ -350,6 +355,7 @@ export const createPrompt = async ({
isActive: isActive,
project: { connect: { id: projectId } },
createdBy: createdBy,
+ config: jsonSchema.parse(config),
},
}),
];
diff --git a/src/pages/api/public/prompts.ts b/src/pages/api/public/prompts.ts
index 35d764c927..ea0fe7b594 100644
--- a/src/pages/api/public/prompts.ts
+++ b/src/pages/api/public/prompts.ts
@@ -2,6 +2,7 @@ import { createPrompt } from "@/src/features/prompts/server/prompt-router";
import { verifyAuthHeaderAndReturnScope } from "@/src/features/public-api/server/apiAuth";
import { cors, runMiddleware } from "@/src/features/public-api/server/cors";
import { prisma } from "@/src/server/db";
+import { jsonSchema } from "@/src/utils/zod";
import { type NextApiRequest, type NextApiResponse } from "next";
import { z } from "zod";
@@ -14,6 +15,7 @@ const PromptCreateSchema = z.object({
name: z.string(),
prompt: z.string(),
isActive: z.boolean(),
+ config: jsonSchema.nullable().default({}),
});
export default async function handler(
@@ -56,7 +58,7 @@ export default async function handler(
name: searchParams.name,
version: searchParams.version ?? undefined,
// if no version is given, we take the latest active prompt
- // if no prompt is active, there will no prompt be available
+ // if no prompt is active, there will be no prompt available
isActive: !searchParams.version ? true : undefined,
},
});
@@ -111,8 +113,10 @@ export default async function handler(
prompt: input.prompt,
isActive: input.isActive,
createdBy: "API",
+ config: input.config ?? {},
prisma: prisma,
});
+ console.log("created prompt", prompt);
return res.status(200).json(prompt);
} catch (error: unknown) {
console.error(error);