UNPKG

@lobehub/chat

Version:

Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.

53 lines (41 loc) 1.45 kB
import { Langfuse } from 'langfuse'; import { CreateLangfuseTraceBody } from 'langfuse-core'; import { getLangfuseConfig } from '@/config/langfuse'; 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 } = getLangfuseConfig(); 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(); } }