@posthog/ai 7.16.14 → 7.17.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/README.md +31 -0
- package/dist/anthropic/index.cjs +83 -95
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +83 -95
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +92 -106
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +92 -106
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +267 -318
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +83 -2
- package/dist/index.mjs +245 -297
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +2 -3
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +2 -3
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +161 -185
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.mjs +161 -185
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-agents/index.cjs +2 -3
- package/dist/openai-agents/index.cjs.map +1 -1
- package/dist/openai-agents/index.mjs +2 -3
- package/dist/openai-agents/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +104 -111
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +104 -111
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/openai/index.cjs
CHANGED
|
@@ -6,8 +6,6 @@ var openai = require('openai');
|
|
|
6
6
|
var uuid = require('uuid');
|
|
7
7
|
var core = require('@posthog/core');
|
|
8
8
|
|
|
9
|
-
var version = "7.16.14";
|
|
10
|
-
|
|
11
9
|
// Type guards for safer type checking
|
|
12
10
|
|
|
13
11
|
const isString = value => {
|
|
@@ -480,73 +478,114 @@ function addDefaults(params) {
|
|
|
480
478
|
traceId: params.traceId ?? uuid.v4()
|
|
481
479
|
};
|
|
482
480
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
const properties = {
|
|
491
|
-
client,
|
|
492
|
-
traceId,
|
|
493
|
-
httpStatus,
|
|
494
|
-
error: JSON.stringify(error),
|
|
495
|
-
...args
|
|
496
|
-
};
|
|
497
|
-
const enrichedError = error;
|
|
498
|
-
if (client.options?.enableExceptionAutocapture) {
|
|
499
|
-
// assign a uuid that can be used to link the trace and exception events
|
|
500
|
-
const exceptionId = core.uuidv7();
|
|
501
|
-
client.captureException(error, undefined, {
|
|
502
|
-
$ai_trace_id: traceId
|
|
503
|
-
}, exceptionId);
|
|
504
|
-
enrichedError.__posthog_previously_captured_error = true;
|
|
505
|
-
properties.exceptionId = exceptionId;
|
|
481
|
+
function formatOpenAIResponsesInput(input, instructions) {
|
|
482
|
+
const messages = [];
|
|
483
|
+
if (instructions) {
|
|
484
|
+
messages.push({
|
|
485
|
+
role: 'system',
|
|
486
|
+
content: instructions
|
|
487
|
+
});
|
|
506
488
|
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
}
|
|
489
|
+
if (Array.isArray(input)) {
|
|
490
|
+
for (const item of input) {
|
|
491
|
+
if (typeof item === 'string') {
|
|
492
|
+
messages.push({
|
|
493
|
+
role: 'user',
|
|
494
|
+
content: item
|
|
495
|
+
});
|
|
496
|
+
} else if (item && typeof item === 'object') {
|
|
497
|
+
const obj = item;
|
|
498
|
+
const role = isString(obj.role) ? obj.role : 'user';
|
|
499
|
+
|
|
500
|
+
// Handle content properly - preserve structure for objects/arrays
|
|
501
|
+
const content = obj.content ?? obj.text ?? item;
|
|
502
|
+
messages.push({
|
|
503
|
+
role,
|
|
504
|
+
content: toContentString(content)
|
|
505
|
+
});
|
|
506
|
+
} else {
|
|
507
|
+
messages.push({
|
|
508
|
+
role: 'user',
|
|
509
|
+
content: toContentString(item)
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
} else if (typeof input === 'string') {
|
|
514
|
+
messages.push({
|
|
515
|
+
role: 'user',
|
|
516
|
+
content: input
|
|
517
|
+
});
|
|
518
|
+
} else if (input) {
|
|
519
|
+
messages.push({
|
|
520
|
+
role: 'user',
|
|
521
|
+
content: toContentString(input)
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
return messages;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
var version = "7.17.0";
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
|
|
531
|
+
* directly so that any caller — first-party SDK wrappers and external code
|
|
532
|
+
* alike — produces an identical event.
|
|
533
|
+
*/
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
|
|
537
|
+
*
|
|
538
|
+
* This is the canonical primitive that every `@posthog/ai` wrapper
|
|
539
|
+
* (`withTracing`, `OpenAI`, `Anthropic`, `GoogleGenAI`, …) funnels through, so
|
|
540
|
+
* external code can use it directly to instrument LLM calls made through
|
|
541
|
+
* arbitrary clients (Cloudflare Workers AI, custom HTTP, etc.) and get the
|
|
542
|
+
* same events the SDK wrappers produce.
|
|
543
|
+
*
|
|
544
|
+
* When `error` is set, the event is captured as an error. If the error is an
|
|
545
|
+
* object, it is mutated in place to set `__posthog_previously_captured_error`
|
|
546
|
+
* so callers can re-throw the original error reference safely.
|
|
547
|
+
*/
|
|
548
|
+
const captureAiGeneration = async (client, options) => {
|
|
531
549
|
if (!client.capture) {
|
|
532
|
-
return
|
|
550
|
+
return;
|
|
533
551
|
}
|
|
534
|
-
|
|
535
|
-
const
|
|
536
|
-
const
|
|
537
|
-
const
|
|
552
|
+
const traceId = options.traceId ?? uuid.v4();
|
|
553
|
+
const eventType = options.eventType ?? AIEvent.Generation;
|
|
554
|
+
const privacyMode = options.privacyMode ?? false;
|
|
555
|
+
const usage = options.usage ?? {};
|
|
556
|
+
const safeInput = sanitizeValues(options.input);
|
|
557
|
+
const safeOutput = sanitizeValues(options.output);
|
|
558
|
+
let httpStatus = options.httpStatus;
|
|
538
559
|
let errorData = {};
|
|
539
|
-
if (error) {
|
|
560
|
+
if (options.error) {
|
|
561
|
+
if (httpStatus === undefined) {
|
|
562
|
+
if (typeof options.error === 'object' && 'status' in options.error && typeof options.error.status === 'number') {
|
|
563
|
+
httpStatus = options.error.status;
|
|
564
|
+
} else {
|
|
565
|
+
httpStatus = 500;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
let exceptionId;
|
|
569
|
+
if (client.options?.enableExceptionAutocapture) {
|
|
570
|
+
exceptionId = core.uuidv7();
|
|
571
|
+
client.captureException(options.error, undefined, {
|
|
572
|
+
$ai_trace_id: traceId
|
|
573
|
+
}, exceptionId);
|
|
574
|
+
if (typeof options.error === 'object') {
|
|
575
|
+
options.error.__posthog_previously_captured_error = true;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
540
578
|
errorData = {
|
|
541
579
|
$ai_is_error: true,
|
|
542
|
-
$ai_error:
|
|
580
|
+
$ai_error: sanitizeValues(JSON.stringify(options.error)),
|
|
543
581
|
$exception_event_id: exceptionId
|
|
544
582
|
};
|
|
545
583
|
}
|
|
584
|
+
httpStatus = httpStatus ?? 200;
|
|
546
585
|
let costOverrideData = {};
|
|
547
|
-
if (
|
|
548
|
-
const inputCostUSD = (
|
|
549
|
-
const outputCostUSD = (
|
|
586
|
+
if (options.costOverride) {
|
|
587
|
+
const inputCostUSD = (options.costOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
|
|
588
|
+
const outputCostUSD = (options.costOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
|
|
550
589
|
costOverrideData = {
|
|
551
590
|
$ai_input_cost_usd: inputCostUSD,
|
|
552
591
|
$ai_output_cost_usd: outputCostUSD,
|
|
@@ -573,96 +612,49 @@ const sendEventToPosthog = async ({
|
|
|
573
612
|
const properties = {
|
|
574
613
|
$ai_lib: 'posthog-ai',
|
|
575
614
|
$ai_lib_version: version,
|
|
576
|
-
$ai_provider:
|
|
577
|
-
$ai_model:
|
|
578
|
-
$ai_model_parameters:
|
|
579
|
-
$ai_input: withPrivacyMode(client,
|
|
580
|
-
$ai_output_choices: withPrivacyMode(client,
|
|
615
|
+
$ai_provider: options.providerOverride ?? options.provider,
|
|
616
|
+
$ai_model: options.modelOverride ?? options.model,
|
|
617
|
+
$ai_model_parameters: options.modelParameters ?? {},
|
|
618
|
+
$ai_input: withPrivacyMode(client, privacyMode, safeInput),
|
|
619
|
+
$ai_output_choices: withPrivacyMode(client, privacyMode, safeOutput),
|
|
581
620
|
$ai_http_status: httpStatus,
|
|
582
621
|
$ai_input_tokens: usage.inputTokens ?? 0,
|
|
583
622
|
...(usage.outputTokens !== undefined ? {
|
|
584
623
|
$ai_output_tokens: usage.outputTokens
|
|
585
624
|
} : {}),
|
|
586
625
|
...additionalTokenValues,
|
|
587
|
-
$ai_latency: latency,
|
|
588
|
-
...(timeToFirstToken !== undefined ? {
|
|
589
|
-
$ai_time_to_first_token: timeToFirstToken
|
|
626
|
+
$ai_latency: options.latency ?? 0,
|
|
627
|
+
...(options.timeToFirstToken !== undefined ? {
|
|
628
|
+
$ai_time_to_first_token: options.timeToFirstToken
|
|
590
629
|
} : {}),
|
|
591
630
|
$ai_trace_id: traceId,
|
|
592
|
-
$ai_base_url: baseURL,
|
|
593
|
-
...
|
|
594
|
-
$ai_tokens_source: getTokensSource(
|
|
595
|
-
...(distinctId ? {} : {
|
|
631
|
+
$ai_base_url: options.baseURL ?? '',
|
|
632
|
+
...options.properties,
|
|
633
|
+
$ai_tokens_source: getTokensSource(options.properties),
|
|
634
|
+
...(options.distinctId ? {} : {
|
|
596
635
|
$process_person_profile: false
|
|
597
636
|
}),
|
|
598
|
-
...(stopReason ? {
|
|
599
|
-
$ai_stop_reason: stopReason
|
|
637
|
+
...(options.stopReason ? {
|
|
638
|
+
$ai_stop_reason: options.stopReason
|
|
600
639
|
} : {}),
|
|
601
|
-
...(tools ? {
|
|
602
|
-
$ai_tools: tools
|
|
640
|
+
...(options.tools ? {
|
|
641
|
+
$ai_tools: options.tools
|
|
603
642
|
} : {}),
|
|
604
643
|
...errorData,
|
|
605
644
|
...costOverrideData
|
|
606
645
|
};
|
|
607
646
|
const event = {
|
|
608
|
-
distinctId: distinctId ?? traceId,
|
|
647
|
+
distinctId: options.distinctId ?? traceId,
|
|
609
648
|
event: eventType,
|
|
610
649
|
properties,
|
|
611
|
-
groups:
|
|
650
|
+
groups: options.groups
|
|
612
651
|
};
|
|
613
|
-
if (captureImmediate) {
|
|
614
|
-
// await capture promise to send single event in serverless environments
|
|
652
|
+
if (options.captureImmediate) {
|
|
615
653
|
await client.captureImmediate(event);
|
|
616
654
|
} else {
|
|
617
655
|
client.capture(event);
|
|
618
656
|
}
|
|
619
|
-
return Promise.resolve();
|
|
620
657
|
};
|
|
621
|
-
function formatOpenAIResponsesInput(input, instructions) {
|
|
622
|
-
const messages = [];
|
|
623
|
-
if (instructions) {
|
|
624
|
-
messages.push({
|
|
625
|
-
role: 'system',
|
|
626
|
-
content: instructions
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
if (Array.isArray(input)) {
|
|
630
|
-
for (const item of input) {
|
|
631
|
-
if (typeof item === 'string') {
|
|
632
|
-
messages.push({
|
|
633
|
-
role: 'user',
|
|
634
|
-
content: item
|
|
635
|
-
});
|
|
636
|
-
} else if (item && typeof item === 'object') {
|
|
637
|
-
const obj = item;
|
|
638
|
-
const role = isString(obj.role) ? obj.role : 'user';
|
|
639
|
-
|
|
640
|
-
// Handle content properly - preserve structure for objects/arrays
|
|
641
|
-
const content = obj.content ?? obj.text ?? item;
|
|
642
|
-
messages.push({
|
|
643
|
-
role,
|
|
644
|
-
content: toContentString(content)
|
|
645
|
-
});
|
|
646
|
-
} else {
|
|
647
|
-
messages.push({
|
|
648
|
-
role: 'user',
|
|
649
|
-
content: toContentString(item)
|
|
650
|
-
});
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
} else if (typeof input === 'string') {
|
|
654
|
-
messages.push({
|
|
655
|
-
role: 'user',
|
|
656
|
-
content: input
|
|
657
|
-
});
|
|
658
|
-
} else if (input) {
|
|
659
|
-
messages.push({
|
|
660
|
-
role: 'user',
|
|
661
|
-
content: toContentString(input)
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
return messages;
|
|
665
|
-
}
|
|
666
658
|
|
|
667
659
|
/**
|
|
668
660
|
* Checks if a ResponseStreamEvent chunk represents the first token/content from the model.
|
|
@@ -845,8 +837,7 @@ class WrappedCompletions extends Completions {
|
|
|
845
837
|
const latency = (Date.now() - startTime) / 1000;
|
|
846
838
|
const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
|
|
847
839
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
848
|
-
await
|
|
849
|
-
client: this.phClient,
|
|
840
|
+
await captureAiGeneration(this.phClient, {
|
|
850
841
|
...posthogParams,
|
|
851
842
|
model: openAIParams.model ?? modelFromResponse,
|
|
852
843
|
provider: 'openai',
|
|
@@ -855,7 +846,7 @@ class WrappedCompletions extends Completions {
|
|
|
855
846
|
latency,
|
|
856
847
|
timeToFirstToken,
|
|
857
848
|
baseURL: this.baseURL,
|
|
858
|
-
|
|
849
|
+
modelParameters: getModelParams(body),
|
|
859
850
|
httpStatus: 200,
|
|
860
851
|
usage: {
|
|
861
852
|
inputTokens: usage.inputTokens,
|
|
@@ -869,8 +860,7 @@ class WrappedCompletions extends Completions {
|
|
|
869
860
|
tools: availableTools
|
|
870
861
|
});
|
|
871
862
|
} catch (error) {
|
|
872
|
-
|
|
873
|
-
client: this.phClient,
|
|
863
|
+
await captureAiGeneration(this.phClient, {
|
|
874
864
|
...posthogParams,
|
|
875
865
|
model: openAIParams.model,
|
|
876
866
|
provider: 'openai',
|
|
@@ -878,14 +868,14 @@ class WrappedCompletions extends Completions {
|
|
|
878
868
|
output: [],
|
|
879
869
|
latency: 0,
|
|
880
870
|
baseURL: this.baseURL,
|
|
881
|
-
|
|
871
|
+
modelParameters: getModelParams(body),
|
|
882
872
|
usage: {
|
|
883
873
|
inputTokens: 0,
|
|
884
874
|
outputTokens: 0
|
|
885
875
|
},
|
|
886
876
|
error
|
|
887
877
|
});
|
|
888
|
-
throw
|
|
878
|
+
throw error;
|
|
889
879
|
}
|
|
890
880
|
})();
|
|
891
881
|
|
|
@@ -900,8 +890,7 @@ class WrappedCompletions extends Completions {
|
|
|
900
890
|
const latency = (Date.now() - startTime) / 1000;
|
|
901
891
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
902
892
|
const formattedOutput = formatResponseOpenAI(result);
|
|
903
|
-
await
|
|
904
|
-
client: this.phClient,
|
|
893
|
+
await captureAiGeneration(this.phClient, {
|
|
905
894
|
...posthogParams,
|
|
906
895
|
model: openAIParams.model ?? result.model,
|
|
907
896
|
provider: 'openai',
|
|
@@ -909,7 +898,7 @@ class WrappedCompletions extends Completions {
|
|
|
909
898
|
output: formattedOutput,
|
|
910
899
|
latency,
|
|
911
900
|
baseURL: this.baseURL,
|
|
912
|
-
|
|
901
|
+
modelParameters: getModelParams(body),
|
|
913
902
|
httpStatus: 200,
|
|
914
903
|
usage: {
|
|
915
904
|
inputTokens: result.usage?.prompt_tokens ?? 0,
|
|
@@ -926,8 +915,7 @@ class WrappedCompletions extends Completions {
|
|
|
926
915
|
return result;
|
|
927
916
|
}, async error => {
|
|
928
917
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
929
|
-
await
|
|
930
|
-
client: this.phClient,
|
|
918
|
+
await captureAiGeneration(this.phClient, {
|
|
931
919
|
...posthogParams,
|
|
932
920
|
model: openAIParams.model,
|
|
933
921
|
provider: 'openai',
|
|
@@ -935,13 +923,13 @@ class WrappedCompletions extends Completions {
|
|
|
935
923
|
output: [],
|
|
936
924
|
latency: 0,
|
|
937
925
|
baseURL: this.baseURL,
|
|
938
|
-
|
|
926
|
+
modelParameters: getModelParams(body),
|
|
939
927
|
httpStatus,
|
|
940
928
|
usage: {
|
|
941
929
|
inputTokens: 0,
|
|
942
930
|
outputTokens: 0
|
|
943
931
|
},
|
|
944
|
-
error
|
|
932
|
+
error
|
|
945
933
|
});
|
|
946
934
|
throw error;
|
|
947
935
|
});
|
|
@@ -1021,8 +1009,7 @@ class WrappedResponses extends Responses {
|
|
|
1021
1009
|
const latency = (Date.now() - startTime) / 1000;
|
|
1022
1010
|
const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
|
|
1023
1011
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1024
|
-
await
|
|
1025
|
-
client: this.phClient,
|
|
1012
|
+
await captureAiGeneration(this.phClient, {
|
|
1026
1013
|
...posthogParams,
|
|
1027
1014
|
model: openAIParams.model ?? modelFromResponse,
|
|
1028
1015
|
provider: 'openai',
|
|
@@ -1031,7 +1018,7 @@ class WrappedResponses extends Responses {
|
|
|
1031
1018
|
latency,
|
|
1032
1019
|
timeToFirstToken,
|
|
1033
1020
|
baseURL: this.baseURL,
|
|
1034
|
-
|
|
1021
|
+
modelParameters: getModelParams(body),
|
|
1035
1022
|
httpStatus: 200,
|
|
1036
1023
|
usage: {
|
|
1037
1024
|
inputTokens: usage.inputTokens,
|
|
@@ -1045,8 +1032,7 @@ class WrappedResponses extends Responses {
|
|
|
1045
1032
|
tools: availableTools
|
|
1046
1033
|
});
|
|
1047
1034
|
} catch (error) {
|
|
1048
|
-
|
|
1049
|
-
client: this.phClient,
|
|
1035
|
+
await captureAiGeneration(this.phClient, {
|
|
1050
1036
|
...posthogParams,
|
|
1051
1037
|
model: openAIParams.model,
|
|
1052
1038
|
provider: 'openai',
|
|
@@ -1054,14 +1040,14 @@ class WrappedResponses extends Responses {
|
|
|
1054
1040
|
output: [],
|
|
1055
1041
|
latency: 0,
|
|
1056
1042
|
baseURL: this.baseURL,
|
|
1057
|
-
|
|
1043
|
+
modelParameters: getModelParams(body),
|
|
1058
1044
|
usage: {
|
|
1059
1045
|
inputTokens: 0,
|
|
1060
1046
|
outputTokens: 0
|
|
1061
1047
|
},
|
|
1062
|
-
error
|
|
1048
|
+
error
|
|
1063
1049
|
});
|
|
1064
|
-
throw
|
|
1050
|
+
throw error;
|
|
1065
1051
|
}
|
|
1066
1052
|
})();
|
|
1067
1053
|
return stream2;
|
|
@@ -1076,8 +1062,7 @@ class WrappedResponses extends Responses {
|
|
|
1076
1062
|
const formattedOutput = formatResponseOpenAI({
|
|
1077
1063
|
output: result.output
|
|
1078
1064
|
});
|
|
1079
|
-
await
|
|
1080
|
-
client: this.phClient,
|
|
1065
|
+
await captureAiGeneration(this.phClient, {
|
|
1081
1066
|
...posthogParams,
|
|
1082
1067
|
model: openAIParams.model ?? result.model,
|
|
1083
1068
|
provider: 'openai',
|
|
@@ -1085,7 +1070,7 @@ class WrappedResponses extends Responses {
|
|
|
1085
1070
|
output: formattedOutput,
|
|
1086
1071
|
latency,
|
|
1087
1072
|
baseURL: this.baseURL,
|
|
1088
|
-
|
|
1073
|
+
modelParameters: getModelParams(body),
|
|
1089
1074
|
httpStatus: 200,
|
|
1090
1075
|
usage: {
|
|
1091
1076
|
inputTokens: result.usage?.input_tokens ?? 0,
|
|
@@ -1102,8 +1087,7 @@ class WrappedResponses extends Responses {
|
|
|
1102
1087
|
return result;
|
|
1103
1088
|
}, async error => {
|
|
1104
1089
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1105
|
-
await
|
|
1106
|
-
client: this.phClient,
|
|
1090
|
+
await captureAiGeneration(this.phClient, {
|
|
1107
1091
|
...posthogParams,
|
|
1108
1092
|
model: openAIParams.model,
|
|
1109
1093
|
provider: 'openai',
|
|
@@ -1111,13 +1095,13 @@ class WrappedResponses extends Responses {
|
|
|
1111
1095
|
output: [],
|
|
1112
1096
|
latency: 0,
|
|
1113
1097
|
baseURL: this.baseURL,
|
|
1114
|
-
|
|
1098
|
+
modelParameters: getModelParams(body),
|
|
1115
1099
|
httpStatus,
|
|
1116
1100
|
usage: {
|
|
1117
1101
|
inputTokens: 0,
|
|
1118
1102
|
outputTokens: 0
|
|
1119
1103
|
},
|
|
1120
|
-
error
|
|
1104
|
+
error
|
|
1121
1105
|
});
|
|
1122
1106
|
throw error;
|
|
1123
1107
|
});
|
|
@@ -1138,8 +1122,7 @@ class WrappedResponses extends Responses {
|
|
|
1138
1122
|
const parentPromise = super.parse(openAIParams, options);
|
|
1139
1123
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1140
1124
|
const latency = (Date.now() - startTime) / 1000;
|
|
1141
|
-
await
|
|
1142
|
-
client: this.phClient,
|
|
1125
|
+
await captureAiGeneration(this.phClient, {
|
|
1143
1126
|
...posthogParams,
|
|
1144
1127
|
model: openAIParams.model ?? result.model,
|
|
1145
1128
|
provider: 'openai',
|
|
@@ -1147,7 +1130,7 @@ class WrappedResponses extends Responses {
|
|
|
1147
1130
|
output: result.output,
|
|
1148
1131
|
latency,
|
|
1149
1132
|
baseURL: this.baseURL,
|
|
1150
|
-
|
|
1133
|
+
modelParameters: getModelParams(body),
|
|
1151
1134
|
httpStatus: 200,
|
|
1152
1135
|
usage: {
|
|
1153
1136
|
inputTokens: result.usage?.input_tokens ?? 0,
|
|
@@ -1160,8 +1143,7 @@ class WrappedResponses extends Responses {
|
|
|
1160
1143
|
});
|
|
1161
1144
|
return result;
|
|
1162
1145
|
}, async error => {
|
|
1163
|
-
|
|
1164
|
-
client: this.phClient,
|
|
1146
|
+
await captureAiGeneration(this.phClient, {
|
|
1165
1147
|
...posthogParams,
|
|
1166
1148
|
model: openAIParams.model,
|
|
1167
1149
|
provider: 'openai',
|
|
@@ -1169,14 +1151,14 @@ class WrappedResponses extends Responses {
|
|
|
1169
1151
|
output: [],
|
|
1170
1152
|
latency: 0,
|
|
1171
1153
|
baseURL: this.baseURL,
|
|
1172
|
-
|
|
1154
|
+
modelParameters: getModelParams(body),
|
|
1173
1155
|
usage: {
|
|
1174
1156
|
inputTokens: 0,
|
|
1175
1157
|
outputTokens: 0
|
|
1176
1158
|
},
|
|
1177
|
-
error
|
|
1159
|
+
error
|
|
1178
1160
|
});
|
|
1179
|
-
throw
|
|
1161
|
+
throw error;
|
|
1180
1162
|
});
|
|
1181
1163
|
return wrappedPromise;
|
|
1182
1164
|
} finally {
|
|
@@ -1200,8 +1182,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1200
1182
|
const parentPromise = super.create(openAIParams, options);
|
|
1201
1183
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1202
1184
|
const latency = (Date.now() - startTime) / 1000;
|
|
1203
|
-
await
|
|
1204
|
-
client: this.phClient,
|
|
1185
|
+
await captureAiGeneration(this.phClient, {
|
|
1205
1186
|
...posthogParams,
|
|
1206
1187
|
eventType: AIEvent.Embedding,
|
|
1207
1188
|
model: openAIParams.model,
|
|
@@ -1211,7 +1192,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1211
1192
|
// Embeddings don't have output content
|
|
1212
1193
|
latency,
|
|
1213
1194
|
baseURL: this.baseURL,
|
|
1214
|
-
|
|
1195
|
+
modelParameters: getModelParams(body),
|
|
1215
1196
|
httpStatus: 200,
|
|
1216
1197
|
usage: {
|
|
1217
1198
|
inputTokens: result.usage?.prompt_tokens ?? 0,
|
|
@@ -1221,8 +1202,7 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1221
1202
|
return result;
|
|
1222
1203
|
}, async error => {
|
|
1223
1204
|
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1224
|
-
await
|
|
1225
|
-
client: this.phClient,
|
|
1205
|
+
await captureAiGeneration(this.phClient, {
|
|
1226
1206
|
eventType: AIEvent.Embedding,
|
|
1227
1207
|
...posthogParams,
|
|
1228
1208
|
model: openAIParams.model,
|
|
@@ -1232,12 +1212,12 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1232
1212
|
// Embeddings don't have output content
|
|
1233
1213
|
latency: 0,
|
|
1234
1214
|
baseURL: this.baseURL,
|
|
1235
|
-
|
|
1215
|
+
modelParameters: getModelParams(body),
|
|
1236
1216
|
httpStatus,
|
|
1237
1217
|
usage: {
|
|
1238
1218
|
inputTokens: 0
|
|
1239
1219
|
},
|
|
1240
|
-
error
|
|
1220
|
+
error
|
|
1241
1221
|
});
|
|
1242
1222
|
throw error;
|
|
1243
1223
|
});
|
|
@@ -1311,8 +1291,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1311
1291
|
const latency = (Date.now() - startTime) / 1000;
|
|
1312
1292
|
const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
|
|
1313
1293
|
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1314
|
-
await
|
|
1315
|
-
client: this.phClient,
|
|
1294
|
+
await captureAiGeneration(this.phClient, {
|
|
1316
1295
|
...posthogParams,
|
|
1317
1296
|
model: openAIParams.model,
|
|
1318
1297
|
provider: 'openai',
|
|
@@ -1321,14 +1300,13 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1321
1300
|
latency,
|
|
1322
1301
|
timeToFirstToken,
|
|
1323
1302
|
baseURL: this.baseURL,
|
|
1324
|
-
|
|
1303
|
+
modelParameters: getModelParams(body),
|
|
1325
1304
|
httpStatus: 200,
|
|
1326
1305
|
usage,
|
|
1327
1306
|
tools: availableTools
|
|
1328
1307
|
});
|
|
1329
1308
|
} catch (error) {
|
|
1330
|
-
|
|
1331
|
-
client: this.phClient,
|
|
1309
|
+
await captureAiGeneration(this.phClient, {
|
|
1332
1310
|
...posthogParams,
|
|
1333
1311
|
model: openAIParams.model,
|
|
1334
1312
|
provider: 'openai',
|
|
@@ -1336,14 +1314,14 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1336
1314
|
output: [],
|
|
1337
1315
|
latency: 0,
|
|
1338
1316
|
baseURL: this.baseURL,
|
|
1339
|
-
|
|
1317
|
+
modelParameters: getModelParams(body),
|
|
1340
1318
|
usage: {
|
|
1341
1319
|
inputTokens: 0,
|
|
1342
1320
|
outputTokens: 0
|
|
1343
1321
|
},
|
|
1344
|
-
error
|
|
1322
|
+
error
|
|
1345
1323
|
});
|
|
1346
|
-
throw
|
|
1324
|
+
throw error;
|
|
1347
1325
|
}
|
|
1348
1326
|
})();
|
|
1349
1327
|
return stream2;
|
|
@@ -1354,8 +1332,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1354
1332
|
const wrappedPromise = parentPromise.then(async result => {
|
|
1355
1333
|
if ('text' in result) {
|
|
1356
1334
|
const latency = (Date.now() - startTime) / 1000;
|
|
1357
|
-
await
|
|
1358
|
-
client: this.phClient,
|
|
1335
|
+
await captureAiGeneration(this.phClient, {
|
|
1359
1336
|
...posthogParams,
|
|
1360
1337
|
model: openAIParams.model,
|
|
1361
1338
|
provider: 'openai',
|
|
@@ -1363,7 +1340,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1363
1340
|
output: result.text,
|
|
1364
1341
|
latency,
|
|
1365
1342
|
baseURL: this.baseURL,
|
|
1366
|
-
|
|
1343
|
+
modelParameters: getModelParams(body),
|
|
1367
1344
|
httpStatus: 200,
|
|
1368
1345
|
usage: {
|
|
1369
1346
|
inputTokens: result.usage?.type === 'tokens' ? result.usage.input_tokens ?? 0 : 0,
|
|
@@ -1374,8 +1351,7 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1374
1351
|
return result;
|
|
1375
1352
|
}
|
|
1376
1353
|
}, async error => {
|
|
1377
|
-
|
|
1378
|
-
client: this.phClient,
|
|
1354
|
+
await captureAiGeneration(this.phClient, {
|
|
1379
1355
|
...posthogParams,
|
|
1380
1356
|
model: openAIParams.model,
|
|
1381
1357
|
provider: 'openai',
|
|
@@ -1383,14 +1359,14 @@ class WrappedTranscriptions extends Transcriptions {
|
|
|
1383
1359
|
output: [],
|
|
1384
1360
|
latency: 0,
|
|
1385
1361
|
baseURL: this.baseURL,
|
|
1386
|
-
|
|
1362
|
+
modelParameters: getModelParams(body),
|
|
1387
1363
|
usage: {
|
|
1388
1364
|
inputTokens: 0,
|
|
1389
1365
|
outputTokens: 0
|
|
1390
1366
|
},
|
|
1391
|
-
error
|
|
1367
|
+
error
|
|
1392
1368
|
});
|
|
1393
|
-
throw
|
|
1369
|
+
throw error;
|
|
1394
1370
|
});
|
|
1395
1371
|
return wrappedPromise;
|
|
1396
1372
|
}
|