-
Notifications
You must be signed in to change notification settings - Fork 694
/
JumpToPlaygroundButton.tsx
206 lines (177 loc) · 5.86 KB
/
JumpToPlaygroundButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { Terminal } from "lucide-react";
import Link from "next/link";
import { useEffect, useState } from "react";
import { z } from "zod";
import { createEmptyMessage } from "@/src/components/ChatMessages/utils/createEmptyMessage";
import { Button } from "@/src/components/ui/button";
import usePlaygroundCache from "@/src/ee/features/playground/page/hooks/usePlaygroundCache";
import { type PlaygroundCache } from "@/src/ee/features/playground/page/types";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { PromptType } from "@/src/features/prompts/server/utils/validation";
import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";
import {
ChatMessageRole,
type Observation,
type Prompt,
supportedModels as playgroundSupportedModels,
type UIModelParams,
ZodModelConfig,
} from "@langfuse/shared";
import { useHasEntitlement } from "@/src/features/entitlements/hooks";
type JumpToPlaygroundButtonProps = (
| {
source: "prompt";
prompt: Prompt;
analyticsEventName: "prompt_detail:test_in_playground_button_click";
}
| {
source: "generation";
generation: Observation;
analyticsEventName: "trace_detail:test_in_playground_button_click";
}
) & {
variant?: "outline" | "secondary";
};
export const JumpToPlaygroundButton: React.FC<JumpToPlaygroundButtonProps> = (
props,
) => {
const capture = usePostHogClientCapture();
const projectId = useProjectIdFromURL();
const { setPlaygroundCache } = usePlaygroundCache();
const [capturedState, setCapturedState] = useState<PlaygroundCache>(null);
const available = useHasEntitlement("playground");
useEffect(() => {
if (props.source === "prompt") {
setCapturedState(parsePrompt(props.prompt));
} else if (props.source === "generation") {
setCapturedState(parseGeneration(props.generation));
}
}, [props]);
const handleClick = () => {
capture(props.analyticsEventName);
setPlaygroundCache(capturedState);
};
if (!available) return null;
return (
<Button
variant={props.variant ?? "secondary"}
size={props.source === "prompt" ? "icon" : "default"}
title="Test in LLM playground"
onClick={handleClick}
asChild
>
<Link href={`/project/${projectId}/playground`}>
<Terminal className="h-4 w-4" />
{props.source === "generation" && (
<span className="ml-2">Test in playground</span>
)}
</Link>
</Button>
);
};
const ParsedChatMessageListSchema = z.array(
z.object({
role: z.nativeEnum(ChatMessageRole),
content: z.union([
z.string(),
// If system message is cached, the message is an array of objects with a text property
z
.array(
z
.object({
text: z.string(),
})
.transform((v) => v.text),
)
.transform((v) => v.join("")),
z.any().transform((v) => JSON.stringify(v, null, 2)),
]),
}),
);
const parsePrompt = (prompt: Prompt): PlaygroundCache => {
if (prompt.type === PromptType.Chat) {
const parsedMessages = ParsedChatMessageListSchema.safeParse(prompt.prompt);
return parsedMessages.success ? { messages: parsedMessages.data } : null;
} else {
const promptString = prompt.prompt?.valueOf();
return {
messages: [
createEmptyMessage(
ChatMessageRole.System,
typeof promptString === "string" ? promptString : "",
),
],
};
}
};
const parseGeneration = (generation: Observation): PlaygroundCache => {
if (generation.type !== "GENERATION") return null;
const modelParams = parseModelParams(generation);
let input = generation.input?.valueOf();
if (typeof input === "string") {
try {
input = JSON.parse(input);
if (typeof input === "string") {
return {
messages: [createEmptyMessage(ChatMessageRole.System, input)],
modelParams,
};
}
} catch (err) {
return {
messages: [
createEmptyMessage(ChatMessageRole.System, input?.toString()),
],
modelParams,
};
}
}
if (typeof input === "object") {
const parsedMessages = ParsedChatMessageListSchema.safeParse(input);
if (parsedMessages.success)
return { messages: parsedMessages.data, modelParams };
}
if (typeof input === "object" && "messages" in input) {
const parsedMessages = ParsedChatMessageListSchema.safeParse(
input["messages"],
);
if (parsedMessages.success)
return { messages: parsedMessages.data, modelParams };
}
return null;
};
function parseModelParams(
generation: Observation,
):
| (Partial<UIModelParams> & Pick<UIModelParams, "provider" | "model">)
| undefined {
const generationModel = generation.model?.valueOf();
let modelParams:
| (Partial<UIModelParams> & Pick<UIModelParams, "provider" | "model">)
| undefined = undefined;
if (generationModel) {
const provider = Object.entries(playgroundSupportedModels).find(
([_, models]) =>
generationModel ? models.some((m) => m === generationModel) : false,
)?.[0];
if (!provider) return;
modelParams = {
provider: { value: provider, enabled: true },
model: { value: generationModel, enabled: true },
} as Partial<UIModelParams> & Pick<UIModelParams, "provider" | "model">;
const generationModelParams = generation.modelParameters?.valueOf();
if (generationModelParams && typeof generationModelParams === "object") {
const parsedParams = ZodModelConfig.safeParse(generationModelParams);
if (parsedParams.success) {
Object.entries(parsedParams.data).forEach(([key, value]) => {
if (!modelParams) return;
modelParams[key as keyof typeof parsedParams.data] = {
value,
enabled: true,
};
});
}
}
}
return modelParams;
}