|
1 | 1 | import { NodeTracerProvider, Tracer } from "@opentelemetry/sdk-trace-node"; |
2 | 2 | import { HumanloopClient as BaseHumanloopClient } from "./Client"; |
3 | | -import { HumanloopSpanProcessor } from "otel/processor"; |
4 | | -import { HumanloopSpanExporter } from "otel/exporter"; |
5 | | -import { instrumentProvider } from "otel"; |
6 | | -import { UtilityPromptKernel, prompt as promptUtilityFactory } from "decorators/prompt"; |
7 | | -import { tool as toolUtilityFactory } from "decorators/tool"; |
8 | | -import { flow as flowUtilityFactory } from "decorators/flow"; |
9 | | -import { ToolKernelRequest } from "api/types/ToolKernelRequest"; |
10 | | -import { FlowKernelRequest } from "api/types/FlowKernelRequest"; |
| 3 | +import { HumanloopSpanProcessor } from "./otel/processor"; |
| 4 | +import { HumanloopSpanExporter } from "./otel/exporter"; |
| 5 | +import { UtilityPromptKernel, promptUtilityFactory } from "./utilities/prompt"; |
| 6 | +import { toolUtilityFactory } from "./utilities/tool"; |
| 7 | +import { flowUtilityFactory } from "./utilities/flow"; |
| 8 | +import { ToolKernelRequest } from "./api/types/ToolKernelRequest"; |
| 9 | +import { FlowKernelRequest } from "./api/types/FlowKernelRequest"; |
| 10 | +import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai"; |
| 11 | +import { CohereInstrumentation } from "@traceloop/instrumentation-cohere"; |
| 12 | +import { AnthropicInstrumentation } from "@traceloop/instrumentation-anthropic"; |
| 13 | +import { registerInstrumentations } from "@opentelemetry/instrumentation"; |
11 | 14 |
|
12 | 15 | export class HumanloopClient extends BaseHumanloopClient { |
13 | 16 | protected readonly opentelemetryTracerProvider: NodeTracerProvider; |
14 | 17 | protected readonly opentelemetryTracer: Tracer; |
15 | 18 |
|
16 | | - constructor( |
17 | | - _options: BaseHumanloopClient.Options, |
18 | | - opentelemetryTracerProvider: NodeTracerProvider, |
19 | | - opentelemetryTracer: Tracer |
20 | | - ) { |
| 19 | + constructor(_options: BaseHumanloopClient.Options) { |
21 | 20 | super(_options); |
22 | 21 |
|
23 | | - if (opentelemetryTracerProvider) { |
24 | | - this.opentelemetryTracerProvider = opentelemetryTracerProvider; |
25 | | - } else { |
26 | | - this.opentelemetryTracerProvider = new NodeTracerProvider({ |
27 | | - spanProcessors: [new HumanloopSpanProcessor(new HumanloopSpanExporter(this))], |
28 | | - }); |
29 | | - } |
| 22 | + this.opentelemetryTracerProvider = new NodeTracerProvider({ |
| 23 | + spanProcessors: [new HumanloopSpanProcessor(new HumanloopSpanExporter(this))], |
| 24 | + }); |
30 | 25 |
|
31 | | - instrumentProvider(this.opentelemetryTracerProvider); |
| 26 | + // if (moduleIsInstalled("openai")) { |
| 27 | + // const openai = require("openai"); |
| 28 | + // console.log("FOO", openai); |
| 29 | + // const instrumentor = new OpenAIInstrumentation({ enrichTokens: true }); |
| 30 | + // instrumentor.manuallyInstrument(openai); |
| 31 | + // instrumentor.setTracerProvider(this.opentelemetryTracerProvider); |
| 32 | + // instrumentor.enable(); |
| 33 | + // } |
| 34 | + |
| 35 | + // if (moduleIsInstalled("@anthropic-ai/sdk")) { |
| 36 | + // const anthropic = require("@anthropic-ai/sdk"); |
| 37 | + // const instrumentor = new AnthropicInstrumentation(); |
| 38 | + // instrumentor.manuallyInstrument(anthropic); |
| 39 | + // instrumentor.setTracerProvider(this.opentelemetryTracerProvider); |
| 40 | + // instrumentor.enable(); |
| 41 | + // } |
| 42 | + |
| 43 | + // if (moduleIsInstalled("cohere-ai")) { |
| 44 | + // const cohere = require("cohere-ai"); |
| 45 | + // const instrumentor = new CohereInstrumentation(); |
| 46 | + // instrumentor.manuallyInstrument(cohere); |
| 47 | + // instrumentor.setTracerProvider(this.opentelemetryTracerProvider); |
| 48 | + // instrumentor.enable(); |
| 49 | + // } |
32 | 50 |
|
33 | 51 | this.opentelemetryTracerProvider.register(); |
34 | 52 |
|
35 | | - if (this.opentelemetryTracerProvider !== undefined) { |
36 | | - this.opentelemetryTracer = this.opentelemetryTracerProvider.getTracer("humanloop.sdk"); |
37 | | - } else { |
38 | | - this.opentelemetryTracer = opentelemetryTracer; |
39 | | - } |
| 53 | + registerInstrumentations({ |
| 54 | + tracerProvider: this.opentelemetryTracerProvider, |
| 55 | + instrumentations: [ |
| 56 | + new OpenAIInstrumentation(), |
| 57 | + new AnthropicInstrumentation(), |
| 58 | + new CohereInstrumentation(), |
| 59 | + ], |
| 60 | + }); |
| 61 | + |
| 62 | + this.opentelemetryTracer = this.opentelemetryTracerProvider.getTracer("humanloop.sdk"); |
40 | 63 | } |
41 | 64 |
|
42 | | - public prompt<T extends (...args: any[]) => any>(func: T, promptKernel?: UtilityPromptKernel, path?: string) { |
43 | | - return promptUtilityFactory(this.opentelemetryTracer, func, promptKernel, path); |
| 65 | + public prompt<T extends (...args: any[]) => any>(promptUtilityArguments: { |
| 66 | + callable: T; |
| 67 | + promptKernel?: UtilityPromptKernel; |
| 68 | + path?: string; |
| 69 | + }) { |
| 70 | + return promptUtilityFactory( |
| 71 | + this.opentelemetryTracer, |
| 72 | + promptUtilityArguments.callable, |
| 73 | + promptUtilityArguments.promptKernel, |
| 74 | + promptUtilityArguments.path |
| 75 | + ); |
44 | 76 | } |
45 | 77 |
|
46 | | - public tool<T extends (...args: any[]) => any>(func: T, toolKernel: ToolKernelRequest, path?: string) { |
47 | | - return toolUtilityFactory(this.opentelemetryTracer, func, toolKernel, path); |
| 78 | + public tool<T extends (...args: any[]) => any>(toolUtilityArguments: { |
| 79 | + callable: T; |
| 80 | + toolKernel: ToolKernelRequest; |
| 81 | + path?: string; |
| 82 | + }) { |
| 83 | + return toolUtilityFactory( |
| 84 | + this.opentelemetryTracer, |
| 85 | + toolUtilityArguments.callable, |
| 86 | + toolUtilityArguments.toolKernel, |
| 87 | + toolUtilityArguments.path |
| 88 | + ); |
48 | 89 | } |
49 | 90 |
|
50 | | - public flow<T extends (...args: any[]) => any>(func: T, flowKernel: FlowKernelRequest, path?: string) { |
51 | | - return flowUtilityFactory(this.opentelemetryTracer, func, flowKernel, path); |
| 91 | + public flow<T extends (...args: any[]) => any>(flowUtilityArguments: { |
| 92 | + callable: T; |
| 93 | + flowKernel?: FlowKernelRequest; |
| 94 | + path?: string; |
| 95 | + }) { |
| 96 | + return flowUtilityFactory( |
| 97 | + this.opentelemetryTracer, |
| 98 | + flowUtilityArguments.callable, |
| 99 | + flowUtilityArguments.flowKernel, |
| 100 | + flowUtilityArguments.path |
| 101 | + ); |
52 | 102 | } |
53 | 103 | } |
0 commit comments