@posthog/ai 3.0.0 → 3.1.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/CHANGELOG.md +3 -1
- package/lib/index.cjs.js +30 -10
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +7 -4
- package/lib/index.esm.js +30 -10
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-ai/src/utils.d.ts +7 -0
- package/lib/posthog-ai/src/vercel/middleware.d.ts +1 -4
- package/package.json +1 -1
- package/src/langchain/callbacks.ts +10 -4
- package/src/utils.ts +22 -2
- package/src/vercel/middleware.ts +7 -8
package/lib/index.d.ts
CHANGED
|
@@ -20,6 +20,13 @@ interface MonitoringParams {
|
|
|
20
20
|
posthogProperties?: Record<string, any>;
|
|
21
21
|
posthogPrivacyMode?: boolean;
|
|
22
22
|
posthogGroups?: Record<string, any>;
|
|
23
|
+
posthogModelOverride?: string;
|
|
24
|
+
posthogProviderOverride?: string;
|
|
25
|
+
posthogCostOverride?: CostOverride;
|
|
26
|
+
}
|
|
27
|
+
interface CostOverride {
|
|
28
|
+
inputCost: number;
|
|
29
|
+
outputCost: number;
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
type ChatCompletion$1 = OpenAIOrignal.ChatCompletion;
|
|
@@ -88,10 +95,6 @@ interface ClientOptions {
|
|
|
88
95
|
posthogProviderOverride?: string;
|
|
89
96
|
posthogCostOverride?: CostOverride;
|
|
90
97
|
}
|
|
91
|
-
interface CostOverride {
|
|
92
|
-
inputTokens: number;
|
|
93
|
-
outputTokens: number;
|
|
94
|
-
}
|
|
95
98
|
declare const wrapVercelLanguageModel: (model: LanguageModelV1, phClient: PostHog, options: ClientOptions) => LanguageModelV1;
|
|
96
99
|
|
|
97
100
|
type MessageCreateParamsNonStreaming = AnthropicOriginal.Messages.MessageCreateParamsNonStreaming;
|
package/lib/index.esm.js
CHANGED
|
@@ -84,12 +84,22 @@ const sendEventToPosthog = ({
|
|
|
84
84
|
$ai_error: error
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
|
+
let costOverrideData = {};
|
|
88
|
+
if (params.posthogCostOverride) {
|
|
89
|
+
const inputCostUSD = (params.posthogCostOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
|
|
90
|
+
const outputCostUSD = (params.posthogCostOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
|
|
91
|
+
costOverrideData = {
|
|
92
|
+
$ai_input_cost_usd: inputCostUSD,
|
|
93
|
+
$ai_output_cost_usd: outputCostUSD,
|
|
94
|
+
$ai_total_cost_usd: inputCostUSD + outputCostUSD
|
|
95
|
+
};
|
|
96
|
+
}
|
|
87
97
|
client.capture({
|
|
88
98
|
distinctId: distinctId ?? traceId,
|
|
89
99
|
event: '$ai_generation',
|
|
90
100
|
properties: {
|
|
91
|
-
$ai_provider: provider,
|
|
92
|
-
$ai_model: model,
|
|
101
|
+
$ai_provider: params.posthogProviderOverride ?? provider,
|
|
102
|
+
$ai_model: params.posthogModelOverride ?? model,
|
|
93
103
|
$ai_model_parameters: getModelParams(params),
|
|
94
104
|
$ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, input),
|
|
95
105
|
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, output),
|
|
@@ -103,7 +113,8 @@ const sendEventToPosthog = ({
|
|
|
103
113
|
...(distinctId ? {} : {
|
|
104
114
|
$process_person_profile: false
|
|
105
115
|
}),
|
|
106
|
-
...errorData
|
|
116
|
+
...errorData,
|
|
117
|
+
...costOverrideData
|
|
107
118
|
},
|
|
108
119
|
groups: params.posthogGroups
|
|
109
120
|
});
|
|
@@ -485,6 +496,11 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
|
|
|
485
496
|
const modelId = options.posthogModelOverride ?? (result.response?.modelId ? result.response.modelId : model.modelId);
|
|
486
497
|
const provider = options.posthogProviderOverride ?? extractProvider(model);
|
|
487
498
|
const baseURL = ''; // cannot currently get baseURL from vercel
|
|
499
|
+
let content = result.text;
|
|
500
|
+
if (!content) {
|
|
501
|
+
// support generate Object
|
|
502
|
+
content = result.toolCalls?.[0].args || JSON.stringify(result);
|
|
503
|
+
}
|
|
488
504
|
sendEventToPosthog({
|
|
489
505
|
client: phClient,
|
|
490
506
|
distinctId: options.posthogDistinctId,
|
|
@@ -493,7 +509,7 @@ const createInstrumentationMiddleware = (phClient, model, options) => {
|
|
|
493
509
|
provider: provider,
|
|
494
510
|
input: options.posthogPrivacyMode ? '' : mapVercelPrompt(params.prompt),
|
|
495
511
|
output: [{
|
|
496
|
-
content
|
|
512
|
+
content,
|
|
497
513
|
role: 'assistant'
|
|
498
514
|
}],
|
|
499
515
|
latency,
|
|
@@ -1256,9 +1272,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1256
1272
|
this.debug = options.debug || false;
|
|
1257
1273
|
}
|
|
1258
1274
|
// ===== CALLBACK METHODS =====
|
|
1259
|
-
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata,
|
|
1260
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1261
|
-
runType, runName) {
|
|
1275
|
+
handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, runName) {
|
|
1262
1276
|
this._logDebugEvent('on_chain_start', runId, parentRunId, {
|
|
1263
1277
|
inputs,
|
|
1264
1278
|
tags
|
|
@@ -1456,7 +1470,9 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1456
1470
|
const traceId = this._getTraceId(runId);
|
|
1457
1471
|
this._popParentOfRun(runId);
|
|
1458
1472
|
const run = this._popRunMetadata(runId);
|
|
1459
|
-
if (!run)
|
|
1473
|
+
if (!run) {
|
|
1474
|
+
return;
|
|
1475
|
+
}
|
|
1460
1476
|
if ('modelParams' in run) {
|
|
1461
1477
|
console.warn(`Run ${runId} is a generation, but attempted to be captured as a trace/span.`);
|
|
1462
1478
|
return;
|
|
@@ -1565,10 +1581,14 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1565
1581
|
_getLangchainRunName(serialized, ...args) {
|
|
1566
1582
|
if (args && args.length > 0) {
|
|
1567
1583
|
for (const arg of args) {
|
|
1568
|
-
if (arg && typeof arg === 'object' && 'name' in arg)
|
|
1584
|
+
if (arg && typeof arg === 'object' && 'name' in arg) {
|
|
1585
|
+
return arg.name;
|
|
1586
|
+
}
|
|
1569
1587
|
}
|
|
1570
1588
|
}
|
|
1571
|
-
if (serialized && serialized.name)
|
|
1589
|
+
if (serialized && serialized.name) {
|
|
1590
|
+
return serialized.name;
|
|
1591
|
+
}
|
|
1572
1592
|
if (serialized && serialized.id) {
|
|
1573
1593
|
return Array.isArray(serialized.id) ? serialized.id[serialized.id.length - 1] : serialized.id;
|
|
1574
1594
|
}
|