@posthog/ai 6.6.0 → 7.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/dist/anthropic/index.cjs +2 -2
- package/dist/anthropic/index.cjs.map +1 -1
- package/dist/anthropic/index.mjs +2 -2
- package/dist/anthropic/index.mjs.map +1 -1
- package/dist/gemini/index.cjs +2 -2
- package/dist/gemini/index.cjs.map +1 -1
- package/dist/gemini/index.mjs +2 -2
- package/dist/gemini/index.mjs.map +1 -1
- package/dist/index.cjs +432 -352
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +19 -0
- package/dist/index.mjs +432 -352
- package/dist/index.mjs.map +1 -1
- package/dist/langchain/index.cjs +290 -347
- package/dist/langchain/index.cjs.map +1 -1
- package/dist/langchain/index.mjs +290 -347
- package/dist/langchain/index.mjs.map +1 -1
- package/dist/openai/index.cjs +161 -7
- package/dist/openai/index.cjs.map +1 -1
- package/dist/openai/index.d.ts +20 -1
- package/dist/openai/index.mjs +160 -8
- package/dist/openai/index.mjs.map +1 -1
- package/dist/vercel/index.cjs +8 -7
- package/dist/vercel/index.cjs.map +1 -1
- package/dist/vercel/index.mjs +8 -7
- package/dist/vercel/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/openai/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { OpenAI } from 'openai';
|
|
|
2
2
|
import { Buffer } from 'buffer';
|
|
3
3
|
import { v4 } from 'uuid';
|
|
4
4
|
|
|
5
|
-
var version = "
|
|
5
|
+
var version = "7.1.0";
|
|
6
6
|
|
|
7
7
|
// Type guards for safer type checking
|
|
8
8
|
|
|
@@ -45,7 +45,7 @@ const getModelParams = params => {
|
|
|
45
45
|
return {};
|
|
46
46
|
}
|
|
47
47
|
const modelParams = {};
|
|
48
|
-
const paramKeys = ['temperature', 'max_tokens', 'max_completion_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'n', 'stop', 'stream', 'streaming'];
|
|
48
|
+
const paramKeys = ['temperature', 'max_tokens', 'max_completion_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'n', 'stop', 'stream', 'streaming', 'language', 'response_format', 'timestamp_granularities'];
|
|
49
49
|
for (const key of paramKeys) {
|
|
50
50
|
if (key in params && params[key] !== undefined) {
|
|
51
51
|
modelParams[key] = params[key];
|
|
@@ -195,13 +195,14 @@ function calculateWebSearchCount(result) {
|
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
197
|
|
|
198
|
-
// Check for annotations with url_citation in choices[].message (OpenAI/Perplexity)
|
|
198
|
+
// Check for annotations with url_citation in choices[].message or choices[].delta (OpenAI/Perplexity)
|
|
199
199
|
if ('choices' in result && Array.isArray(result.choices)) {
|
|
200
200
|
for (const choice of result.choices) {
|
|
201
|
-
if (typeof choice === 'object' && choice !== null
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
201
|
+
if (typeof choice === 'object' && choice !== null) {
|
|
202
|
+
// Check both message (non-streaming) and delta (streaming) for annotations
|
|
203
|
+
const content = ('message' in choice ? choice.message : null) || ('delta' in choice ? choice.delta : null);
|
|
204
|
+
if (typeof content === 'object' && content !== null && 'annotations' in content) {
|
|
205
|
+
const annotations = content.annotations;
|
|
205
206
|
if (Array.isArray(annotations)) {
|
|
206
207
|
const hasUrlCitation = annotations.some(ann => {
|
|
207
208
|
return typeof ann === 'object' && ann !== null && 'type' in ann && ann.type === 'url_citation';
|
|
@@ -562,6 +563,8 @@ const Chat = OpenAI.Chat;
|
|
|
562
563
|
const Completions = Chat.Completions;
|
|
563
564
|
const Responses = OpenAI.Responses;
|
|
564
565
|
const Embeddings = OpenAI.Embeddings;
|
|
566
|
+
const Audio = OpenAI.Audio;
|
|
567
|
+
const Transcriptions = OpenAI.Audio.Transcriptions;
|
|
565
568
|
class PostHogOpenAI extends OpenAI {
|
|
566
569
|
constructor(config) {
|
|
567
570
|
const {
|
|
@@ -573,6 +576,7 @@ class PostHogOpenAI extends OpenAI {
|
|
|
573
576
|
this.chat = new WrappedChat(this, this.phClient);
|
|
574
577
|
this.responses = new WrappedResponses(this, this.phClient);
|
|
575
578
|
this.embeddings = new WrappedEmbeddings(this, this.phClient);
|
|
579
|
+
this.audio = new WrappedAudio(this, this.phClient);
|
|
576
580
|
}
|
|
577
581
|
}
|
|
578
582
|
class WrappedChat extends Chat {
|
|
@@ -1091,6 +1095,154 @@ class WrappedEmbeddings extends Embeddings {
|
|
|
1091
1095
|
return wrappedPromise;
|
|
1092
1096
|
}
|
|
1093
1097
|
}
|
|
1098
|
+
class WrappedAudio extends Audio {
|
|
1099
|
+
constructor(parentClient, phClient) {
|
|
1100
|
+
super(parentClient);
|
|
1101
|
+
this.transcriptions = new WrappedTranscriptions(parentClient, phClient);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
class WrappedTranscriptions extends Transcriptions {
|
|
1105
|
+
constructor(client, phClient) {
|
|
1106
|
+
super(client);
|
|
1107
|
+
this.phClient = phClient;
|
|
1108
|
+
this.baseURL = client.baseURL;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
// --- Overload #1: Non-streaming
|
|
1112
|
+
|
|
1113
|
+
// --- Overload #2: Non-streaming
|
|
1114
|
+
|
|
1115
|
+
// --- Overload #3: Non-streaming
|
|
1116
|
+
|
|
1117
|
+
// --- Overload #4: Non-streaming
|
|
1118
|
+
|
|
1119
|
+
// --- Overload #5: Streaming
|
|
1120
|
+
|
|
1121
|
+
// --- Overload #6: Streaming
|
|
1122
|
+
|
|
1123
|
+
// --- Overload #7: Generic base
|
|
1124
|
+
|
|
1125
|
+
// --- Implementation Signature
|
|
1126
|
+
create(body, options) {
|
|
1127
|
+
const {
|
|
1128
|
+
providerParams: openAIParams,
|
|
1129
|
+
posthogParams
|
|
1130
|
+
} = extractPosthogParams(body);
|
|
1131
|
+
const startTime = Date.now();
|
|
1132
|
+
const parentPromise = openAIParams.stream ? super.create(openAIParams, options) : super.create(openAIParams, options);
|
|
1133
|
+
if (openAIParams.stream) {
|
|
1134
|
+
return parentPromise.then(value => {
|
|
1135
|
+
if ('tee' in value && typeof value.tee === 'function') {
|
|
1136
|
+
const [stream1, stream2] = value.tee();
|
|
1137
|
+
(async () => {
|
|
1138
|
+
try {
|
|
1139
|
+
let finalContent = '';
|
|
1140
|
+
let usage = {
|
|
1141
|
+
inputTokens: 0,
|
|
1142
|
+
outputTokens: 0
|
|
1143
|
+
};
|
|
1144
|
+
const doneEvent = 'transcript.text.done';
|
|
1145
|
+
for await (const chunk of stream1) {
|
|
1146
|
+
if (chunk.type === doneEvent && 'text' in chunk && chunk.text && chunk.text.length > 0) {
|
|
1147
|
+
finalContent = chunk.text;
|
|
1148
|
+
}
|
|
1149
|
+
if ('usage' in chunk && chunk.usage) {
|
|
1150
|
+
usage = {
|
|
1151
|
+
inputTokens: chunk.usage?.type === 'tokens' ? chunk.usage.input_tokens ?? 0 : 0,
|
|
1152
|
+
outputTokens: chunk.usage?.type === 'tokens' ? chunk.usage.output_tokens ?? 0 : 0
|
|
1153
|
+
};
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1157
|
+
const availableTools = extractAvailableToolCalls('openai', openAIParams);
|
|
1158
|
+
await sendEventToPosthog({
|
|
1159
|
+
client: this.phClient,
|
|
1160
|
+
...posthogParams,
|
|
1161
|
+
model: openAIParams.model,
|
|
1162
|
+
provider: 'openai',
|
|
1163
|
+
input: openAIParams.prompt,
|
|
1164
|
+
output: finalContent,
|
|
1165
|
+
latency,
|
|
1166
|
+
baseURL: this.baseURL,
|
|
1167
|
+
params: body,
|
|
1168
|
+
httpStatus: 200,
|
|
1169
|
+
usage,
|
|
1170
|
+
tools: availableTools
|
|
1171
|
+
});
|
|
1172
|
+
} catch (error) {
|
|
1173
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1174
|
+
await sendEventToPosthog({
|
|
1175
|
+
client: this.phClient,
|
|
1176
|
+
...posthogParams,
|
|
1177
|
+
model: openAIParams.model,
|
|
1178
|
+
provider: 'openai',
|
|
1179
|
+
input: openAIParams.prompt,
|
|
1180
|
+
output: [],
|
|
1181
|
+
latency: 0,
|
|
1182
|
+
baseURL: this.baseURL,
|
|
1183
|
+
params: body,
|
|
1184
|
+
httpStatus,
|
|
1185
|
+
usage: {
|
|
1186
|
+
inputTokens: 0,
|
|
1187
|
+
outputTokens: 0
|
|
1188
|
+
},
|
|
1189
|
+
isError: true,
|
|
1190
|
+
error: JSON.stringify(error)
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
})();
|
|
1194
|
+
return stream2;
|
|
1195
|
+
}
|
|
1196
|
+
return value;
|
|
1197
|
+
});
|
|
1198
|
+
} else {
|
|
1199
|
+
const wrappedPromise = parentPromise.then(async result => {
|
|
1200
|
+
if ('text' in result) {
|
|
1201
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1202
|
+
await sendEventToPosthog({
|
|
1203
|
+
client: this.phClient,
|
|
1204
|
+
...posthogParams,
|
|
1205
|
+
model: String(openAIParams.model ?? ''),
|
|
1206
|
+
provider: 'openai',
|
|
1207
|
+
input: openAIParams.prompt,
|
|
1208
|
+
output: result.text,
|
|
1209
|
+
latency,
|
|
1210
|
+
baseURL: this.baseURL,
|
|
1211
|
+
params: body,
|
|
1212
|
+
httpStatus: 200,
|
|
1213
|
+
usage: {
|
|
1214
|
+
inputTokens: result.usage?.type === 'tokens' ? result.usage.input_tokens ?? 0 : 0,
|
|
1215
|
+
outputTokens: result.usage?.type === 'tokens' ? result.usage.output_tokens ?? 0 : 0
|
|
1216
|
+
}
|
|
1217
|
+
});
|
|
1218
|
+
return result;
|
|
1219
|
+
}
|
|
1220
|
+
}, async error => {
|
|
1221
|
+
const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
|
|
1222
|
+
await sendEventToPosthog({
|
|
1223
|
+
client: this.phClient,
|
|
1224
|
+
...posthogParams,
|
|
1225
|
+
model: String(openAIParams.model ?? ''),
|
|
1226
|
+
provider: 'openai',
|
|
1227
|
+
input: openAIParams.prompt,
|
|
1228
|
+
output: [],
|
|
1229
|
+
latency: 0,
|
|
1230
|
+
baseURL: this.baseURL,
|
|
1231
|
+
params: body,
|
|
1232
|
+
httpStatus,
|
|
1233
|
+
usage: {
|
|
1234
|
+
inputTokens: 0,
|
|
1235
|
+
outputTokens: 0
|
|
1236
|
+
},
|
|
1237
|
+
isError: true,
|
|
1238
|
+
error: JSON.stringify(error)
|
|
1239
|
+
});
|
|
1240
|
+
throw error;
|
|
1241
|
+
});
|
|
1242
|
+
return wrappedPromise;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1094
1246
|
|
|
1095
|
-
export { PostHogOpenAI as OpenAI, PostHogOpenAI, WrappedChat, WrappedCompletions, WrappedEmbeddings, WrappedResponses, PostHogOpenAI as default };
|
|
1247
|
+
export { PostHogOpenAI as OpenAI, PostHogOpenAI, WrappedAudio, WrappedChat, WrappedCompletions, WrappedEmbeddings, WrappedResponses, WrappedTranscriptions, PostHogOpenAI as default };
|
|
1096
1248
|
//# sourceMappingURL=index.mjs.map
|