mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-11 15:51:09 +00:00
Add concrete Langfuse tracer and prompt gateway
This commit is contained in:
40
src/core/langfuse/LangfuseTracer.ts
Normal file
40
src/core/langfuse/LangfuseTracer.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user