openlit 1.3.1 → 1.4.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/config.d.ts +14 -0
- package/dist/config.js +30 -0
- package/dist/config.js.map +1 -0
- package/dist/constant.d.ts +6 -0
- package/dist/constant.js +10 -0
- package/dist/constant.js.map +1 -0
- package/{src/features/base.ts → dist/features/base.d.ts} +2 -3
- package/dist/features/base.js +13 -0
- package/dist/features/base.js.map +1 -0
- package/dist/features/prompt-hub.d.ts +4 -0
- package/dist/features/prompt-hub.js +66 -0
- package/dist/features/prompt-hub.js.map +1 -0
- package/dist/features/vault.d.ts +4 -0
- package/dist/features/vault.js +63 -0
- package/dist/features/vault.js.map +1 -0
- package/dist/helpers.d.ts +13 -0
- package/dist/helpers.js +118 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -0
- package/dist/instrumentation/anthropic/index.d.ts +12 -0
- package/dist/instrumentation/anthropic/index.js +43 -0
- package/dist/instrumentation/anthropic/index.js.map +1 -0
- package/dist/instrumentation/anthropic/wrapper.d.ts +24 -0
- package/dist/instrumentation/anthropic/wrapper.js +186 -0
- package/dist/instrumentation/anthropic/wrapper.js.map +1 -0
- package/dist/instrumentation/base-wrapper.d.ts +12 -0
- package/dist/instrumentation/base-wrapper.js +31 -0
- package/dist/instrumentation/base-wrapper.js.map +1 -0
- package/dist/instrumentation/cohere/index.d.ts +12 -0
- package/dist/instrumentation/cohere/index.js +53 -0
- package/dist/instrumentation/cohere/index.js.map +1 -0
- package/dist/instrumentation/cohere/wrapper.d.ts +27 -0
- package/dist/instrumentation/cohere/wrapper.js +200 -0
- package/dist/instrumentation/cohere/wrapper.js.map +1 -0
- package/dist/instrumentation/index.d.ts +7 -0
- package/dist/instrumentation/index.js +58 -0
- package/dist/instrumentation/index.js.map +1 -0
- package/dist/instrumentation/ollama/index.d.ts +11 -0
- package/dist/instrumentation/ollama/index.js +43 -0
- package/dist/instrumentation/ollama/index.js.map +1 -0
- package/dist/instrumentation/ollama/wrapper.d.ts +23 -0
- package/dist/instrumentation/ollama/wrapper.js +210 -0
- package/dist/instrumentation/ollama/wrapper.js.map +1 -0
- package/dist/instrumentation/openai/index.d.ts +12 -0
- package/dist/instrumentation/openai/index.js +68 -0
- package/dist/instrumentation/openai/index.js.map +1 -0
- package/dist/instrumentation/openai/wrapper.d.ts +29 -0
- package/dist/instrumentation/openai/wrapper.js +436 -0
- package/dist/instrumentation/openai/wrapper.js.map +1 -0
- package/dist/prompt-hub.d.ts +4 -0
- package/dist/prompt-hub.js +66 -0
- package/dist/prompt-hub.js.map +1 -0
- package/dist/semantic-convention.d.ts +99 -0
- package/dist/semantic-convention.js +109 -0
- package/dist/semantic-convention.js.map +1 -0
- package/dist/tracing.d.ts +8 -0
- package/dist/tracing.js +44 -0
- package/dist/tracing.js.map +1 -0
- package/dist/types.d.ts +58 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +16 -2
- package/tsconfig.json +19 -12
- package/src/config.ts +0 -45
- package/src/constant.ts +0 -6
- package/src/features/prompt-hub.ts +0 -64
- package/src/features/vault.ts +0 -62
- package/src/helpers.ts +0 -122
- package/src/index.ts +0 -73
- package/src/instrumentation/anthropic/index.ts +0 -59
- package/src/instrumentation/anthropic/wrapper.ts +0 -255
- package/src/instrumentation/base-wrapper.ts +0 -39
- package/src/instrumentation/cohere/index.ts +0 -79
- package/src/instrumentation/cohere/wrapper.ts +0 -286
- package/src/instrumentation/index.ts +0 -68
- package/src/instrumentation/openai/index.ts +0 -114
- package/src/instrumentation/openai/wrapper.ts +0 -615
- package/src/prompt-hub.ts +0 -64
- package/src/semantic-convention.ts +0 -113
- package/src/tracing.ts +0 -59
- package/src/types.ts +0 -68
package/src/index.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { Resource } from '@opentelemetry/resources';
|
|
2
|
-
import {
|
|
3
|
-
SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,
|
|
4
|
-
SEMRESATTRS_SERVICE_NAME,
|
|
5
|
-
SEMRESATTRS_TELEMETRY_SDK_NAME,
|
|
6
|
-
} from '@opentelemetry/semantic-conventions';
|
|
7
|
-
import { NodeSDK } from '@opentelemetry/sdk-node';
|
|
8
|
-
import { OpenlitOptions } from './types';
|
|
9
|
-
import Tracing from './tracing';
|
|
10
|
-
import { DEFAULT_APPLICATION_NAME, DEFAULT_ENVIRONMENT, SDK_NAME } from './constant';
|
|
11
|
-
import { SpanExporter } from '@opentelemetry/sdk-trace-base';
|
|
12
|
-
import BaseOpenlit from './features/base';
|
|
13
|
-
|
|
14
|
-
export default class Openlit extends BaseOpenlit {
|
|
15
|
-
static resource: Resource;
|
|
16
|
-
static options: OpenlitOptions;
|
|
17
|
-
static _sdk: NodeSDK;
|
|
18
|
-
static init(options?: OpenlitOptions) {
|
|
19
|
-
try {
|
|
20
|
-
const { environment = DEFAULT_ENVIRONMENT, applicationName = DEFAULT_APPLICATION_NAME } =
|
|
21
|
-
options || {};
|
|
22
|
-
|
|
23
|
-
const otlpEndpoint =
|
|
24
|
-
options?.otlpEndpoint || process.env.OTEL_EXPORTER_OTLP_ENDPOINT || undefined;
|
|
25
|
-
let otlpHeaders = options?.otlpHeaders;
|
|
26
|
-
if (!otlpHeaders) {
|
|
27
|
-
if (process.env.OTEL_EXPORTER_OTLP_HEADERS) {
|
|
28
|
-
otlpHeaders = process.env.OTEL_EXPORTER_OTLP_HEADERS.split(',').reduce(
|
|
29
|
-
(acc: Record<string, any>, items: string) => {
|
|
30
|
-
const keyVal: string[] = items.split('=');
|
|
31
|
-
acc[keyVal[0]] = keyVal[1];
|
|
32
|
-
return acc;
|
|
33
|
-
},
|
|
34
|
-
{}
|
|
35
|
-
);
|
|
36
|
-
} else {
|
|
37
|
-
otlpHeaders = {};
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
this.options = options || {};
|
|
42
|
-
this.options.otlpEndpoint = otlpEndpoint;
|
|
43
|
-
this.options.otlpHeaders = otlpHeaders;
|
|
44
|
-
this.options.disableBatch =
|
|
45
|
-
options?.disableBatch === undefined ? true : !!options.disableBatch;
|
|
46
|
-
|
|
47
|
-
this.resource = new Resource({
|
|
48
|
-
[SEMRESATTRS_SERVICE_NAME]: applicationName,
|
|
49
|
-
[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT]: environment,
|
|
50
|
-
[SEMRESATTRS_TELEMETRY_SDK_NAME]: SDK_NAME,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
Tracing.setup({
|
|
54
|
-
...this.options,
|
|
55
|
-
environment,
|
|
56
|
-
applicationName,
|
|
57
|
-
otlpEndpoint,
|
|
58
|
-
otlpHeaders,
|
|
59
|
-
resource: this.resource,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
this._sdk = new NodeSDK({
|
|
63
|
-
resource: this.resource,
|
|
64
|
-
traceExporter: Tracing.traceExporter as SpanExporter,
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// This was causing the traceProvider initilization with multiple instances.
|
|
68
|
-
// this._sdk.start();
|
|
69
|
-
} catch (e) {
|
|
70
|
-
console.log('Connection time out', e);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
InstrumentationBase,
|
|
3
|
-
InstrumentationModuleDefinition,
|
|
4
|
-
InstrumentationNodeModuleDefinition,
|
|
5
|
-
isWrapped,
|
|
6
|
-
} from '@opentelemetry/instrumentation';
|
|
7
|
-
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
|
|
8
|
-
import { INSTRUMENTATION_PREFIX } from '../../constant';
|
|
9
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
10
|
-
import AnthropicWrapper from './wrapper';
|
|
11
|
-
|
|
12
|
-
export interface AnthropicInstrumentationConfig extends InstrumentationConfig {}
|
|
13
|
-
|
|
14
|
-
export default class OpenlitAnthropicInstrumentation extends InstrumentationBase {
|
|
15
|
-
constructor(config: AnthropicInstrumentationConfig = {}) {
|
|
16
|
-
super(`${INSTRUMENTATION_PREFIX}/instrumentation-anthropic`, '1.0.0', config);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
protected init(): void | InstrumentationModuleDefinition | InstrumentationModuleDefinition[] {
|
|
20
|
-
const module = new InstrumentationNodeModuleDefinition(
|
|
21
|
-
'@anthropic-ai/sdk',
|
|
22
|
-
['>= 0.21.0'],
|
|
23
|
-
(moduleExports) => {
|
|
24
|
-
this._patch(moduleExports);
|
|
25
|
-
return moduleExports;
|
|
26
|
-
},
|
|
27
|
-
(moduleExports) => {
|
|
28
|
-
if (moduleExports !== undefined) {
|
|
29
|
-
this._unpatch(moduleExports);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
);
|
|
33
|
-
return [module];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public manualPatch(anthropic: any): void {
|
|
37
|
-
this._patch(anthropic);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
protected _patch(moduleExports: typeof Anthropic) {
|
|
41
|
-
try {
|
|
42
|
-
if (isWrapped(moduleExports.Anthropic.Messages.prototype.create)) {
|
|
43
|
-
this._unwrap(moduleExports.Anthropic.Messages.prototype, 'create');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this._wrap(
|
|
47
|
-
moduleExports.Anthropic.Messages.prototype,
|
|
48
|
-
'create',
|
|
49
|
-
AnthropicWrapper._patchMessageCreate(this.tracer)
|
|
50
|
-
);
|
|
51
|
-
} catch (e) {
|
|
52
|
-
console.error('Error in _patch method:', e);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
protected _unpatch(moduleExports: typeof Anthropic) {
|
|
57
|
-
this._unwrap(moduleExports.Anthropic.Messages.prototype, 'create');
|
|
58
|
-
}
|
|
59
|
-
}
|
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
import { Span, SpanKind, Tracer, context, trace } from '@opentelemetry/api';
|
|
2
|
-
import OpenlitConfig from '../../config';
|
|
3
|
-
import OpenLitHelper from '../../helpers';
|
|
4
|
-
import SemanticConvention from '../../semantic-convention';
|
|
5
|
-
import BaseWrapper from '../base-wrapper';
|
|
6
|
-
|
|
7
|
-
export default class AnthropicWrapper extends BaseWrapper {
|
|
8
|
-
static aiSystem = SemanticConvention.GEN_AI_SYSTEM_ANTHROPIC;
|
|
9
|
-
|
|
10
|
-
static _patchMessageCreate(tracer: Tracer): any {
|
|
11
|
-
const genAIEndpoint = 'anthropic.resources.messages';
|
|
12
|
-
return (originalMethod: (...args: any[]) => any) => {
|
|
13
|
-
return async function (this: any, ...args: any[]) {
|
|
14
|
-
const span = tracer.startSpan(genAIEndpoint, { kind: SpanKind.CLIENT });
|
|
15
|
-
const { stream = false } = args[0];
|
|
16
|
-
return context
|
|
17
|
-
.with(trace.setSpan(context.active(), span), async () => {
|
|
18
|
-
return originalMethod.apply(this, args);
|
|
19
|
-
})
|
|
20
|
-
.then((response) => {
|
|
21
|
-
if (!!stream) {
|
|
22
|
-
return OpenLitHelper.createStreamProxy(
|
|
23
|
-
response,
|
|
24
|
-
AnthropicWrapper._messageCreateGenerator({
|
|
25
|
-
args,
|
|
26
|
-
genAIEndpoint,
|
|
27
|
-
response,
|
|
28
|
-
span,
|
|
29
|
-
})
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return AnthropicWrapper._messageCreate({ args, genAIEndpoint, response, span });
|
|
34
|
-
})
|
|
35
|
-
.catch((e: any) => {
|
|
36
|
-
OpenLitHelper.handleException(span, e);
|
|
37
|
-
span.end();
|
|
38
|
-
throw e;
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
static async _messageCreate({
|
|
45
|
-
args,
|
|
46
|
-
genAIEndpoint,
|
|
47
|
-
response,
|
|
48
|
-
span,
|
|
49
|
-
}: {
|
|
50
|
-
args: any[];
|
|
51
|
-
genAIEndpoint: string;
|
|
52
|
-
response: any;
|
|
53
|
-
span: Span;
|
|
54
|
-
}) {
|
|
55
|
-
try {
|
|
56
|
-
await AnthropicWrapper._messageCreateCommonSetter({
|
|
57
|
-
args,
|
|
58
|
-
genAIEndpoint,
|
|
59
|
-
result: response,
|
|
60
|
-
span,
|
|
61
|
-
});
|
|
62
|
-
return response;
|
|
63
|
-
} catch (e: any) {
|
|
64
|
-
OpenLitHelper.handleException(span, e);
|
|
65
|
-
} finally {
|
|
66
|
-
span.end();
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
static async *_messageCreateGenerator({
|
|
71
|
-
args,
|
|
72
|
-
genAIEndpoint,
|
|
73
|
-
response,
|
|
74
|
-
span,
|
|
75
|
-
}: {
|
|
76
|
-
args: any[];
|
|
77
|
-
genAIEndpoint: string;
|
|
78
|
-
response: any;
|
|
79
|
-
span: Span;
|
|
80
|
-
}) {
|
|
81
|
-
try {
|
|
82
|
-
const result = {
|
|
83
|
-
id: '0',
|
|
84
|
-
model: '',
|
|
85
|
-
stop_reason: '',
|
|
86
|
-
content: [
|
|
87
|
-
{
|
|
88
|
-
text: '',
|
|
89
|
-
role: '',
|
|
90
|
-
},
|
|
91
|
-
],
|
|
92
|
-
usage: {
|
|
93
|
-
input_tokens: 0,
|
|
94
|
-
output_tokens: 0,
|
|
95
|
-
total_tokens: 0,
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
for await (const chunk of response) {
|
|
99
|
-
switch (chunk.type) {
|
|
100
|
-
case 'content_block_delta':
|
|
101
|
-
result.content[0].text += chunk.delta?.text ?? '';
|
|
102
|
-
break;
|
|
103
|
-
case 'message_stop':
|
|
104
|
-
break;
|
|
105
|
-
|
|
106
|
-
case 'content_block_stop':
|
|
107
|
-
break;
|
|
108
|
-
|
|
109
|
-
case 'message_start':
|
|
110
|
-
if (chunk.message) {
|
|
111
|
-
result.id = chunk.message.id;
|
|
112
|
-
result.model = chunk.message.model;
|
|
113
|
-
result.content[0].role = chunk.message.role;
|
|
114
|
-
result.usage.input_tokens += Number(chunk.message.usage?.input_tokens) ?? 0;
|
|
115
|
-
result.usage.output_tokens += Number(chunk.message.usage?.output_tokens) ?? 0;
|
|
116
|
-
result.stop_reason = chunk.message?.stop_reason ?? '';
|
|
117
|
-
}
|
|
118
|
-
break;
|
|
119
|
-
|
|
120
|
-
case 'content_block_start':
|
|
121
|
-
result.content[0].text = chunk.content_block?.text ?? '';
|
|
122
|
-
break;
|
|
123
|
-
case 'message_delta':
|
|
124
|
-
result.stop_reason = chunk.delta?.stop_reason ?? '';
|
|
125
|
-
result.usage.output_tokens += Number(chunk.usage?.output_tokens) ?? 0;
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
yield chunk;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
result.usage.total_tokens = result.usage.output_tokens + result.usage.input_tokens;
|
|
133
|
-
|
|
134
|
-
await AnthropicWrapper._messageCreateCommonSetter({
|
|
135
|
-
args,
|
|
136
|
-
genAIEndpoint,
|
|
137
|
-
result,
|
|
138
|
-
span,
|
|
139
|
-
});
|
|
140
|
-
return response;
|
|
141
|
-
} catch (e: any) {
|
|
142
|
-
OpenLitHelper.handleException(span, e);
|
|
143
|
-
} finally {
|
|
144
|
-
span.end();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
static async _messageCreateCommonSetter({
|
|
149
|
-
args,
|
|
150
|
-
genAIEndpoint,
|
|
151
|
-
result,
|
|
152
|
-
span,
|
|
153
|
-
}: {
|
|
154
|
-
args: any[];
|
|
155
|
-
genAIEndpoint: string;
|
|
156
|
-
result: any;
|
|
157
|
-
span: Span;
|
|
158
|
-
}) {
|
|
159
|
-
const traceContent = OpenlitConfig.traceContent;
|
|
160
|
-
const {
|
|
161
|
-
messages,
|
|
162
|
-
max_tokens = null,
|
|
163
|
-
seed = null,
|
|
164
|
-
temperature = 1,
|
|
165
|
-
top_p,
|
|
166
|
-
top_k,
|
|
167
|
-
user,
|
|
168
|
-
stream = false,
|
|
169
|
-
stop_reason,
|
|
170
|
-
} = args[0];
|
|
171
|
-
|
|
172
|
-
// Format 'messages' into a single string
|
|
173
|
-
const messagePrompt = messages || '';
|
|
174
|
-
const formattedMessages = [];
|
|
175
|
-
|
|
176
|
-
for (const message of messagePrompt) {
|
|
177
|
-
const role = message.role;
|
|
178
|
-
const content = message.content;
|
|
179
|
-
|
|
180
|
-
if (Array.isArray(content)) {
|
|
181
|
-
const contentStr = content
|
|
182
|
-
.map((item) => {
|
|
183
|
-
if ('type' in item) {
|
|
184
|
-
return `${item.type}: ${item.text ? item.text : item.image_url}`;
|
|
185
|
-
} else {
|
|
186
|
-
return `text: ${item.text}`;
|
|
187
|
-
}
|
|
188
|
-
})
|
|
189
|
-
.join(', ');
|
|
190
|
-
formattedMessages.push(`${role}: ${contentStr}`);
|
|
191
|
-
} else {
|
|
192
|
-
formattedMessages.push(`${role}: ${content}`);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const prompt = formattedMessages.join('\n');
|
|
197
|
-
span.setAttribute(SemanticConvention.GEN_AI_TYPE, SemanticConvention.GEN_AI_TYPE_CHAT);
|
|
198
|
-
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_ID, result.id);
|
|
199
|
-
|
|
200
|
-
const model = result.model || 'claude-3-sonnet-20240229';
|
|
201
|
-
|
|
202
|
-
const pricingInfo = await OpenlitConfig.updatePricingJson(OpenlitConfig.pricing_json);
|
|
203
|
-
|
|
204
|
-
// Calculate cost of the operation
|
|
205
|
-
const cost = OpenLitHelper.getChatModelCost(
|
|
206
|
-
model,
|
|
207
|
-
pricingInfo,
|
|
208
|
-
result.usage.input_tokens,
|
|
209
|
-
result.usage.output_tokens
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
AnthropicWrapper.setBaseSpanAttributes(span, {
|
|
213
|
-
genAIEndpoint,
|
|
214
|
-
model,
|
|
215
|
-
user,
|
|
216
|
-
cost,
|
|
217
|
-
aiSystem: AnthropicWrapper.aiSystem,
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
// Request Params attributes : Start
|
|
221
|
-
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TOP_P, top_p);
|
|
222
|
-
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TOP_K, top_k);
|
|
223
|
-
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, max_tokens);
|
|
224
|
-
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, temperature);
|
|
225
|
-
|
|
226
|
-
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, stop_reason);
|
|
227
|
-
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, stream);
|
|
228
|
-
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_SEED, seed);
|
|
229
|
-
if (traceContent) {
|
|
230
|
-
span.setAttribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt);
|
|
231
|
-
}
|
|
232
|
-
// Request Params attributes : End
|
|
233
|
-
|
|
234
|
-
span.setAttribute(SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS, result.usage.input_tokens);
|
|
235
|
-
span.setAttribute(
|
|
236
|
-
SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS,
|
|
237
|
-
result.usage.output_tokens
|
|
238
|
-
);
|
|
239
|
-
span.setAttribute(
|
|
240
|
-
SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
|
|
241
|
-
result.usage.input_tokens + result.usage.output_tokens
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
if (result.stop_reason) {
|
|
245
|
-
span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_FINISH_REASON, result.stop_reason);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
if (traceContent) {
|
|
249
|
-
span.setAttribute(
|
|
250
|
-
SemanticConvention.GEN_AI_CONTENT_COMPLETION,
|
|
251
|
-
result.content?.[0]?.text || ''
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import OpenlitConfig from '../config';
|
|
2
|
-
import { SDK_NAME, TELEMETRY_SDK_NAME } from '../constant';
|
|
3
|
-
import SemanticConvention from '../semantic-convention';
|
|
4
|
-
import { Span, SpanStatusCode } from '@opentelemetry/api';
|
|
5
|
-
|
|
6
|
-
type BaseSpanAttributes = {
|
|
7
|
-
genAIEndpoint: string;
|
|
8
|
-
model: string;
|
|
9
|
-
user?: unknown;
|
|
10
|
-
cost?: number | string;
|
|
11
|
-
aiSystem: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export default class BaseWrapper {
|
|
15
|
-
static setBaseSpanAttributes(
|
|
16
|
-
span: Span,
|
|
17
|
-
{ genAIEndpoint, model, user, cost, aiSystem }: BaseSpanAttributes
|
|
18
|
-
) {
|
|
19
|
-
const applicationName = OpenlitConfig.applicationName!;
|
|
20
|
-
const environment = OpenlitConfig.environment!;
|
|
21
|
-
|
|
22
|
-
span.setAttributes({
|
|
23
|
-
[TELEMETRY_SDK_NAME]: SDK_NAME,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
span.setAttribute(TELEMETRY_SDK_NAME, SDK_NAME);
|
|
27
|
-
span.setAttribute(SemanticConvention.GEN_AI_SYSTEM, aiSystem);
|
|
28
|
-
span.setAttribute(SemanticConvention.GEN_AI_ENDPOINT, genAIEndpoint);
|
|
29
|
-
span.setAttribute(SemanticConvention.GEN_AI_ENVIRONMENT, environment);
|
|
30
|
-
span.setAttribute(SemanticConvention.GEN_AI_APPLICATION_NAME, applicationName);
|
|
31
|
-
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_MODEL, model);
|
|
32
|
-
span.setAttribute(SemanticConvention.GEN_AI_REQUEST_USER, user as any);
|
|
33
|
-
if (cost !== undefined) {
|
|
34
|
-
span.setAttribute(SemanticConvention.GEN_AI_USAGE_COST, cost);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
span.setStatus({ code: SpanStatusCode.OK });
|
|
38
|
-
}
|
|
39
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
InstrumentationBase,
|
|
3
|
-
InstrumentationModuleDefinition,
|
|
4
|
-
InstrumentationNodeModuleDefinition,
|
|
5
|
-
isWrapped,
|
|
6
|
-
} from '@opentelemetry/instrumentation';
|
|
7
|
-
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
|
|
8
|
-
import { INSTRUMENTATION_PREFIX } from '../../constant';
|
|
9
|
-
import Cohere from 'cohere-ai';
|
|
10
|
-
import CohereWrapper from './wrapper';
|
|
11
|
-
|
|
12
|
-
export interface CohereInstrumentationConfig extends InstrumentationConfig {}
|
|
13
|
-
|
|
14
|
-
export default class OpenlitCohereInstrumentation extends InstrumentationBase {
|
|
15
|
-
constructor(config: CohereInstrumentationConfig = {}) {
|
|
16
|
-
super(`${INSTRUMENTATION_PREFIX}/instrumentation-cohere-ai`, '1.0.0', config);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
protected init(): void | InstrumentationModuleDefinition | InstrumentationModuleDefinition[] {
|
|
20
|
-
const module = new InstrumentationNodeModuleDefinition(
|
|
21
|
-
'cohere-ai',
|
|
22
|
-
['>=7.2.0'],
|
|
23
|
-
(moduleExports) => {
|
|
24
|
-
this._patch(moduleExports);
|
|
25
|
-
return moduleExports;
|
|
26
|
-
},
|
|
27
|
-
(moduleExports) => {
|
|
28
|
-
if (moduleExports !== undefined) {
|
|
29
|
-
this._unpatch(moduleExports);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
);
|
|
33
|
-
return [module];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public manualPatch(cohere: any): void {
|
|
37
|
-
this._patch(cohere);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
protected _patch(moduleExports: typeof Cohere) {
|
|
41
|
-
try {
|
|
42
|
-
if (isWrapped(moduleExports.CohereClient.prototype.embed)) {
|
|
43
|
-
this._unwrap(moduleExports.CohereClient.prototype, 'embed');
|
|
44
|
-
}
|
|
45
|
-
if (isWrapped(moduleExports.CohereClient.prototype.chat)) {
|
|
46
|
-
this._unwrap(moduleExports.CohereClient.prototype, 'chat');
|
|
47
|
-
}
|
|
48
|
-
if (isWrapped(moduleExports.CohereClient.prototype.chatStream)) {
|
|
49
|
-
this._unwrap(moduleExports.CohereClient.prototype, 'chatStream');
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this._wrap(
|
|
53
|
-
moduleExports.CohereClient.prototype,
|
|
54
|
-
'embed',
|
|
55
|
-
CohereWrapper._patchEmbed(this.tracer)
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
this._wrap(
|
|
59
|
-
moduleExports.CohereClient.prototype,
|
|
60
|
-
'chat',
|
|
61
|
-
CohereWrapper._patchChat(this.tracer)
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
this._wrap(
|
|
65
|
-
moduleExports.CohereClient.prototype,
|
|
66
|
-
'chatStream',
|
|
67
|
-
CohereWrapper._patchChatStream(this.tracer)
|
|
68
|
-
);
|
|
69
|
-
} catch (e) {
|
|
70
|
-
console.error('Error in _patch method:', e);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
protected _unpatch(moduleExports: typeof Cohere) {
|
|
75
|
-
this._unwrap(moduleExports.CohereClient.prototype, 'embed');
|
|
76
|
-
this._unwrap(moduleExports.CohereClient.prototype, 'chat');
|
|
77
|
-
this._unwrap(moduleExports.CohereClient.prototype, 'chatStream');
|
|
78
|
-
}
|
|
79
|
-
}
|