-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhumanloop.client.ts
More file actions
99 lines (87 loc) · 3.67 KB
/
humanloop.client.ts
File metadata and controls
99 lines (87 loc) · 3.67 KB
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
import { NodeTracerProvider, Tracer } from "@opentelemetry/sdk-trace-node";
import { AnthropicInstrumentation } from "@traceloop/instrumentation-anthropic";
import { CohereInstrumentation } from "@traceloop/instrumentation-cohere";
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
import { HumanloopClient as BaseHumanloopClient } from "./Client";
import { FlowKernelRequest } from "./api/types/FlowKernelRequest";
import { ToolKernelRequest } from "./api/types/ToolKernelRequest";
import { HumanloopSpanExporter } from "./otel/exporter";
import { moduleIsInstalled } from "./otel/helpers";
import { HumanloopSpanProcessor } from "./otel/processor";
import { flowUtilityFactory } from "./utilities/flow";
import { UtilityPromptKernel, promptUtilityFactory } from "./utilities/prompt";
import { toolUtilityFactory } from "./utilities/tool";
export class HumanloopClient extends BaseHumanloopClient {
protected readonly opentelemetryTracerProvider: NodeTracerProvider;
protected readonly opentelemetryTracer: Tracer;
constructor(_options: BaseHumanloopClient.Options) {
super(_options);
this.opentelemetryTracerProvider = new NodeTracerProvider({
spanProcessors: [
new HumanloopSpanProcessor(new HumanloopSpanExporter(this)),
],
});
if (moduleIsInstalled("openai")) {
const openai = require("openai").default;
const instrumentor = new OpenAIInstrumentation({
enrichTokens: true,
});
instrumentor.manuallyInstrument(openai);
instrumentor.setTracerProvider(this.opentelemetryTracerProvider);
instrumentor.enable();
}
if (moduleIsInstalled("@anthropic-ai/sdk")) {
const anthropic = require("@anthropic-ai/sdk");
const instrumentor = new AnthropicInstrumentation();
instrumentor.manuallyInstrument(anthropic);
instrumentor.setTracerProvider(this.opentelemetryTracerProvider);
instrumentor.enable();
}
if (moduleIsInstalled("cohere-ai")) {
const cohere = require("cohere-ai");
const instrumentor = new CohereInstrumentation();
instrumentor.manuallyInstrument(cohere);
instrumentor.setTracerProvider(this.opentelemetryTracerProvider);
instrumentor.enable();
}
this.opentelemetryTracerProvider.register();
this.opentelemetryTracer =
this.opentelemetryTracerProvider.getTracer("humanloop.sdk");
}
public prompt<T extends (...args: any[]) => any>(promptUtilityArguments: {
callable: T;
promptKernel?: UtilityPromptKernel;
path?: string;
}) {
return promptUtilityFactory(
this.opentelemetryTracer,
promptUtilityArguments.callable,
promptUtilityArguments.promptKernel,
promptUtilityArguments.path,
);
}
public tool<T extends (...args: any[]) => any>(toolUtilityArguments: {
callable: T;
toolKernel: ToolKernelRequest;
path?: string;
}) {
return toolUtilityFactory(
this.opentelemetryTracer,
toolUtilityArguments.callable,
toolUtilityArguments.toolKernel,
toolUtilityArguments.path,
);
}
public flow<T extends (...args: any[]) => any>(flowUtilityArguments: {
callable: T;
flowKernel?: FlowKernelRequest;
path?: string;
}) {
return flowUtilityFactory(
this.opentelemetryTracer,
flowUtilityArguments.callable,
flowUtilityArguments.flowKernel,
flowUtilityArguments.path,
);
}
}