@posthog/ai 6.2.0 → 6.3.1
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/dist/anthropic/index.cjs +17 -12
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +1 -0
- package/dist/anthropic/index.mjs +17 -12
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +11 -7
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +11 -7
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +219 -85
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +28 -5
- package/dist/index.mjs +219 -85
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +7 -15
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.d.ts +5 -5
- package/dist/langchain/index.mjs +7 -15
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +93 -25
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +13 -1
- package/dist/openai/index.mjs +93 -26
- package/dist/openai/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +49 -14
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +49 -14
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -26,7 +26,7 @@ function _interopNamespaceDefault(e) {
|
|
|
26
26
|
|
|
27
27
|
var uuid__namespace = /*#__PURE__*/_interopNamespaceDefault(uuid);
|
|
28
28
|
|
|
29
|
-
var version = "6.
|
|
29
|
+
var version = "6.3.1";
|
|
30
30
|
|
|
31
31
|
// limit large outputs by truncating to 200kb (approx 200k bytes)
|
|
32
32
|
const MAX_OUTPUT_SIZE = 200000;
|
|
@@ -226,18 +226,43 @@ const mergeSystemPrompt = (params, provider) => {
|
|
|
226
226
|
const withPrivacyMode = (client, privacyMode, input) => {
|
|
227
227
|
return client.privacy_mode || privacyMode ? null : input;
|
|
228
228
|
};
|
|
229
|
-
|
|
229
|
+
function toSafeString(input) {
|
|
230
|
+
if (input === undefined || input === null) {
|
|
231
|
+
return '';
|
|
232
|
+
}
|
|
233
|
+
if (typeof input === 'string') {
|
|
234
|
+
return input;
|
|
235
|
+
}
|
|
230
236
|
try {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
237
|
+
return JSON.stringify(input);
|
|
238
|
+
} catch {
|
|
239
|
+
console.warn('Failed to stringify input', input);
|
|
240
|
+
return '';
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const truncate = input => {
|
|
244
|
+
const str = toSafeString(input);
|
|
245
|
+
if (str === '') {
|
|
246
|
+
return '';
|
|
247
|
+
}
|
|
248
|
+
// Check if we need to truncate and ensure STRING_FORMAT is respected
|
|
249
|
+
const encoder = new TextEncoder();
|
|
250
|
+
const buffer = encoder.encode(str);
|
|
251
|
+
if (buffer.length <= MAX_OUTPUT_SIZE) {
|
|
252
|
+
// Ensure STRING_FORMAT is respected
|
|
253
|
+
return new TextDecoder(STRING_FORMAT).decode(buffer);
|
|
254
|
+
}
|
|
255
|
+
// Truncate the buffer and ensure a valid string is returned
|
|
256
|
+
const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
|
|
257
|
+
// fatal: false means we get U+FFFD at the end if truncation broke the encoding
|
|
258
|
+
const decoder = new TextDecoder(STRING_FORMAT, {
|
|
259
|
+
fatal: false
|
|
260
|
+
});
|
|
261
|
+
let truncatedStr = decoder.decode(truncatedBuffer);
|
|
262
|
+
if (truncatedStr.endsWith('\uFFFD')) {
|
|
263
|
+
truncatedStr = truncatedStr.slice(0, -1);
|
|
240
264
|
}
|
|
265
|
+
return `${truncatedStr}... [truncated]`;
|
|
241
266
|
};
|
|
242
267
|
/**
|
|
243
268
|
* Extract available tool calls from the request parameters.
|
|
@@ -267,6 +292,11 @@ const extractAvailableToolCalls = (provider, params) => {
|
|
|
267
292
|
}
|
|
268
293
|
return null;
|
|
269
294
|
};
|
|
295
|
+
var AIEvent;
|
|
296
|
+
(function (AIEvent) {
|
|
297
|
+
AIEvent["Generation"] = "$ai_generation";
|
|
298
|
+
AIEvent["Embedding"] = "$ai_embedding";
|
|
299
|
+
})(AIEvent || (AIEvent = {}));
|
|
270
300
|
function sanitizeValues(obj) {
|
|
271
301
|
if (obj === undefined || obj === null) {
|
|
272
302
|
return obj;
|
|
@@ -283,6 +313,7 @@ function sanitizeValues(obj) {
|
|
|
283
313
|
}
|
|
284
314
|
const sendEventToPosthog = async ({
|
|
285
315
|
client,
|
|
316
|
+
eventType = AIEvent.Generation,
|
|
286
317
|
distinctId,
|
|
287
318
|
traceId,
|
|
288
319
|
model,
|
|
@@ -344,7 +375,9 @@ const sendEventToPosthog = async ({
|
|
|
344
375
|
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
|
|
345
376
|
$ai_http_status: httpStatus,
|
|
346
377
|
$ai_input_tokens: usage.inputTokens ?? 0,
|
|
347
|
-
|
|
378
|
+
...(usage.outputTokens !== undefined ? {
|
|
379
|
+
$ai_output_tokens: usage.outputTokens
|
|
380
|
+
} : {}),
|
|
348
381
|
...additionalTokenValues,
|
|
349
382
|
$ai_latency: latency,
|
|
350
383
|
$ai_trace_id: traceId,
|
|
@@ -361,7 +394,7 @@ const sendEventToPosthog = async ({
|
|
|
361
394
|
};
|
|
362
395
|
const event = {
|
|
363
396
|
distinctId: distinctId ?? traceId,
|
|
364
|
-
event:
|
|
397
|
+
event: eventType,
|
|
365
398
|
properties,
|
|
366
399
|
groups: params.posthogGroups
|
|
367
400
|
};
|
|
@@ -574,6 +607,7 @@ const sanitizeLangChain = data => {
|
|
|
574
607
|
const Chat = openai.OpenAI.Chat;
|
|
575
608
|
const Completions = Chat.Completions;
|
|
576
609
|
const Responses = openai.OpenAI.Responses;
|
|
610
|
+
const Embeddings = openai.OpenAI.Embeddings;
|
|
577
611
|
class PostHogOpenAI extends openai.OpenAI {
|
|
578
612
|
constructor(config) {
|
|
579
613
|
const {
|
|
@@ -584,6 +618,7 @@ class PostHogOpenAI extends openai.OpenAI {
|
|
|
584
618
|
this.phClient = posthog;
|
|
585
619
|
this.chat = new WrappedChat$1(this, this.phClient);
|
|
586
620
|
this.responses = new WrappedResponses$1(this, this.phClient);
|
|
621
|
+
this.embeddings = new WrappedEmbeddings$1(this, this.phClient);
|
|
587
622
|
}
|
|
588
623
|
}
|
|
589
624
|
let WrappedChat$1 = class WrappedChat extends Chat {
|
|
@@ -596,16 +631,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
596
631
|
constructor(client, phClient) {
|
|
597
632
|
super(client);
|
|
598
633
|
this.phClient = phClient;
|
|
634
|
+
this.baseURL = client.baseURL;
|
|
599
635
|
}
|
|
600
636
|
// --- Implementation Signature
|
|
601
637
|
create(body, options) {
|
|
602
638
|
const {
|
|
603
639
|
posthogDistinctId,
|
|
604
640
|
posthogTraceId,
|
|
605
|
-
posthogProperties,
|
|
606
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
607
|
-
posthogPrivacyMode = false,
|
|
608
|
-
posthogGroups,
|
|
609
641
|
posthogCaptureImmediate,
|
|
610
642
|
...openAIParams
|
|
611
643
|
} = body;
|
|
@@ -715,7 +747,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
715
747
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
716
748
|
output: formattedOutput,
|
|
717
749
|
latency,
|
|
718
|
-
baseURL: this.baseURL
|
|
750
|
+
baseURL: this.baseURL,
|
|
719
751
|
params: body,
|
|
720
752
|
httpStatus: 200,
|
|
721
753
|
usage,
|
|
@@ -733,7 +765,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
733
765
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
734
766
|
output: [],
|
|
735
767
|
latency: 0,
|
|
736
|
-
baseURL: this.baseURL
|
|
768
|
+
baseURL: this.baseURL,
|
|
737
769
|
params: body,
|
|
738
770
|
httpStatus,
|
|
739
771
|
usage: {
|
|
@@ -765,7 +797,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
765
797
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
766
798
|
output: formatResponseOpenAI(result),
|
|
767
799
|
latency,
|
|
768
|
-
baseURL: this.baseURL
|
|
800
|
+
baseURL: this.baseURL,
|
|
769
801
|
params: body,
|
|
770
802
|
httpStatus: 200,
|
|
771
803
|
usage: {
|
|
@@ -790,7 +822,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
790
822
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
791
823
|
output: [],
|
|
792
824
|
latency: 0,
|
|
793
|
-
baseURL: this.baseURL
|
|
825
|
+
baseURL: this.baseURL,
|
|
794
826
|
params: body,
|
|
795
827
|
httpStatus,
|
|
796
828
|
usage: {
|
|
@@ -811,16 +843,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
811
843
|
constructor(client, phClient) {
|
|
812
844
|
super(client);
|
|
813
845
|
this.phClient = phClient;
|
|
846
|
+
this.baseURL = client.baseURL;
|
|
814
847
|
}
|
|
815
848
|
// --- Implementation Signature
|
|
816
849
|
create(body, options) {
|
|
817
850
|
const {
|
|
818
851
|
posthogDistinctId,
|
|
819
852
|
posthogTraceId,
|
|
820
|
-
posthogProperties,
|
|
821
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
822
|
-
posthogPrivacyMode = false,
|
|
823
|
-
posthogGroups,
|
|
824
853
|
posthogCaptureImmediate,
|
|
825
854
|
...openAIParams
|
|
826
855
|
} = body;
|
|
@@ -863,7 +892,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
863
892
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
864
893
|
output: finalContent,
|
|
865
894
|
latency,
|
|
866
|
-
baseURL: this.baseURL
|
|
895
|
+
baseURL: this.baseURL,
|
|
867
896
|
params: body,
|
|
868
897
|
httpStatus: 200,
|
|
869
898
|
usage,
|
|
@@ -882,7 +911,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
882
911
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
883
912
|
output: [],
|
|
884
913
|
latency: 0,
|
|
885
|
-
baseURL: this.baseURL
|
|
914
|
+
baseURL: this.baseURL,
|
|
886
915
|
params: body,
|
|
887
916
|
httpStatus,
|
|
888
917
|
usage: {
|
|
@@ -916,7 +945,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
916
945
|
output: result.output
|
|
917
946
|
}),
|
|
918
947
|
latency,
|
|
919
|
-
baseURL: this.baseURL
|
|
948
|
+
baseURL: this.baseURL,
|
|
920
949
|
params: body,
|
|
921
950
|
httpStatus: 200,
|
|
922
951
|
usage: {
|
|
@@ -942,7 +971,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
942
971
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
943
972
|
output: [],
|
|
944
973
|
latency: 0,
|
|
945
|
-
baseURL: this.baseURL
|
|
974
|
+
baseURL: this.baseURL,
|
|
946
975
|
params: body,
|
|
947
976
|
httpStatus,
|
|
948
977
|
usage: {
|
|
@@ -962,10 +991,6 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
962
991
|
const {
|
|
963
992
|
posthogDistinctId,
|
|
964
993
|
posthogTraceId,
|
|
965
|
-
posthogProperties,
|
|
966
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
967
|
-
posthogPrivacyMode = false,
|
|
968
|
-
posthogGroups,
|
|
969
994
|
posthogCaptureImmediate,
|
|
970
995
|
...openAIParams
|
|
971
996
|
} = body;
|
|
@@ -990,7 +1015,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
990
1015
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
991
1016
|
output: result.output,
|
|
992
1017
|
latency,
|
|
993
|
-
baseURL: this.baseURL
|
|
1018
|
+
baseURL: this.baseURL,
|
|
994
1019
|
params: body,
|
|
995
1020
|
httpStatus: 200,
|
|
996
1021
|
usage: {
|
|
@@ -1014,7 +1039,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1014
1039
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
1015
1040
|
output: [],
|
|
1016
1041
|
latency: 0,
|
|
1017
|
-
baseURL: this.baseURL
|
|
1042
|
+
baseURL: this.baseURL,
|
|
1018
1043
|
params: body,
|
|
1019
1044
|
httpStatus,
|
|
1020
1045
|
usage: {
|
|
@@ -1034,6 +1059,73 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1034
1059
|
}
|
|
1035
1060
|
}
|
|
1036
1061
|
};
|
|
1062
|
+
let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
1063
|
+
constructor(client, phClient) {
|
|
1064
|
+
super(client);
|
|
1065
|
+
this.phClient = phClient;
|
|
1066
|
+
this.baseURL = client.baseURL;
|
|
1067
|
+
}
|
|
1068
|
+
create(body, options) {
|
|
1069
|
+
const {
|
|
1070
|
+
posthogDistinctId,
|
|
1071
|
+
posthogTraceId,
|
|
1072
|
+
posthogPrivacyMode = false,
|
|
1073
|
+
posthogCaptureImmediate,
|
|
1074
|
+
...openAIParams
|
|
1075
|
+
} = body;
|
|
1076
|
+
const traceId = posthogTraceId ?? uuid.v4();
|
|
1077
|
+
const startTime = Date.now();
|
|
1078
|
+
const parentPromise = super.create(openAIParams, options);
|
|
1079
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1080
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1081
|
+
await sendEventToPosthog({
|
|
1082
|
+
client: this.phClient,
|
|
1083
|
+
eventType: AIEvent.Embedding,
|
|
1084
|
+
distinctId: posthogDistinctId,
|
|
1085
|
+
traceId,
|
|
1086
|
+
model: openAIParams.model,
|
|
1087
|
+
provider: 'openai',
|
|
1088
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1089
|
+
output: null,
|
|
1090
|
+
// Embeddings don't have output content
|
|
1091
|
+
latency,
|
|
1092
|
+
baseURL: this.baseURL,
|
|
1093
|
+
params: body,
|
|
1094
|
+
httpStatus: 200,
|
|
1095
|
+
usage: {
|
|
1096
|
+
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1097
|
+
},
|
|
1098
|
+
captureImmediate: posthogCaptureImmediate
|
|
1099
|
+
});
|
|
1100
|
+
return result;
|
|
1101
|
+
}, async error => {
|
|
1102
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1103
|
+
await sendEventToPosthog({
|
|
1104
|
+
client: this.phClient,
|
|
1105
|
+
eventType: AIEvent.Embedding,
|
|
1106
|
+
distinctId: posthogDistinctId,
|
|
1107
|
+
traceId,
|
|
1108
|
+
model: openAIParams.model,
|
|
1109
|
+
provider: 'openai',
|
|
1110
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1111
|
+
output: null,
|
|
1112
|
+
// Embeddings don't have output content
|
|
1113
|
+
latency: 0,
|
|
1114
|
+
baseURL: this.baseURL,
|
|
1115
|
+
params: body,
|
|
1116
|
+
httpStatus,
|
|
1117
|
+
usage: {
|
|
1118
|
+
inputTokens: 0
|
|
1119
|
+
},
|
|
1120
|
+
isError: true,
|
|
1121
|
+
error: JSON.stringify(error),
|
|
1122
|
+
captureImmediate: posthogCaptureImmediate
|
|
1123
|
+
});
|
|
1124
|
+
throw error;
|
|
1125
|
+
});
|
|
1126
|
+
return wrappedPromise;
|
|
1127
|
+
}
|
|
1128
|
+
};
|
|
1037
1129
|
|
|
1038
1130
|
class PostHogAzureOpenAI extends openai.AzureOpenAI {
|
|
1039
1131
|
constructor(config) {
|
|
@@ -1044,6 +1136,7 @@ class PostHogAzureOpenAI extends openai.AzureOpenAI {
|
|
|
1044
1136
|
super(openAIConfig);
|
|
1045
1137
|
this.phClient = posthog;
|
|
1046
1138
|
this.chat = new WrappedChat(this, this.phClient);
|
|
1139
|
+
this.embeddings = new WrappedEmbeddings(this, this.phClient);
|
|
1047
1140
|
}
|
|
1048
1141
|
}
|
|
1049
1142
|
class WrappedChat extends openai.AzureOpenAI.Chat {
|
|
@@ -1056,16 +1149,13 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1056
1149
|
constructor(client, phClient) {
|
|
1057
1150
|
super(client);
|
|
1058
1151
|
this.phClient = phClient;
|
|
1152
|
+
this.baseURL = client.baseURL;
|
|
1059
1153
|
}
|
|
1060
1154
|
// --- Implementation Signature
|
|
1061
1155
|
create(body, options) {
|
|
1062
1156
|
const {
|
|
1063
1157
|
posthogDistinctId,
|
|
1064
1158
|
posthogTraceId,
|
|
1065
|
-
posthogProperties,
|
|
1066
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1067
|
-
posthogPrivacyMode = false,
|
|
1068
|
-
posthogGroups,
|
|
1069
1159
|
posthogCaptureImmediate,
|
|
1070
1160
|
...openAIParams
|
|
1071
1161
|
} = body;
|
|
@@ -1174,7 +1264,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1174
1264
|
input: openAIParams.messages,
|
|
1175
1265
|
output: formattedOutput,
|
|
1176
1266
|
latency,
|
|
1177
|
-
baseURL: this.baseURL
|
|
1267
|
+
baseURL: this.baseURL,
|
|
1178
1268
|
params: body,
|
|
1179
1269
|
httpStatus: 200,
|
|
1180
1270
|
usage,
|
|
@@ -1191,7 +1281,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1191
1281
|
input: openAIParams.messages,
|
|
1192
1282
|
output: [],
|
|
1193
1283
|
latency: 0,
|
|
1194
|
-
baseURL: this.baseURL
|
|
1284
|
+
baseURL: this.baseURL,
|
|
1195
1285
|
params: body,
|
|
1196
1286
|
httpStatus,
|
|
1197
1287
|
usage: {
|
|
@@ -1222,7 +1312,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1222
1312
|
input: openAIParams.messages,
|
|
1223
1313
|
output: formatResponseOpenAI(result),
|
|
1224
1314
|
latency,
|
|
1225
|
-
baseURL: this.baseURL
|
|
1315
|
+
baseURL: this.baseURL,
|
|
1226
1316
|
params: body,
|
|
1227
1317
|
httpStatus: 200,
|
|
1228
1318
|
usage: {
|
|
@@ -1246,7 +1336,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1246
1336
|
input: openAIParams.messages,
|
|
1247
1337
|
output: [],
|
|
1248
1338
|
latency: 0,
|
|
1249
|
-
baseURL: this.baseURL
|
|
1339
|
+
baseURL: this.baseURL,
|
|
1250
1340
|
params: body,
|
|
1251
1341
|
httpStatus,
|
|
1252
1342
|
usage: {
|
|
@@ -1267,16 +1357,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1267
1357
|
constructor(client, phClient) {
|
|
1268
1358
|
super(client);
|
|
1269
1359
|
this.phClient = phClient;
|
|
1360
|
+
this.baseURL = client.baseURL;
|
|
1270
1361
|
}
|
|
1271
1362
|
// --- Implementation Signature
|
|
1272
1363
|
create(body, options) {
|
|
1273
1364
|
const {
|
|
1274
1365
|
posthogDistinctId,
|
|
1275
1366
|
posthogTraceId,
|
|
1276
|
-
posthogProperties,
|
|
1277
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1278
|
-
posthogPrivacyMode = false,
|
|
1279
|
-
posthogGroups,
|
|
1280
1367
|
posthogCaptureImmediate,
|
|
1281
1368
|
...openAIParams
|
|
1282
1369
|
} = body;
|
|
@@ -1318,7 +1405,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1318
1405
|
input: openAIParams.input,
|
|
1319
1406
|
output: finalContent,
|
|
1320
1407
|
latency,
|
|
1321
|
-
baseURL: this.baseURL
|
|
1408
|
+
baseURL: this.baseURL,
|
|
1322
1409
|
params: body,
|
|
1323
1410
|
httpStatus: 200,
|
|
1324
1411
|
usage,
|
|
@@ -1336,7 +1423,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1336
1423
|
input: openAIParams.input,
|
|
1337
1424
|
output: [],
|
|
1338
1425
|
latency: 0,
|
|
1339
|
-
baseURL: this.baseURL
|
|
1426
|
+
baseURL: this.baseURL,
|
|
1340
1427
|
params: body,
|
|
1341
1428
|
httpStatus,
|
|
1342
1429
|
usage: {
|
|
@@ -1367,7 +1454,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1367
1454
|
input: openAIParams.input,
|
|
1368
1455
|
output: result.output,
|
|
1369
1456
|
latency,
|
|
1370
|
-
baseURL: this.baseURL
|
|
1457
|
+
baseURL: this.baseURL,
|
|
1371
1458
|
params: body,
|
|
1372
1459
|
httpStatus: 200,
|
|
1373
1460
|
usage: {
|
|
@@ -1392,7 +1479,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1392
1479
|
input: openAIParams.input,
|
|
1393
1480
|
output: [],
|
|
1394
1481
|
latency: 0,
|
|
1395
|
-
baseURL: this.baseURL
|
|
1482
|
+
baseURL: this.baseURL,
|
|
1396
1483
|
params: body,
|
|
1397
1484
|
httpStatus,
|
|
1398
1485
|
usage: {
|
|
@@ -1412,10 +1499,6 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1412
1499
|
const {
|
|
1413
1500
|
posthogDistinctId,
|
|
1414
1501
|
posthogTraceId,
|
|
1415
|
-
posthogProperties,
|
|
1416
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1417
|
-
posthogPrivacyMode = false,
|
|
1418
|
-
posthogGroups,
|
|
1419
1502
|
posthogCaptureImmediate,
|
|
1420
1503
|
...openAIParams
|
|
1421
1504
|
} = body;
|
|
@@ -1434,7 +1517,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1434
1517
|
input: openAIParams.input,
|
|
1435
1518
|
output: result.output,
|
|
1436
1519
|
latency,
|
|
1437
|
-
baseURL: this.baseURL
|
|
1520
|
+
baseURL: this.baseURL,
|
|
1438
1521
|
params: body,
|
|
1439
1522
|
httpStatus: 200,
|
|
1440
1523
|
usage: {
|
|
@@ -1457,7 +1540,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1457
1540
|
input: openAIParams.input,
|
|
1458
1541
|
output: [],
|
|
1459
1542
|
latency: 0,
|
|
1460
|
-
baseURL: this.baseURL
|
|
1543
|
+
baseURL: this.baseURL,
|
|
1461
1544
|
params: body,
|
|
1462
1545
|
httpStatus: error?.status ? error.status : 500,
|
|
1463
1546
|
usage: {
|
|
@@ -1473,6 +1556,72 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1473
1556
|
return wrappedPromise;
|
|
1474
1557
|
}
|
|
1475
1558
|
}
|
|
1559
|
+
class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
|
|
1560
|
+
constructor(client, phClient) {
|
|
1561
|
+
super(client);
|
|
1562
|
+
this.phClient = phClient;
|
|
1563
|
+
this.baseURL = client.baseURL;
|
|
1564
|
+
}
|
|
1565
|
+
create(body, options) {
|
|
1566
|
+
const {
|
|
1567
|
+
posthogDistinctId,
|
|
1568
|
+
posthogTraceId,
|
|
1569
|
+
posthogPrivacyMode = false,
|
|
1570
|
+
posthogCaptureImmediate,
|
|
1571
|
+
...openAIParams
|
|
1572
|
+
} = body;
|
|
1573
|
+
const traceId = posthogTraceId ?? uuid.v4();
|
|
1574
|
+
const startTime = Date.now();
|
|
1575
|
+
const parentPromise = super.create(openAIParams, options);
|
|
1576
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1577
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1578
|
+
await sendEventToPosthog({
|
|
1579
|
+
client: this.phClient,
|
|
1580
|
+
eventType: AIEvent.Embedding,
|
|
1581
|
+
distinctId: posthogDistinctId,
|
|
1582
|
+
traceId,
|
|
1583
|
+
model: openAIParams.model,
|
|
1584
|
+
provider: 'azure',
|
|
1585
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1586
|
+
output: null,
|
|
1587
|
+
// Embeddings don't have output content
|
|
1588
|
+
latency,
|
|
1589
|
+
baseURL: this.baseURL,
|
|
1590
|
+
params: body,
|
|
1591
|
+
httpStatus: 200,
|
|
1592
|
+
usage: {
|
|
1593
|
+
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1594
|
+
},
|
|
1595
|
+
captureImmediate: posthogCaptureImmediate
|
|
1596
|
+
});
|
|
1597
|
+
return result;
|
|
1598
|
+
}, async error => {
|
|
1599
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1600
|
+
await sendEventToPosthog({
|
|
1601
|
+
client: this.phClient,
|
|
1602
|
+
eventType: AIEvent.Embedding,
|
|
1603
|
+
distinctId: posthogDistinctId,
|
|
1604
|
+
traceId,
|
|
1605
|
+
model: openAIParams.model,
|
|
1606
|
+
provider: 'azure',
|
|
1607
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1608
|
+
output: null,
|
|
1609
|
+
latency: 0,
|
|
1610
|
+
baseURL: this.baseURL,
|
|
1611
|
+
params: body,
|
|
1612
|
+
httpStatus,
|
|
1613
|
+
usage: {
|
|
1614
|
+
inputTokens: 0
|
|
1615
|
+
},
|
|
1616
|
+
isError: true,
|
|
1617
|
+
error: JSON.stringify(error),
|
|
1618
|
+
captureImmediate: posthogCaptureImmediate
|
|
1619
|
+
});
|
|
1620
|
+
throw error;
|
|
1621
|
+
});
|
|
1622
|
+
return wrappedPromise;
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1476
1625
|
|
|
1477
1626
|
const mapVercelParams = params => {
|
|
1478
1627
|
return {
|
|
@@ -1660,7 +1809,7 @@ const mapVercelOutput = result => {
|
|
|
1660
1809
|
content: truncate(jsonOutput),
|
|
1661
1810
|
role: 'assistant'
|
|
1662
1811
|
}];
|
|
1663
|
-
} catch
|
|
1812
|
+
} catch {
|
|
1664
1813
|
console.error('Error stringifying output');
|
|
1665
1814
|
return [];
|
|
1666
1815
|
}
|
|
@@ -1935,15 +2084,12 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1935
2084
|
constructor(parentClient, phClient) {
|
|
1936
2085
|
super(parentClient);
|
|
1937
2086
|
this.phClient = phClient;
|
|
2087
|
+
this.baseURL = parentClient.baseURL;
|
|
1938
2088
|
}
|
|
1939
2089
|
create(body, options) {
|
|
1940
2090
|
const {
|
|
1941
2091
|
posthogDistinctId,
|
|
1942
2092
|
posthogTraceId,
|
|
1943
|
-
posthogProperties,
|
|
1944
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1945
|
-
posthogPrivacyMode = false,
|
|
1946
|
-
posthogGroups,
|
|
1947
2093
|
posthogCaptureImmediate,
|
|
1948
2094
|
...anthropicParams
|
|
1949
2095
|
} = body;
|
|
@@ -1995,7 +2141,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1995
2141
|
// Handle text delta events
|
|
1996
2142
|
if ('delta' in chunk) {
|
|
1997
2143
|
if ('text' in chunk.delta) {
|
|
1998
|
-
const delta = chunk
|
|
2144
|
+
const delta = chunk.delta.text;
|
|
1999
2145
|
accumulatedContent += delta;
|
|
2000
2146
|
if (currentTextBlock) {
|
|
2001
2147
|
currentTextBlock.text += delta;
|
|
@@ -2064,7 +2210,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2064
2210
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
|
|
2065
2211
|
output: formattedOutput,
|
|
2066
2212
|
latency,
|
|
2067
|
-
baseURL: this.baseURL
|
|
2213
|
+
baseURL: this.baseURL,
|
|
2068
2214
|
params: body,
|
|
2069
2215
|
httpStatus: 200,
|
|
2070
2216
|
usage,
|
|
@@ -2082,7 +2228,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2082
2228
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2083
2229
|
output: [],
|
|
2084
2230
|
latency: 0,
|
|
2085
|
-
baseURL: this.baseURL
|
|
2231
|
+
baseURL: this.baseURL,
|
|
2086
2232
|
params: body,
|
|
2087
2233
|
httpStatus: error?.status ? error.status : 500,
|
|
2088
2234
|
usage: {
|
|
@@ -2114,7 +2260,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2114
2260
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2115
2261
|
output: formatResponseAnthropic(result),
|
|
2116
2262
|
latency,
|
|
2117
|
-
baseURL: this.baseURL
|
|
2263
|
+
baseURL: this.baseURL,
|
|
2118
2264
|
params: body,
|
|
2119
2265
|
httpStatus: 200,
|
|
2120
2266
|
usage: {
|
|
@@ -2138,7 +2284,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2138
2284
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2139
2285
|
output: [],
|
|
2140
2286
|
latency: 0,
|
|
2141
|
-
baseURL: this.baseURL
|
|
2287
|
+
baseURL: this.baseURL,
|
|
2142
2288
|
params: body,
|
|
2143
2289
|
httpStatus: error?.status ? error.status : 500,
|
|
2144
2290
|
usage: {
|
|
@@ -2176,8 +2322,6 @@ class WrappedModels {
|
|
|
2176
2322
|
const {
|
|
2177
2323
|
posthogDistinctId,
|
|
2178
2324
|
posthogTraceId,
|
|
2179
|
-
posthogProperties,
|
|
2180
|
-
posthogGroups,
|
|
2181
2325
|
posthogCaptureImmediate,
|
|
2182
2326
|
...geminiParams
|
|
2183
2327
|
} = params;
|
|
@@ -2239,8 +2383,6 @@ class WrappedModels {
|
|
|
2239
2383
|
const {
|
|
2240
2384
|
posthogDistinctId,
|
|
2241
2385
|
posthogTraceId,
|
|
2242
|
-
posthogProperties,
|
|
2243
|
-
posthogGroups,
|
|
2244
2386
|
posthogCaptureImmediate,
|
|
2245
2387
|
...geminiParams
|
|
2246
2388
|
} = params;
|
|
@@ -2963,7 +3105,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2963
3105
|
this.debug = options.debug || false;
|
|
2964
3106
|
}
|
|
2965
3107
|
// ===== CALLBACK METHODS =====
|
|
2966
|
-
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata,
|
|
3108
|
+
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
|
|
2967
3109
|
this._logDebugEvent('on_chain_start', runId, parentRunId, {
|
|
2968
3110
|
inputs,
|
|
2969
3111
|
tags
|
|
@@ -2971,18 +3113,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2971
3113
|
this._setParentOfRun(runId, parentRunId);
|
|
2972
3114
|
this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
|
|
2973
3115
|
}
|
|
2974
|
-
handleChainEnd(outputs, runId, parentRunId, tags,
|
|
2975
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2976
|
-
kwargs) {
|
|
3116
|
+
handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
|
|
2977
3117
|
this._logDebugEvent('on_chain_end', runId, parentRunId, {
|
|
2978
3118
|
outputs,
|
|
2979
3119
|
tags
|
|
2980
3120
|
});
|
|
2981
3121
|
this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
|
|
2982
3122
|
}
|
|
2983
|
-
handleChainError(error, runId, parentRunId, tags,
|
|
2984
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2985
|
-
kwargs) {
|
|
3123
|
+
handleChainError(error, runId, parentRunId, tags, _kwargs) {
|
|
2986
3124
|
this._logDebugEvent('on_chain_error', runId, parentRunId, {
|
|
2987
3125
|
error,
|
|
2988
3126
|
tags
|
|
@@ -3007,18 +3145,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3007
3145
|
this._setParentOfRun(runId, parentRunId);
|
|
3008
3146
|
this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
|
|
3009
3147
|
}
|
|
3010
|
-
handleLLMEnd(output, runId, parentRunId, tags,
|
|
3011
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3012
|
-
extraParams) {
|
|
3148
|
+
handleLLMEnd(output, runId, parentRunId, tags, _extraParams) {
|
|
3013
3149
|
this._logDebugEvent('on_llm_end', runId, parentRunId, {
|
|
3014
3150
|
output,
|
|
3015
3151
|
tags
|
|
3016
3152
|
});
|
|
3017
3153
|
this._popRunAndCaptureGeneration(runId, parentRunId, output);
|
|
3018
3154
|
}
|
|
3019
|
-
handleLLMError(err, runId, parentRunId, tags,
|
|
3020
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3021
|
-
extraParams) {
|
|
3155
|
+
handleLLMError(err, runId, parentRunId, tags, _extraParams) {
|
|
3022
3156
|
this._logDebugEvent('on_llm_error', runId, parentRunId, {
|
|
3023
3157
|
err,
|
|
3024
3158
|
tags
|
|
@@ -3153,7 +3287,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3153
3287
|
_getTraceId(runId) {
|
|
3154
3288
|
return this.traceId ? String(this.traceId) : this._findRootRun(runId);
|
|
3155
3289
|
}
|
|
3156
|
-
_getParentRunId(traceId,
|
|
3290
|
+
_getParentRunId(traceId, _runId, parentRunId) {
|
|
3157
3291
|
// Replace the parent-run if not found in our stored parent tree.
|
|
3158
3292
|
if (parentRunId && !this.parentTree[parentRunId]) {
|
|
3159
3293
|
return traceId;
|