-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
index.ts
52 lines (41 loc) 路 1.41 KB
/
index.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
import { Langfuse } from 'langfuse';
import { CreateLangfuseTraceBody } from 'langfuse-core';
import { getServerConfig } from '@/config/server';
import { CURRENT_VERSION } from '@/const/version';
import { TraceEventClient } from '@/libs/traces/event';
/**
* We use langfuse as the tracing system to trace the request and response
*/
export class TraceClient {
private _client?: Langfuse;
constructor() {
const { ENABLE_LANGFUSE, LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, LANGFUSE_HOST } =
getServerConfig();
if (!ENABLE_LANGFUSE) return;
// when enabled langfuse, make sure the key are ready in envs
if (!LANGFUSE_PUBLIC_KEY || !LANGFUSE_SECRET_KEY) {
console.log('-----');
console.error(
"You are enabling langfuse but don't set the `LANGFUSE_PUBLIC_KEY` or `LANGFUSE_SECRET_KEY`. Please check your env",
);
throw new TypeError('NO_LANGFUSE_KEY_ERROR');
}
this._client = new Langfuse({
baseUrl: LANGFUSE_HOST,
publicKey: LANGFUSE_PUBLIC_KEY,
release: CURRENT_VERSION,
secretKey: LANGFUSE_SECRET_KEY,
});
}
createEvent(traceId: string) {
const trace = this.createTrace({ id: traceId });
if (!trace) return;
return new TraceEventClient(trace);
}
createTrace(param: CreateLangfuseTraceBody) {
return this._client?.trace({ ...param });
}
async shutdownAsync() {
await this._client?.shutdownAsync();
}
}