@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.mjs
CHANGED
|
@@ -6,7 +6,7 @@ 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.
|
|
9
|
+
var version = "6.3.0";
|
|
10
10
|
|
|
11
11
|
// limit large outputs by truncating to 200kb (approx 200k bytes)
|
|
12
12
|
const MAX_OUTPUT_SIZE = 200000;
|
|
@@ -214,7 +214,7 @@ const truncate = str => {
|
|
|
214
214
|
}
|
|
215
215
|
const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
|
|
216
216
|
return `${truncatedBuffer.toString(STRING_FORMAT)}... [truncated]`;
|
|
217
|
-
} catch
|
|
217
|
+
} catch {
|
|
218
218
|
console.error('Error truncating, likely not a string');
|
|
219
219
|
return str;
|
|
220
220
|
}
|
|
@@ -247,6 +247,11 @@ const extractAvailableToolCalls = (provider, params) => {
|
|
|
247
247
|
}
|
|
248
248
|
return null;
|
|
249
249
|
};
|
|
250
|
+
var AIEvent;
|
|
251
|
+
(function (AIEvent) {
|
|
252
|
+
AIEvent["Generation"] = "$ai_generation";
|
|
253
|
+
AIEvent["Embedding"] = "$ai_embedding";
|
|
254
|
+
})(AIEvent || (AIEvent = {}));
|
|
250
255
|
function sanitizeValues(obj) {
|
|
251
256
|
if (obj === undefined || obj === null) {
|
|
252
257
|
return obj;
|
|
@@ -263,6 +268,7 @@ function sanitizeValues(obj) {
|
|
|
263
268
|
}
|
|
264
269
|
const sendEventToPosthog = async ({
|
|
265
270
|
client,
|
|
271
|
+
eventType = AIEvent.Generation,
|
|
266
272
|
distinctId,
|
|
267
273
|
traceId,
|
|
268
274
|
model,
|
|
@@ -324,7 +330,9 @@ const sendEventToPosthog = async ({
|
|
|
324
330
|
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
|
|
325
331
|
$ai_http_status: httpStatus,
|
|
326
332
|
$ai_input_tokens: usage.inputTokens ?? 0,
|
|
327
|
-
|
|
333
|
+
...(usage.outputTokens !== undefined ? {
|
|
334
|
+
$ai_output_tokens: usage.outputTokens
|
|
335
|
+
} : {}),
|
|
328
336
|
...additionalTokenValues,
|
|
329
337
|
$ai_latency: latency,
|
|
330
338
|
$ai_trace_id: traceId,
|
|
@@ -341,7 +349,7 @@ const sendEventToPosthog = async ({
|
|
|
341
349
|
};
|
|
342
350
|
const event = {
|
|
343
351
|
distinctId: distinctId ?? traceId,
|
|
344
|
-
event:
|
|
352
|
+
event: eventType,
|
|
345
353
|
properties,
|
|
346
354
|
groups: params.posthogGroups
|
|
347
355
|
};
|
|
@@ -554,6 +562,7 @@ const sanitizeLangChain = data => {
|
|
|
554
562
|
const Chat = OpenAI.Chat;
|
|
555
563
|
const Completions = Chat.Completions;
|
|
556
564
|
const Responses = OpenAI.Responses;
|
|
565
|
+
const Embeddings = OpenAI.Embeddings;
|
|
557
566
|
class PostHogOpenAI extends OpenAI {
|
|
558
567
|
constructor(config) {
|
|
559
568
|
const {
|
|
@@ -564,6 +573,7 @@ class PostHogOpenAI extends OpenAI {
|
|
|
564
573
|
this.phClient = posthog;
|
|
565
574
|
this.chat = new WrappedChat$1(this, this.phClient);
|
|
566
575
|
this.responses = new WrappedResponses$1(this, this.phClient);
|
|
576
|
+
this.embeddings = new WrappedEmbeddings$1(this, this.phClient);
|
|
567
577
|
}
|
|
568
578
|
}
|
|
569
579
|
let WrappedChat$1 = class WrappedChat extends Chat {
|
|
@@ -576,16 +586,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
576
586
|
constructor(client, phClient) {
|
|
577
587
|
super(client);
|
|
578
588
|
this.phClient = phClient;
|
|
589
|
+
this.baseURL = client.baseURL;
|
|
579
590
|
}
|
|
580
591
|
// --- Implementation Signature
|
|
581
592
|
create(body, options) {
|
|
582
593
|
const {
|
|
583
594
|
posthogDistinctId,
|
|
584
595
|
posthogTraceId,
|
|
585
|
-
posthogProperties,
|
|
586
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
587
|
-
posthogPrivacyMode = false,
|
|
588
|
-
posthogGroups,
|
|
589
596
|
posthogCaptureImmediate,
|
|
590
597
|
...openAIParams
|
|
591
598
|
} = body;
|
|
@@ -695,7 +702,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
695
702
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
696
703
|
output: formattedOutput,
|
|
697
704
|
latency,
|
|
698
|
-
baseURL: this.baseURL
|
|
705
|
+
baseURL: this.baseURL,
|
|
699
706
|
params: body,
|
|
700
707
|
httpStatus: 200,
|
|
701
708
|
usage,
|
|
@@ -713,7 +720,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
713
720
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
714
721
|
output: [],
|
|
715
722
|
latency: 0,
|
|
716
|
-
baseURL: this.baseURL
|
|
723
|
+
baseURL: this.baseURL,
|
|
717
724
|
params: body,
|
|
718
725
|
httpStatus,
|
|
719
726
|
usage: {
|
|
@@ -745,7 +752,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
745
752
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
746
753
|
output: formatResponseOpenAI(result),
|
|
747
754
|
latency,
|
|
748
|
-
baseURL: this.baseURL
|
|
755
|
+
baseURL: this.baseURL,
|
|
749
756
|
params: body,
|
|
750
757
|
httpStatus: 200,
|
|
751
758
|
usage: {
|
|
@@ -770,7 +777,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
770
777
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
771
778
|
output: [],
|
|
772
779
|
latency: 0,
|
|
773
|
-
baseURL: this.baseURL
|
|
780
|
+
baseURL: this.baseURL,
|
|
774
781
|
params: body,
|
|
775
782
|
httpStatus,
|
|
776
783
|
usage: {
|
|
@@ -791,16 +798,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
791
798
|
constructor(client, phClient) {
|
|
792
799
|
super(client);
|
|
793
800
|
this.phClient = phClient;
|
|
801
|
+
this.baseURL = client.baseURL;
|
|
794
802
|
}
|
|
795
803
|
// --- Implementation Signature
|
|
796
804
|
create(body, options) {
|
|
797
805
|
const {
|
|
798
806
|
posthogDistinctId,
|
|
799
807
|
posthogTraceId,
|
|
800
|
-
posthogProperties,
|
|
801
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
802
|
-
posthogPrivacyMode = false,
|
|
803
|
-
posthogGroups,
|
|
804
808
|
posthogCaptureImmediate,
|
|
805
809
|
...openAIParams
|
|
806
810
|
} = body;
|
|
@@ -843,7 +847,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
843
847
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
844
848
|
output: finalContent,
|
|
845
849
|
latency,
|
|
846
|
-
baseURL: this.baseURL
|
|
850
|
+
baseURL: this.baseURL,
|
|
847
851
|
params: body,
|
|
848
852
|
httpStatus: 200,
|
|
849
853
|
usage,
|
|
@@ -862,7 +866,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
862
866
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
863
867
|
output: [],
|
|
864
868
|
latency: 0,
|
|
865
|
-
baseURL: this.baseURL
|
|
869
|
+
baseURL: this.baseURL,
|
|
866
870
|
params: body,
|
|
867
871
|
httpStatus,
|
|
868
872
|
usage: {
|
|
@@ -896,7 +900,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
896
900
|
output: result.output
|
|
897
901
|
}),
|
|
898
902
|
latency,
|
|
899
|
-
baseURL: this.baseURL
|
|
903
|
+
baseURL: this.baseURL,
|
|
900
904
|
params: body,
|
|
901
905
|
httpStatus: 200,
|
|
902
906
|
usage: {
|
|
@@ -922,7 +926,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
922
926
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
923
927
|
output: [],
|
|
924
928
|
latency: 0,
|
|
925
|
-
baseURL: this.baseURL
|
|
929
|
+
baseURL: this.baseURL,
|
|
926
930
|
params: body,
|
|
927
931
|
httpStatus,
|
|
928
932
|
usage: {
|
|
@@ -942,10 +946,6 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
942
946
|
const {
|
|
943
947
|
posthogDistinctId,
|
|
944
948
|
posthogTraceId,
|
|
945
|
-
posthogProperties,
|
|
946
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
947
|
-
posthogPrivacyMode = false,
|
|
948
|
-
posthogGroups,
|
|
949
949
|
posthogCaptureImmediate,
|
|
950
950
|
...openAIParams
|
|
951
951
|
} = body;
|
|
@@ -970,7 +970,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
970
970
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
971
971
|
output: result.output,
|
|
972
972
|
latency,
|
|
973
|
-
baseURL: this.baseURL
|
|
973
|
+
baseURL: this.baseURL,
|
|
974
974
|
params: body,
|
|
975
975
|
httpStatus: 200,
|
|
976
976
|
usage: {
|
|
@@ -994,7 +994,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
994
994
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
995
995
|
output: [],
|
|
996
996
|
latency: 0,
|
|
997
|
-
baseURL: this.baseURL
|
|
997
|
+
baseURL: this.baseURL,
|
|
998
998
|
params: body,
|
|
999
999
|
httpStatus,
|
|
1000
1000
|
usage: {
|
|
@@ -1014,6 +1014,73 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1014
1014
|
}
|
|
1015
1015
|
}
|
|
1016
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
|
+
};
|
|
1017
1084
|
|
|
1018
1085
|
class PostHogAzureOpenAI extends AzureOpenAI {
|
|
1019
1086
|
constructor(config) {
|
|
@@ -1024,6 +1091,7 @@ class PostHogAzureOpenAI extends AzureOpenAI {
|
|
|
1024
1091
|
super(openAIConfig);
|
|
1025
1092
|
this.phClient = posthog;
|
|
1026
1093
|
this.chat = new WrappedChat(this, this.phClient);
|
|
1094
|
+
this.embeddings = new WrappedEmbeddings(this, this.phClient);
|
|
1027
1095
|
}
|
|
1028
1096
|
}
|
|
1029
1097
|
class WrappedChat extends AzureOpenAI.Chat {
|
|
@@ -1036,16 +1104,13 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1036
1104
|
constructor(client, phClient) {
|
|
1037
1105
|
super(client);
|
|
1038
1106
|
this.phClient = phClient;
|
|
1107
|
+
this.baseURL = client.baseURL;
|
|
1039
1108
|
}
|
|
1040
1109
|
// --- Implementation Signature
|
|
1041
1110
|
create(body, options) {
|
|
1042
1111
|
const {
|
|
1043
1112
|
posthogDistinctId,
|
|
1044
1113
|
posthogTraceId,
|
|
1045
|
-
posthogProperties,
|
|
1046
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1047
|
-
posthogPrivacyMode = false,
|
|
1048
|
-
posthogGroups,
|
|
1049
1114
|
posthogCaptureImmediate,
|
|
1050
1115
|
...openAIParams
|
|
1051
1116
|
} = body;
|
|
@@ -1154,7 +1219,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1154
1219
|
input: openAIParams.messages,
|
|
1155
1220
|
output: formattedOutput,
|
|
1156
1221
|
latency,
|
|
1157
|
-
baseURL: this.baseURL
|
|
1222
|
+
baseURL: this.baseURL,
|
|
1158
1223
|
params: body,
|
|
1159
1224
|
httpStatus: 200,
|
|
1160
1225
|
usage,
|
|
@@ -1171,7 +1236,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1171
1236
|
input: openAIParams.messages,
|
|
1172
1237
|
output: [],
|
|
1173
1238
|
latency: 0,
|
|
1174
|
-
baseURL: this.baseURL
|
|
1239
|
+
baseURL: this.baseURL,
|
|
1175
1240
|
params: body,
|
|
1176
1241
|
httpStatus,
|
|
1177
1242
|
usage: {
|
|
@@ -1202,7 +1267,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1202
1267
|
input: openAIParams.messages,
|
|
1203
1268
|
output: formatResponseOpenAI(result),
|
|
1204
1269
|
latency,
|
|
1205
|
-
baseURL: this.baseURL
|
|
1270
|
+
baseURL: this.baseURL,
|
|
1206
1271
|
params: body,
|
|
1207
1272
|
httpStatus: 200,
|
|
1208
1273
|
usage: {
|
|
@@ -1226,7 +1291,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1226
1291
|
input: openAIParams.messages,
|
|
1227
1292
|
output: [],
|
|
1228
1293
|
latency: 0,
|
|
1229
|
-
baseURL: this.baseURL
|
|
1294
|
+
baseURL: this.baseURL,
|
|
1230
1295
|
params: body,
|
|
1231
1296
|
httpStatus,
|
|
1232
1297
|
usage: {
|
|
@@ -1247,16 +1312,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1247
1312
|
constructor(client, phClient) {
|
|
1248
1313
|
super(client);
|
|
1249
1314
|
this.phClient = phClient;
|
|
1315
|
+
this.baseURL = client.baseURL;
|
|
1250
1316
|
}
|
|
1251
1317
|
// --- Implementation Signature
|
|
1252
1318
|
create(body, options) {
|
|
1253
1319
|
const {
|
|
1254
1320
|
posthogDistinctId,
|
|
1255
1321
|
posthogTraceId,
|
|
1256
|
-
posthogProperties,
|
|
1257
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1258
|
-
posthogPrivacyMode = false,
|
|
1259
|
-
posthogGroups,
|
|
1260
1322
|
posthogCaptureImmediate,
|
|
1261
1323
|
...openAIParams
|
|
1262
1324
|
} = body;
|
|
@@ -1298,7 +1360,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1298
1360
|
input: openAIParams.input,
|
|
1299
1361
|
output: finalContent,
|
|
1300
1362
|
latency,
|
|
1301
|
-
baseURL: this.baseURL
|
|
1363
|
+
baseURL: this.baseURL,
|
|
1302
1364
|
params: body,
|
|
1303
1365
|
httpStatus: 200,
|
|
1304
1366
|
usage,
|
|
@@ -1316,7 +1378,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1316
1378
|
input: openAIParams.input,
|
|
1317
1379
|
output: [],
|
|
1318
1380
|
latency: 0,
|
|
1319
|
-
baseURL: this.baseURL
|
|
1381
|
+
baseURL: this.baseURL,
|
|
1320
1382
|
params: body,
|
|
1321
1383
|
httpStatus,
|
|
1322
1384
|
usage: {
|
|
@@ -1347,7 +1409,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1347
1409
|
input: openAIParams.input,
|
|
1348
1410
|
output: result.output,
|
|
1349
1411
|
latency,
|
|
1350
|
-
baseURL: this.baseURL
|
|
1412
|
+
baseURL: this.baseURL,
|
|
1351
1413
|
params: body,
|
|
1352
1414
|
httpStatus: 200,
|
|
1353
1415
|
usage: {
|
|
@@ -1372,7 +1434,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1372
1434
|
input: openAIParams.input,
|
|
1373
1435
|
output: [],
|
|
1374
1436
|
latency: 0,
|
|
1375
|
-
baseURL: this.baseURL
|
|
1437
|
+
baseURL: this.baseURL,
|
|
1376
1438
|
params: body,
|
|
1377
1439
|
httpStatus,
|
|
1378
1440
|
usage: {
|
|
@@ -1392,10 +1454,6 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1392
1454
|
const {
|
|
1393
1455
|
posthogDistinctId,
|
|
1394
1456
|
posthogTraceId,
|
|
1395
|
-
posthogProperties,
|
|
1396
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1397
|
-
posthogPrivacyMode = false,
|
|
1398
|
-
posthogGroups,
|
|
1399
1457
|
posthogCaptureImmediate,
|
|
1400
1458
|
...openAIParams
|
|
1401
1459
|
} = body;
|
|
@@ -1414,7 +1472,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1414
1472
|
input: openAIParams.input,
|
|
1415
1473
|
output: result.output,
|
|
1416
1474
|
latency,
|
|
1417
|
-
baseURL: this.baseURL
|
|
1475
|
+
baseURL: this.baseURL,
|
|
1418
1476
|
params: body,
|
|
1419
1477
|
httpStatus: 200,
|
|
1420
1478
|
usage: {
|
|
@@ -1437,7 +1495,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1437
1495
|
input: openAIParams.input,
|
|
1438
1496
|
output: [],
|
|
1439
1497
|
latency: 0,
|
|
1440
|
-
baseURL: this.baseURL
|
|
1498
|
+
baseURL: this.baseURL,
|
|
1441
1499
|
params: body,
|
|
1442
1500
|
httpStatus: error?.status ? error.status : 500,
|
|
1443
1501
|
usage: {
|
|
@@ -1453,6 +1511,72 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1453
1511
|
return wrappedPromise;
|
|
1454
1512
|
}
|
|
1455
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
|
+
}
|
|
1456
1580
|
|
|
1457
1581
|
const mapVercelParams = params => {
|
|
1458
1582
|
return {
|
|
@@ -1640,7 +1764,7 @@ const mapVercelOutput = result => {
|
|
|
1640
1764
|
content: truncate(jsonOutput),
|
|
1641
1765
|
role: 'assistant'
|
|
1642
1766
|
}];
|
|
1643
|
-
} catch
|
|
1767
|
+
} catch {
|
|
1644
1768
|
console.error('Error stringifying output');
|
|
1645
1769
|
return [];
|
|
1646
1770
|
}
|
|
@@ -1915,15 +2039,12 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1915
2039
|
constructor(parentClient, phClient) {
|
|
1916
2040
|
super(parentClient);
|
|
1917
2041
|
this.phClient = phClient;
|
|
2042
|
+
this.baseURL = parentClient.baseURL;
|
|
1918
2043
|
}
|
|
1919
2044
|
create(body, options) {
|
|
1920
2045
|
const {
|
|
1921
2046
|
posthogDistinctId,
|
|
1922
2047
|
posthogTraceId,
|
|
1923
|
-
posthogProperties,
|
|
1924
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1925
|
-
posthogPrivacyMode = false,
|
|
1926
|
-
posthogGroups,
|
|
1927
2048
|
posthogCaptureImmediate,
|
|
1928
2049
|
...anthropicParams
|
|
1929
2050
|
} = body;
|
|
@@ -1975,7 +2096,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1975
2096
|
// Handle text delta events
|
|
1976
2097
|
if ('delta' in chunk) {
|
|
1977
2098
|
if ('text' in chunk.delta) {
|
|
1978
|
-
const delta = chunk
|
|
2099
|
+
const delta = chunk.delta.text;
|
|
1979
2100
|
accumulatedContent += delta;
|
|
1980
2101
|
if (currentTextBlock) {
|
|
1981
2102
|
currentTextBlock.text += delta;
|
|
@@ -2044,7 +2165,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2044
2165
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
|
|
2045
2166
|
output: formattedOutput,
|
|
2046
2167
|
latency,
|
|
2047
|
-
baseURL: this.baseURL
|
|
2168
|
+
baseURL: this.baseURL,
|
|
2048
2169
|
params: body,
|
|
2049
2170
|
httpStatus: 200,
|
|
2050
2171
|
usage,
|
|
@@ -2062,7 +2183,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2062
2183
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2063
2184
|
output: [],
|
|
2064
2185
|
latency: 0,
|
|
2065
|
-
baseURL: this.baseURL
|
|
2186
|
+
baseURL: this.baseURL,
|
|
2066
2187
|
params: body,
|
|
2067
2188
|
httpStatus: error?.status ? error.status : 500,
|
|
2068
2189
|
usage: {
|
|
@@ -2094,7 +2215,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2094
2215
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2095
2216
|
output: formatResponseAnthropic(result),
|
|
2096
2217
|
latency,
|
|
2097
|
-
baseURL: this.baseURL
|
|
2218
|
+
baseURL: this.baseURL,
|
|
2098
2219
|
params: body,
|
|
2099
2220
|
httpStatus: 200,
|
|
2100
2221
|
usage: {
|
|
@@ -2118,7 +2239,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2118
2239
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2119
2240
|
output: [],
|
|
2120
2241
|
latency: 0,
|
|
2121
|
-
baseURL: this.baseURL
|
|
2242
|
+
baseURL: this.baseURL,
|
|
2122
2243
|
params: body,
|
|
2123
2244
|
httpStatus: error?.status ? error.status : 500,
|
|
2124
2245
|
usage: {
|
|
@@ -2156,8 +2277,6 @@ class WrappedModels {
|
|
|
2156
2277
|
const {
|
|
2157
2278
|
posthogDistinctId,
|
|
2158
2279
|
posthogTraceId,
|
|
2159
|
-
posthogProperties,
|
|
2160
|
-
posthogGroups,
|
|
2161
2280
|
posthogCaptureImmediate,
|
|
2162
2281
|
...geminiParams
|
|
2163
2282
|
} = params;
|
|
@@ -2219,8 +2338,6 @@ class WrappedModels {
|
|
|
2219
2338
|
const {
|
|
2220
2339
|
posthogDistinctId,
|
|
2221
2340
|
posthogTraceId,
|
|
2222
|
-
posthogProperties,
|
|
2223
|
-
posthogGroups,
|
|
2224
2341
|
posthogCaptureImmediate,
|
|
2225
2342
|
...geminiParams
|
|
2226
2343
|
} = params;
|
|
@@ -2943,7 +3060,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2943
3060
|
this.debug = options.debug || false;
|
|
2944
3061
|
}
|
|
2945
3062
|
// ===== CALLBACK METHODS =====
|
|
2946
|
-
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata,
|
|
3063
|
+
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
|
|
2947
3064
|
this._logDebugEvent('on_chain_start', runId, parentRunId, {
|
|
2948
3065
|
inputs,
|
|
2949
3066
|
tags
|
|
@@ -2951,18 +3068,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2951
3068
|
this._setParentOfRun(runId, parentRunId);
|
|
2952
3069
|
this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
|
|
2953
3070
|
}
|
|
2954
|
-
handleChainEnd(outputs, runId, parentRunId, tags,
|
|
2955
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2956
|
-
kwargs) {
|
|
3071
|
+
handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
|
|
2957
3072
|
this._logDebugEvent('on_chain_end', runId, parentRunId, {
|
|
2958
3073
|
outputs,
|
|
2959
3074
|
tags
|
|
2960
3075
|
});
|
|
2961
3076
|
this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
|
|
2962
3077
|
}
|
|
2963
|
-
handleChainError(error, runId, parentRunId, tags,
|
|
2964
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2965
|
-
kwargs) {
|
|
3078
|
+
handleChainError(error, runId, parentRunId, tags, _kwargs) {
|
|
2966
3079
|
this._logDebugEvent('on_chain_error', runId, parentRunId, {
|
|
2967
3080
|
error,
|
|
2968
3081
|
tags
|
|
@@ -2987,18 +3100,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2987
3100
|
this._setParentOfRun(runId, parentRunId);
|
|
2988
3101
|
this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
|
|
2989
3102
|
}
|
|
2990
|
-
handleLLMEnd(output, runId, parentRunId, tags,
|
|
2991
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2992
|
-
extraParams) {
|
|
3103
|
+
handleLLMEnd(output, runId, parentRunId, tags, _extraParams) {
|
|
2993
3104
|
this._logDebugEvent('on_llm_end', runId, parentRunId, {
|
|
2994
3105
|
output,
|
|
2995
3106
|
tags
|
|
2996
3107
|
});
|
|
2997
3108
|
this._popRunAndCaptureGeneration(runId, parentRunId, output);
|
|
2998
3109
|
}
|
|
2999
|
-
handleLLMError(err, runId, parentRunId, tags,
|
|
3000
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3001
|
-
extraParams) {
|
|
3110
|
+
handleLLMError(err, runId, parentRunId, tags, _extraParams) {
|
|
3002
3111
|
this._logDebugEvent('on_llm_error', runId, parentRunId, {
|
|
3003
3112
|
err,
|
|
3004
3113
|
tags
|
|
@@ -3133,7 +3242,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3133
3242
|
_getTraceId(runId) {
|
|
3134
3243
|
return this.traceId ? String(this.traceId) : this._findRootRun(runId);
|
|
3135
3244
|
}
|
|
3136
|
-
_getParentRunId(traceId,
|
|
3245
|
+
_getParentRunId(traceId, _runId, parentRunId) {
|
|
3137
3246
|
// Replace the parent-run if not found in our stored parent tree.
|
|
3138
3247
|
if (parentRunId && !this.parentTree[parentRunId]) {
|
|
3139
3248
|
return traceId;
|