@posthog/ai 6.1.2 → 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 +20 -11
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.d.ts +1 -0
- package/dist/anthropic/index.mjs +20 -11
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +14 -6
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +14 -6
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +205 -88
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +28 -5
- package/dist/index.mjs +192 -75
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +12 -14
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.d.ts +5 -5
- package/dist/langchain/index.mjs +12 -14
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +96 -24
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +13 -1
- package/dist/openai/index.mjs +96 -25
- package/dist/openai/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +16 -4
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +16 -4
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -6,6 +6,8 @@ import { wrapLanguageModel } from 'ai';
|
|
|
6
6
|
import AnthropicOriginal from '@anthropic-ai/sdk';
|
|
7
7
|
import { GoogleGenAI } from '@google/genai';
|
|
8
8
|
|
|
9
|
+
var version = "6.3.0";
|
|
10
|
+
|
|
9
11
|
// limit large outputs by truncating to 200kb (approx 200k bytes)
|
|
10
12
|
const MAX_OUTPUT_SIZE = 200000;
|
|
11
13
|
const STRING_FORMAT = 'utf8';
|
|
@@ -212,7 +214,7 @@ const truncate = str => {
|
|
|
212
214
|
}
|
|
213
215
|
const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
|
|
214
216
|
return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`;
|
|
215
|
-
} catch
|
|
217
|
+
} catch {
|
|
216
218
|
console.error('Error truncating, likely not a string');
|
|
217
219
|
return str;
|
|
218
220
|
}
|
|
@@ -245,6 +247,11 @@ const extractAvailableToolCalls = (provider, params) => {
|
|
|
245
247
|
}
|
|
246
248
|
return null;
|
|
247
249
|
};
|
|
250
|
+
var AIEvent;
|
|
251
|
+
(function (AIEvent) {
|
|
252
|
+
AIEvent["Generation"] = "$ai_generation";
|
|
253
|
+
AIEvent["Embedding"] = "$ai_embedding";
|
|
254
|
+
})(AIEvent || (AIEvent = {}));
|
|
248
255
|
function sanitizeValues(obj) {
|
|
249
256
|
if (obj === undefined || obj === null) {
|
|
250
257
|
return obj;
|
|
@@ -261,6 +268,7 @@ function sanitizeValues(obj) {
|
|
|
261
268
|
}
|
|
262
269
|
const sendEventToPosthog = async ({
|
|
263
270
|
client,
|
|
271
|
+
eventType = AIEvent.Generation,
|
|
264
272
|
distinctId,
|
|
265
273
|
traceId,
|
|
266
274
|
model,
|
|
@@ -313,6 +321,8 @@ const sendEventToPosthog = async ({
|
|
|
313
321
|
} : {})
|
|
314
322
|
};
|
|
315
323
|
const properties = {
|
|
324
|
+
$ai_lib: 'posthog-ai',
|
|
325
|
+
$ai_lib_version: version,
|
|
316
326
|
$ai_provider: params.posthogProviderOverride ?? provider,
|
|
317
327
|
$ai_model: params.posthogModelOverride ?? model,
|
|
318
328
|
$ai_model_parameters: getModelParams(params),
|
|
@@ -320,7 +330,9 @@ const sendEventToPosthog = async ({
|
|
|
320
330
|
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
|
|
321
331
|
$ai_http_status: httpStatus,
|
|
322
332
|
$ai_input_tokens: usage.inputTokens ?? 0,
|
|
323
|
-
|
|
333
|
+
...(usage.outputTokens !== undefined ? {
|
|
334
|
+
$ai_output_tokens: usage.outputTokens
|
|
335
|
+
} : {}),
|
|
324
336
|
...additionalTokenValues,
|
|
325
337
|
$ai_latency: latency,
|
|
326
338
|
$ai_trace_id: traceId,
|
|
@@ -337,7 +349,7 @@ const sendEventToPosthog = async ({
|
|
|
337
349
|
};
|
|
338
350
|
const event = {
|
|
339
351
|
distinctId: distinctId ?? traceId,
|
|
340
|
-
event:
|
|
352
|
+
event: eventType,
|
|
341
353
|
properties,
|
|
342
354
|
groups: params.posthogGroups
|
|
343
355
|
};
|
|
@@ -550,6 +562,7 @@ const sanitizeLangChain = data => {
|
|
|
550
562
|
const Chat = OpenAI.Chat;
|
|
551
563
|
const Completions = Chat.Completions;
|
|
552
564
|
const Responses = OpenAI.Responses;
|
|
565
|
+
const Embeddings = OpenAI.Embeddings;
|
|
553
566
|
class PostHogOpenAI extends OpenAI {
|
|
554
567
|
constructor(config) {
|
|
555
568
|
const {
|
|
@@ -560,6 +573,7 @@ class PostHogOpenAI extends OpenAI {
|
|
|
560
573
|
this.phClient = posthog;
|
|
561
574
|
this.chat = new WrappedChat$1(this, this.phClient);
|
|
562
575
|
this.responses = new WrappedResponses$1(this, this.phClient);
|
|
576
|
+
this.embeddings = new WrappedEmbeddings$1(this, this.phClient);
|
|
563
577
|
}
|
|
564
578
|
}
|
|
565
579
|
let WrappedChat$1 = class WrappedChat extends Chat {
|
|
@@ -572,16 +586,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
572
586
|
constructor(client, phClient) {
|
|
573
587
|
super(client);
|
|
574
588
|
this.phClient = phClient;
|
|
589
|
+
this.baseURL = client.baseURL;
|
|
575
590
|
}
|
|
576
591
|
// --- Implementation Signature
|
|
577
592
|
create(body, options) {
|
|
578
593
|
const {
|
|
579
594
|
posthogDistinctId,
|
|
580
595
|
posthogTraceId,
|
|
581
|
-
posthogProperties,
|
|
582
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
583
|
-
posthogPrivacyMode = false,
|
|
584
|
-
posthogGroups,
|
|
585
596
|
posthogCaptureImmediate,
|
|
586
597
|
...openAIParams
|
|
587
598
|
} = body;
|
|
@@ -691,7 +702,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
691
702
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
692
703
|
output: formattedOutput,
|
|
693
704
|
latency,
|
|
694
|
-
baseURL: this.baseURL
|
|
705
|
+
baseURL: this.baseURL,
|
|
695
706
|
params: body,
|
|
696
707
|
httpStatus: 200,
|
|
697
708
|
usage,
|
|
@@ -709,7 +720,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
709
720
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
710
721
|
output: [],
|
|
711
722
|
latency: 0,
|
|
712
|
-
baseURL: this.baseURL
|
|
723
|
+
baseURL: this.baseURL,
|
|
713
724
|
params: body,
|
|
714
725
|
httpStatus,
|
|
715
726
|
usage: {
|
|
@@ -741,7 +752,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
741
752
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
742
753
|
output: formatResponseOpenAI(result),
|
|
743
754
|
latency,
|
|
744
|
-
baseURL: this.baseURL
|
|
755
|
+
baseURL: this.baseURL,
|
|
745
756
|
params: body,
|
|
746
757
|
httpStatus: 200,
|
|
747
758
|
usage: {
|
|
@@ -766,7 +777,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
766
777
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
767
778
|
output: [],
|
|
768
779
|
latency: 0,
|
|
769
|
-
baseURL: this.baseURL
|
|
780
|
+
baseURL: this.baseURL,
|
|
770
781
|
params: body,
|
|
771
782
|
httpStatus,
|
|
772
783
|
usage: {
|
|
@@ -787,16 +798,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
787
798
|
constructor(client, phClient) {
|
|
788
799
|
super(client);
|
|
789
800
|
this.phClient = phClient;
|
|
801
|
+
this.baseURL = client.baseURL;
|
|
790
802
|
}
|
|
791
803
|
// --- Implementation Signature
|
|
792
804
|
create(body, options) {
|
|
793
805
|
const {
|
|
794
806
|
posthogDistinctId,
|
|
795
807
|
posthogTraceId,
|
|
796
|
-
posthogProperties,
|
|
797
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
798
|
-
posthogPrivacyMode = false,
|
|
799
|
-
posthogGroups,
|
|
800
808
|
posthogCaptureImmediate,
|
|
801
809
|
...openAIParams
|
|
802
810
|
} = body;
|
|
@@ -839,7 +847,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
839
847
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
840
848
|
output: finalContent,
|
|
841
849
|
latency,
|
|
842
|
-
baseURL: this.baseURL
|
|
850
|
+
baseURL: this.baseURL,
|
|
843
851
|
params: body,
|
|
844
852
|
httpStatus: 200,
|
|
845
853
|
usage,
|
|
@@ -858,7 +866,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
858
866
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
859
867
|
output: [],
|
|
860
868
|
latency: 0,
|
|
861
|
-
baseURL: this.baseURL
|
|
869
|
+
baseURL: this.baseURL,
|
|
862
870
|
params: body,
|
|
863
871
|
httpStatus,
|
|
864
872
|
usage: {
|
|
@@ -892,7 +900,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
892
900
|
output: result.output
|
|
893
901
|
}),
|
|
894
902
|
latency,
|
|
895
|
-
baseURL: this.baseURL
|
|
903
|
+
baseURL: this.baseURL,
|
|
896
904
|
params: body,
|
|
897
905
|
httpStatus: 200,
|
|
898
906
|
usage: {
|
|
@@ -918,7 +926,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
918
926
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
919
927
|
output: [],
|
|
920
928
|
latency: 0,
|
|
921
|
-
baseURL: this.baseURL
|
|
929
|
+
baseURL: this.baseURL,
|
|
922
930
|
params: body,
|
|
923
931
|
httpStatus,
|
|
924
932
|
usage: {
|
|
@@ -938,10 +946,6 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
938
946
|
const {
|
|
939
947
|
posthogDistinctId,
|
|
940
948
|
posthogTraceId,
|
|
941
|
-
posthogProperties,
|
|
942
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
943
|
-
posthogPrivacyMode = false,
|
|
944
|
-
posthogGroups,
|
|
945
949
|
posthogCaptureImmediate,
|
|
946
950
|
...openAIParams
|
|
947
951
|
} = body;
|
|
@@ -966,7 +970,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
966
970
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
967
971
|
output: result.output,
|
|
968
972
|
latency,
|
|
969
|
-
baseURL: this.baseURL
|
|
973
|
+
baseURL: this.baseURL,
|
|
970
974
|
params: body,
|
|
971
975
|
httpStatus: 200,
|
|
972
976
|
usage: {
|
|
@@ -990,7 +994,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
990
994
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
991
995
|
output: [],
|
|
992
996
|
latency: 0,
|
|
993
|
-
baseURL: this.baseURL
|
|
997
|
+
baseURL: this.baseURL,
|
|
994
998
|
params: body,
|
|
995
999
|
httpStatus,
|
|
996
1000
|
usage: {
|
|
@@ -1010,6 +1014,73 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1010
1014
|
}
|
|
1011
1015
|
}
|
|
1012
1016
|
};
|
|
1017
|
+
let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
1018
|
+
constructor(client, phClient) {
|
|
1019
|
+
super(client);
|
|
1020
|
+
this.phClient = phClient;
|
|
1021
|
+
this.baseURL = client.baseURL;
|
|
1022
|
+
}
|
|
1023
|
+
create(body, options) {
|
|
1024
|
+
const {
|
|
1025
|
+
posthogDistinctId,
|
|
1026
|
+
posthogTraceId,
|
|
1027
|
+
posthogPrivacyMode = false,
|
|
1028
|
+
posthogCaptureImmediate,
|
|
1029
|
+
...openAIParams
|
|
1030
|
+
} = body;
|
|
1031
|
+
const traceId = posthogTraceId ?? v4();
|
|
1032
|
+
const startTime = Date.now();
|
|
1033
|
+
const parentPromise = super.create(openAIParams, options);
|
|
1034
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1035
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1036
|
+
await sendEventToPosthog({
|
|
1037
|
+
client: this.phClient,
|
|
1038
|
+
eventType: AIEvent.Embedding,
|
|
1039
|
+
distinctId: posthogDistinctId,
|
|
1040
|
+
traceId,
|
|
1041
|
+
model: openAIParams.model,
|
|
1042
|
+
provider: 'openai',
|
|
1043
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1044
|
+
output: null,
|
|
1045
|
+
// Embeddings don't have output content
|
|
1046
|
+
latency,
|
|
1047
|
+
baseURL: this.baseURL,
|
|
1048
|
+
params: body,
|
|
1049
|
+
httpStatus: 200,
|
|
1050
|
+
usage: {
|
|
1051
|
+
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1052
|
+
},
|
|
1053
|
+
captureImmediate: posthogCaptureImmediate
|
|
1054
|
+
});
|
|
1055
|
+
return result;
|
|
1056
|
+
}, async error => {
|
|
1057
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1058
|
+
await sendEventToPosthog({
|
|
1059
|
+
client: this.phClient,
|
|
1060
|
+
eventType: AIEvent.Embedding,
|
|
1061
|
+
distinctId: posthogDistinctId,
|
|
1062
|
+
traceId,
|
|
1063
|
+
model: openAIParams.model,
|
|
1064
|
+
provider: 'openai',
|
|
1065
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1066
|
+
output: null,
|
|
1067
|
+
// Embeddings don't have output content
|
|
1068
|
+
latency: 0,
|
|
1069
|
+
baseURL: this.baseURL,
|
|
1070
|
+
params: body,
|
|
1071
|
+
httpStatus,
|
|
1072
|
+
usage: {
|
|
1073
|
+
inputTokens: 0
|
|
1074
|
+
},
|
|
1075
|
+
isError: true,
|
|
1076
|
+
error: JSON.stringify(error),
|
|
1077
|
+
captureImmediate: posthogCaptureImmediate
|
|
1078
|
+
});
|
|
1079
|
+
throw error;
|
|
1080
|
+
});
|
|
1081
|
+
return wrappedPromise;
|
|
1082
|
+
}
|
|
1083
|
+
};
|
|
1013
1084
|
|
|
1014
1085
|
class PostHogAzureOpenAI extends AzureOpenAI {
|
|
1015
1086
|
constructor(config) {
|
|
@@ -1020,6 +1091,7 @@ class PostHogAzureOpenAI extends AzureOpenAI {
|
|
|
1020
1091
|
super(openAIConfig);
|
|
1021
1092
|
this.phClient = posthog;
|
|
1022
1093
|
this.chat = new WrappedChat(this, this.phClient);
|
|
1094
|
+
this.embeddings = new WrappedEmbeddings(this, this.phClient);
|
|
1023
1095
|
}
|
|
1024
1096
|
}
|
|
1025
1097
|
class WrappedChat extends AzureOpenAI.Chat {
|
|
@@ -1032,16 +1104,13 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1032
1104
|
constructor(client, phClient) {
|
|
1033
1105
|
super(client);
|
|
1034
1106
|
this.phClient = phClient;
|
|
1107
|
+
this.baseURL = client.baseURL;
|
|
1035
1108
|
}
|
|
1036
1109
|
// --- Implementation Signature
|
|
1037
1110
|
create(body, options) {
|
|
1038
1111
|
const {
|
|
1039
1112
|
posthogDistinctId,
|
|
1040
1113
|
posthogTraceId,
|
|
1041
|
-
posthogProperties,
|
|
1042
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1043
|
-
posthogPrivacyMode = false,
|
|
1044
|
-
posthogGroups,
|
|
1045
1114
|
posthogCaptureImmediate,
|
|
1046
1115
|
...openAIParams
|
|
1047
1116
|
} = body;
|
|
@@ -1150,7 +1219,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1150
1219
|
input: openAIParams.messages,
|
|
1151
1220
|
output: formattedOutput,
|
|
1152
1221
|
latency,
|
|
1153
|
-
baseURL: this.baseURL
|
|
1222
|
+
baseURL: this.baseURL,
|
|
1154
1223
|
params: body,
|
|
1155
1224
|
httpStatus: 200,
|
|
1156
1225
|
usage,
|
|
@@ -1167,7 +1236,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1167
1236
|
input: openAIParams.messages,
|
|
1168
1237
|
output: [],
|
|
1169
1238
|
latency: 0,
|
|
1170
|
-
baseURL: this.baseURL
|
|
1239
|
+
baseURL: this.baseURL,
|
|
1171
1240
|
params: body,
|
|
1172
1241
|
httpStatus,
|
|
1173
1242
|
usage: {
|
|
@@ -1198,7 +1267,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1198
1267
|
input: openAIParams.messages,
|
|
1199
1268
|
output: formatResponseOpenAI(result),
|
|
1200
1269
|
latency,
|
|
1201
|
-
baseURL: this.baseURL
|
|
1270
|
+
baseURL: this.baseURL,
|
|
1202
1271
|
params: body,
|
|
1203
1272
|
httpStatus: 200,
|
|
1204
1273
|
usage: {
|
|
@@ -1222,7 +1291,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1222
1291
|
input: openAIParams.messages,
|
|
1223
1292
|
output: [],
|
|
1224
1293
|
latency: 0,
|
|
1225
|
-
baseURL: this.baseURL
|
|
1294
|
+
baseURL: this.baseURL,
|
|
1226
1295
|
params: body,
|
|
1227
1296
|
httpStatus,
|
|
1228
1297
|
usage: {
|
|
@@ -1243,16 +1312,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1243
1312
|
constructor(client, phClient) {
|
|
1244
1313
|
super(client);
|
|
1245
1314
|
this.phClient = phClient;
|
|
1315
|
+
this.baseURL = client.baseURL;
|
|
1246
1316
|
}
|
|
1247
1317
|
// --- Implementation Signature
|
|
1248
1318
|
create(body, options) {
|
|
1249
1319
|
const {
|
|
1250
1320
|
posthogDistinctId,
|
|
1251
1321
|
posthogTraceId,
|
|
1252
|
-
posthogProperties,
|
|
1253
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1254
|
-
posthogPrivacyMode = false,
|
|
1255
|
-
posthogGroups,
|
|
1256
1322
|
posthogCaptureImmediate,
|
|
1257
1323
|
...openAIParams
|
|
1258
1324
|
} = body;
|
|
@@ -1294,7 +1360,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1294
1360
|
input: openAIParams.input,
|
|
1295
1361
|
output: finalContent,
|
|
1296
1362
|
latency,
|
|
1297
|
-
baseURL: this.baseURL
|
|
1363
|
+
baseURL: this.baseURL,
|
|
1298
1364
|
params: body,
|
|
1299
1365
|
httpStatus: 200,
|
|
1300
1366
|
usage,
|
|
@@ -1312,7 +1378,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1312
1378
|
input: openAIParams.input,
|
|
1313
1379
|
output: [],
|
|
1314
1380
|
latency: 0,
|
|
1315
|
-
baseURL: this.baseURL
|
|
1381
|
+
baseURL: this.baseURL,
|
|
1316
1382
|
params: body,
|
|
1317
1383
|
httpStatus,
|
|
1318
1384
|
usage: {
|
|
@@ -1343,7 +1409,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1343
1409
|
input: openAIParams.input,
|
|
1344
1410
|
output: result.output,
|
|
1345
1411
|
latency,
|
|
1346
|
-
baseURL: this.baseURL
|
|
1412
|
+
baseURL: this.baseURL,
|
|
1347
1413
|
params: body,
|
|
1348
1414
|
httpStatus: 200,
|
|
1349
1415
|
usage: {
|
|
@@ -1368,7 +1434,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1368
1434
|
input: openAIParams.input,
|
|
1369
1435
|
output: [],
|
|
1370
1436
|
latency: 0,
|
|
1371
|
-
baseURL: this.baseURL
|
|
1437
|
+
baseURL: this.baseURL,
|
|
1372
1438
|
params: body,
|
|
1373
1439
|
httpStatus,
|
|
1374
1440
|
usage: {
|
|
@@ -1388,10 +1454,6 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1388
1454
|
const {
|
|
1389
1455
|
posthogDistinctId,
|
|
1390
1456
|
posthogTraceId,
|
|
1391
|
-
posthogProperties,
|
|
1392
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1393
|
-
posthogPrivacyMode = false,
|
|
1394
|
-
posthogGroups,
|
|
1395
1457
|
posthogCaptureImmediate,
|
|
1396
1458
|
...openAIParams
|
|
1397
1459
|
} = body;
|
|
@@ -1410,7 +1472,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1410
1472
|
input: openAIParams.input,
|
|
1411
1473
|
output: result.output,
|
|
1412
1474
|
latency,
|
|
1413
|
-
baseURL: this.baseURL
|
|
1475
|
+
baseURL: this.baseURL,
|
|
1414
1476
|
params: body,
|
|
1415
1477
|
httpStatus: 200,
|
|
1416
1478
|
usage: {
|
|
@@ -1433,7 +1495,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1433
1495
|
input: openAIParams.input,
|
|
1434
1496
|
output: [],
|
|
1435
1497
|
latency: 0,
|
|
1436
|
-
baseURL: this.baseURL
|
|
1498
|
+
baseURL: this.baseURL,
|
|
1437
1499
|
params: body,
|
|
1438
1500
|
httpStatus: error?.status ? error.status : 500,
|
|
1439
1501
|
usage: {
|
|
@@ -1449,6 +1511,72 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1449
1511
|
return wrappedPromise;
|
|
1450
1512
|
}
|
|
1451
1513
|
}
|
|
1514
|
+
class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
1515
|
+
constructor(client, phClient) {
|
|
1516
|
+
super(client);
|
|
1517
|
+
this.phClient = phClient;
|
|
1518
|
+
this.baseURL = client.baseURL;
|
|
1519
|
+
}
|
|
1520
|
+
create(body, options) {
|
|
1521
|
+
const {
|
|
1522
|
+
posthogDistinctId,
|
|
1523
|
+
posthogTraceId,
|
|
1524
|
+
posthogPrivacyMode = false,
|
|
1525
|
+
posthogCaptureImmediate,
|
|
1526
|
+
...openAIParams
|
|
1527
|
+
} = body;
|
|
1528
|
+
const traceId = posthogTraceId ?? v4();
|
|
1529
|
+
const startTime = Date.now();
|
|
1530
|
+
const parentPromise = super.create(openAIParams, options);
|
|
1531
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1532
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1533
|
+
await sendEventToPosthog({
|
|
1534
|
+
client: this.phClient,
|
|
1535
|
+
eventType: AIEvent.Embedding,
|
|
1536
|
+
distinctId: posthogDistinctId,
|
|
1537
|
+
traceId,
|
|
1538
|
+
model: openAIParams.model,
|
|
1539
|
+
provider: 'azure',
|
|
1540
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1541
|
+
output: null,
|
|
1542
|
+
// Embeddings don't have output content
|
|
1543
|
+
latency,
|
|
1544
|
+
baseURL: this.baseURL,
|
|
1545
|
+
params: body,
|
|
1546
|
+
httpStatus: 200,
|
|
1547
|
+
usage: {
|
|
1548
|
+
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1549
|
+
},
|
|
1550
|
+
captureImmediate: posthogCaptureImmediate
|
|
1551
|
+
});
|
|
1552
|
+
return result;
|
|
1553
|
+
}, async error => {
|
|
1554
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1555
|
+
await sendEventToPosthog({
|
|
1556
|
+
client: this.phClient,
|
|
1557
|
+
eventType: AIEvent.Embedding,
|
|
1558
|
+
distinctId: posthogDistinctId,
|
|
1559
|
+
traceId,
|
|
1560
|
+
model: openAIParams.model,
|
|
1561
|
+
provider: 'azure',
|
|
1562
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1563
|
+
output: null,
|
|
1564
|
+
latency: 0,
|
|
1565
|
+
baseURL: this.baseURL,
|
|
1566
|
+
params: body,
|
|
1567
|
+
httpStatus,
|
|
1568
|
+
usage: {
|
|
1569
|
+
inputTokens: 0
|
|
1570
|
+
},
|
|
1571
|
+
isError: true,
|
|
1572
|
+
error: JSON.stringify(error),
|
|
1573
|
+
captureImmediate: posthogCaptureImmediate
|
|
1574
|
+
});
|
|
1575
|
+
throw error;
|
|
1576
|
+
});
|
|
1577
|
+
return wrappedPromise;
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1452
1580
|
|
|
1453
1581
|
const mapVercelParams = params => {
|
|
1454
1582
|
return {
|
|
@@ -1636,7 +1764,7 @@ const mapVercelOutput = result => {
|
|
|
1636
1764
|
content: truncate(jsonOutput),
|
|
1637
1765
|
role: 'assistant'
|
|
1638
1766
|
}];
|
|
1639
|
-
} catch
|
|
1767
|
+
} catch {
|
|
1640
1768
|
console.error('Error stringifying output');
|
|
1641
1769
|
return [];
|
|
1642
1770
|
}
|
|
@@ -1911,15 +2039,12 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1911
2039
|
constructor(parentClient, phClient) {
|
|
1912
2040
|
super(parentClient);
|
|
1913
2041
|
this.phClient = phClient;
|
|
2042
|
+
this.baseURL = parentClient.baseURL;
|
|
1914
2043
|
}
|
|
1915
2044
|
create(body, options) {
|
|
1916
2045
|
const {
|
|
1917
2046
|
posthogDistinctId,
|
|
1918
2047
|
posthogTraceId,
|
|
1919
|
-
posthogProperties,
|
|
1920
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1921
|
-
posthogPrivacyMode = false,
|
|
1922
|
-
posthogGroups,
|
|
1923
2048
|
posthogCaptureImmediate,
|
|
1924
2049
|
...anthropicParams
|
|
1925
2050
|
} = body;
|
|
@@ -1971,7 +2096,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1971
2096
|
// Handle text delta events
|
|
1972
2097
|
if ('delta' in chunk) {
|
|
1973
2098
|
if ('text' in chunk.delta) {
|
|
1974
|
-
const delta = chunk
|
|
2099
|
+
const delta = chunk.delta.text;
|
|
1975
2100
|
accumulatedContent += delta;
|
|
1976
2101
|
if (currentTextBlock) {
|
|
1977
2102
|
currentTextBlock.text += delta;
|
|
@@ -2040,7 +2165,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2040
2165
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
|
|
2041
2166
|
output: formattedOutput,
|
|
2042
2167
|
latency,
|
|
2043
|
-
baseURL: this.baseURL
|
|
2168
|
+
baseURL: this.baseURL,
|
|
2044
2169
|
params: body,
|
|
2045
2170
|
httpStatus: 200,
|
|
2046
2171
|
usage,
|
|
@@ -2058,7 +2183,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2058
2183
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2059
2184
|
output: [],
|
|
2060
2185
|
latency: 0,
|
|
2061
|
-
baseURL: this.baseURL
|
|
2186
|
+
baseURL: this.baseURL,
|
|
2062
2187
|
params: body,
|
|
2063
2188
|
httpStatus: error?.status ? error.status : 500,
|
|
2064
2189
|
usage: {
|
|
@@ -2090,7 +2215,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2090
2215
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2091
2216
|
output: formatResponseAnthropic(result),
|
|
2092
2217
|
latency,
|
|
2093
|
-
baseURL: this.baseURL
|
|
2218
|
+
baseURL: this.baseURL,
|
|
2094
2219
|
params: body,
|
|
2095
2220
|
httpStatus: 200,
|
|
2096
2221
|
usage: {
|
|
@@ -2114,7 +2239,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2114
2239
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2115
2240
|
output: [],
|
|
2116
2241
|
latency: 0,
|
|
2117
|
-
baseURL: this.baseURL
|
|
2242
|
+
baseURL: this.baseURL,
|
|
2118
2243
|
params: body,
|
|
2119
2244
|
httpStatus: error?.status ? error.status : 500,
|
|
2120
2245
|
usage: {
|
|
@@ -2152,8 +2277,6 @@ class WrappedModels {
|
|
|
2152
2277
|
const {
|
|
2153
2278
|
posthogDistinctId,
|
|
2154
2279
|
posthogTraceId,
|
|
2155
|
-
posthogProperties,
|
|
2156
|
-
posthogGroups,
|
|
2157
2280
|
posthogCaptureImmediate,
|
|
2158
2281
|
...geminiParams
|
|
2159
2282
|
} = params;
|
|
@@ -2215,8 +2338,6 @@ class WrappedModels {
|
|
|
2215
2338
|
const {
|
|
2216
2339
|
posthogDistinctId,
|
|
2217
2340
|
posthogTraceId,
|
|
2218
|
-
posthogProperties,
|
|
2219
|
-
posthogGroups,
|
|
2220
2341
|
posthogCaptureImmediate,
|
|
2221
2342
|
...geminiParams
|
|
2222
2343
|
} = params;
|
|
@@ -2939,7 +3060,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2939
3060
|
this.debug = options.debug || false;
|
|
2940
3061
|
}
|
|
2941
3062
|
// ===== CALLBACK METHODS =====
|
|
2942
|
-
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata,
|
|
3063
|
+
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
|
|
2943
3064
|
this._logDebugEvent('on_chain_start', runId, parentRunId, {
|
|
2944
3065
|
inputs,
|
|
2945
3066
|
tags
|
|
@@ -2947,18 +3068,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2947
3068
|
this._setParentOfRun(runId, parentRunId);
|
|
2948
3069
|
this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
|
|
2949
3070
|
}
|
|
2950
|
-
handleChainEnd(outputs, runId, parentRunId, tags,
|
|
2951
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2952
|
-
kwargs) {
|
|
3071
|
+
handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
|
|
2953
3072
|
this._logDebugEvent('on_chain_end', runId, parentRunId, {
|
|
2954
3073
|
outputs,
|
|
2955
3074
|
tags
|
|
2956
3075
|
});
|
|
2957
3076
|
this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
|
|
2958
3077
|
}
|
|
2959
|
-
handleChainError(error, runId, parentRunId, tags,
|
|
2960
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2961
|
-
kwargs) {
|
|
3078
|
+
handleChainError(error, runId, parentRunId, tags, _kwargs) {
|
|
2962
3079
|
this._logDebugEvent('on_chain_error', runId, parentRunId, {
|
|
2963
3080
|
error,
|
|
2964
3081
|
tags
|
|
@@ -2983,18 +3100,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2983
3100
|
this._setParentOfRun(runId, parentRunId);
|
|
2984
3101
|
this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
|
|
2985
3102
|
}
|
|
2986
|
-
handleLLMEnd(output, runId, parentRunId, tags,
|
|
2987
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2988
|
-
extraParams) {
|
|
3103
|
+
handleLLMEnd(output, runId, parentRunId, tags, _extraParams) {
|
|
2989
3104
|
this._logDebugEvent('on_llm_end', runId, parentRunId, {
|
|
2990
3105
|
output,
|
|
2991
3106
|
tags
|
|
2992
3107
|
});
|
|
2993
3108
|
this._popRunAndCaptureGeneration(runId, parentRunId, output);
|
|
2994
3109
|
}
|
|
2995
|
-
handleLLMError(err, runId, parentRunId, tags,
|
|
2996
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2997
|
-
extraParams) {
|
|
3110
|
+
handleLLMError(err, runId, parentRunId, tags, _extraParams) {
|
|
2998
3111
|
this._logDebugEvent('on_llm_error', runId, parentRunId, {
|
|
2999
3112
|
err,
|
|
3000
3113
|
tags
|
|
@@ -3129,7 +3242,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3129
3242
|
_getTraceId(runId) {
|
|
3130
3243
|
return this.traceId ? String(this.traceId) : this._findRootRun(runId);
|
|
3131
3244
|
}
|
|
3132
|
-
_getParentRunId(traceId,
|
|
3245
|
+
_getParentRunId(traceId, _runId, parentRunId) {
|
|
3133
3246
|
// Replace the parent-run if not found in our stored parent tree.
|
|
3134
3247
|
if (parentRunId && !this.parentTree[parentRunId]) {
|
|
3135
3248
|
return traceId;
|
|
@@ -3154,6 +3267,8 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3154
3267
|
const eventName = parentRunId ? '$ai_span' : '$ai_trace';
|
|
3155
3268
|
const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
|
|
3156
3269
|
const eventProperties = {
|
|
3270
|
+
$ai_lib: 'posthog-ai',
|
|
3271
|
+
$ai_lib_version: version,
|
|
3157
3272
|
$ai_trace_id: traceId,
|
|
3158
3273
|
$ai_input_state: withPrivacyMode(this.client, this.privacyMode, run.input),
|
|
3159
3274
|
$ai_latency: latency,
|
|
@@ -3194,6 +3309,8 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3194
3309
|
_captureGeneration(traceId, runId, run, output, parentRunId) {
|
|
3195
3310
|
const latency = run.endTime ? (run.endTime - run.startTime) / 1000 : 0;
|
|
3196
3311
|
const eventProperties = {
|
|
3312
|
+
$ai_lib: 'posthog-ai',
|
|
3313
|
+
$ai_lib_version: version,
|
|
3197
3314
|
$ai_trace_id: traceId,
|
|
3198
3315
|
$ai_span_id: runId,
|
|
3199
3316
|
$ai_span_name: run.name,
|