Add concrete Langfuse tracer and prompt gateway

This commit is contained in:
2026-08-12 20:55:51 +02:00
parent 299375bcd3
commit cf3c12774f
4 changed files with 130 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
import { Langfuse } from 'langfuse';
import { Tracer } from '../llm/Tracer';
import type { GenerationHandle, GenerationInput, GenerationOutput, TraceHandle } from '../llm/Tracer';
export class LangfuseTracer extends Tracer {
private readonly client: Langfuse;
constructor(opts: { publicKey: string; secretKey: string; baseUrl: string }) {
super();
this.client = new Langfuse({
publicKey: opts.publicKey,
secretKey: opts.secretKey,
baseUrl: opts.baseUrl,
});
}
startTrace(name: string, metadata?: Record<string, unknown>): TraceHandle {
const trace = this.client.trace({ name, metadata });
return {
id: trace.id,
startGeneration(input: GenerationInput): GenerationHandle {
const generation = trace.generation({
name: input.name,
model: input.model,
input: input.input,
metadata: input.metadata,
});
return {
end(output: GenerationOutput) {
generation.end({ output: output.output, usage: output.usage, metadata: output.metadata });
},
};
},
};
}
async flush(): Promise<void> {
await this.client.flushAsync();
}
}