@posthog/ai 6.2.0 → 6.3.0
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 +185 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +28 -5
- package/dist/index.mjs +185 -76
- 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 +13 -5
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +13 -5
- 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.0";
|
|
30
30
|
|
|
31
31
|
// limit large outputs by truncating to 200kb (approx 200k bytes)
|
|
32
32
|
const MAX_OUTPUT_SIZE = 200000;
|
|
@@ -234,7 +234,7 @@ const truncate = str => {
|
|
|
234
234
|
}
|
|
235
235
|
const truncatedBuffer = buffer$1.slice(0, MAX_OUTPUT_SIZE);
|
|
236
236
|
return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`;
|
|
237
|
-
} catch
|
|
237
|
+
} catch {
|
|
238
238
|
console.error('Error truncating, likely not a string');
|
|
239
239
|
return str;
|
|
240
240
|
}
|
|
@@ -267,6 +267,11 @@ const extractAvailableToolCalls = (provider, params) => {
|
|
|
267
267
|
}
|
|
268
268
|
return null;
|
|
269
269
|
};
|
|
270
|
+
var AIEvent;
|
|
271
|
+
(function (AIEvent) {
|
|
272
|
+
AIEvent["Generation"] = "$ai_generation";
|
|
273
|
+
AIEvent["Embedding"] = "$ai_embedding";
|
|
274
|
+
})(AIEvent || (AIEvent = {}));
|
|
270
275
|
function sanitizeValues(obj) {
|
|
271
276
|
if (obj === undefined || obj === null) {
|
|
272
277
|
return obj;
|
|
@@ -283,6 +288,7 @@ function sanitizeValues(obj) {
|
|
|
283
288
|
}
|
|
284
289
|
const sendEventToPosthog = async ({
|
|
285
290
|
client,
|
|
291
|
+
eventType = AIEvent.Generation,
|
|
286
292
|
distinctId,
|
|
287
293
|
traceId,
|
|
288
294
|
model,
|
|
@@ -344,7 +350,9 @@ const sendEventToPosthog = async ({
|
|
|
344
350
|
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
|
|
345
351
|
$ai_http_status: httpStatus,
|
|
346
352
|
$ai_input_tokens: usage.inputTokens ?? 0,
|
|
347
|
-
|
|
353
|
+
...(usage.outputTokens !== undefined ? {
|
|
354
|
+
$ai_output_tokens: usage.outputTokens
|
|
355
|
+
} : {}),
|
|
348
356
|
...additionalTokenValues,
|
|
349
357
|
$ai_latency: latency,
|
|
350
358
|
$ai_trace_id: traceId,
|
|
@@ -361,7 +369,7 @@ const sendEventToPosthog = async ({
|
|
|
361
369
|
};
|
|
362
370
|
const event = {
|
|
363
371
|
distinctId: distinctId ?? traceId,
|
|
364
|
-
event:
|
|
372
|
+
event: eventType,
|
|
365
373
|
properties,
|
|
366
374
|
groups: params.posthogGroups
|
|
367
375
|
};
|
|
@@ -574,6 +582,7 @@ const sanitizeLangChain = data => {
|
|
|
574
582
|
const Chat = openai.OpenAI.Chat;
|
|
575
583
|
const Completions = Chat.Completions;
|
|
576
584
|
const Responses = openai.OpenAI.Responses;
|
|
585
|
+
const Embeddings = openai.OpenAI.Embeddings;
|
|
577
586
|
class PostHogOpenAI extends openai.OpenAI {
|
|
578
587
|
constructor(config) {
|
|
579
588
|
const {
|
|
@@ -584,6 +593,7 @@ class PostHogOpenAI extends openai.OpenAI {
|
|
|
584
593
|
this.phClient = posthog;
|
|
585
594
|
this.chat = new WrappedChat$1(this, this.phClient);
|
|
586
595
|
this.responses = new WrappedResponses$1(this, this.phClient);
|
|
596
|
+
this.embeddings = new WrappedEmbeddings$1(this, this.phClient);
|
|
587
597
|
}
|
|
588
598
|
}
|
|
589
599
|
let WrappedChat$1 = class WrappedChat extends Chat {
|
|
@@ -596,16 +606,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
596
606
|
constructor(client, phClient) {
|
|
597
607
|
super(client);
|
|
598
608
|
this.phClient = phClient;
|
|
609
|
+
this.baseURL = client.baseURL;
|
|
599
610
|
}
|
|
600
611
|
// --- Implementation Signature
|
|
601
612
|
create(body, options) {
|
|
602
613
|
const {
|
|
603
614
|
posthogDistinctId,
|
|
604
615
|
posthogTraceId,
|
|
605
|
-
posthogProperties,
|
|
606
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
607
|
-
posthogPrivacyMode = false,
|
|
608
|
-
posthogGroups,
|
|
609
616
|
posthogCaptureImmediate,
|
|
610
617
|
...openAIParams
|
|
611
618
|
} = body;
|
|
@@ -715,7 +722,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
715
722
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
716
723
|
output: formattedOutput,
|
|
717
724
|
latency,
|
|
718
|
-
baseURL: this.baseURL
|
|
725
|
+
baseURL: this.baseURL,
|
|
719
726
|
params: body,
|
|
720
727
|
httpStatus: 200,
|
|
721
728
|
usage,
|
|
@@ -733,7 +740,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
733
740
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
734
741
|
output: [],
|
|
735
742
|
latency: 0,
|
|
736
|
-
baseURL: this.baseURL
|
|
743
|
+
baseURL: this.baseURL,
|
|
737
744
|
params: body,
|
|
738
745
|
httpStatus,
|
|
739
746
|
usage: {
|
|
@@ -765,7 +772,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
765
772
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
766
773
|
output: formatResponseOpenAI(result),
|
|
767
774
|
latency,
|
|
768
|
-
baseURL: this.baseURL
|
|
775
|
+
baseURL: this.baseURL,
|
|
769
776
|
params: body,
|
|
770
777
|
httpStatus: 200,
|
|
771
778
|
usage: {
|
|
@@ -790,7 +797,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
790
797
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
791
798
|
output: [],
|
|
792
799
|
latency: 0,
|
|
793
|
-
baseURL: this.baseURL
|
|
800
|
+
baseURL: this.baseURL,
|
|
794
801
|
params: body,
|
|
795
802
|
httpStatus,
|
|
796
803
|
usage: {
|
|
@@ -811,16 +818,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
811
818
|
constructor(client, phClient) {
|
|
812
819
|
super(client);
|
|
813
820
|
this.phClient = phClient;
|
|
821
|
+
this.baseURL = client.baseURL;
|
|
814
822
|
}
|
|
815
823
|
// --- Implementation Signature
|
|
816
824
|
create(body, options) {
|
|
817
825
|
const {
|
|
818
826
|
posthogDistinctId,
|
|
819
827
|
posthogTraceId,
|
|
820
|
-
posthogProperties,
|
|
821
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
822
|
-
posthogPrivacyMode = false,
|
|
823
|
-
posthogGroups,
|
|
824
828
|
posthogCaptureImmediate,
|
|
825
829
|
...openAIParams
|
|
826
830
|
} = body;
|
|
@@ -863,7 +867,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
863
867
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
864
868
|
output: finalContent,
|
|
865
869
|
latency,
|
|
866
|
-
baseURL: this.baseURL
|
|
870
|
+
baseURL: this.baseURL,
|
|
867
871
|
params: body,
|
|
868
872
|
httpStatus: 200,
|
|
869
873
|
usage,
|
|
@@ -882,7 +886,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
882
886
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
883
887
|
output: [],
|
|
884
888
|
latency: 0,
|
|
885
|
-
baseURL: this.baseURL
|
|
889
|
+
baseURL: this.baseURL,
|
|
886
890
|
params: body,
|
|
887
891
|
httpStatus,
|
|
888
892
|
usage: {
|
|
@@ -916,7 +920,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
916
920
|
output: result.output
|
|
917
921
|
}),
|
|
918
922
|
latency,
|
|
919
|
-
baseURL: this.baseURL
|
|
923
|
+
baseURL: this.baseURL,
|
|
920
924
|
params: body,
|
|
921
925
|
httpStatus: 200,
|
|
922
926
|
usage: {
|
|
@@ -942,7 +946,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
942
946
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
943
947
|
output: [],
|
|
944
948
|
latency: 0,
|
|
945
|
-
baseURL: this.baseURL
|
|
949
|
+
baseURL: this.baseURL,
|
|
946
950
|
params: body,
|
|
947
951
|
httpStatus,
|
|
948
952
|
usage: {
|
|
@@ -962,10 +966,6 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
962
966
|
const {
|
|
963
967
|
posthogDistinctId,
|
|
964
968
|
posthogTraceId,
|
|
965
|
-
posthogProperties,
|
|
966
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
967
|
-
posthogPrivacyMode = false,
|
|
968
|
-
posthogGroups,
|
|
969
969
|
posthogCaptureImmediate,
|
|
970
970
|
...openAIParams
|
|
971
971
|
} = body;
|
|
@@ -990,7 +990,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
990
990
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
991
991
|
output: result.output,
|
|
992
992
|
latency,
|
|
993
|
-
baseURL: this.baseURL
|
|
993
|
+
baseURL: this.baseURL,
|
|
994
994
|
params: body,
|
|
995
995
|
httpStatus: 200,
|
|
996
996
|
usage: {
|
|
@@ -1014,7 +1014,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1014
1014
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
1015
1015
|
output: [],
|
|
1016
1016
|
latency: 0,
|
|
1017
|
-
baseURL: this.baseURL
|
|
1017
|
+
baseURL: this.baseURL,
|
|
1018
1018
|
params: body,
|
|
1019
1019
|
httpStatus,
|
|
1020
1020
|
usage: {
|
|
@@ -1034,6 +1034,73 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1034
1034
|
}
|
|
1035
1035
|
}
|
|
1036
1036
|
};
|
|
1037
|
+
let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
1038
|
+
constructor(client, phClient) {
|
|
1039
|
+
super(client);
|
|
1040
|
+
this.phClient = phClient;
|
|
1041
|
+
this.baseURL = client.baseURL;
|
|
1042
|
+
}
|
|
1043
|
+
create(body, options) {
|
|
1044
|
+
const {
|
|
1045
|
+
posthogDistinctId,
|
|
1046
|
+
posthogTraceId,
|
|
1047
|
+
posthogPrivacyMode = false,
|
|
1048
|
+
posthogCaptureImmediate,
|
|
1049
|
+
...openAIParams
|
|
1050
|
+
} = body;
|
|
1051
|
+
const traceId = posthogTraceId ?? uuid.v4();
|
|
1052
|
+
const startTime = Date.now();
|
|
1053
|
+
const parentPromise = super.create(openAIParams, options);
|
|
1054
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1055
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1056
|
+
await sendEventToPosthog({
|
|
1057
|
+
client: this.phClient,
|
|
1058
|
+
eventType: AIEvent.Embedding,
|
|
1059
|
+
distinctId: posthogDistinctId,
|
|
1060
|
+
traceId,
|
|
1061
|
+
model: openAIParams.model,
|
|
1062
|
+
provider: 'openai',
|
|
1063
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1064
|
+
output: null,
|
|
1065
|
+
// Embeddings don't have output content
|
|
1066
|
+
latency,
|
|
1067
|
+
baseURL: this.baseURL,
|
|
1068
|
+
params: body,
|
|
1069
|
+
httpStatus: 200,
|
|
1070
|
+
usage: {
|
|
1071
|
+
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1072
|
+
},
|
|
1073
|
+
captureImmediate: posthogCaptureImmediate
|
|
1074
|
+
});
|
|
1075
|
+
return result;
|
|
1076
|
+
}, async error => {
|
|
1077
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1078
|
+
await sendEventToPosthog({
|
|
1079
|
+
client: this.phClient,
|
|
1080
|
+
eventType: AIEvent.Embedding,
|
|
1081
|
+
distinctId: posthogDistinctId,
|
|
1082
|
+
traceId,
|
|
1083
|
+
model: openAIParams.model,
|
|
1084
|
+
provider: 'openai',
|
|
1085
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1086
|
+
output: null,
|
|
1087
|
+
// Embeddings don't have output content
|
|
1088
|
+
latency: 0,
|
|
1089
|
+
baseURL: this.baseURL,
|
|
1090
|
+
params: body,
|
|
1091
|
+
httpStatus,
|
|
1092
|
+
usage: {
|
|
1093
|
+
inputTokens: 0
|
|
1094
|
+
},
|
|
1095
|
+
isError: true,
|
|
1096
|
+
error: JSON.stringify(error),
|
|
1097
|
+
captureImmediate: posthogCaptureImmediate
|
|
1098
|
+
});
|
|
1099
|
+
throw error;
|
|
1100
|
+
});
|
|
1101
|
+
return wrappedPromise;
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1037
1104
|
|
|
1038
1105
|
class PostHogAzureOpenAI extends openai.AzureOpenAI {
|
|
1039
1106
|
constructor(config) {
|
|
@@ -1044,6 +1111,7 @@ class PostHogAzureOpenAI extends openai.AzureOpenAI {
|
|
|
1044
1111
|
super(openAIConfig);
|
|
1045
1112
|
this.phClient = posthog;
|
|
1046
1113
|
this.chat = new WrappedChat(this, this.phClient);
|
|
1114
|
+
this.embeddings = new WrappedEmbeddings(this, this.phClient);
|
|
1047
1115
|
}
|
|
1048
1116
|
}
|
|
1049
1117
|
class WrappedChat extends openai.AzureOpenAI.Chat {
|
|
@@ -1056,16 +1124,13 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1056
1124
|
constructor(client, phClient) {
|
|
1057
1125
|
super(client);
|
|
1058
1126
|
this.phClient = phClient;
|
|
1127
|
+
this.baseURL = client.baseURL;
|
|
1059
1128
|
}
|
|
1060
1129
|
// --- Implementation Signature
|
|
1061
1130
|
create(body, options) {
|
|
1062
1131
|
const {
|
|
1063
1132
|
posthogDistinctId,
|
|
1064
1133
|
posthogTraceId,
|
|
1065
|
-
posthogProperties,
|
|
1066
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1067
|
-
posthogPrivacyMode = false,
|
|
1068
|
-
posthogGroups,
|
|
1069
1134
|
posthogCaptureImmediate,
|
|
1070
1135
|
...openAIParams
|
|
1071
1136
|
} = body;
|
|
@@ -1174,7 +1239,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1174
1239
|
input: openAIParams.messages,
|
|
1175
1240
|
output: formattedOutput,
|
|
1176
1241
|
latency,
|
|
1177
|
-
baseURL: this.baseURL
|
|
1242
|
+
baseURL: this.baseURL,
|
|
1178
1243
|
params: body,
|
|
1179
1244
|
httpStatus: 200,
|
|
1180
1245
|
usage,
|
|
@@ -1191,7 +1256,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1191
1256
|
input: openAIParams.messages,
|
|
1192
1257
|
output: [],
|
|
1193
1258
|
latency: 0,
|
|
1194
|
-
baseURL: this.baseURL
|
|
1259
|
+
baseURL: this.baseURL,
|
|
1195
1260
|
params: body,
|
|
1196
1261
|
httpStatus,
|
|
1197
1262
|
usage: {
|
|
@@ -1222,7 +1287,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1222
1287
|
input: openAIParams.messages,
|
|
1223
1288
|
output: formatResponseOpenAI(result),
|
|
1224
1289
|
latency,
|
|
1225
|
-
baseURL: this.baseURL
|
|
1290
|
+
baseURL: this.baseURL,
|
|
1226
1291
|
params: body,
|
|
1227
1292
|
httpStatus: 200,
|
|
1228
1293
|
usage: {
|
|
@@ -1246,7 +1311,7 @@ class WrappedCompletions extends openai.AzureOpenAI.Chat.Completions {
|
|
|
1246
1311
|
input: openAIParams.messages,
|
|
1247
1312
|
output: [],
|
|
1248
1313
|
latency: 0,
|
|
1249
|
-
baseURL: this.baseURL
|
|
1314
|
+
baseURL: this.baseURL,
|
|
1250
1315
|
params: body,
|
|
1251
1316
|
httpStatus,
|
|
1252
1317
|
usage: {
|
|
@@ -1267,16 +1332,13 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1267
1332
|
constructor(client, phClient) {
|
|
1268
1333
|
super(client);
|
|
1269
1334
|
this.phClient = phClient;
|
|
1335
|
+
this.baseURL = client.baseURL;
|
|
1270
1336
|
}
|
|
1271
1337
|
// --- Implementation Signature
|
|
1272
1338
|
create(body, options) {
|
|
1273
1339
|
const {
|
|
1274
1340
|
posthogDistinctId,
|
|
1275
1341
|
posthogTraceId,
|
|
1276
|
-
posthogProperties,
|
|
1277
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1278
|
-
posthogPrivacyMode = false,
|
|
1279
|
-
posthogGroups,
|
|
1280
1342
|
posthogCaptureImmediate,
|
|
1281
1343
|
...openAIParams
|
|
1282
1344
|
} = body;
|
|
@@ -1318,7 +1380,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1318
1380
|
input: openAIParams.input,
|
|
1319
1381
|
output: finalContent,
|
|
1320
1382
|
latency,
|
|
1321
|
-
baseURL: this.baseURL
|
|
1383
|
+
baseURL: this.baseURL,
|
|
1322
1384
|
params: body,
|
|
1323
1385
|
httpStatus: 200,
|
|
1324
1386
|
usage,
|
|
@@ -1336,7 +1398,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1336
1398
|
input: openAIParams.input,
|
|
1337
1399
|
output: [],
|
|
1338
1400
|
latency: 0,
|
|
1339
|
-
baseURL: this.baseURL
|
|
1401
|
+
baseURL: this.baseURL,
|
|
1340
1402
|
params: body,
|
|
1341
1403
|
httpStatus,
|
|
1342
1404
|
usage: {
|
|
@@ -1367,7 +1429,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1367
1429
|
input: openAIParams.input,
|
|
1368
1430
|
output: result.output,
|
|
1369
1431
|
latency,
|
|
1370
|
-
baseURL: this.baseURL
|
|
1432
|
+
baseURL: this.baseURL,
|
|
1371
1433
|
params: body,
|
|
1372
1434
|
httpStatus: 200,
|
|
1373
1435
|
usage: {
|
|
@@ -1392,7 +1454,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1392
1454
|
input: openAIParams.input,
|
|
1393
1455
|
output: [],
|
|
1394
1456
|
latency: 0,
|
|
1395
|
-
baseURL: this.baseURL
|
|
1457
|
+
baseURL: this.baseURL,
|
|
1396
1458
|
params: body,
|
|
1397
1459
|
httpStatus,
|
|
1398
1460
|
usage: {
|
|
@@ -1412,10 +1474,6 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1412
1474
|
const {
|
|
1413
1475
|
posthogDistinctId,
|
|
1414
1476
|
posthogTraceId,
|
|
1415
|
-
posthogProperties,
|
|
1416
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1417
|
-
posthogPrivacyMode = false,
|
|
1418
|
-
posthogGroups,
|
|
1419
1477
|
posthogCaptureImmediate,
|
|
1420
1478
|
...openAIParams
|
|
1421
1479
|
} = body;
|
|
@@ -1434,7 +1492,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1434
1492
|
input: openAIParams.input,
|
|
1435
1493
|
output: result.output,
|
|
1436
1494
|
latency,
|
|
1437
|
-
baseURL: this.baseURL
|
|
1495
|
+
baseURL: this.baseURL,
|
|
1438
1496
|
params: body,
|
|
1439
1497
|
httpStatus: 200,
|
|
1440
1498
|
usage: {
|
|
@@ -1457,7 +1515,7 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1457
1515
|
input: openAIParams.input,
|
|
1458
1516
|
output: [],
|
|
1459
1517
|
latency: 0,
|
|
1460
|
-
baseURL: this.baseURL
|
|
1518
|
+
baseURL: this.baseURL,
|
|
1461
1519
|
params: body,
|
|
1462
1520
|
httpStatus: error?.status ? error.status : 500,
|
|
1463
1521
|
usage: {
|
|
@@ -1473,6 +1531,72 @@ class WrappedResponses extends openai.AzureOpenAI.Responses {
|
|
|
1473
1531
|
return wrappedPromise;
|
|
1474
1532
|
}
|
|
1475
1533
|
}
|
|
1534
|
+
class WrappedEmbeddings extends openai.AzureOpenAI.Embeddings {
|
|
1535
|
+
constructor(client, phClient) {
|
|
1536
|
+
super(client);
|
|
1537
|
+
this.phClient = phClient;
|
|
1538
|
+
this.baseURL = client.baseURL;
|
|
1539
|
+
}
|
|
1540
|
+
create(body, options) {
|
|
1541
|
+
const {
|
|
1542
|
+
posthogDistinctId,
|
|
1543
|
+
posthogTraceId,
|
|
1544
|
+
posthogPrivacyMode = false,
|
|
1545
|
+
posthogCaptureImmediate,
|
|
1546
|
+
...openAIParams
|
|
1547
|
+
} = body;
|
|
1548
|
+
const traceId = posthogTraceId ?? uuid.v4();
|
|
1549
|
+
const startTime = Date.now();
|
|
1550
|
+
const parentPromise = super.create(openAIParams, options);
|
|
1551
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1552
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1553
|
+
await sendEventToPosthog({
|
|
1554
|
+
client: this.phClient,
|
|
1555
|
+
eventType: AIEvent.Embedding,
|
|
1556
|
+
distinctId: posthogDistinctId,
|
|
1557
|
+
traceId,
|
|
1558
|
+
model: openAIParams.model,
|
|
1559
|
+
provider: 'azure',
|
|
1560
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1561
|
+
output: null,
|
|
1562
|
+
// Embeddings don't have output content
|
|
1563
|
+
latency,
|
|
1564
|
+
baseURL: this.baseURL,
|
|
1565
|
+
params: body,
|
|
1566
|
+
httpStatus: 200,
|
|
1567
|
+
usage: {
|
|
1568
|
+
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1569
|
+
},
|
|
1570
|
+
captureImmediate: posthogCaptureImmediate
|
|
1571
|
+
});
|
|
1572
|
+
return result;
|
|
1573
|
+
}, async error => {
|
|
1574
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1575
|
+
await sendEventToPosthog({
|
|
1576
|
+
client: this.phClient,
|
|
1577
|
+
eventType: AIEvent.Embedding,
|
|
1578
|
+
distinctId: posthogDistinctId,
|
|
1579
|
+
traceId,
|
|
1580
|
+
model: openAIParams.model,
|
|
1581
|
+
provider: 'azure',
|
|
1582
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1583
|
+
output: null,
|
|
1584
|
+
latency: 0,
|
|
1585
|
+
baseURL: this.baseURL,
|
|
1586
|
+
params: body,
|
|
1587
|
+
httpStatus,
|
|
1588
|
+
usage: {
|
|
1589
|
+
inputTokens: 0
|
|
1590
|
+
},
|
|
1591
|
+
isError: true,
|
|
1592
|
+
error: JSON.stringify(error),
|
|
1593
|
+
captureImmediate: posthogCaptureImmediate
|
|
1594
|
+
});
|
|
1595
|
+
throw error;
|
|
1596
|
+
});
|
|
1597
|
+
return wrappedPromise;
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1476
1600
|
|
|
1477
1601
|
const mapVercelParams = params => {
|
|
1478
1602
|
return {
|
|
@@ -1660,7 +1784,7 @@ const mapVercelOutput = result => {
|
|
|
1660
1784
|
content: truncate(jsonOutput),
|
|
1661
1785
|
role: 'assistant'
|
|
1662
1786
|
}];
|
|
1663
|
-
} catch
|
|
1787
|
+
} catch {
|
|
1664
1788
|
console.error('Error stringifying output');
|
|
1665
1789
|
return [];
|
|
1666
1790
|
}
|
|
@@ -1935,15 +2059,12 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1935
2059
|
constructor(parentClient, phClient) {
|
|
1936
2060
|
super(parentClient);
|
|
1937
2061
|
this.phClient = phClient;
|
|
2062
|
+
this.baseURL = parentClient.baseURL;
|
|
1938
2063
|
}
|
|
1939
2064
|
create(body, options) {
|
|
1940
2065
|
const {
|
|
1941
2066
|
posthogDistinctId,
|
|
1942
2067
|
posthogTraceId,
|
|
1943
|
-
posthogProperties,
|
|
1944
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1945
|
-
posthogPrivacyMode = false,
|
|
1946
|
-
posthogGroups,
|
|
1947
2068
|
posthogCaptureImmediate,
|
|
1948
2069
|
...anthropicParams
|
|
1949
2070
|
} = body;
|
|
@@ -1995,7 +2116,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1995
2116
|
// Handle text delta events
|
|
1996
2117
|
if ('delta' in chunk) {
|
|
1997
2118
|
if ('text' in chunk.delta) {
|
|
1998
|
-
const delta = chunk
|
|
2119
|
+
const delta = chunk.delta.text;
|
|
1999
2120
|
accumulatedContent += delta;
|
|
2000
2121
|
if (currentTextBlock) {
|
|
2001
2122
|
currentTextBlock.text += delta;
|
|
@@ -2064,7 +2185,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2064
2185
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
|
|
2065
2186
|
output: formattedOutput,
|
|
2066
2187
|
latency,
|
|
2067
|
-
baseURL: this.baseURL
|
|
2188
|
+
baseURL: this.baseURL,
|
|
2068
2189
|
params: body,
|
|
2069
2190
|
httpStatus: 200,
|
|
2070
2191
|
usage,
|
|
@@ -2082,7 +2203,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2082
2203
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2083
2204
|
output: [],
|
|
2084
2205
|
latency: 0,
|
|
2085
|
-
baseURL: this.baseURL
|
|
2206
|
+
baseURL: this.baseURL,
|
|
2086
2207
|
params: body,
|
|
2087
2208
|
httpStatus: error?.status ? error.status : 500,
|
|
2088
2209
|
usage: {
|
|
@@ -2114,7 +2235,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2114
2235
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2115
2236
|
output: formatResponseAnthropic(result),
|
|
2116
2237
|
latency,
|
|
2117
|
-
baseURL: this.baseURL
|
|
2238
|
+
baseURL: this.baseURL,
|
|
2118
2239
|
params: body,
|
|
2119
2240
|
httpStatus: 200,
|
|
2120
2241
|
usage: {
|
|
@@ -2138,7 +2259,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2138
2259
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2139
2260
|
output: [],
|
|
2140
2261
|
latency: 0,
|
|
2141
|
-
baseURL: this.baseURL
|
|
2262
|
+
baseURL: this.baseURL,
|
|
2142
2263
|
params: body,
|
|
2143
2264
|
httpStatus: error?.status ? error.status : 500,
|
|
2144
2265
|
usage: {
|
|
@@ -2176,8 +2297,6 @@ class WrappedModels {
|
|
|
2176
2297
|
const {
|
|
2177
2298
|
posthogDistinctId,
|
|
2178
2299
|
posthogTraceId,
|
|
2179
|
-
posthogProperties,
|
|
2180
|
-
posthogGroups,
|
|
2181
2300
|
posthogCaptureImmediate,
|
|
2182
2301
|
...geminiParams
|
|
2183
2302
|
} = params;
|
|
@@ -2239,8 +2358,6 @@ class WrappedModels {
|
|
|
2239
2358
|
const {
|
|
2240
2359
|
posthogDistinctId,
|
|
2241
2360
|
posthogTraceId,
|
|
2242
|
-
posthogProperties,
|
|
2243
|
-
posthogGroups,
|
|
2244
2361
|
posthogCaptureImmediate,
|
|
2245
2362
|
...geminiParams
|
|
2246
2363
|
} = params;
|
|
@@ -2963,7 +3080,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2963
3080
|
this.debug = options.debug || false;
|
|
2964
3081
|
}
|
|
2965
3082
|
// ===== CALLBACK METHODS =====
|
|
2966
|
-
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata,
|
|
3083
|
+
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
|
|
2967
3084
|
this._logDebugEvent('on_chain_start', runId, parentRunId, {
|
|
2968
3085
|
inputs,
|
|
2969
3086
|
tags
|
|
@@ -2971,18 +3088,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2971
3088
|
this._setParentOfRun(runId, parentRunId);
|
|
2972
3089
|
this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
|
|
2973
3090
|
}
|
|
2974
|
-
handleChainEnd(outputs, runId, parentRunId, tags,
|
|
2975
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2976
|
-
kwargs) {
|
|
3091
|
+
handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
|
|
2977
3092
|
this._logDebugEvent('on_chain_end', runId, parentRunId, {
|
|
2978
3093
|
outputs,
|
|
2979
3094
|
tags
|
|
2980
3095
|
});
|
|
2981
3096
|
this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
|
|
2982
3097
|
}
|
|
2983
|
-
handleChainError(error, runId, parentRunId, tags,
|
|
2984
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2985
|
-
kwargs) {
|
|
3098
|
+
handleChainError(error, runId, parentRunId, tags, _kwargs) {
|
|
2986
3099
|
this._logDebugEvent('on_chain_error', runId, parentRunId, {
|
|
2987
3100
|
error,
|
|
2988
3101
|
tags
|
|
@@ -3007,18 +3120,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3007
3120
|
this._setParentOfRun(runId, parentRunId);
|
|
3008
3121
|
this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
|
|
3009
3122
|
}
|
|
3010
|
-
handleLLMEnd(output, runId, parentRunId, tags,
|
|
3011
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3012
|
-
extraParams) {
|
|
3123
|
+
handleLLMEnd(output, runId, parentRunId, tags, _extraParams) {
|
|
3013
3124
|
this._logDebugEvent('on_llm_end', runId, parentRunId, {
|
|
3014
3125
|
output,
|
|
3015
3126
|
tags
|
|
3016
3127
|
});
|
|
3017
3128
|
this._popRunAndCaptureGeneration(runId, parentRunId, output);
|
|
3018
3129
|
}
|
|
3019
|
-
handleLLMError(err, runId, parentRunId, tags,
|
|
3020
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3021
|
-
extraParams) {
|
|
3130
|
+
handleLLMError(err, runId, parentRunId, tags, _extraParams) {
|
|
3022
3131
|
this._logDebugEvent('on_llm_error', runId, parentRunId, {
|
|
3023
3132
|
err,
|
|
3024
3133
|
tags
|
|
@@ -3153,7 +3262,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3153
3262
|
_getTraceId(runId) {
|
|
3154
3263
|
return this.traceId ? String(this.traceId) : this._findRootRun(runId);
|
|
3155
3264
|
}
|
|
3156
|
-
_getParentRunId(traceId,
|
|
3265
|
+
_getParentRunId(traceId, _runId, parentRunId) {
|
|
3157
3266
|
// Replace the parent-run if not found in our stored parent tree.
|
|
3158
3267
|
if (parentRunId && !this.parentTree[parentRunId]) {
|
|
3159
3268
|
return traceId;
|