@posthog/ai 4.4.0 → 5.0.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 +6 -0
- package/lib/anthropic/index.cjs.map +1 -1
- package/lib/anthropic/index.mjs.map +1 -1
- package/lib/index.cjs +230 -0
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.ts +42 -1
- package/lib/index.mjs +230 -1
- package/lib/index.mjs.map +1 -1
- package/lib/langchain/index.cjs.map +1 -1
- package/lib/langchain/index.mjs.map +1 -1
- package/lib/openai/index.cjs.map +1 -1
- package/lib/openai/index.mjs.map +1 -1
- package/lib/vercel/index.cjs.map +1 -1
- package/lib/vercel/index.mjs.map +1 -1
- package/package.json +11 -1
- package/src/gemini/index.ts +254 -0
- package/src/index.ts +2 -0
- package/src/utils.ts +33 -0
- package/tests/gemini.test.ts +313 -0
package/lib/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { LanguageModelV1 } from 'ai';
|
|
|
6
6
|
import AnthropicOriginal from '@anthropic-ai/sdk';
|
|
7
7
|
import { RequestOptions as RequestOptions$1, APIPromise as APIPromise$1 } from '@anthropic-ai/sdk/core';
|
|
8
8
|
import { Stream as Stream$1 } from '@anthropic-ai/sdk/streaming';
|
|
9
|
+
import { GoogleGenAI } from '@google/genai';
|
|
9
10
|
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
10
11
|
import { Serialized } from '@langchain/core/load/serializable';
|
|
11
12
|
import { ChainValues } from '@langchain/core/utils/types';
|
|
@@ -123,6 +124,46 @@ declare class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
123
124
|
create(body: MessageCreateParamsBase & MonitoringParams, options?: RequestOptions$1): APIPromise$1<Stream$1<RawMessageStreamEvent> | Message>;
|
|
124
125
|
}
|
|
125
126
|
|
|
127
|
+
type GenerateContentRequest = {
|
|
128
|
+
model: string;
|
|
129
|
+
contents: any;
|
|
130
|
+
config?: any;
|
|
131
|
+
[key: string]: any;
|
|
132
|
+
};
|
|
133
|
+
type GenerateContentResponse = {
|
|
134
|
+
text?: string;
|
|
135
|
+
candidates?: any[];
|
|
136
|
+
usageMetadata?: {
|
|
137
|
+
promptTokenCount?: number;
|
|
138
|
+
candidatesTokenCount?: number;
|
|
139
|
+
totalTokenCount?: number;
|
|
140
|
+
};
|
|
141
|
+
[key: string]: any;
|
|
142
|
+
};
|
|
143
|
+
interface MonitoringGeminiConfig {
|
|
144
|
+
apiKey?: string;
|
|
145
|
+
vertexai?: boolean;
|
|
146
|
+
project?: string;
|
|
147
|
+
location?: string;
|
|
148
|
+
apiVersion?: string;
|
|
149
|
+
posthog: PostHog;
|
|
150
|
+
}
|
|
151
|
+
declare class PostHogGoogleGenAI {
|
|
152
|
+
private readonly phClient;
|
|
153
|
+
private readonly client;
|
|
154
|
+
models: WrappedModels;
|
|
155
|
+
constructor(config: MonitoringGeminiConfig);
|
|
156
|
+
}
|
|
157
|
+
declare class WrappedModels {
|
|
158
|
+
private readonly phClient;
|
|
159
|
+
private readonly client;
|
|
160
|
+
constructor(client: GoogleGenAI, phClient: PostHog);
|
|
161
|
+
generateContent(params: GenerateContentRequest & MonitoringParams): Promise<GenerateContentResponse>;
|
|
162
|
+
generateContentStream(params: GenerateContentRequest & MonitoringParams): AsyncGenerator<any, void, unknown>;
|
|
163
|
+
private formatInput;
|
|
164
|
+
private formatOutput;
|
|
165
|
+
}
|
|
166
|
+
|
|
126
167
|
declare class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
127
168
|
name: string;
|
|
128
169
|
private client;
|
|
@@ -181,4 +222,4 @@ declare class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
181
222
|
private parseUsage;
|
|
182
223
|
}
|
|
183
224
|
|
|
184
|
-
export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, wrapVercelLanguageModel as withTracing };
|
|
225
|
+
export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, PostHogGoogleGenAI as GoogleGenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, wrapVercelLanguageModel as withTracing };
|
package/lib/index.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import { v4 } from 'uuid';
|
|
|
4
4
|
import { Buffer } from 'buffer';
|
|
5
5
|
import { experimental_wrapLanguageModel } from 'ai';
|
|
6
6
|
import AnthropicOriginal from '@anthropic-ai/sdk';
|
|
7
|
+
import { GoogleGenAI } from '@google/genai';
|
|
7
8
|
|
|
8
9
|
// limit large outputs by truncating to 200kb (approx 200k bytes)
|
|
9
10
|
const MAX_OUTPUT_SIZE = 200000;
|
|
@@ -1037,6 +1038,234 @@ class WrappedMessages extends AnthropicOriginal.Messages {
|
|
|
1037
1038
|
}
|
|
1038
1039
|
}
|
|
1039
1040
|
|
|
1041
|
+
class PostHogGoogleGenAI {
|
|
1042
|
+
constructor(config) {
|
|
1043
|
+
const {
|
|
1044
|
+
posthog,
|
|
1045
|
+
...geminiConfig
|
|
1046
|
+
} = config;
|
|
1047
|
+
this.phClient = posthog;
|
|
1048
|
+
this.client = new GoogleGenAI(geminiConfig);
|
|
1049
|
+
this.models = new WrappedModels(this.client, this.phClient);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
class WrappedModels {
|
|
1053
|
+
constructor(client, phClient) {
|
|
1054
|
+
this.client = client;
|
|
1055
|
+
this.phClient = phClient;
|
|
1056
|
+
}
|
|
1057
|
+
async generateContent(params) {
|
|
1058
|
+
const {
|
|
1059
|
+
posthogDistinctId,
|
|
1060
|
+
posthogTraceId,
|
|
1061
|
+
posthogProperties,
|
|
1062
|
+
posthogGroups,
|
|
1063
|
+
posthogCaptureImmediate,
|
|
1064
|
+
...geminiParams
|
|
1065
|
+
} = params;
|
|
1066
|
+
const traceId = posthogTraceId ?? v4();
|
|
1067
|
+
const startTime = Date.now();
|
|
1068
|
+
try {
|
|
1069
|
+
const response = await this.client.models.generateContent(geminiParams);
|
|
1070
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1071
|
+
await sendEventToPosthog({
|
|
1072
|
+
client: this.phClient,
|
|
1073
|
+
distinctId: posthogDistinctId ?? traceId,
|
|
1074
|
+
traceId,
|
|
1075
|
+
model: geminiParams.model,
|
|
1076
|
+
provider: 'gemini',
|
|
1077
|
+
input: this.formatInput(geminiParams.contents),
|
|
1078
|
+
output: this.formatOutput(response),
|
|
1079
|
+
latency,
|
|
1080
|
+
baseURL: 'https://generativelanguage.googleapis.com',
|
|
1081
|
+
params: params,
|
|
1082
|
+
httpStatus: 200,
|
|
1083
|
+
usage: {
|
|
1084
|
+
inputTokens: response.usageMetadata?.promptTokenCount ?? 0,
|
|
1085
|
+
outputTokens: response.usageMetadata?.candidatesTokenCount ?? 0
|
|
1086
|
+
},
|
|
1087
|
+
captureImmediate: posthogCaptureImmediate
|
|
1088
|
+
});
|
|
1089
|
+
return response;
|
|
1090
|
+
} catch (error) {
|
|
1091
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1092
|
+
await sendEventToPosthog({
|
|
1093
|
+
client: this.phClient,
|
|
1094
|
+
distinctId: posthogDistinctId ?? traceId,
|
|
1095
|
+
traceId,
|
|
1096
|
+
model: geminiParams.model,
|
|
1097
|
+
provider: 'gemini',
|
|
1098
|
+
input: this.formatInput(geminiParams.contents),
|
|
1099
|
+
output: [],
|
|
1100
|
+
latency,
|
|
1101
|
+
baseURL: 'https://generativelanguage.googleapis.com',
|
|
1102
|
+
params: params,
|
|
1103
|
+
httpStatus: error?.status ?? 500,
|
|
1104
|
+
usage: {
|
|
1105
|
+
inputTokens: 0,
|
|
1106
|
+
outputTokens: 0
|
|
1107
|
+
},
|
|
1108
|
+
isError: true,
|
|
1109
|
+
error: JSON.stringify(error),
|
|
1110
|
+
captureImmediate: posthogCaptureImmediate
|
|
1111
|
+
});
|
|
1112
|
+
throw error;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
async *generateContentStream(params) {
|
|
1116
|
+
const {
|
|
1117
|
+
posthogDistinctId,
|
|
1118
|
+
posthogTraceId,
|
|
1119
|
+
posthogProperties,
|
|
1120
|
+
posthogGroups,
|
|
1121
|
+
posthogCaptureImmediate,
|
|
1122
|
+
...geminiParams
|
|
1123
|
+
} = params;
|
|
1124
|
+
const traceId = posthogTraceId ?? v4();
|
|
1125
|
+
const startTime = Date.now();
|
|
1126
|
+
let accumulatedContent = '';
|
|
1127
|
+
let usage = {
|
|
1128
|
+
inputTokens: 0,
|
|
1129
|
+
outputTokens: 0
|
|
1130
|
+
};
|
|
1131
|
+
try {
|
|
1132
|
+
const stream = await this.client.models.generateContentStream(geminiParams);
|
|
1133
|
+
for await (const chunk of stream) {
|
|
1134
|
+
if (chunk.text) {
|
|
1135
|
+
accumulatedContent += chunk.text;
|
|
1136
|
+
}
|
|
1137
|
+
if (chunk.usageMetadata) {
|
|
1138
|
+
usage = {
|
|
1139
|
+
inputTokens: chunk.usageMetadata.promptTokenCount ?? 0,
|
|
1140
|
+
outputTokens: chunk.usageMetadata.candidatesTokenCount ?? 0
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
yield chunk;
|
|
1144
|
+
}
|
|
1145
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1146
|
+
await sendEventToPosthog({
|
|
1147
|
+
client: this.phClient,
|
|
1148
|
+
distinctId: posthogDistinctId ?? traceId,
|
|
1149
|
+
traceId,
|
|
1150
|
+
model: geminiParams.model,
|
|
1151
|
+
provider: 'gemini',
|
|
1152
|
+
input: this.formatInput(geminiParams.contents),
|
|
1153
|
+
output: [{
|
|
1154
|
+
content: accumulatedContent,
|
|
1155
|
+
role: 'assistant'
|
|
1156
|
+
}],
|
|
1157
|
+
latency,
|
|
1158
|
+
baseURL: 'https://generativelanguage.googleapis.com',
|
|
1159
|
+
params: params,
|
|
1160
|
+
httpStatus: 200,
|
|
1161
|
+
usage,
|
|
1162
|
+
captureImmediate: posthogCaptureImmediate
|
|
1163
|
+
});
|
|
1164
|
+
} catch (error) {
|
|
1165
|
+
const latency = (Date.now() - startTime) / 1000;
|
|
1166
|
+
await sendEventToPosthog({
|
|
1167
|
+
client: this.phClient,
|
|
1168
|
+
distinctId: posthogDistinctId ?? traceId,
|
|
1169
|
+
traceId,
|
|
1170
|
+
model: geminiParams.model,
|
|
1171
|
+
provider: 'gemini',
|
|
1172
|
+
input: this.formatInput(geminiParams.contents),
|
|
1173
|
+
output: [],
|
|
1174
|
+
latency,
|
|
1175
|
+
baseURL: 'https://generativelanguage.googleapis.com',
|
|
1176
|
+
params: params,
|
|
1177
|
+
httpStatus: error?.status ?? 500,
|
|
1178
|
+
usage: {
|
|
1179
|
+
inputTokens: 0,
|
|
1180
|
+
outputTokens: 0
|
|
1181
|
+
},
|
|
1182
|
+
isError: true,
|
|
1183
|
+
error: JSON.stringify(error),
|
|
1184
|
+
captureImmediate: posthogCaptureImmediate
|
|
1185
|
+
});
|
|
1186
|
+
throw error;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
formatInput(contents) {
|
|
1190
|
+
if (typeof contents === 'string') {
|
|
1191
|
+
return [{
|
|
1192
|
+
role: 'user',
|
|
1193
|
+
content: contents
|
|
1194
|
+
}];
|
|
1195
|
+
}
|
|
1196
|
+
if (Array.isArray(contents)) {
|
|
1197
|
+
return contents.map(item => {
|
|
1198
|
+
if (typeof item === 'string') {
|
|
1199
|
+
return {
|
|
1200
|
+
role: 'user',
|
|
1201
|
+
content: item
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
if (item && typeof item === 'object') {
|
|
1205
|
+
if (item.text) {
|
|
1206
|
+
return {
|
|
1207
|
+
role: item.role || 'user',
|
|
1208
|
+
content: item.text
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1211
|
+
if (item.content) {
|
|
1212
|
+
return {
|
|
1213
|
+
role: item.role || 'user',
|
|
1214
|
+
content: item.content
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
return {
|
|
1219
|
+
role: 'user',
|
|
1220
|
+
content: String(item)
|
|
1221
|
+
};
|
|
1222
|
+
});
|
|
1223
|
+
}
|
|
1224
|
+
if (contents && typeof contents === 'object') {
|
|
1225
|
+
if (contents.text) {
|
|
1226
|
+
return [{
|
|
1227
|
+
role: 'user',
|
|
1228
|
+
content: contents.text
|
|
1229
|
+
}];
|
|
1230
|
+
}
|
|
1231
|
+
if (contents.content) {
|
|
1232
|
+
return [{
|
|
1233
|
+
role: 'user',
|
|
1234
|
+
content: contents.content
|
|
1235
|
+
}];
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
return [{
|
|
1239
|
+
role: 'user',
|
|
1240
|
+
content: String(contents)
|
|
1241
|
+
}];
|
|
1242
|
+
}
|
|
1243
|
+
formatOutput(response) {
|
|
1244
|
+
if (response.text) {
|
|
1245
|
+
return [{
|
|
1246
|
+
role: 'assistant',
|
|
1247
|
+
content: response.text
|
|
1248
|
+
}];
|
|
1249
|
+
}
|
|
1250
|
+
if (response.candidates && Array.isArray(response.candidates)) {
|
|
1251
|
+
return response.candidates.map(candidate => {
|
|
1252
|
+
if (candidate.content && candidate.content.parts) {
|
|
1253
|
+
const text = candidate.content.parts.filter(part => part.text).map(part => part.text).join('');
|
|
1254
|
+
return {
|
|
1255
|
+
role: 'assistant',
|
|
1256
|
+
content: text
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
return {
|
|
1260
|
+
role: 'assistant',
|
|
1261
|
+
content: String(candidate)
|
|
1262
|
+
};
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
return [];
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1040
1269
|
var decamelize = function (str, sep) {
|
|
1041
1270
|
if (typeof str !== 'string') {
|
|
1042
1271
|
throw new TypeError('Expected a string');
|
|
@@ -1960,5 +2189,5 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
|
|
|
1960
2189
|
}
|
|
1961
2190
|
}
|
|
1962
2191
|
|
|
1963
|
-
export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, wrapVercelLanguageModel as withTracing };
|
|
2192
|
+
export { PostHogAnthropic as Anthropic, PostHogAzureOpenAI as AzureOpenAI, PostHogGoogleGenAI as GoogleGenAI, LangChainCallbackHandler, PostHogOpenAI as OpenAI, wrapVercelLanguageModel as withTracing };
|
|
1964
2193
|
//# sourceMappingURL=index.mjs.map
|