Skip to content

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

refactor: move llm playground logic to shared package (#1476)

Max Deichmanndaa6330098c1
diff --git a/packages/shared/package.json b/packages/shared/package.json
index a74ddf9081..5bdcf9da0f 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -8,6 +8,9 @@
"files": [
"dist/**"
],
+ "engines": {
+ "node": ">=20"
+ },
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
@@ -25,10 +28,14 @@
"seed": "ts-node -r tsconfig-paths/register -r dotenv/config --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"dependencies": {
+ "@langchain/anthropic": "^0.1.8",
+ "@langchain/core": "^0.1.49",
+ "@langchain/openai": "^0.0.23",
"@prisma/client": "^5.11.0",
"@types/bcryptjs": "^2.4.6",
"bcryptjs": "^2.4.3",
"kysely": "^0.27.3",
+ "langchain": "^0.1.28",
"prisma-extension-kysely": "^2.1.0",
"prisma-kysely": "^1.8.0",
"zod": "^3.22.4"
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts
index 58fa335213..05d6939e82 100644
--- a/packages/shared/src/index.ts
+++ b/packages/shared/src/index.ts
@@ -3,4 +3,7 @@ export { type DB } from "../prisma/generated/types";
export * from "./auth/auth";
+export * from "./server/llm/types";
+export * from "./server/llm/fetchLLMCompletion";
+
export { ModelUsageUnit };
diff --git a/web/src/features/playground/server/fetchLLMCompletion.ts b/packages/shared/src/server/llm/fetchLLMCompletion.ts
similarity index 95%
rename from web/src/features/playground/server/fetchLLMCompletion.ts
rename to packages/shared/src/server/llm/fetchLLMCompletion.ts
index ae266dee16..df120d5b62 100644
--- a/web/src/features/playground/server/fetchLLMCompletion.ts
+++ b/packages/shared/src/server/llm/fetchLLMCompletion.ts
@@ -3,7 +3,7 @@ import {
ChatMessageRole,
type ModelParams,
ModelProvider,
-} from "@/src/features/playground/types";
+} from "./types";
import { ChatAnthropic } from "@langchain/anthropic";
import {
AIMessage,
@@ -16,6 +16,7 @@ import {
} from "@langchain/core/output_parsers";
import { ChatOpenAI } from "@langchain/openai";
+console.log("fetchLLMCompletion.ts");
type LLMCompletionParams = {
messages: ChatMessage[];
modelParams: ModelParams;
@@ -28,17 +29,17 @@ type FetchLLMCompletionParams = LLMCompletionParams & {
export async function fetchLLMCompletion(
params: LLMCompletionParams & {
streaming: true;
- },
+ }
): Promise<ReadableStream<Uint8Array>>;
export async function fetchLLMCompletion(
params: LLMCompletionParams & {
streaming: false;
- },
+ }
): Promise<string>;
export async function fetchLLMCompletion(
- params: FetchLLMCompletionParams,
+ params: FetchLLMCompletionParams
): Promise<string | ReadableStream<Uint8Array>> {
const { messages, modelParams, streaming } = params;
const finalMessages = messages.map((message) => {
diff --git a/web/src/features/playground/types.ts b/packages/shared/src/server/llm/types.ts
similarity index 100%
rename from web/src/features/playground/types.ts
rename to packages/shared/src/server/llm/types.ts
diff --git a/packages/shared/tsconfig.json b/packages/shared/tsconfig.json
index 487bbca86d..d657ad7892 100644
--- a/packages/shared/tsconfig.json
+++ b/packages/shared/tsconfig.json
@@ -1,11 +1,12 @@
{
"extends": "@repo/typescript-config/base.json",
"compilerOptions": {
- "moduleResolution": "Node",
- "module": "commonjs",
- "lib": ["ES2015"],
+ "moduleResolution": "NodeNext",
+ "module": "NodeNext",
+ "lib": ["ES2020"],
"outDir": "./dist",
- "types": ["node"]
+ "types": ["node"],
+ "target": "ES2020"
},
"include": ["."],
"exclude": ["node_modules", "dist"]
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bef9951e9f..da035232a8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -46,6 +46,15 @@ importers:
packages/shared:
dependencies:
+ '@langchain/anthropic':
+ specifier: ^0.1.8
+ version: 0.1.8
+ '@langchain/core':
+ specifier: ^0.1.49
+ version: 0.1.49
+ '@langchain/openai':
+ specifier: ^0.0.23
+ version: 0.0.23
'@prisma/client':
specifier: ^5.11.0
version: 5.11.0([email protected])
@@ -58,6 +67,9 @@ importers:
kysely:
specifier: ^0.27.3
version: 0.27.3
+ langchain:
+ specifier: ^0.1.28
+ version: 0.1.28(@aws-sdk/[email protected])(@aws-sdk/[email protected])([email protected])
prisma-extension-kysely:
specifier: ^2.1.0
version: 2.1.0(@prisma/[email protected])
diff --git a/web/src/features/playground/client/components/ChatMessageComponent.tsx b/web/src/features/playground/client/components/ChatMessageComponent.tsx
index 306b7845b1..cc45b7ab40 100644
--- a/web/src/features/playground/client/components/ChatMessageComponent.tsx
+++ b/web/src/features/playground/client/components/ChatMessageComponent.tsx
@@ -1,10 +1,7 @@
import { capitalize } from "lodash";
import { MinusCircleIcon } from "lucide-react";
import { type ChangeEvent, useEffect, useState, useRef } from "react";
-import {
- ChatMessageRole,
- type ChatMessageWithId,
-} from "@/src/features/playground/types";
+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";
diff --git a/web/src/features/playground/client/components/GenerationOutput.tsx b/web/src/features/playground/client/components/GenerationOutput.tsx
index c622c5b190..98232843d8 100644
--- a/web/src/features/playground/client/components/GenerationOutput.tsx
+++ b/web/src/features/playground/client/components/GenerationOutput.tsx
@@ -2,7 +2,7 @@ import { useEffect, useRef, useState } from "react";
import { Button } from "@/src/components/ui/button";
import { usePlaygroundContext } from "../context";
import { CheckIcon, CopyIcon, PlusIcon } from "@radix-ui/react-icons";
-import { ChatMessageRole } from "@/src/features/playground/types";
+import { ChatMessageRole } from "@langfuse/shared";
import { BracesIcon } from "lucide-react";
export const GenerationOutput = () => {
diff --git a/web/src/features/playground/client/components/Messages.tsx b/web/src/features/playground/client/components/Messages.tsx
index d747b9aec6..f7c712b4e6 100644
--- a/web/src/features/playground/client/components/Messages.tsx
+++ b/web/src/features/playground/client/components/Messages.tsx
@@ -1,7 +1,7 @@
import { PlusCircleIcon } from "lucide-react";
import { useEffect, useRef } from "react";
-import { ChatMessageRole } from "@/src/features/playground/types";
+import { ChatMessageRole } from "@langfuse/shared";
import { Button } from "@/src/components/ui/button";
import { usePlaygroundContext } from "../context";
diff --git a/web/src/features/playground/client/components/ModelParameters.tsx b/web/src/features/playground/client/components/ModelParameters.tsx
index f1ed99e523..bf3e36544a 100644
--- a/web/src/features/playground/client/components/ModelParameters.tsx
+++ b/web/src/features/playground/client/components/ModelParameters.tsx
@@ -2,7 +2,7 @@ import {
supportedModels,
ModelProvider,
type UIModelParams,
-} from "@/src/features/playground/types";
+} from "@langfuse/shared";
import { Input } from "@/src/components/ui/input";
import {
Select,
diff --git a/web/src/features/playground/client/components/PromptVariableComponent.tsx b/web/src/features/playground/client/components/PromptVariableComponent.tsx
index ab806c3147..56baf865fe 100644
--- a/web/src/features/playground/client/components/PromptVariableComponent.tsx
+++ b/web/src/features/playground/client/components/PromptVariableComponent.tsx
@@ -3,7 +3,7 @@ import { usePlaygroundContext } from "../context";
import { Input } from "@/src/components/ui/input";
import { CheckCircle2, Circle, Trash2Icon } from "lucide-react";
import { Button } from "@/src/components/ui/button";
-import { type PromptVariable } from "@/src/features/playground/types";
+import { type PromptVariable } from "@langfuse/shared";
export const PromptVariableComponent: React.FC<{
promptVariable: PromptVariable;
diff --git a/web/src/features/playground/client/context/index.tsx b/web/src/features/playground/client/context/index.tsx
index 73131964b6..ba0ebe3411 100644
--- a/web/src/features/playground/client/context/index.tsx
+++ b/web/src/features/playground/client/context/index.tsx
@@ -16,7 +16,7 @@ import {
type UIModelParams,
type PromptVariable,
ModelProvider,
-} from "@/src/features/playground/types";
+} from "@langfuse/shared";
import { extractVariables } from "@/src/utils/string";
type PlaygroundContextType = {
diff --git a/web/src/features/playground/server/chatCompletionHandler.ts b/web/src/features/playground/server/chatCompletionHandler.ts
index 92a7e0c84d..d103b5e954 100644
--- a/web/src/features/playground/server/chatCompletionHandler.ts
+++ b/web/src/features/playground/server/chatCompletionHandler.ts
@@ -2,7 +2,7 @@ import { StreamingTextResponse } from "ai";
import { getToken } from "next-auth/jwt";
import { type NextRequest, NextResponse } from "next/server";
-import { fetchLLMCompletion } from "./fetchLLMCompletion";
+import { fetchLLMCompletion } from "@langfuse/shared/src/server/llm/fetchLLMCompletion";
import {
type ValidatedChatCompletionBody,
validateChatCompletionBody,
diff --git a/web/src/features/playground/server/validateChatCompletionBody.ts b/web/src/features/playground/server/validateChatCompletionBody.ts
index 81759d2e1b..a6ff68bf7c 100644
--- a/web/src/features/playground/server/validateChatCompletionBody.ts
+++ b/web/src/features/playground/server/validateChatCompletionBody.ts
@@ -4,7 +4,7 @@ import {
ModelProvider,
anthropicModels,
openAIModels,
-} from "@/src/features/playground/types";
+} from "@langfuse/shared";
const OpenAIModelSchema = z.enum([...openAIModels]);
const AnthropicModelSchema = z.enum([...anthropicModels]);
diff --git a/worker/package.json b/worker/package.json
index 24f7aa0891..1ffd34badf 100644
--- a/worker/package.json
+++ b/worker/package.json
@@ -5,6 +5,9 @@
"license": "MIT",
"main": "index.js",
"private": true,
+ "engines": {
+ "node": ">=18"
+ },
"scripts": {
"test": "vitest run",
"coverage": "vitest run --coverage",