forked from i-am-alice/3rd-devs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLangfuseService.ts
141 lines (120 loc) · 4 KB
/
LangfuseService.ts
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
import { Langfuse, LangfuseTraceClient, LangfuseSpanClient, LangfuseGenerationClient } from 'langfuse';
import type { LangfusePromptClient, TextPromptClient, ChatPromptClient, CreateChatPromptBody } from "langfuse-core";
// Define a new type that's compatible with both Langfuse and OpenAI
type CompatibleChatMessage = Omit<{ role: string; content: string }, "externalId" | "traceIdType">;
export class LangfuseService {
public langfuse: Langfuse;
constructor() {
this.langfuse = new Langfuse({
secretKey: process.env.LANGFUSE_SECRET_KEY,
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
baseUrl: process.env.LANGFUSE_HOST
});
this.langfuse.on("error", (error: Error) => {
console.error("Langfuse error:", error);
});
if (process.env.NODE_ENV === 'development') {
this.langfuse.debug();
}
}
flushAsync(): Promise<void> {
return this.langfuse.flushAsync();
}
createTrace(options: { id: string, name: string, sessionId: string, userId: string }): LangfuseTraceClient {
return this.langfuse.trace(options);
}
createSpan(trace: LangfuseTraceClient, name: string, input?: any): LangfuseSpanClient {
return trace.span({ name, input: input ? input : undefined });
}
finalizeSpan(span: LangfuseSpanClient, name: string, input: any, output: any): void {
span.update({
name,
output,
});
span.end();
}
async finalizeTrace(trace: LangfuseTraceClient, input: any, output: any): Promise<void> {
await trace.update({
input,
output,
});
await this.langfuse.flushAsync();
}
async shutdownAsync(): Promise<void> {
await this.langfuse.shutdownAsync();
}
createGeneration(trace: LangfuseTraceClient, name: string, input: any, prompt?: LangfusePromptClient, config?: any): LangfuseGenerationClient {
return trace.generation({
name,
input,
prompt,
...config
});
}
createEvent(trace: LangfuseTraceClient, name: string, input?: any, output?: any): void {
trace.event({
name,
input: input ? JSON.stringify(input) : undefined,
output: output ? JSON.stringify(output) : undefined,
});
}
finalizeGeneration(generation: LangfuseGenerationClient, output: any, model: string, usage?: { promptTokens?: number, completionTokens?: number, totalTokens?: number }): void {
generation.update({
output,
model,
usage,
});
generation.end();
}
async createPrompt(body: CreateChatPromptBody): Promise<ChatPromptClient> {
return this.langfuse.createPrompt(body);
}
async getPrompt(
name: string,
version?: number,
options?: {
label?: string;
cacheTtlSeconds?: number;
fallback?: string;
maxRetries?: number;
type?: "text";
fetchTimeoutMs?: number;
}
): Promise<TextPromptClient>;
async getPrompt(
name: string,
version?: number,
options?: {
label?: string;
cacheTtlSeconds?: number;
fallback?: Omit<{ role: string; content: string }, "externalId" | "traceIdType">[];
maxRetries?: number;
type: "chat";
fetchTimeoutMs?: number;
}
): Promise<ChatPromptClient>;
async getPrompt(
name: string,
version?: number,
options?: any
): Promise<TextPromptClient | ChatPromptClient> {
return this.langfuse.getPrompt(name, version, options);
}
compilePrompt(prompt: TextPromptClient | ChatPromptClient, variables: Record<string, any>): string | CompatibleChatMessage[] {
const compiled = prompt.compile(variables);
if (typeof compiled === 'string') {
return compiled;
} else if (Array.isArray(compiled)) {
return compiled.map(message => ({
role: message.role,
content: message.content
}));
} else {
throw new Error('Unexpected prompt compilation result');
}
}
async preFetchPrompts(promptNames: string[]): Promise<void> {
await Promise.all(promptNames.map(name => this.getPrompt(name)));
}
// Remove the finalizeEvent method as it's not needed for LangfuseEventClient
}