🪢 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
refactor: moves playground to enterprise edition (#1797)
Hassieb Pakzadaeec1ebe79da
diff --git a/.env.dev.example b/.env.dev.exampleindex 5fda02c2fe..7cdfca9aea 100644--- a/.env.dev.example+++ b/.env.dev.example@@ -14,6 +14,9 @@ DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres"NEXTAUTH_URL="http://localhost:3000"NEXTAUTH_SECRET="secret"+# Langfuse Cloud Environment+NEXT_PUBLIC_LANGFUSE_CLOUD_REGION="DEV"+# Langfuse experimental featuresLANGFUSE_ENABLE_EXPERIMENTAL_FEATURES="true"diff --git a/web/src/features/playground/client/components/ChatMessageComponent.tsx b/web/src/components/ChatMessages/ChatMessageComponent.tsxsimilarity index 96%rename from web/src/features/playground/client/components/ChatMessageComponent.tsxrename to web/src/components/ChatMessages/ChatMessageComponent.tsxindex a8b27248c7..9bb965792b 100644--- a/web/src/features/playground/client/components/ChatMessageComponent.tsx+++ b/web/src/components/ChatMessages/ChatMessageComponent.tsx@@ -5,7 +5,7 @@ import { ChatMessageRole, type ChatMessageWithId } from "@langfuse/shared";import { Button } from "@/src/components/ui/button";import { Card, CardContent } from "@/src/components/ui/card";import { Textarea } from "@/src/components/ui/textarea";-import type { MessagesContext } from "@/src/features/playground/client/components/Messages";+import type { MessagesContext } from "./types";type ChatMessageProps = Pick<MessagesContext,diff --git a/web/src/features/playground/client/components/ChatMessages.tsx b/web/src/components/ChatMessages/index.tsxsimilarity index 88%rename from web/src/features/playground/client/components/ChatMessages.tsxrename to web/src/components/ChatMessages/index.tsxindex 53be4ea87c..0f6da53155 100644--- a/web/src/features/playground/client/components/ChatMessages.tsx+++ b/web/src/components/ChatMessages/index.tsx@@ -1,10 +1,12 @@import { PlusCircleIcon } from "lucide-react";+import { useEffect, useRef } from "react";import { Button } from "@/src/components/ui/button";-import { ChatMessageComponent } from "@/src/features/playground/client/components/ChatMessageComponent";-import type { MessagesContext } from "@/src/features/playground/client/components/Messages";import { ChatMessageRole } from "@langfuse/shared";-import { useRef, useEffect } from "react";++import { ChatMessageComponent } from "./ChatMessageComponent";++import type { MessagesContext } from "./types";type ChatMessagesProps = MessagesContext;export const ChatMessages: React.FC<ChatMessagesProps> = (props) => {diff --git a/web/src/components/ChatMessages/types.ts b/web/src/components/ChatMessages/types.tsnew file mode 100644index 0000000000..a011c32d50--- /dev/null+++ b/web/src/components/ChatMessages/types.ts@@ -0,0 +1,13 @@+import type { ChatMessageRole, ChatMessageWithId } from "@langfuse/shared";+++export type MessagesContext = {+ messages: ChatMessageWithId[];+ addMessage: (role: ChatMessageRole, content?: string) => ChatMessageWithId;+ deleteMessage: (id: string) => void;+ updateMessage: <Key extends keyof ChatMessageWithId>(+ id: string,+ key: Key,+ value: ChatMessageWithId[Key]+ ) => void;+};diff --git a/web/src/features/playground/client/utils/createEmptyMessage.ts b/web/src/components/ChatMessages/utils/createEmptyMessage.tssimilarity index 100%rename from web/src/features/playground/client/utils/createEmptyMessage.tsrename to web/src/components/ChatMessages/utils/createEmptyMessage.tsdiff --git a/web/src/features/playground/client/components/ModelParameters.tsx b/web/src/components/ModelParameters.tsxsimilarity index 98%rename from web/src/features/playground/client/components/ModelParameters.tsxrename to web/src/components/ModelParameters.tsxindex 246bc32556..2275c92505 100644--- a/web/src/features/playground/client/components/ModelParameters.tsx+++ b/web/src/components/ModelParameters.tsx@@ -20,7 +20,6 @@ export type ModelParamsContext = {key: Key,value: UIModelParams[Key],) => void;- updateModelParams: <UIModelParams>(params: UIModelParams) => void;disabled?: boolean;};diff --git a/web/src/features/playground/client/components/GenerationOutput.tsx b/web/src/ee/features/playground/page/components/GenerationOutput.tsxsimilarity index 100%rename from web/src/features/playground/client/components/GenerationOutput.tsxrename to web/src/ee/features/playground/page/components/GenerationOutput.tsxdiff --git a/web/src/features/playground/client/components/Messages.tsx b/web/src/ee/features/playground/page/components/Messages.tsxsimilarity index 57%rename from web/src/features/playground/client/components/Messages.tsxrename to web/src/ee/features/playground/page/components/Messages.tsxindex d42205990a..1138472a2f 100644--- a/web/src/features/playground/client/components/Messages.tsx+++ b/web/src/ee/features/playground/page/components/Messages.tsx@@ -1,20 +1,9 @@import { Button } from "@/src/components/ui/button";-import { usePlaygroundContext } from "@/src/features/playground/client/context";-import type { ChatMessageRole, ChatMessageWithId } from "@langfuse/shared";+import { usePlaygroundContext } from "@/src/ee/features/playground/page/context";import { GenerationOutput } from "./GenerationOutput";-import { ChatMessages } from "@/src/features/playground/client/components/ChatMessages";--export type MessagesContext = {- messages: ChatMessageWithId[];- addMessage: (role: ChatMessageRole, content?: string) => ChatMessageWithId;- deleteMessage: (id: string) => void;- updateMessage: <Key extends keyof ChatMessageWithId>(- id: string,- key: Key,- value: ChatMessageWithId[Key],- ) => void;-};+import { ChatMessages } from "@/src/components/ChatMessages";+import { type MessagesContext } from "@/src/components/ChatMessages/types";export const Messages: React.FC<MessagesContext> = (props) => {return (diff --git a/web/src/features/playground/client/components/PromptVariableComponent.tsx b/web/src/ee/features/playground/page/components/PromptVariableComponent.tsxsimilarity index 100%rename from web/src/features/playground/client/components/PromptVariableComponent.tsxrename to web/src/ee/features/playground/page/components/PromptVariableComponent.tsxdiff --git a/web/src/features/playground/client/components/Variables.tsx b/web/src/ee/features/playground/page/components/Variables.tsxsimilarity index 100%rename from web/src/features/playground/client/components/Variables.tsxrename to web/src/ee/features/playground/page/components/Variables.tsxdiff --git a/web/src/features/playground/client/context/index.tsx b/web/src/ee/features/playground/page/context/index.tsxsimilarity index 91%rename from web/src/features/playground/client/context/index.tsxrename to web/src/ee/features/playground/page/context/index.tsxindex c61562a12d..df0e6a5e04 100644--- a/web/src/features/playground/client/context/index.tsx+++ b/web/src/ee/features/playground/page/context/index.tsx@@ -7,9 +7,15 @@ import React, {useState,} from "react";-import type { MessagesContext } from "@/src/features/playground/client/components/Messages";-import type { ModelParamsContext } from "@/src/features/playground/client/components/ModelParameters";-import useCommandEnter from "@/src/features/playground/client/hooks/useCommandEnter";+import { StringParam, useQueryParam } from "use-query-params";+import { v4 as uuidv4 } from "uuid";++import { createEmptyMessage } from "@/src/components/ChatMessages/utils/createEmptyMessage";+import useCommandEnter from "@/src/ee/features/playground/page/hooks/useCommandEnter";+import { ChatMessageListSchema } from "@/src/features/prompts/components/NewPromptForm/validation";+import { PromptType } from "@/src/features/prompts/server/validation";+import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";+import { api } from "@/src/utils/api";import { extractVariables } from "@/src/utils/string";import {ChatMessageRole,@@ -18,13 +24,9 @@ import {type PromptVariable,type UIModelParams,} from "@langfuse/shared";-import { createEmptyMessage } from "../utils/createEmptyMessage";-import { StringParam, useQueryParam } from "use-query-params";-import { api } from "@/src/utils/api";-import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";-import { PromptType } from "@/src/features/prompts/server/validation";-import { ChatMessageListSchema } from "@/src/features/prompts/components/NewPromptForm/validation";-import { v4 as uuidv4 } from "uuid";++import type { MessagesContext } from "@/src/components/ChatMessages/types";+import type { ModelParamsContext } from "@/src/components/ModelParameters";type PlaygroundContextType = {promptVariables: PromptVariable[];@@ -54,13 +56,8 @@ export const usePlaygroundContext = () => {return context;};-export type PlaygroundProviderProps = PropsWithChildren & {- avilableModels?: UIModelParams[];-};--export const PlaygroundProvider: React.FC<PlaygroundProviderProps> = ({+export const PlaygroundProvider: React.FC<PropsWithChildren> = ({children,- avilableModels,}) => {const projectId = useProjectIdFromURL();const [initialPromptId] = useQueryParam("promptId", StringParam);@@ -73,9 +70,7 @@ export const PlaygroundProvider: React.FC<PlaygroundProviderProps> = ({createEmptyMessage(ChatMessageRole.User),]);const [modelParams, setModelParams] = useState<UIModelParams>(- avilableModels && avilableModels.length > 0- ? avilableModels[0]- : getDefaultModelParams(ModelProvider.OpenAI),+ getDefaultModelParams(ModelProvider.OpenAI),);const { data: initialPrompt, isInitialLoading } = api.prompts.byId.useQuery(@@ -212,12 +207,6 @@ export const PlaygroundProvider: React.FC<PlaygroundProviderProps> = ({setModelParams((prev) => ({ ...prev, [key]: value }));};- const updateModelParams: PlaygroundContextType["updateModelParams"] = (- params,- ) => {- setModelParams((prev) => ({ ...prev, ...params }));- };-const updatePromptVariableValue = (variable: string, value: string) => {setPromptVariables((prev) =>prev.map((v) => (v.name === variable ? { ...v, value } : v)),@@ -241,8 +230,7 @@ export const PlaygroundProvider: React.FC<PlaygroundProviderProps> = ({deleteMessage,modelParams,- updateModelParam: updateModelParam,- updateModelParams: updateModelParams,+ updateModelParam,output,outputJson,diff --git a/web/src/features/playground/client/hooks/useCommandEnter.ts b/web/src/ee/features/playground/page/hooks/useCommandEnter.tssimilarity index 100%rename from web/src/features/playground/client/hooks/useCommandEnter.tsrename to web/src/ee/features/playground/page/hooks/useCommandEnter.tsdiff --git a/web/src/ee/features/playground/page/index.tsx b/web/src/ee/features/playground/page/index.tsxnew file mode 100644index 0000000000..8c1ae927cb--- /dev/null+++ b/web/src/ee/features/playground/page/index.tsx@@ -0,0 +1,23 @@+import Header from "@/src/components/layouts/header";+import Playground from "@/src/ee/features/playground/page/playground";+import { PlaygroundProvider } from "@/src/ee/features/playground/page/context";+import { getIsCloudEnvironment } from "@/src/ee/utils/getIsCloudEnvironment";++export default function PlaygroundPage() {+ return getIsCloudEnvironment() ? (+ <div className="flex h-[95vh] flex-col">+ <Header+ title="Playground"+ help={{+ description: "A sandbox to test and iterate your prompts",+ }}+ featureBetaURL="https://github.com/orgs/langfuse/discussions/1170"+ />+ <div className="flex-1 overflow-auto">+ <PlaygroundProvider>+ <Playground />+ </PlaygroundProvider>+ </div>+ </div>+ ) : null;+}diff --git a/web/src/features/playground/client/index.tsx b/web/src/ee/features/playground/page/playground.tsxsimilarity index 92%rename from web/src/features/playground/client/index.tsxrename to web/src/ee/features/playground/page/playground.tsxindex 9c8d4a8eda..fcce646570 100644--- a/web/src/features/playground/client/index.tsx+++ b/web/src/ee/features/playground/page/playground.tsx@@ -1,5 +1,5 @@+import { ModelParameters } from "@/src/components/ModelParameters";import { usePlaygroundContext } from "./context";-import { ModelParameters } from "./components/ModelParameters";import { Variables } from "./components/Variables";import { Messages } from "./components/Messages";diff --git a/web/src/features/playground/server/analytics/posthogCallback.ts b/web/src/ee/features/playground/server/analytics/posthogCallback.tssimilarity index 93%rename from web/src/features/playground/server/analytics/posthogCallback.tsrename to web/src/ee/features/playground/server/analytics/posthogCallback.tsindex b8d4acb3f4..be970bcd43 100644--- a/web/src/features/playground/server/analytics/posthogCallback.ts+++ b/web/src/ee/features/playground/server/analytics/posthogCallback.ts@@ -1,4 +1,4 @@-import type { ValidatedChatCompletionBody } from "@/src/features/playground/server/validateChatCompletionBody";+import type { ValidatedChatCompletionBody } from "@/src/ee/features/playground/server/validateChatCompletionBody";import { ServerPosthog } from "@/src/server/services/posthog";import type { LLMResult } from "@langchain/core/outputs";import { BaseCallbackHandler } from "@langchain/core/callbacks/base";diff --git a/web/src/features/playground/server/chatCompletionHandler.ts b/web/src/ee/features/playground/server/chatCompletionHandler.tssimilarity index 87%rename from web/src/features/playground/server/chatCompletionHandler.tsrename to web/src/ee/features/playground/server/chatCompletionHandler.tsindex 5c8850f5c9..242ed08d76 100644--- a/web/src/features/playground/server/chatCompletionHandler.ts+++ b/web/src/ee/features/playground/server/chatCompletionHandler.ts@@ -11,8 +11,16 @@ import {} from "./validateChatCompletionBody";import { getCookieName } from "@/src/server/utils/cookies";import { env } from "@/src/env.mjs";+import { getIsCloudEnvironment } from "@/src/ee/utils/getIsCloudEnvironment";export default async function chatCompletionHandler(req: NextRequest) {+ if (!getIsCloudEnvironment()) {+ return NextResponse.json(+ { message: "This endpoint is available in Langfuse cloud only." },+ { status: 501 },+ );+ }+const token = await getToken({req,cookieName: getCookieName("next-auth.session-token"),diff --git a/web/src/features/playground/server/validateChatCompletionBody.ts b/web/src/ee/features/playground/server/validateChatCompletionBody.tssimilarity index 100%rename from web/src/features/playground/server/validateChatCompletionBody.tsrename to web/src/ee/features/playground/server/validateChatCompletionBody.tsdiff --git a/web/src/ee/utils/getIsCloudEnvironment.ts b/web/src/ee/utils/getIsCloudEnvironment.tsnew file mode 100644index 0000000000..720623c263--- /dev/null+++ b/web/src/ee/utils/getIsCloudEnvironment.ts@@ -0,0 +1,4 @@+import { env } from "@/src/env.mjs";++export const getIsCloudEnvironment = () =>+ Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION)diff --git a/web/src/features/evals/components/eval-template-detail.tsx b/web/src/features/evals/components/eval-template-detail.tsxindex 1f486a31a2..6f06d59438 100644--- a/web/src/features/evals/components/eval-template-detail.tsx+++ b/web/src/features/evals/components/eval-template-detail.tsx@@ -1,9 +1,8 @@import * as React from "react";import Header from "@/src/components/layouts/header";import { EvalTemplateForm } from "@/src/features/evals/components/template-form";-import { PlaygroundProvider } from "@/src/features/playground/client/context";import { api } from "@/src/utils/api";-import { type EvalTemplate, evalLLMModels } from "@langfuse/shared";+import { type EvalTemplate } from "@langfuse/shared";import { useRouter } from "next/router";import {Select,@@ -76,14 +75,12 @@ export const EvalTemplateDetail = () => {{allTemplates.isLoading || !allTemplates.data ? (<div>Loading...</div>) : (- <PlaygroundProvider avilableModels={[...evalLLMModels]}>- <EvalTemplateForm- projectId={projectId}- existingEvalTemplate={template.data ?? undefined}- isEditing={isEditing}- setIsEditing={setIsEditing}- />- </PlaygroundProvider>+ <EvalTemplateForm+ projectId={projectId}+ existingEvalTemplate={template.data ?? undefined}+ isEditing={isEditing}+ setIsEditing={setIsEditing}+ />)}</div>);diff --git a/web/src/features/evals/components/template-form.tsx b/web/src/features/evals/components/template-form.tsxindex ffb335d075..3fec0d58cf 100644--- a/web/src/features/evals/components/template-form.tsx+++ b/web/src/features/evals/components/template-form.tsx@@ -19,9 +19,16 @@ import { zodResolver } from "@hookform/resolvers/zod";import { extractVariables, getIsCharOrUnderscore } from "@/src/utils/string";import router from "next/router";import { type EvalTemplate } from "@prisma/client";-import { usePlaygroundContext } from "@/src/features/playground/client/context";-import { ModelParameters } from "@/src/features/playground/client/components/ModelParameters";-import { EvalModelNames, OutputSchema, evalLLMModels } from "@langfuse/shared";+import {+ ModelParameters,+ type ModelParamsContext,+} from "@/src/components/ModelParameters";+import {+ EvalModelNames,+ OutputSchema,+ evalLLMModels,+ type UIModelParams,+} from "@langfuse/shared";import { PromptDescription } from "@/src/features/prompts/components/prompt-description";const formSchema = z.object({@@ -56,10 +63,18 @@ export const EvalTemplateForm = (props: {isEditing?: boolean;setIsEditing?: (isEditing: boolean) => void;}) => {+ const posthog = usePostHog();const [formError, setFormError] = useState<string | null>(null);- const playgroundContext = usePlaygroundContext();+ const [modelParams, setModelParams] = useState<UIModelParams>(+ evalLLMModels[0],+ );- const posthog = usePostHog();+ const updateModelParam: ModelParamsContext["updateModelParam"] = (+ key,+ value,+ ) => {+ setModelParams((prev) => ({ ...prev, [key]: value }));+ };const form = useForm<z.infer<typeof formSchema>>({resolver: zodResolver(formSchema),@@ -98,16 +113,17 @@ export const EvalTemplateForm = (props: {});// also set the context for the playground- playgroundContext.updateModelParam("model", model);- playgroundContext.updateModelParams(- props.existingEvalTemplate.modelParams,- );+ updateModelParam("model", model);+ setModelParams((prev) => ({+ ...prev,+ ...(props.existingEvalTemplate?.modelParams as UIModelParams),+ }));const modelProvider = evalLLMModels.find((m) => m.model === model,)?.provider;if (modelProvider) {- playgroundContext.updateModelParam("provider", modelProvider);+ updateModelParam("provider", modelProvider);}}}, [props.existingEvalTemplate, form]);@@ -131,8 +147,8 @@ export const EvalTemplateForm = (props: {name: values.name,projectId: props.projectId,prompt: values.prompt,- model: EvalModelNames.parse(playgroundContext.modelParams.model),- modelParameters: playgroundContext.modelParams,+ model: EvalModelNames.parse(modelParams.model),+ modelParameters: modelParams,variables: extractedVariables ?? [],outputSchema: {score: values.outputScore,@@ -257,7 +273,7 @@ export const EvalTemplateForm = (props: {</div><div className="col-span-1 row-span-3"><ModelParameters- {...playgroundContext}+ {...{ modelParams, updateModelParam }}availableModels={[...evalLLMModels]}disabled={!props.isEditing}/>diff --git a/web/src/features/playground/Readme.md b/web/src/features/playground/Readme.mddeleted file mode 100644index 4c950a25c0..0000000000--- a/web/src/features/playground/Readme.md+++ /dev/null@@ -1,4 +0,0 @@-NOTE: We may transition this feature from our MIT licensed repository to the-a commercial License (ee folder) once we release a first stable version.-Please consider this when planning long-term use and integration of this functionality into your projects.-For more information see https://langfuse.com/docs/open-sourcediff --git a/web/src/features/prompts/components/NewPromptForm/PromptChatMessages.tsx b/web/src/features/prompts/components/NewPromptForm/PromptChatMessages.tsxindex 0020d59950..b9a77aea3b 100644--- a/web/src/features/prompts/components/NewPromptForm/PromptChatMessages.tsx+++ b/web/src/features/prompts/components/NewPromptForm/PromptChatMessages.tsx@@ -1,9 +1,9 @@import { useEffect, useState } from "react";import type { ControllerRenderProps } from "react-hook-form";-import { ChatMessages } from "@/src/features/playground/client/components/ChatMessages";+import { ChatMessages } from "@/src/components/ChatMessages";import { ChatMessageRole } from "@langfuse/shared";-import { createEmptyMessage } from "@/src/features/playground/client/utils/createEmptyMessage";-import type { MessagesContext } from "@/src/features/playground/client/components/Messages";+import { createEmptyMessage } from "@/src/components/ChatMessages/utils/createEmptyMessage";+import type { MessagesContext } from "@/src/components/ChatMessages/types";import {ChatMessageListSchema,type NewPromptFormSchemaType,diff --git a/web/src/pages/api/chatCompletion.ts b/web/src/pages/api/chatCompletion.tsindex 7561203b2e..600152cb84 100644--- a/web/src/pages/api/chatCompletion.ts+++ b/web/src/pages/api/chatCompletion.ts@@ -1,4 +1,4 @@-import chatCompletionHandler from "@/src/features/playground/server/chatCompletionHandler";+import chatCompletionHandler from "@/src/ee/features/playground/server/chatCompletionHandler";export const runtime = "edge";diff --git a/web/src/pages/project/[projectId]/evals/templates/new.tsx b/web/src/pages/project/[projectId]/evals/templates/new.tsxindex 7c2d5159db..a6b0c5b036 100644--- a/web/src/pages/project/[projectId]/evals/templates/new.tsx+++ b/web/src/pages/project/[projectId]/evals/templates/new.tsx@@ -1,7 +1,5 @@import Header from "@/src/components/layouts/header";import { EvalTemplateForm } from "@/src/features/evals/components/template-form";-import { PlaygroundProvider } from "@/src/features/playground/client/context";-import { evalLLMModels } from "@langfuse/shared";import { useRouter } from "next/router";@@ -12,9 +10,7 @@ export default function NewTemplatesPage() {return (<div className="md:container"><Header title="Create eval template" />- <PlaygroundProvider avilableModels={[...evalLLMModels]}>- <EvalTemplateForm projectId={projectId} isEditing={true} />- </PlaygroundProvider>+ <EvalTemplateForm projectId={projectId} isEditing={true} /></div>);}diff --git a/web/src/pages/project/[projectId]/playground.tsx b/web/src/pages/project/[projectId]/playground.tsxindex fccf85a4eb..b379fadf89 100644--- a/web/src/pages/project/[projectId]/playground.tsx+++ b/web/src/pages/project/[projectId]/playground.tsx@@ -1,27 +1 @@-// NOTE: We may transition this feature from our MIT licensed repository to the-// a commercial License (ee folder) once we release a first stable version.-// Please consider this when planning long-term use and integration of this functionality into your projects.-// For more information see https://langfuse.com/docs/open-source--import Header from "@/src/components/layouts/header";-import Playground from "@/src/features/playground/client";-import { PlaygroundProvider } from "@/src/features/playground/client/context";--export default function PlaygroundPage() {- return (- <div className="flex h-[95vh] flex-col">- <Header- title="Playground"- help={{- description: "A sandbox to test and iterate your prompts",- }}- featureBetaURL="https://github.com/orgs/langfuse/discussions/1170"- />- <div className="flex-1 overflow-auto">- <PlaygroundProvider>- <Playground />- </PlaygroundProvider>- </div>- </div>- );-}+export { default as default } from "@/src/ee/features/playground/page";