@posthog/ai
Version:
PostHog Node.js AI integrations
328 lines (319 loc) • 10.6 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var AnthropicOriginal = require('@anthropic-ai/sdk');
var uuid = require('uuid');
var buffer = require('buffer');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var AnthropicOriginal__default = /*#__PURE__*/_interopDefaultLegacy(AnthropicOriginal);
const STRING_FORMAT = 'utf8';
const getModelParams = params => {
if (!params) {
return {};
}
const modelParams = {};
const paramKeys = ['temperature', 'max_tokens', 'max_completion_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'n', 'stop', 'stream', 'streaming'];
for (const key of paramKeys) {
if (key in params && params[key] !== undefined) {
modelParams[key] = params[key];
}
}
return modelParams;
};
const formatResponseAnthropic = response => {
// Example approach if "response.content" holds array of text segments, etc.
const output = [];
for (const choice of response.content ?? []) {
if (choice?.text) {
output.push({
role: 'assistant',
content: choice.text
});
}
}
return output;
};
const mergeSystemPrompt = (params, provider) => {
if (provider == 'anthropic') {
const messages = params.messages || [];
if (!params.system) {
return messages;
}
const systemMessage = params.system;
return [{
role: 'system',
content: systemMessage
}, ...messages];
}
return params.messages;
};
const withPrivacyMode = (client, privacyMode, input) => {
return client.privacy_mode || privacyMode ? null : input;
};
function sanitizeValues(obj) {
if (obj === undefined || obj === null) {
return obj;
}
const jsonSafe = JSON.parse(JSON.stringify(obj));
if (typeof jsonSafe === 'string') {
return buffer.Buffer.from(jsonSafe, STRING_FORMAT).toString(STRING_FORMAT);
} else if (Array.isArray(jsonSafe)) {
return jsonSafe.map(sanitizeValues);
} else if (jsonSafe && typeof jsonSafe === 'object') {
return Object.fromEntries(Object.entries(jsonSafe).map(([k, v]) => [k, sanitizeValues(v)]));
}
return jsonSafe;
}
const sendEventToPosthog = async ({
client,
distinctId,
traceId,
model,
provider,
input,
output,
latency,
baseURL,
params,
httpStatus = 200,
usage = {},
isError = false,
error,
tools,
captureImmediate = false
}) => {
if (!client.capture) {
return Promise.resolve();
}
// sanitize input and output for UTF-8 validity
const safeInput = sanitizeValues(input);
const safeOutput = sanitizeValues(output);
const safeError = sanitizeValues(error);
let errorData = {};
if (isError) {
errorData = {
$ai_is_error: true,
$ai_error: safeError
};
}
let costOverrideData = {};
if (params.posthogCostOverride) {
const inputCostUSD = (params.posthogCostOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
const outputCostUSD = (params.posthogCostOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
costOverrideData = {
$ai_input_cost_usd: inputCostUSD,
$ai_output_cost_usd: outputCostUSD,
$ai_total_cost_usd: inputCostUSD + outputCostUSD
};
}
const additionalTokenValues = {
...(usage.reasoningTokens ? {
$ai_reasoning_tokens: usage.reasoningTokens
} : {}),
...(usage.cacheReadInputTokens ? {
$ai_cache_read_input_tokens: usage.cacheReadInputTokens
} : {}),
...(usage.cacheCreationInputTokens ? {
$ai_cache_creation_input_tokens: usage.cacheCreationInputTokens
} : {})
};
const properties = {
$ai_provider: params.posthogProviderOverride ?? provider,
$ai_model: params.posthogModelOverride ?? model,
$ai_model_parameters: getModelParams(params),
$ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeInput),
$ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
$ai_http_status: httpStatus,
$ai_input_tokens: usage.inputTokens ?? 0,
$ai_output_tokens: usage.outputTokens ?? 0,
...additionalTokenValues,
$ai_latency: latency,
$ai_trace_id: traceId,
$ai_base_url: baseURL,
...params.posthogProperties,
...(distinctId ? {} : {
$process_person_profile: false
}),
...(tools ? {
$ai_tools: tools
} : {}),
...errorData,
...costOverrideData
};
const event = {
distinctId: distinctId ?? traceId,
event: '$ai_generation',
properties,
groups: params.posthogGroups
};
if (captureImmediate) {
// await capture promise to send single event in serverless environments
await client.captureImmediate(event);
} else {
client.capture(event);
}
};
class PostHogAnthropic extends AnthropicOriginal__default["default"] {
constructor(config) {
const {
posthog,
...anthropicConfig
} = config;
super(anthropicConfig);
this.phClient = posthog;
this.messages = new WrappedMessages(this, this.phClient);
}
}
class WrappedMessages extends AnthropicOriginal__default["default"].Messages {
constructor(parentClient, phClient) {
super(parentClient);
this.phClient = phClient;
}
create(body, options) {
const {
posthogDistinctId,
posthogTraceId,
posthogProperties,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
posthogPrivacyMode = false,
posthogGroups,
posthogCaptureImmediate,
...anthropicParams
} = body;
const traceId = posthogTraceId ?? uuid.v4();
const startTime = Date.now();
const parentPromise = super.create(anthropicParams, options);
if (anthropicParams.stream) {
return parentPromise.then(value => {
let accumulatedContent = '';
const usage = {
inputTokens: 0,
outputTokens: 0,
cacheCreationInputTokens: 0,
cacheReadInputTokens: 0
};
if ('tee' in value) {
const [stream1, stream2] = value.tee();
(async () => {
try {
for await (const chunk of stream1) {
if ('delta' in chunk) {
if ('text' in chunk.delta) {
const delta = chunk?.delta?.text ?? '';
accumulatedContent += delta;
}
}
if (chunk.type == 'message_start') {
usage.inputTokens = chunk.message.usage.input_tokens ?? 0;
usage.cacheCreationInputTokens = chunk.message.usage.cache_creation_input_tokens ?? 0;
usage.cacheReadInputTokens = chunk.message.usage.cache_read_input_tokens ?? 0;
}
if ('usage' in chunk) {
usage.outputTokens = chunk.usage.output_tokens ?? 0;
}
}
const latency = (Date.now() - startTime) / 1000;
await sendEventToPosthog({
client: this.phClient,
distinctId: posthogDistinctId ?? traceId,
traceId,
model: anthropicParams.model,
provider: 'anthropic',
input: mergeSystemPrompt(anthropicParams, 'anthropic'),
output: [{
content: accumulatedContent,
role: 'assistant'
}],
latency,
baseURL: this.baseURL ?? '',
params: body,
httpStatus: 200,
usage,
captureImmediate: posthogCaptureImmediate
});
} catch (error) {
// error handling
await sendEventToPosthog({
client: this.phClient,
distinctId: posthogDistinctId ?? traceId,
traceId,
model: anthropicParams.model,
provider: 'anthropic',
input: mergeSystemPrompt(anthropicParams, 'anthropic'),
output: [],
latency: 0,
baseURL: this.baseURL ?? '',
params: body,
httpStatus: error?.status ? error.status : 500,
usage: {
inputTokens: 0,
outputTokens: 0
},
isError: true,
error: JSON.stringify(error),
captureImmediate: posthogCaptureImmediate
});
}
})();
// Return the other stream to the user
return stream2;
}
return value;
});
} else {
const wrappedPromise = parentPromise.then(async result => {
if ('content' in result) {
const latency = (Date.now() - startTime) / 1000;
await sendEventToPosthog({
client: this.phClient,
distinctId: posthogDistinctId ?? traceId,
traceId,
model: anthropicParams.model,
provider: 'anthropic',
input: mergeSystemPrompt(anthropicParams, 'anthropic'),
output: formatResponseAnthropic(result),
latency,
baseURL: this.baseURL ?? '',
params: body,
httpStatus: 200,
usage: {
inputTokens: result.usage.input_tokens ?? 0,
outputTokens: result.usage.output_tokens ?? 0,
cacheCreationInputTokens: result.usage.cache_creation_input_tokens ?? 0,
cacheReadInputTokens: result.usage.cache_read_input_tokens ?? 0
},
captureImmediate: posthogCaptureImmediate
});
}
return result;
}, async error => {
await sendEventToPosthog({
client: this.phClient,
distinctId: posthogDistinctId ?? traceId,
traceId,
model: anthropicParams.model,
provider: 'anthropic',
input: mergeSystemPrompt(anthropicParams, 'anthropic'),
output: [],
latency: 0,
baseURL: this.baseURL ?? '',
params: body,
httpStatus: error?.status ? error.status : 500,
usage: {
inputTokens: 0,
outputTokens: 0
},
isError: true,
error: JSON.stringify(error),
captureImmediate: posthogCaptureImmediate
});
throw error;
});
return wrappedPromise;
}
}
}
exports.Anthropic = PostHogAnthropic;
exports.PostHogAnthropic = PostHogAnthropic;
exports.WrappedMessages = WrappedMessages;
exports["default"] = PostHogAnthropic;
//# sourceMappingURL=index.cjs.map