@posthog/ai 7.0.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.
@@ -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 = "7.0.0";
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];
@@ -563,6 +563,8 @@ const Chat = OpenAI.Chat;
563
563
  const Completions = Chat.Completions;
564
564
  const Responses = OpenAI.Responses;
565
565
  const Embeddings = OpenAI.Embeddings;
566
+ const Audio = OpenAI.Audio;
567
+ const Transcriptions = OpenAI.Audio.Transcriptions;
566
568
  class PostHogOpenAI extends OpenAI {
567
569
  constructor(config) {
568
570
  const {
@@ -574,6 +576,7 @@ class PostHogOpenAI extends OpenAI {
574
576
  this.chat = new WrappedChat(this, this.phClient);
575
577
  this.responses = new WrappedResponses(this, this.phClient);
576
578
  this.embeddings = new WrappedEmbeddings(this, this.phClient);
579
+ this.audio = new WrappedAudio(this, this.phClient);
577
580
  }
578
581
  }
579
582
  class WrappedChat extends Chat {
@@ -1092,6 +1095,154 @@ class WrappedEmbeddings extends Embeddings {
1092
1095
  return wrappedPromise;
1093
1096
  }
1094
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
+ }
1095
1246
 
1096
- 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 };
1097
1248
  //# sourceMappingURL=index.mjs.map