🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(models): allow custom regex for user-defined models (#1022)
Richard Krümmel766102797b89
diff --git a/src/features/dashboard/components/MetricTable.tsx b/src/features/dashboard/components/MetricTable.tsxindex 9c9e49edf7..634a240502 100644--- a/src/features/dashboard/components/MetricTable.tsx+++ b/src/features/dashboard/components/MetricTable.tsx@@ -88,7 +88,7 @@ export const MetricTable = ({><DocPopupdescription="Calculated multiplying the number of tokens with cost per token for each model."- href="https://langfuse.com/docs/token-usage"+ href="https://langfuse.com/docs/model-usage-and-cost"/></TotalMetric></DashboardTable>diff --git a/src/features/models/components/NewModelForm.tsx b/src/features/models/components/NewModelForm.tsxindex d173cb0a51..e55465b003 100644--- a/src/features/models/components/NewModelForm.tsx+++ b/src/features/models/components/NewModelForm.tsx@@ -29,12 +29,7 @@ import JsonView from "react18-json-view";const formSchema = z.object({modelName: z.string().min(1),- exactMatchPattern: z- .string()- .regex(- /^[a-zA-Z0-9_.-]+$/,- "Match pattern must be alphanumeric and may contain _.-",- ), // risk of invalid regex injection that would break the db join,+ matchPattern: z.string(),startDate: z.date().optional(),inputPrice: z.string()@@ -81,7 +76,7 @@ export const NewModelForm = (props: {resolver: zodResolver(formSchema),defaultValues: {modelName: "",- exactMatchPattern: "",+ matchPattern: "",startDate: undefined,inputPrice: "",outputPrice: "",@@ -108,7 +103,7 @@ export const NewModelForm = (props: {.mutateAsync({projectId: props.projectId,modelName: values.modelName,- exactMatchPattern: values.exactMatchPattern,+ matchPattern: values.matchPattern,inputPrice: !!values.inputPrice? parseFloat(values.inputPrice): undefined,@@ -186,7 +181,7 @@ export const NewModelForm = (props: {<Header level="h3" title="Scope" /><FormFieldcontrol={form.control}- name="exactMatchPattern"+ name="matchPattern"render={({ field }) => (<FormItem><FormLabel>Match pattern</FormLabel>@@ -194,8 +189,10 @@ export const NewModelForm = (props: {<Input {...field} /></FormControl><FormDescription>- `model` on generations that should match this model,- case-insensitive.+ Regular expression (POSIX) to match ingested generations (model+ attribute) to this model definition. For an exact,+ case-insensitive match to a model name, use the expression:+ (~*)^modelname$</FormDescription><FormMessage /></FormItem>diff --git a/src/pages/project/[projectId]/models.tsx b/src/pages/project/[projectId]/models.tsxindex ed96d54a3b..e9f4d12294 100644--- a/src/pages/project/[projectId]/models.tsx+++ b/src/pages/project/[projectId]/models.tsx@@ -19,7 +19,7 @@ export default function ModelsPage() {help={{description:"A model represents a LLM model. It is used to calculate tokens and cost.",- href: "https://langfuse.com/docs/token-usage",+ href: "https://langfuse.com/docs/model-usage-and-cost",}}actionButtons={<Button disabled={!hasWriteAccess} asChild>diff --git a/src/pages/project/[projectId]/models/new.tsx b/src/pages/project/[projectId]/models/new.tsxindex 06004df57e..2efbef1f3b 100644--- a/src/pages/project/[projectId]/models/new.tsx+++ b/src/pages/project/[projectId]/models/new.tsx@@ -10,7 +10,7 @@ export default function ModelsPage() {return (<div className="mb-12 md:container"><Header- title="New Model"+ title="New Model Definition"breadcrumb={[{name: "Models",@@ -20,6 +20,11 @@ export default function ModelsPage() {name: "New",},]}+ help={{+ description:+ "Create a project-specific model definition. This will be used by Langfuse to infer model usage (eg tokens) and cost (USD).",+ href: "https://langfuse.com/docs/model-usage-and-cost",+ }}/><NewModelFormprojectId={projectId}diff --git a/src/server/api/routers/models.ts b/src/server/api/routers/models.tsindex 4ab9de3721..587a4bfc0d 100644--- a/src/server/api/routers/models.ts+++ b/src/server/api/routers/models.ts@@ -6,6 +6,8 @@ import {} from "@/src/server/api/trpc";import { paginationZod } from "@/src/utils/zod";import { throwIfNoAccess } from "@/src/features/rbac/utils/checkAccess";+import { Prisma } from "@prisma/client";+import { TRPCError } from "@trpc/server";const ModelAllOptions = z.object({projectId: z.string(),@@ -39,9 +41,6 @@ export const modelRouter = createTRPCRouter({OR: [{ projectId: input.projectId }, { projectId: null }],},});-- console.log("models", models);-return {models,totalCount: totalAmount,@@ -90,12 +89,7 @@ export const modelRouter = createTRPCRouter({.object({projectId: z.string(),modelName: z.string(),- exactMatchPattern: z- .string()- .regex(- /^[a-zA-Z0-9_.-]+$/,- "Match pattern must be alphanumeric and may contain _.-",- ), // risk of invalid regex injection that would break the db join+ matchPattern: z.string(),startDate: z.date().optional(),inputPrice: z.number().positive().optional(),outputPrice: z.number().positive().optional(),@@ -121,11 +115,25 @@ export const modelRouter = createTRPCRouter({projectId: input.projectId,scope: "models:CUD",});++ // Check if regex is valid POSIX regex+ // Use DB to check, because JS regex is not POSIX compliant+ try {+ await ctx.prisma.$queryRaw(+ Prisma.sql`SELECT 'test_string' ~ ${input.matchPattern}`,+ );+ } catch (error) {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "Invalid regex, needs to be POSIX compliant",+ });+ }+return ctx.prisma.model.create({data: {projectId: input.projectId,modelName: input.modelName,- matchPattern: `(?i)^(${input.exactMatchPattern})$`,+ matchPattern: input.matchPattern,startDate: input.startDate,inputPrice: input.inputPrice,outputPrice: input.outputPrice,