@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.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.1";
|
|
10
10
|
|
|
11
11
|
// limit large outputs by truncating to 200kb (approx 200k bytes)
|
|
12
12
|
const MAX_OUTPUT_SIZE = 200000;
|
|
@@ -206,18 +206,43 @@ const mergeSystemPrompt = (params, provider) => {
|
|
|
206
206
|
const withPrivacyMode = (client, privacyMode, input) => {
|
|
207
207
|
return client.privacy_mode || privacyMode ? null : input;
|
|
208
208
|
};
|
|
209
|
-
|
|
209
|
+
function toSafeString(input) {
|
|
210
|
+
if (input === undefined || input === null) {
|
|
211
|
+
return '';
|
|
212
|
+
}
|
|
213
|
+
if (typeof input === 'string') {
|
|
214
|
+
return input;
|
|
215
|
+
}
|
|
210
216
|
try {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
217
|
+
return JSON.stringify(input);
|
|
218
|
+
} catch {
|
|
219
|
+
console.warn('Failed to stringify input', input);
|
|
220
|
+
return '';
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const truncate = input => {
|
|
224
|
+
const str = toSafeString(input);
|
|
225
|
+
if (str === '') {
|
|
226
|
+
return '';
|
|
227
|
+
}
|
|
228
|
+
// Check if we need to truncate and ensure STRING_FORMAT is respected
|
|
229
|
+
const encoder = new TextEncoder();
|
|
230
|
+
const buffer = encoder.encode(str);
|
|
231
|
+
if (buffer.length <= MAX_OUTPUT_SIZE) {
|
|
232
|
+
// Ensure STRING_FORMAT is respected
|
|
233
|
+
return new TextDecoder(STRING_FORMAT).decode(buffer);
|
|
234
|
+
}
|
|
235
|
+
// Truncate the buffer and ensure a valid string is returned
|
|
236
|
+
const truncatedBuffer = buffer.slice(0, MAX_OUTPUT_SIZE);
|
|
237
|
+
// fatal: false means we get U+FFFD at the end if truncation broke the encoding
|
|
238
|
+
const decoder = new TextDecoder(STRING_FORMAT, {
|
|
239
|
+
fatal: false
|
|
240
|
+
});
|
|
241
|
+
let truncatedStr = decoder.decode(truncatedBuffer);
|
|
242
|
+
if (truncatedStr.endsWith('\uFFFD')) {
|
|
243
|
+
truncatedStr = truncatedStr.slice(0, -1);
|
|
220
244
|
}
|
|
245
|
+
return `${truncatedStr}... [truncated]`;
|
|
221
246
|
};
|
|
222
247
|
/**
|
|
223
248
|
* Extract available tool calls from the request parameters.
|
|
@@ -247,6 +272,11 @@ const extractAvailableToolCalls = (provider, params) => {
|
|
|
247
272
|
}
|
|
248
273
|
return null;
|
|
249
274
|
};
|
|
275
|
+
var AIEvent;
|
|
276
|
+
(function (AIEvent) {
|
|
277
|
+
AIEvent["Generation"] = "$ai_generation";
|
|
278
|
+
AIEvent["Embedding"] = "$ai_embedding";
|
|
279
|
+
})(AIEvent || (AIEvent = {}));
|
|
250
280
|
function sanitizeValues(obj) {
|
|
251
281
|
if (obj === undefined || obj === null) {
|
|
252
282
|
return obj;
|
|
@@ -263,6 +293,7 @@ function sanitizeValues(obj) {
|
|
|
263
293
|
}
|
|
264
294
|
const sendEventToPosthog = async ({
|
|
265
295
|
client,
|
|
296
|
+
eventType = AIEvent.Generation,
|
|
266
297
|
distinctId,
|
|
267
298
|
traceId,
|
|
268
299
|
model,
|
|
@@ -324,7 +355,9 @@ const sendEventToPosthog = async ({
|
|
|
324
355
|
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
|
|
325
356
|
$ai_http_status: httpStatus,
|
|
326
357
|
$ai_input_tokens: usage.inputTokens ?? 0,
|
|
327
|
-
|
|
358
|
+
...(usage.outputTokens !== undefined ? {
|
|
359
|
+
$ai_output_tokens: usage.outputTokens
|
|
360
|
+
} : {}),
|
|
328
361
|
...additionalTokenValues,
|
|
329
362
|
$ai_latency: latency,
|
|
330
363
|
$ai_trace_id: traceId,
|
|
@@ -341,7 +374,7 @@ const sendEventToPosthog = async ({
|
|
|
341
374
|
};
|
|
342
375
|
const event = {
|
|
343
376
|
distinctId: distinctId ?? traceId,
|
|
344
|
-
event:
|
|
377
|
+
event: eventType,
|
|
345
378
|
properties,
|
|
346
379
|
groups: params.posthogGroups
|
|
347
380
|
};
|
|
@@ -554,6 +587,7 @@ const sanitizeLangChain = data => {
|
|
|
554
587
|
const Chat = OpenAI.Chat;
|
|
555
588
|
const Completions = Chat.Completions;
|
|
556
589
|
const Responses = OpenAI.Responses;
|
|
590
|
+
const Embeddings = OpenAI.Embeddings;
|
|
557
591
|
class PostHogOpenAI extends OpenAI {
|
|
558
592
|
constructor(config) {
|
|
559
593
|
const {
|
|
@@ -564,6 +598,7 @@ class PostHogOpenAI extends OpenAI {
|
|
|
564
598
|
this.phClient = posthog;
|
|
565
599
|
this.chat = new WrappedChat$1(this, this.phClient);
|
|
566
600
|
this.responses = new WrappedResponses$1(this, this.phClient);
|
|
601
|
+
this.embeddings = new WrappedEmbeddings$1(this, this.phClient);
|
|
567
602
|
}
|
|
568
603
|
}
|
|
569
604
|
let WrappedChat$1 = class WrappedChat extends Chat {
|
|
@@ -576,16 +611,13 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
576
611
|
constructor(client, phClient) {
|
|
577
612
|
super(client);
|
|
578
613
|
this.phClient = phClient;
|
|
614
|
+
this.baseURL = client.baseURL;
|
|
579
615
|
}
|
|
580
616
|
// --- Implementation Signature
|
|
581
617
|
create(body, options) {
|
|
582
618
|
const {
|
|
583
619
|
posthogDistinctId,
|
|
584
620
|
posthogTraceId,
|
|
585
|
-
posthogProperties,
|
|
586
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
587
|
-
posthogPrivacyMode = false,
|
|
588
|
-
posthogGroups,
|
|
589
621
|
posthogCaptureImmediate,
|
|
590
622
|
...openAIParams
|
|
591
623
|
} = body;
|
|
@@ -695,7 +727,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
695
727
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
696
728
|
output: formattedOutput,
|
|
697
729
|
latency,
|
|
698
|
-
baseURL: this.baseURL
|
|
730
|
+
baseURL: this.baseURL,
|
|
699
731
|
params: body,
|
|
700
732
|
httpStatus: 200,
|
|
701
733
|
usage,
|
|
@@ -713,7 +745,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
713
745
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
714
746
|
output: [],
|
|
715
747
|
latency: 0,
|
|
716
|
-
baseURL: this.baseURL
|
|
748
|
+
baseURL: this.baseURL,
|
|
717
749
|
params: body,
|
|
718
750
|
httpStatus,
|
|
719
751
|
usage: {
|
|
@@ -745,7 +777,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
745
777
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
746
778
|
output: formatResponseOpenAI(result),
|
|
747
779
|
latency,
|
|
748
|
-
baseURL: this.baseURL
|
|
780
|
+
baseURL: this.baseURL,
|
|
749
781
|
params: body,
|
|
750
782
|
httpStatus: 200,
|
|
751
783
|
usage: {
|
|
@@ -770,7 +802,7 @@ let WrappedCompletions$1 = class WrappedCompletions extends Completions {
|
|
|
770
802
|
input: sanitizeOpenAI(openAIParams.messages),
|
|
771
803
|
output: [],
|
|
772
804
|
latency: 0,
|
|
773
|
-
baseURL: this.baseURL
|
|
805
|
+
baseURL: this.baseURL,
|
|
774
806
|
params: body,
|
|
775
807
|
httpStatus,
|
|
776
808
|
usage: {
|
|
@@ -791,16 +823,13 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
791
823
|
constructor(client, phClient) {
|
|
792
824
|
super(client);
|
|
793
825
|
this.phClient = phClient;
|
|
826
|
+
this.baseURL = client.baseURL;
|
|
794
827
|
}
|
|
795
828
|
// --- Implementation Signature
|
|
796
829
|
create(body, options) {
|
|
797
830
|
const {
|
|
798
831
|
posthogDistinctId,
|
|
799
832
|
posthogTraceId,
|
|
800
|
-
posthogProperties,
|
|
801
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
802
|
-
posthogPrivacyMode = false,
|
|
803
|
-
posthogGroups,
|
|
804
833
|
posthogCaptureImmediate,
|
|
805
834
|
...openAIParams
|
|
806
835
|
} = body;
|
|
@@ -843,7 +872,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
843
872
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
844
873
|
output: finalContent,
|
|
845
874
|
latency,
|
|
846
|
-
baseURL: this.baseURL
|
|
875
|
+
baseURL: this.baseURL,
|
|
847
876
|
params: body,
|
|
848
877
|
httpStatus: 200,
|
|
849
878
|
usage,
|
|
@@ -862,7 +891,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
862
891
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
863
892
|
output: [],
|
|
864
893
|
latency: 0,
|
|
865
|
-
baseURL: this.baseURL
|
|
894
|
+
baseURL: this.baseURL,
|
|
866
895
|
params: body,
|
|
867
896
|
httpStatus,
|
|
868
897
|
usage: {
|
|
@@ -896,7 +925,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
896
925
|
output: result.output
|
|
897
926
|
}),
|
|
898
927
|
latency,
|
|
899
|
-
baseURL: this.baseURL
|
|
928
|
+
baseURL: this.baseURL,
|
|
900
929
|
params: body,
|
|
901
930
|
httpStatus: 200,
|
|
902
931
|
usage: {
|
|
@@ -922,7 +951,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
922
951
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
923
952
|
output: [],
|
|
924
953
|
latency: 0,
|
|
925
|
-
baseURL: this.baseURL
|
|
954
|
+
baseURL: this.baseURL,
|
|
926
955
|
params: body,
|
|
927
956
|
httpStatus,
|
|
928
957
|
usage: {
|
|
@@ -942,10 +971,6 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
942
971
|
const {
|
|
943
972
|
posthogDistinctId,
|
|
944
973
|
posthogTraceId,
|
|
945
|
-
posthogProperties,
|
|
946
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
947
|
-
posthogPrivacyMode = false,
|
|
948
|
-
posthogGroups,
|
|
949
974
|
posthogCaptureImmediate,
|
|
950
975
|
...openAIParams
|
|
951
976
|
} = body;
|
|
@@ -970,7 +995,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
970
995
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
971
996
|
output: result.output,
|
|
972
997
|
latency,
|
|
973
|
-
baseURL: this.baseURL
|
|
998
|
+
baseURL: this.baseURL,
|
|
974
999
|
params: body,
|
|
975
1000
|
httpStatus: 200,
|
|
976
1001
|
usage: {
|
|
@@ -994,7 +1019,7 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
994
1019
|
input: sanitizeOpenAIResponse(openAIParams.input),
|
|
995
1020
|
output: [],
|
|
996
1021
|
latency: 0,
|
|
997
|
-
baseURL: this.baseURL
|
|
1022
|
+
baseURL: this.baseURL,
|
|
998
1023
|
params: body,
|
|
999
1024
|
httpStatus,
|
|
1000
1025
|
usage: {
|
|
@@ -1014,6 +1039,73 @@ let WrappedResponses$1 = class WrappedResponses extends Responses {
|
|
|
1014
1039
|
}
|
|
1015
1040
|
}
|
|
1016
1041
|
};
|
|
1042
|
+
let WrappedEmbeddings$1 = class WrappedEmbeddings extends Embeddings {
|
|
1043
|
+
constructor(client, phClient) {
|
|
1044
|
+
super(client);
|
|
1045
|
+
this.phClient = phClient;
|
|
1046
|
+
this.baseURL = client.baseURL;
|
|
1047
|
+
}
|
|
1048
|
+
create(body, options) {
|
|
1049
|
+
const {
|
|
1050
|
+
posthogDistinctId,
|
|
1051
|
+
posthogTraceId,
|
|
1052
|
+
posthogPrivacyMode = false,
|
|
1053
|
+
posthogCaptureImmediate,
|
|
1054
|
+
...openAIParams
|
|
1055
|
+
} = body;
|
|
1056
|
+
const traceId = posthogTraceId ?? v4();
|
|
1057
|
+
const startTime = Date.now();
|
|
1058
|
+
const parentPromise = super.create(openAIParams, options);
|
|
1059
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1060
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1061
|
+
await sendEventToPosthog({
|
|
1062
|
+
client: this.phClient,
|
|
1063
|
+
eventType: AIEvent.Embedding,
|
|
1064
|
+
distinctId: posthogDistinctId,
|
|
1065
|
+
traceId,
|
|
1066
|
+
model: openAIParams.model,
|
|
1067
|
+
provider: 'openai',
|
|
1068
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1069
|
+
output: null,
|
|
1070
|
+
// Embeddings don't have output content
|
|
1071
|
+
latency,
|
|
1072
|
+
baseURL: this.baseURL,
|
|
1073
|
+
params: body,
|
|
1074
|
+
httpStatus: 200,
|
|
1075
|
+
usage: {
|
|
1076
|
+
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1077
|
+
},
|
|
1078
|
+
captureImmediate: posthogCaptureImmediate
|
|
1079
|
+
});
|
|
1080
|
+
return result;
|
|
1081
|
+
}, async error => {
|
|
1082
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1083
|
+
await sendEventToPosthog({
|
|
1084
|
+
client: this.phClient,
|
|
1085
|
+
eventType: AIEvent.Embedding,
|
|
1086
|
+
distinctId: posthogDistinctId,
|
|
1087
|
+
traceId,
|
|
1088
|
+
model: openAIParams.model,
|
|
1089
|
+
provider: 'openai',
|
|
1090
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1091
|
+
output: null,
|
|
1092
|
+
// Embeddings don't have output content
|
|
1093
|
+
latency: 0,
|
|
1094
|
+
baseURL: this.baseURL,
|
|
1095
|
+
params: body,
|
|
1096
|
+
httpStatus,
|
|
1097
|
+
usage: {
|
|
1098
|
+
inputTokens: 0
|
|
1099
|
+
},
|
|
1100
|
+
isError: true,
|
|
1101
|
+
error: JSON.stringify(error),
|
|
1102
|
+
captureImmediate: posthogCaptureImmediate
|
|
1103
|
+
});
|
|
1104
|
+
throw error;
|
|
1105
|
+
});
|
|
1106
|
+
return wrappedPromise;
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1017
1109
|
|
|
1018
1110
|
class PostHogAzureOpenAI extends AzureOpenAI {
|
|
1019
1111
|
constructor(config) {
|
|
@@ -1024,6 +1116,7 @@ class PostHogAzureOpenAI extends AzureOpenAI {
|
|
|
1024
1116
|
super(openAIConfig);
|
|
1025
1117
|
this.phClient = posthog;
|
|
1026
1118
|
this.chat = new WrappedChat(this, this.phClient);
|
|
1119
|
+
this.embeddings = new WrappedEmbeddings(this, this.phClient);
|
|
1027
1120
|
}
|
|
1028
1121
|
}
|
|
1029
1122
|
class WrappedChat extends AzureOpenAI.Chat {
|
|
@@ -1036,16 +1129,13 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1036
1129
|
constructor(client, phClient) {
|
|
1037
1130
|
super(client);
|
|
1038
1131
|
this.phClient = phClient;
|
|
1132
|
+
this.baseURL = client.baseURL;
|
|
1039
1133
|
}
|
|
1040
1134
|
// --- Implementation Signature
|
|
1041
1135
|
create(body, options) {
|
|
1042
1136
|
const {
|
|
1043
1137
|
posthogDistinctId,
|
|
1044
1138
|
posthogTraceId,
|
|
1045
|
-
posthogProperties,
|
|
1046
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1047
|
-
posthogPrivacyMode = false,
|
|
1048
|
-
posthogGroups,
|
|
1049
1139
|
posthogCaptureImmediate,
|
|
1050
1140
|
...openAIParams
|
|
1051
1141
|
} = body;
|
|
@@ -1154,7 +1244,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1154
1244
|
input: openAIParams.messages,
|
|
1155
1245
|
output: formattedOutput,
|
|
1156
1246
|
latency,
|
|
1157
|
-
baseURL: this.baseURL
|
|
1247
|
+
baseURL: this.baseURL,
|
|
1158
1248
|
params: body,
|
|
1159
1249
|
httpStatus: 200,
|
|
1160
1250
|
usage,
|
|
@@ -1171,7 +1261,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1171
1261
|
input: openAIParams.messages,
|
|
1172
1262
|
output: [],
|
|
1173
1263
|
latency: 0,
|
|
1174
|
-
baseURL: this.baseURL
|
|
1264
|
+
baseURL: this.baseURL,
|
|
1175
1265
|
params: body,
|
|
1176
1266
|
httpStatus,
|
|
1177
1267
|
usage: {
|
|
@@ -1202,7 +1292,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1202
1292
|
input: openAIParams.messages,
|
|
1203
1293
|
output: formatResponseOpenAI(result),
|
|
1204
1294
|
latency,
|
|
1205
|
-
baseURL: this.baseURL
|
|
1295
|
+
baseURL: this.baseURL,
|
|
1206
1296
|
params: body,
|
|
1207
1297
|
httpStatus: 200,
|
|
1208
1298
|
usage: {
|
|
@@ -1226,7 +1316,7 @@ class WrappedCompletions extends AzureOpenAI.Chat.Completions {
|
|
|
1226
1316
|
input: openAIParams.messages,
|
|
1227
1317
|
output: [],
|
|
1228
1318
|
latency: 0,
|
|
1229
|
-
baseURL: this.baseURL
|
|
1319
|
+
baseURL: this.baseURL,
|
|
1230
1320
|
params: body,
|
|
1231
1321
|
httpStatus,
|
|
1232
1322
|
usage: {
|
|
@@ -1247,16 +1337,13 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1247
1337
|
constructor(client, phClient) {
|
|
1248
1338
|
super(client);
|
|
1249
1339
|
this.phClient = phClient;
|
|
1340
|
+
this.baseURL = client.baseURL;
|
|
1250
1341
|
}
|
|
1251
1342
|
// --- Implementation Signature
|
|
1252
1343
|
create(body, options) {
|
|
1253
1344
|
const {
|
|
1254
1345
|
posthogDistinctId,
|
|
1255
1346
|
posthogTraceId,
|
|
1256
|
-
posthogProperties,
|
|
1257
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1258
|
-
posthogPrivacyMode = false,
|
|
1259
|
-
posthogGroups,
|
|
1260
1347
|
posthogCaptureImmediate,
|
|
1261
1348
|
...openAIParams
|
|
1262
1349
|
} = body;
|
|
@@ -1298,7 +1385,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1298
1385
|
input: openAIParams.input,
|
|
1299
1386
|
output: finalContent,
|
|
1300
1387
|
latency,
|
|
1301
|
-
baseURL: this.baseURL
|
|
1388
|
+
baseURL: this.baseURL,
|
|
1302
1389
|
params: body,
|
|
1303
1390
|
httpStatus: 200,
|
|
1304
1391
|
usage,
|
|
@@ -1316,7 +1403,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1316
1403
|
input: openAIParams.input,
|
|
1317
1404
|
output: [],
|
|
1318
1405
|
latency: 0,
|
|
1319
|
-
baseURL: this.baseURL
|
|
1406
|
+
baseURL: this.baseURL,
|
|
1320
1407
|
params: body,
|
|
1321
1408
|
httpStatus,
|
|
1322
1409
|
usage: {
|
|
@@ -1347,7 +1434,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1347
1434
|
input: openAIParams.input,
|
|
1348
1435
|
output: result.output,
|
|
1349
1436
|
latency,
|
|
1350
|
-
baseURL: this.baseURL
|
|
1437
|
+
baseURL: this.baseURL,
|
|
1351
1438
|
params: body,
|
|
1352
1439
|
httpStatus: 200,
|
|
1353
1440
|
usage: {
|
|
@@ -1372,7 +1459,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1372
1459
|
input: openAIParams.input,
|
|
1373
1460
|
output: [],
|
|
1374
1461
|
latency: 0,
|
|
1375
|
-
baseURL: this.baseURL
|
|
1462
|
+
baseURL: this.baseURL,
|
|
1376
1463
|
params: body,
|
|
1377
1464
|
httpStatus,
|
|
1378
1465
|
usage: {
|
|
@@ -1392,10 +1479,6 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1392
1479
|
const {
|
|
1393
1480
|
posthogDistinctId,
|
|
1394
1481
|
posthogTraceId,
|
|
1395
|
-
posthogProperties,
|
|
1396
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1397
|
-
posthogPrivacyMode = false,
|
|
1398
|
-
posthogGroups,
|
|
1399
1482
|
posthogCaptureImmediate,
|
|
1400
1483
|
...openAIParams
|
|
1401
1484
|
} = body;
|
|
@@ -1414,7 +1497,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1414
1497
|
input: openAIParams.input,
|
|
1415
1498
|
output: result.output,
|
|
1416
1499
|
latency,
|
|
1417
|
-
baseURL: this.baseURL
|
|
1500
|
+
baseURL: this.baseURL,
|
|
1418
1501
|
params: body,
|
|
1419
1502
|
httpStatus: 200,
|
|
1420
1503
|
usage: {
|
|
@@ -1437,7 +1520,7 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1437
1520
|
input: openAIParams.input,
|
|
1438
1521
|
output: [],
|
|
1439
1522
|
latency: 0,
|
|
1440
|
-
baseURL: this.baseURL
|
|
1523
|
+
baseURL: this.baseURL,
|
|
1441
1524
|
params: body,
|
|
1442
1525
|
httpStatus: error?.status ? error.status : 500,
|
|
1443
1526
|
usage: {
|
|
@@ -1453,6 +1536,72 @@ class WrappedResponses extends AzureOpenAI.Responses {
|
|
|
1453
1536
|
return wrappedPromise;
|
|
1454
1537
|
}
|
|
1455
1538
|
}
|
|
1539
|
+
class WrappedEmbeddings extends AzureOpenAI.Embeddings {
|
|
1540
|
+
constructor(client, phClient) {
|
|
1541
|
+
super(client);
|
|
1542
|
+
this.phClient = phClient;
|
|
1543
|
+
this.baseURL = client.baseURL;
|
|
1544
|
+
}
|
|
1545
|
+
create(body, options) {
|
|
1546
|
+
const {
|
|
1547
|
+
posthogDistinctId,
|
|
1548
|
+
posthogTraceId,
|
|
1549
|
+
posthogPrivacyMode = false,
|
|
1550
|
+
posthogCaptureImmediate,
|
|
1551
|
+
...openAIParams
|
|
1552
|
+
} = body;
|
|
1553
|
+
const traceId = posthogTraceId ?? v4();
|
|
1554
|
+
const startTime = Date.now();
|
|
1555
|
+
const parentPromise = super.create(openAIParams, options);
|
|
1556
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1557
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1558
|
+
await sendEventToPosthog({
|
|
1559
|
+
client: this.phClient,
|
|
1560
|
+
eventType: AIEvent.Embedding,
|
|
1561
|
+
distinctId: posthogDistinctId,
|
|
1562
|
+
traceId,
|
|
1563
|
+
model: openAIParams.model,
|
|
1564
|
+
provider: 'azure',
|
|
1565
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1566
|
+
output: null,
|
|
1567
|
+
// Embeddings don't have output content
|
|
1568
|
+
latency,
|
|
1569
|
+
baseURL: this.baseURL,
|
|
1570
|
+
params: body,
|
|
1571
|
+
httpStatus: 200,
|
|
1572
|
+
usage: {
|
|
1573
|
+
inputTokens: result.usage?.prompt_tokens ?? 0
|
|
1574
|
+
},
|
|
1575
|
+
captureImmediate: posthogCaptureImmediate
|
|
1576
|
+
});
|
|
1577
|
+
return result;
|
|
1578
|
+
}, async error => {
|
|
1579
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1580
|
+
await sendEventToPosthog({
|
|
1581
|
+
client: this.phClient,
|
|
1582
|
+
eventType: AIEvent.Embedding,
|
|
1583
|
+
distinctId: posthogDistinctId,
|
|
1584
|
+
traceId,
|
|
1585
|
+
model: openAIParams.model,
|
|
1586
|
+
provider: 'azure',
|
|
1587
|
+
input: withPrivacyMode(this.phClient, posthogPrivacyMode, openAIParams.input),
|
|
1588
|
+
output: null,
|
|
1589
|
+
latency: 0,
|
|
1590
|
+
baseURL: this.baseURL,
|
|
1591
|
+
params: body,
|
|
1592
|
+
httpStatus,
|
|
1593
|
+
usage: {
|
|
1594
|
+
inputTokens: 0
|
|
1595
|
+
},
|
|
1596
|
+
isError: true,
|
|
1597
|
+
error: JSON.stringify(error),
|
|
1598
|
+
captureImmediate: posthogCaptureImmediate
|
|
1599
|
+
});
|
|
1600
|
+
throw error;
|
|
1601
|
+
});
|
|
1602
|
+
return wrappedPromise;
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1456
1605
|
|
|
1457
1606
|
const mapVercelParams = params => {
|
|
1458
1607
|
return {
|
|
@@ -1640,7 +1789,7 @@ const mapVercelOutput = result => {
|
|
|
1640
1789
|
content: truncate(jsonOutput),
|
|
1641
1790
|
role: 'assistant'
|
|
1642
1791
|
}];
|
|
1643
|
-
} catch
|
|
1792
|
+
} catch {
|
|
1644
1793
|
console.error('Error stringifying output');
|
|
1645
1794
|
return [];
|
|
1646
1795
|
}
|
|
@@ -1915,15 +2064,12 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1915
2064
|
constructor(parentClient, phClient) {
|
|
1916
2065
|
super(parentClient);
|
|
1917
2066
|
this.phClient = phClient;
|
|
2067
|
+
this.baseURL = parentClient.baseURL;
|
|
1918
2068
|
}
|
|
1919
2069
|
create(body, options) {
|
|
1920
2070
|
const {
|
|
1921
2071
|
posthogDistinctId,
|
|
1922
2072
|
posthogTraceId,
|
|
1923
|
-
posthogProperties,
|
|
1924
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1925
|
-
posthogPrivacyMode = false,
|
|
1926
|
-
posthogGroups,
|
|
1927
2073
|
posthogCaptureImmediate,
|
|
1928
2074
|
...anthropicParams
|
|
1929
2075
|
} = body;
|
|
@@ -1975,7 +2121,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1975
2121
|
// Handle text delta events
|
|
1976
2122
|
if ('delta' in chunk) {
|
|
1977
2123
|
if ('text' in chunk.delta) {
|
|
1978
|
-
const delta = chunk
|
|
2124
|
+
const delta = chunk.delta.text;
|
|
1979
2125
|
accumulatedContent += delta;
|
|
1980
2126
|
if (currentTextBlock) {
|
|
1981
2127
|
currentTextBlock.text += delta;
|
|
@@ -2044,7 +2190,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2044
2190
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams, 'anthropic')),
|
|
2045
2191
|
output: formattedOutput,
|
|
2046
2192
|
latency,
|
|
2047
|
-
baseURL: this.baseURL
|
|
2193
|
+
baseURL: this.baseURL,
|
|
2048
2194
|
params: body,
|
|
2049
2195
|
httpStatus: 200,
|
|
2050
2196
|
usage,
|
|
@@ -2062,7 +2208,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2062
2208
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2063
2209
|
output: [],
|
|
2064
2210
|
latency: 0,
|
|
2065
|
-
baseURL: this.baseURL
|
|
2211
|
+
baseURL: this.baseURL,
|
|
2066
2212
|
params: body,
|
|
2067
2213
|
httpStatus: error?.status ? error.status : 500,
|
|
2068
2214
|
usage: {
|
|
@@ -2094,7 +2240,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2094
2240
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2095
2241
|
output: formatResponseAnthropic(result),
|
|
2096
2242
|
latency,
|
|
2097
|
-
baseURL: this.baseURL
|
|
2243
|
+
baseURL: this.baseURL,
|
|
2098
2244
|
params: body,
|
|
2099
2245
|
httpStatus: 200,
|
|
2100
2246
|
usage: {
|
|
@@ -2118,7 +2264,7 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
2118
2264
|
input: sanitizeAnthropic(mergeSystemPrompt(anthropicParams)),
|
|
2119
2265
|
output: [],
|
|
2120
2266
|
latency: 0,
|
|
2121
|
-
baseURL: this.baseURL
|
|
2267
|
+
baseURL: this.baseURL,
|
|
2122
2268
|
params: body,
|
|
2123
2269
|
httpStatus: error?.status ? error.status : 500,
|
|
2124
2270
|
usage: {
|
|
@@ -2156,8 +2302,6 @@ class WrappedModels {
|
|
|
2156
2302
|
const {
|
|
2157
2303
|
posthogDistinctId,
|
|
2158
2304
|
posthogTraceId,
|
|
2159
|
-
posthogProperties,
|
|
2160
|
-
posthogGroups,
|
|
2161
2305
|
posthogCaptureImmediate,
|
|
2162
2306
|
...geminiParams
|
|
2163
2307
|
} = params;
|
|
@@ -2219,8 +2363,6 @@ class WrappedModels {
|
|
|
2219
2363
|
const {
|
|
2220
2364
|
posthogDistinctId,
|
|
2221
2365
|
posthogTraceId,
|
|
2222
|
-
posthogProperties,
|
|
2223
|
-
posthogGroups,
|
|
2224
2366
|
posthogCaptureImmediate,
|
|
2225
2367
|
...geminiParams
|
|
2226
2368
|
} = params;
|
|
@@ -2943,7 +3085,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2943
3085
|
this.debug = options.debug || false;
|
|
2944
3086
|
}
|
|
2945
3087
|
// ===== CALLBACK METHODS =====
|
|
2946
|
-
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata,
|
|
3088
|
+
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, _runType, runName) {
|
|
2947
3089
|
this._logDebugEvent('on_chain_start', runId, parentRunId, {
|
|
2948
3090
|
inputs,
|
|
2949
3091
|
tags
|
|
@@ -2951,18 +3093,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2951
3093
|
this._setParentOfRun(runId, parentRunId);
|
|
2952
3094
|
this._setTraceOrSpanMetadata(chain, inputs, runId, parentRunId, metadata, tags, runName);
|
|
2953
3095
|
}
|
|
2954
|
-
handleChainEnd(outputs, runId, parentRunId, tags,
|
|
2955
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2956
|
-
kwargs) {
|
|
3096
|
+
handleChainEnd(outputs, runId, parentRunId, tags, _kwargs) {
|
|
2957
3097
|
this._logDebugEvent('on_chain_end', runId, parentRunId, {
|
|
2958
3098
|
outputs,
|
|
2959
3099
|
tags
|
|
2960
3100
|
});
|
|
2961
3101
|
this._popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs);
|
|
2962
3102
|
}
|
|
2963
|
-
handleChainError(error, runId, parentRunId, tags,
|
|
2964
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2965
|
-
kwargs) {
|
|
3103
|
+
handleChainError(error, runId, parentRunId, tags, _kwargs) {
|
|
2966
3104
|
this._logDebugEvent('on_chain_error', runId, parentRunId, {
|
|
2967
3105
|
error,
|
|
2968
3106
|
tags
|
|
@@ -2987,18 +3125,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
2987
3125
|
this._setParentOfRun(runId, parentRunId);
|
|
2988
3126
|
this._setLLMMetadata(serialized, runId, prompts, metadata, extraParams, runName);
|
|
2989
3127
|
}
|
|
2990
|
-
handleLLMEnd(output, runId, parentRunId, tags,
|
|
2991
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2992
|
-
extraParams) {
|
|
3128
|
+
handleLLMEnd(output, runId, parentRunId, tags, _extraParams) {
|
|
2993
3129
|
this._logDebugEvent('on_llm_end', runId, parentRunId, {
|
|
2994
3130
|
output,
|
|
2995
3131
|
tags
|
|
2996
3132
|
});
|
|
2997
3133
|
this._popRunAndCaptureGeneration(runId, parentRunId, output);
|
|
2998
3134
|
}
|
|
2999
|
-
handleLLMError(err, runId, parentRunId, tags,
|
|
3000
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3001
|
-
extraParams) {
|
|
3135
|
+
handleLLMError(err, runId, parentRunId, tags, _extraParams) {
|
|
3002
3136
|
this._logDebugEvent('on_llm_error', runId, parentRunId, {
|
|
3003
3137
|
err,
|
|
3004
3138
|
tags
|
|
@@ -3133,7 +3267,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
3133
3267
|
_getTraceId(runId) {
|
|
3134
3268
|
return this.traceId ? String(this.traceId) : this._findRootRun(runId);
|
|
3135
3269
|
}
|
|
3136
|
-
_getParentRunId(traceId,
|
|
3270
|
+
_getParentRunId(traceId, _runId, parentRunId) {
|
|
3137
3271
|
// Replace the parent-run if not found in our stored parent tree.
|
|
3138
3272
|
if (parentRunId && !this.parentTree[parentRunId]) {
|
|
3139
3273
|
return traceId;
|