🪢 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: makes playground components reusable (#1501)
Hassieb Pakzade5dce9be58ac
diff --git a/web/src/features/playground/client/components/ChatMessageComponent.tsx b/web/src/features/playground/client/components/ChatMessageComponent.tsxindex cc45b7ab40..48909bc1a1 100644--- a/web/src/features/playground/client/components/ChatMessageComponent.tsx+++ b/web/src/features/playground/client/components/ChatMessageComponent.tsx@@ -5,20 +5,22 @@ 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 { usePlaygroundContext } from "../context";+import { MessagesContext } from "@/src/features/playground/client/components/Messages";++type ChatMessageProps = Pick<+ MessagesContext,+ "deleteMessage" | "updateMessage" | "updatePromptVariables"+> & { message: ChatMessageWithId };-type ChatMessageProps = {- message: ChatMessageWithId;-};export const ChatMessageComponent: React.FC<ChatMessageProps> = ({message,+ updateMessage,+ updatePromptVariables,+ deleteMessage,}) => {const textAreaRef = useRef<HTMLTextAreaElement | null>(null);const [textAreaRows, setTextAreaRows] = useState(1);- const { deleteMessage, updateMessage, updatePromptVariables } =- usePlaygroundContext();-const toggleRole = () => {if (message.role === ChatMessageRole.System) return;diff --git a/web/src/features/playground/client/components/ChatMessages.tsx b/web/src/features/playground/client/components/ChatMessages.tsxnew file mode 100644index 0000000000..b08e885461--- /dev/null+++ b/web/src/features/playground/client/components/ChatMessages.tsx@@ -0,0 +1,59 @@+import { PlusCircleIcon } from "lucide-react";++import { Button } from "@/src/components/ui/button";+import { ChatMessageComponent } from "@/src/features/playground/client/components/ChatMessageComponent";+import { MessagesContext } from "@/src/features/playground/client/components/Messages";+import { ChatMessageRole } from "@langfuse/shared";+import { useRef, useCallback, useEffect, useState } from "react";++type ChatMessagesProps = MessagesContext;+export const ChatMessages: React.FC<ChatMessagesProps> = (props) => {+ const { messages } = props;+ const scrollAreaRef = useRef<HTMLDivElement | null>(null);+ const prevMessageCount = useRef(0);++ // Scroll to bottom when new messages are added+ useEffect(() => {+ if (prevMessageCount.current < messages.length && scrollAreaRef.current) {+ const scrollElement = scrollAreaRef.current;+ scrollElement.scrollTop = scrollElement.scrollHeight;+ }+ prevMessageCount.current = messages.length;+ }, [scrollAreaRef, messages.length]);++ return (+ <div className="h-full overflow-auto scroll-smooth" ref={scrollAreaRef}>+ <div className="mb-4 flex-1 space-y-4">+ {props.messages.map((message) => {+ return (+ <ChatMessageComponent {...{ message, ...props }} key={message.id} />+ );+ })}+ <AddMessageButton {...props} />+ </div>+ </div>+ );+};++type AddMessageButtonProps = Pick<MessagesContext, "messages" | "addMessage">;+const AddMessageButton: React.FC<AddMessageButtonProps> = ({+ messages,+ addMessage,+}) => {+ const lastMessageRole = messages[messages.length - 1]?.role;+ const nextMessageRole =+ lastMessageRole === ChatMessageRole.User+ ? ChatMessageRole.Assistant+ : ChatMessageRole.User;++ return (+ <Button+ variant="outline"+ className="w-full space-x-2 py-6"+ onClick={() => addMessage(nextMessageRole)}+ >+ <PlusCircleIcon />+ <p>Add message</p>+ </Button>+ );+};diff --git a/web/src/features/playground/client/components/Messages.tsx b/web/src/features/playground/client/components/Messages.tsxindex f7c712b4e6..dcca0bf46b 100644--- a/web/src/features/playground/client/components/Messages.tsx+++ b/web/src/features/playground/client/components/Messages.tsx@@ -1,64 +1,33 @@-import { PlusCircleIcon } from "lucide-react";-import { useEffect, useRef } from "react";--import { ChatMessageRole } from "@langfuse/shared";import { Button } from "@/src/components/ui/button";+import { usePlaygroundContext } from "@/src/features/playground/client/context";+import { ChatMessageRole, ChatMessageWithId } from "@langfuse/shared";-import { usePlaygroundContext } from "../context";-import { ChatMessageComponent } from "./ChatMessageComponent";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;+ updatePromptVariables: () => void;+};-export const Messages = () => {- // Keep scrolling the view with the streamed generation- const scrollAreaRef = useRef<HTMLDivElement | null>(null);- const { messages } = usePlaygroundContext();- const latestMessage = messages[messages.length - 1];-- useEffect(() => {- if (scrollAreaRef.current && latestMessage?.content) {- const scrollElement = scrollAreaRef.current;- scrollElement.scrollTop = scrollElement.scrollHeight;- }- }, [latestMessage?.content]);-+export const Messages: React.FC<MessagesContext> = (props) => {return (<div className="flex h-full flex-col space-y-4 pr-4"><p className="font-semibold">Messages</p>- <div className="h-full overflow-auto scroll-smooth" ref={scrollAreaRef}>- <div className="mb-4 flex-1 space-y-4">- {messages.map((message) => {- return <ChatMessageComponent {...{ message }} key={message.id} />;- })}- <AddMessageButton />- </div>- <div></div>- </div>+ <ChatMessages {...props} /><GenerationOutput /><SubmitButton /></div>);};-const AddMessageButton = () => {- const { addMessage: createMessage, messages } = usePlaygroundContext();- const lastMessageRole = messages[messages.length - 1]?.role;- const nextMessageRole =- lastMessageRole === ChatMessageRole.User- ? ChatMessageRole.Assistant- : ChatMessageRole.User;-- return (- <Button- variant="outline"- className="w-full space-x-2 py-6"- onClick={() => createMessage(nextMessageRole)}- >- <PlusCircleIcon />- <p>Add message</p>- </Button>- );-};-const SubmitButton = () => {const { handleSubmit, isStreaming } = usePlaygroundContext();diff --git a/web/src/features/playground/client/components/ModelParameters.tsx b/web/src/features/playground/client/components/ModelParameters.tsxindex bf3e36544a..19034a9caf 100644--- a/web/src/features/playground/client/components/ModelParameters.tsx+++ b/web/src/features/playground/client/components/ModelParameters.tsx@@ -13,11 +13,18 @@ import {} from "@/src/components/ui/select";import { Slider } from "@/src/components/ui/slider";-import { usePlaygroundContext } from "../context";--export const ModelParameters = () => {- const { modelParams } = usePlaygroundContext();+export type ModelParamsContext = {+ modelParams: UIModelParams;+ updateModelParams: <Key extends keyof UIModelParams>(+ key: Key,+ value: UIModelParams[Key],+ ) => void;+};+export const ModelParameters: React.FC<ModelParamsContext> = ({+ modelParams,+ updateModelParams,+}) => {return (<div className="flex flex-col space-y-4"><p className="font-semibold">Model</p>@@ -27,12 +34,14 @@ export const ModelParameters = () => {modelParamsKey="provider"value={modelParams.provider}options={Object.values(ModelProvider)}+ updateModelParams={updateModelParams}/><ModelParamsSelecttitle="Model name"modelParamsKey="model"value={modelParams.model}options={Object.values(supportedModels[modelParams.provider])}+ updateModelParams={updateModelParams}/><ModelParamsSlidertitle="Temperature"@@ -42,6 +51,7 @@ export const ModelParameters = () => {max={modelParams.maxTemperature}step={0.01}tooltip="The sampling temperature. Higher values will make the output more random, while lower values like will make it more focused and deterministic."+ updateModelParams={updateModelParams}/><ModelParamsSlidertitle="Output token limit"@@ -51,6 +61,7 @@ export const ModelParameters = () => {max={4096}step={1}tooltip="The maximum number of tokens that can be generated in the chat completion."+ updateModelParams={updateModelParams}/><ModelParamsSlidertitle="Top P"@@ -60,6 +71,7 @@ export const ModelParameters = () => {max={1}step={0.01}tooltip="An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both."+ updateModelParams={updateModelParams}/></div></div>@@ -71,15 +83,15 @@ type ModelParamsSelectProps = {modelParamsKey: keyof UIModelParams;value: string;options: string[];+ updateModelParams: ModelParamsContext["updateModelParams"];};const ModelParamsSelect = ({title,modelParamsKey,value,options,+ updateModelParams,}: ModelParamsSelectProps) => {- const { updateModelParams } = usePlaygroundContext();-return (<div className="space-y-2"><p className="text-xs font-semibold">{title}</p>@@ -115,6 +127,7 @@ type ModelParamsSliderProps = {min: number;max: number;step: number;+ updateModelParams: ModelParamsContext["updateModelParams"];};const ModelParamsSlider = ({title,@@ -124,9 +137,8 @@ const ModelParamsSlider = ({min,max,step,+ updateModelParams,}: ModelParamsSliderProps) => {- const { updateModelParams } = usePlaygroundContext();-return (<div className="space-y-3" title={tooltip}><div className="flex flex-row">diff --git a/web/src/features/playground/client/context/index.tsx b/web/src/features/playground/client/context/index.tsxindex ba0ebe3411..1ddd9154f3 100644--- a/web/src/features/playground/client/context/index.tsx+++ b/web/src/features/playground/client/context/index.tsx@@ -1,6 +1,6 @@import React, {createContext,- type PropsWithChildren,+ PropsWithChildren,useCallback,useContext,useEffect,@@ -9,43 +9,30 @@ import React, {import { v4 as uuidv4 } from "uuid";+import { MessagesContext } from "@/src/features/playground/client/components/Messages";+import { ModelParamsContext } from "@/src/features/playground/client/components/ModelParameters";import useCommandEnter from "@/src/features/playground/client/hooks/useCommandEnter";+import { extractVariables } from "@/src/utils/string";import {- type ChatMessageWithId,ChatMessageRole,- type UIModelParams,- type PromptVariable,+ ChatMessageWithId,ModelProvider,+ PromptVariable,+ UIModelParams,} from "@langfuse/shared";-import { extractVariables } from "@/src/utils/string";type PlaygroundContextType = {promptVariables: PromptVariable[];- updatePromptVariables: () => void;updatePromptVariableValue: (variable: string, value: string) => void;deletePromptVariable: (variable: string) => void;- modelParams: UIModelParams;- updateModelParams: <Key extends keyof UIModelParams>(- key: Key,- value: UIModelParams[Key],- ) => void;-- 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;-output: string;outputJson: string;handleSubmit: () => Promise<void>;isStreaming: boolean;-};+} & ModelParamsContext &+ MessagesContext;const PlaygroundContext = createContext<PlaygroundContextType | undefined>(undefined,diff --git a/web/src/features/playground/client/index.tsx b/web/src/features/playground/client/index.tsxindex 95057060bd..741d3715b7 100644--- a/web/src/features/playground/client/index.tsx+++ b/web/src/features/playground/client/index.tsx@@ -1,26 +1,26 @@-import { PlaygroundProvider } from "./context";+import { usePlaygroundContext } from "./context";import { ModelParameters } from "./components/ModelParameters";import { Variables } from "./components/Variables";import { Messages } from "./components/Messages";export default function Playground() {+ const playgroundContext = usePlaygroundContext();+return (- <PlaygroundProvider>- <div className="flex h-full flex-row space-x-8">- <div className="h-full basis-3/4 overflow-auto">- <Messages />- </div>- <div className="h-full basis-1/4 pr-2">- <div className="flex h-full flex-col">- <div className="basis-[55%] ">- <ModelParameters />- </div>- <div className="basis-[45%] overflow-auto">- <Variables />- </div>+ <div className="flex h-full flex-row space-x-8">+ <div className="h-full basis-3/4 overflow-auto">+ <Messages {...playgroundContext} />+ </div>+ <div className="h-full basis-1/4 pr-2">+ <div className="flex h-full flex-col">+ <div className="basis-[55%] ">+ <ModelParameters {...playgroundContext} />+ </div>+ <div className="basis-[45%] overflow-auto">+ <Variables /></div></div></div>- </PlaygroundProvider>+ </div>);}diff --git a/web/src/pages/project/[projectId]/playground.tsx b/web/src/pages/project/[projectId]/playground.tsxindex fdf2d3ba1a..2d94473952 100644--- a/web/src/pages/project/[projectId]/playground.tsx+++ b/web/src/pages/project/[projectId]/playground.tsx@@ -1,5 +1,6 @@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 (@@ -11,7 +12,9 @@ export default function PlaygroundPage() {}}/><div className="flex-1 overflow-auto">- <Playground />+ <PlaygroundProvider>+ <Playground />+ </PlaygroundProvider></div></div>);