braintrust 0.0.181 → 0.0.182
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/browser.d.mts +1 -0
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +60 -49
- package/dist/browser.mjs +60 -49
- package/dist/cli.js +2 -2
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +60 -49
- package/dist/index.mjs +60 -49
- package/package.json +2 -2
package/dist/browser.d.mts
CHANGED
package/dist/browser.d.ts
CHANGED
package/dist/browser.js
CHANGED
|
@@ -3990,16 +3990,11 @@ function wrapOpenAI(openai) {
|
|
|
3990
3990
|
}
|
|
3991
3991
|
globalThis.__inherited_braintrust_wrap_openai = wrapOpenAI;
|
|
3992
3992
|
function wrapOpenAIv4(openai) {
|
|
3993
|
-
|
|
3994
|
-
|
|
3995
|
-
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
}
|
|
3999
|
-
return baseVal;
|
|
4000
|
-
}
|
|
4001
|
-
});
|
|
4002
|
-
let chatProxy = new Proxy(openai.chat, {
|
|
3993
|
+
const completionProxy = createEndpointProxy(
|
|
3994
|
+
openai.chat.completions,
|
|
3995
|
+
wrapChatCompletion
|
|
3996
|
+
);
|
|
3997
|
+
const chatProxy = new Proxy(openai.chat, {
|
|
4003
3998
|
get(target, name, receiver) {
|
|
4004
3999
|
if (name === "completions") {
|
|
4005
4000
|
return completionProxy;
|
|
@@ -4007,18 +4002,11 @@ function wrapOpenAIv4(openai) {
|
|
|
4007
4002
|
return Reflect.get(target, name, receiver);
|
|
4008
4003
|
}
|
|
4009
4004
|
});
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
const baseVal = Reflect.get(target, name, receiver);
|
|
4013
|
-
if (name === "create") {
|
|
4014
|
-
return wrapEmbeddings(baseVal.bind(target));
|
|
4015
|
-
}
|
|
4016
|
-
return baseVal;
|
|
4017
|
-
}
|
|
4018
|
-
});
|
|
4005
|
+
const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
|
|
4006
|
+
const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
|
|
4019
4007
|
let betaProxy;
|
|
4020
4008
|
if (openai.beta?.chat?.completions?.stream) {
|
|
4021
|
-
|
|
4009
|
+
const betaChatCompletionProxy = new Proxy(openai?.beta?.chat.completions, {
|
|
4022
4010
|
get(target, name, receiver) {
|
|
4023
4011
|
const baseVal = Reflect.get(target, name, receiver);
|
|
4024
4012
|
if (name === "parse") {
|
|
@@ -4029,7 +4017,7 @@ function wrapOpenAIv4(openai) {
|
|
|
4029
4017
|
return baseVal;
|
|
4030
4018
|
}
|
|
4031
4019
|
});
|
|
4032
|
-
|
|
4020
|
+
const betaChatProxy = new Proxy(openai.beta.chat, {
|
|
4033
4021
|
get(target, name, receiver) {
|
|
4034
4022
|
if (name === "completions") {
|
|
4035
4023
|
return betaChatCompletionProxy;
|
|
@@ -4046,7 +4034,7 @@ function wrapOpenAIv4(openai) {
|
|
|
4046
4034
|
}
|
|
4047
4035
|
});
|
|
4048
4036
|
}
|
|
4049
|
-
|
|
4037
|
+
return new Proxy(openai, {
|
|
4050
4038
|
get(target, name, receiver) {
|
|
4051
4039
|
if (name === "chat") {
|
|
4052
4040
|
return chatProxy;
|
|
@@ -4054,13 +4042,15 @@ function wrapOpenAIv4(openai) {
|
|
|
4054
4042
|
if (name === "embeddings") {
|
|
4055
4043
|
return embeddingProxy;
|
|
4056
4044
|
}
|
|
4045
|
+
if (name === "moderations") {
|
|
4046
|
+
return moderationProxy;
|
|
4047
|
+
}
|
|
4057
4048
|
if (name === "beta" && betaProxy) {
|
|
4058
4049
|
return betaProxy;
|
|
4059
4050
|
}
|
|
4060
4051
|
return Reflect.get(target, name, receiver);
|
|
4061
4052
|
}
|
|
4062
4053
|
});
|
|
4063
|
-
return proxy;
|
|
4064
4054
|
}
|
|
4065
4055
|
function logCompletionResponse(startTime, response, span) {
|
|
4066
4056
|
span.log({
|
|
@@ -4209,7 +4199,7 @@ function wrapChatCompletion(completion) {
|
|
|
4209
4199
|
}
|
|
4210
4200
|
};
|
|
4211
4201
|
}
|
|
4212
|
-
function
|
|
4202
|
+
function parseBaseParams(allParams, inputField) {
|
|
4213
4203
|
const { span_info, ...params } = allParams;
|
|
4214
4204
|
const { metadata: spanInfoMetadata, ...spanInfoRest } = span_info ?? {};
|
|
4215
4205
|
let ret = {
|
|
@@ -4218,10 +4208,12 @@ function parseChatCompletionParams(allParams) {
|
|
|
4218
4208
|
metadata: spanInfoMetadata
|
|
4219
4209
|
}
|
|
4220
4210
|
};
|
|
4221
|
-
const
|
|
4222
|
-
|
|
4211
|
+
const input = params[inputField];
|
|
4212
|
+
const paramsRest = { ...params };
|
|
4213
|
+
delete paramsRest[inputField];
|
|
4214
|
+
return (0, import_core3.mergeDicts)(ret, { event: { input, metadata: paramsRest } });
|
|
4223
4215
|
}
|
|
4224
|
-
function
|
|
4216
|
+
function createApiWrapper(name, create, processResponse, parseParams) {
|
|
4225
4217
|
return async (allParams, options) => {
|
|
4226
4218
|
const { span_info: _, ...params } = allParams;
|
|
4227
4219
|
return traced(
|
|
@@ -4231,42 +4223,61 @@ function wrapEmbeddings(create) {
|
|
|
4231
4223
|
options
|
|
4232
4224
|
).withResponse();
|
|
4233
4225
|
logHeaders(response, span);
|
|
4234
|
-
|
|
4235
|
-
span.log({
|
|
4236
|
-
// TODO: Add a flag to control whether to log the full embedding vector,
|
|
4237
|
-
// possibly w/ JSON compression.
|
|
4238
|
-
output: { embedding_length },
|
|
4239
|
-
metrics: {
|
|
4240
|
-
tokens: result.usage?.total_tokens,
|
|
4241
|
-
prompt_tokens: result.usage?.prompt_tokens
|
|
4242
|
-
}
|
|
4243
|
-
});
|
|
4226
|
+
processResponse(result, span);
|
|
4244
4227
|
return result;
|
|
4245
4228
|
},
|
|
4246
4229
|
(0, import_core3.mergeDicts)(
|
|
4247
4230
|
{
|
|
4248
|
-
name
|
|
4231
|
+
name,
|
|
4249
4232
|
spanAttributes: {
|
|
4250
4233
|
type: import_core2.SpanTypeAttribute.LLM
|
|
4251
4234
|
}
|
|
4252
4235
|
},
|
|
4253
|
-
|
|
4236
|
+
parseParams(allParams)
|
|
4254
4237
|
)
|
|
4255
4238
|
);
|
|
4256
4239
|
};
|
|
4257
4240
|
}
|
|
4258
|
-
function
|
|
4259
|
-
|
|
4260
|
-
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4241
|
+
function createEndpointProxy(target, wrapperFn) {
|
|
4242
|
+
return new Proxy(target, {
|
|
4243
|
+
get(target2, name, receiver) {
|
|
4244
|
+
const baseVal = Reflect.get(target2, name, receiver);
|
|
4245
|
+
if (name === "create") {
|
|
4246
|
+
return wrapperFn(baseVal.bind(target2));
|
|
4247
|
+
}
|
|
4248
|
+
return baseVal;
|
|
4265
4249
|
}
|
|
4266
|
-
};
|
|
4267
|
-
|
|
4268
|
-
|
|
4250
|
+
});
|
|
4251
|
+
}
|
|
4252
|
+
function parseChatCompletionParams(params) {
|
|
4253
|
+
return parseBaseParams(params, "messages");
|
|
4254
|
+
}
|
|
4255
|
+
function processEmbeddingResponse(result, span) {
|
|
4256
|
+
span.log({
|
|
4257
|
+
output: { embedding_length: result.data[0].embedding.length },
|
|
4258
|
+
metrics: {
|
|
4259
|
+
tokens: result.usage?.total_tokens,
|
|
4260
|
+
prompt_tokens: result.usage?.prompt_tokens
|
|
4261
|
+
}
|
|
4262
|
+
});
|
|
4263
|
+
}
|
|
4264
|
+
function processModerationResponse(result, span) {
|
|
4265
|
+
span.log({
|
|
4266
|
+
output: result.results
|
|
4267
|
+
});
|
|
4269
4268
|
}
|
|
4269
|
+
var wrapEmbeddings = (create) => createApiWrapper(
|
|
4270
|
+
"Embedding",
|
|
4271
|
+
create,
|
|
4272
|
+
processEmbeddingResponse,
|
|
4273
|
+
(params) => parseBaseParams(params, "input")
|
|
4274
|
+
);
|
|
4275
|
+
var wrapModerations = (create) => createApiWrapper(
|
|
4276
|
+
"Moderation",
|
|
4277
|
+
create,
|
|
4278
|
+
processModerationResponse,
|
|
4279
|
+
(params) => parseBaseParams(params, "input")
|
|
4280
|
+
);
|
|
4270
4281
|
function postprocessStreamingResults(allResults) {
|
|
4271
4282
|
let role = void 0;
|
|
4272
4283
|
let content = void 0;
|
package/dist/browser.mjs
CHANGED
|
@@ -3940,16 +3940,11 @@ function wrapOpenAI(openai) {
|
|
|
3940
3940
|
}
|
|
3941
3941
|
globalThis.__inherited_braintrust_wrap_openai = wrapOpenAI;
|
|
3942
3942
|
function wrapOpenAIv4(openai) {
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
}
|
|
3949
|
-
return baseVal;
|
|
3950
|
-
}
|
|
3951
|
-
});
|
|
3952
|
-
let chatProxy = new Proxy(openai.chat, {
|
|
3943
|
+
const completionProxy = createEndpointProxy(
|
|
3944
|
+
openai.chat.completions,
|
|
3945
|
+
wrapChatCompletion
|
|
3946
|
+
);
|
|
3947
|
+
const chatProxy = new Proxy(openai.chat, {
|
|
3953
3948
|
get(target, name, receiver) {
|
|
3954
3949
|
if (name === "completions") {
|
|
3955
3950
|
return completionProxy;
|
|
@@ -3957,18 +3952,11 @@ function wrapOpenAIv4(openai) {
|
|
|
3957
3952
|
return Reflect.get(target, name, receiver);
|
|
3958
3953
|
}
|
|
3959
3954
|
});
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
const baseVal = Reflect.get(target, name, receiver);
|
|
3963
|
-
if (name === "create") {
|
|
3964
|
-
return wrapEmbeddings(baseVal.bind(target));
|
|
3965
|
-
}
|
|
3966
|
-
return baseVal;
|
|
3967
|
-
}
|
|
3968
|
-
});
|
|
3955
|
+
const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
|
|
3956
|
+
const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
|
|
3969
3957
|
let betaProxy;
|
|
3970
3958
|
if (openai.beta?.chat?.completions?.stream) {
|
|
3971
|
-
|
|
3959
|
+
const betaChatCompletionProxy = new Proxy(openai?.beta?.chat.completions, {
|
|
3972
3960
|
get(target, name, receiver) {
|
|
3973
3961
|
const baseVal = Reflect.get(target, name, receiver);
|
|
3974
3962
|
if (name === "parse") {
|
|
@@ -3979,7 +3967,7 @@ function wrapOpenAIv4(openai) {
|
|
|
3979
3967
|
return baseVal;
|
|
3980
3968
|
}
|
|
3981
3969
|
});
|
|
3982
|
-
|
|
3970
|
+
const betaChatProxy = new Proxy(openai.beta.chat, {
|
|
3983
3971
|
get(target, name, receiver) {
|
|
3984
3972
|
if (name === "completions") {
|
|
3985
3973
|
return betaChatCompletionProxy;
|
|
@@ -3996,7 +3984,7 @@ function wrapOpenAIv4(openai) {
|
|
|
3996
3984
|
}
|
|
3997
3985
|
});
|
|
3998
3986
|
}
|
|
3999
|
-
|
|
3987
|
+
return new Proxy(openai, {
|
|
4000
3988
|
get(target, name, receiver) {
|
|
4001
3989
|
if (name === "chat") {
|
|
4002
3990
|
return chatProxy;
|
|
@@ -4004,13 +3992,15 @@ function wrapOpenAIv4(openai) {
|
|
|
4004
3992
|
if (name === "embeddings") {
|
|
4005
3993
|
return embeddingProxy;
|
|
4006
3994
|
}
|
|
3995
|
+
if (name === "moderations") {
|
|
3996
|
+
return moderationProxy;
|
|
3997
|
+
}
|
|
4007
3998
|
if (name === "beta" && betaProxy) {
|
|
4008
3999
|
return betaProxy;
|
|
4009
4000
|
}
|
|
4010
4001
|
return Reflect.get(target, name, receiver);
|
|
4011
4002
|
}
|
|
4012
4003
|
});
|
|
4013
|
-
return proxy;
|
|
4014
4004
|
}
|
|
4015
4005
|
function logCompletionResponse(startTime, response, span) {
|
|
4016
4006
|
span.log({
|
|
@@ -4159,7 +4149,7 @@ function wrapChatCompletion(completion) {
|
|
|
4159
4149
|
}
|
|
4160
4150
|
};
|
|
4161
4151
|
}
|
|
4162
|
-
function
|
|
4152
|
+
function parseBaseParams(allParams, inputField) {
|
|
4163
4153
|
const { span_info, ...params } = allParams;
|
|
4164
4154
|
const { metadata: spanInfoMetadata, ...spanInfoRest } = span_info ?? {};
|
|
4165
4155
|
let ret = {
|
|
@@ -4168,10 +4158,12 @@ function parseChatCompletionParams(allParams) {
|
|
|
4168
4158
|
metadata: spanInfoMetadata
|
|
4169
4159
|
}
|
|
4170
4160
|
};
|
|
4171
|
-
const
|
|
4172
|
-
|
|
4161
|
+
const input = params[inputField];
|
|
4162
|
+
const paramsRest = { ...params };
|
|
4163
|
+
delete paramsRest[inputField];
|
|
4164
|
+
return mergeDicts2(ret, { event: { input, metadata: paramsRest } });
|
|
4173
4165
|
}
|
|
4174
|
-
function
|
|
4166
|
+
function createApiWrapper(name, create, processResponse, parseParams) {
|
|
4175
4167
|
return async (allParams, options) => {
|
|
4176
4168
|
const { span_info: _, ...params } = allParams;
|
|
4177
4169
|
return traced(
|
|
@@ -4181,42 +4173,61 @@ function wrapEmbeddings(create) {
|
|
|
4181
4173
|
options
|
|
4182
4174
|
).withResponse();
|
|
4183
4175
|
logHeaders(response, span);
|
|
4184
|
-
|
|
4185
|
-
span.log({
|
|
4186
|
-
// TODO: Add a flag to control whether to log the full embedding vector,
|
|
4187
|
-
// possibly w/ JSON compression.
|
|
4188
|
-
output: { embedding_length },
|
|
4189
|
-
metrics: {
|
|
4190
|
-
tokens: result.usage?.total_tokens,
|
|
4191
|
-
prompt_tokens: result.usage?.prompt_tokens
|
|
4192
|
-
}
|
|
4193
|
-
});
|
|
4176
|
+
processResponse(result, span);
|
|
4194
4177
|
return result;
|
|
4195
4178
|
},
|
|
4196
4179
|
mergeDicts2(
|
|
4197
4180
|
{
|
|
4198
|
-
name
|
|
4181
|
+
name,
|
|
4199
4182
|
spanAttributes: {
|
|
4200
4183
|
type: SpanTypeAttribute2.LLM
|
|
4201
4184
|
}
|
|
4202
4185
|
},
|
|
4203
|
-
|
|
4186
|
+
parseParams(allParams)
|
|
4204
4187
|
)
|
|
4205
4188
|
);
|
|
4206
4189
|
};
|
|
4207
4190
|
}
|
|
4208
|
-
function
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4191
|
+
function createEndpointProxy(target, wrapperFn) {
|
|
4192
|
+
return new Proxy(target, {
|
|
4193
|
+
get(target2, name, receiver) {
|
|
4194
|
+
const baseVal = Reflect.get(target2, name, receiver);
|
|
4195
|
+
if (name === "create") {
|
|
4196
|
+
return wrapperFn(baseVal.bind(target2));
|
|
4197
|
+
}
|
|
4198
|
+
return baseVal;
|
|
4215
4199
|
}
|
|
4216
|
-
};
|
|
4217
|
-
|
|
4218
|
-
|
|
4200
|
+
});
|
|
4201
|
+
}
|
|
4202
|
+
function parseChatCompletionParams(params) {
|
|
4203
|
+
return parseBaseParams(params, "messages");
|
|
4204
|
+
}
|
|
4205
|
+
function processEmbeddingResponse(result, span) {
|
|
4206
|
+
span.log({
|
|
4207
|
+
output: { embedding_length: result.data[0].embedding.length },
|
|
4208
|
+
metrics: {
|
|
4209
|
+
tokens: result.usage?.total_tokens,
|
|
4210
|
+
prompt_tokens: result.usage?.prompt_tokens
|
|
4211
|
+
}
|
|
4212
|
+
});
|
|
4213
|
+
}
|
|
4214
|
+
function processModerationResponse(result, span) {
|
|
4215
|
+
span.log({
|
|
4216
|
+
output: result.results
|
|
4217
|
+
});
|
|
4219
4218
|
}
|
|
4219
|
+
var wrapEmbeddings = (create) => createApiWrapper(
|
|
4220
|
+
"Embedding",
|
|
4221
|
+
create,
|
|
4222
|
+
processEmbeddingResponse,
|
|
4223
|
+
(params) => parseBaseParams(params, "input")
|
|
4224
|
+
);
|
|
4225
|
+
var wrapModerations = (create) => createApiWrapper(
|
|
4226
|
+
"Moderation",
|
|
4227
|
+
create,
|
|
4228
|
+
processModerationResponse,
|
|
4229
|
+
(params) => parseBaseParams(params, "input")
|
|
4230
|
+
);
|
|
4220
4231
|
function postprocessStreamingResults(allResults) {
|
|
4221
4232
|
let role = void 0;
|
|
4222
4233
|
let content = void 0;
|
package/dist/cli.js
CHANGED
|
@@ -1236,7 +1236,7 @@ var require_package = __commonJS({
|
|
|
1236
1236
|
"package.json"(exports2, module2) {
|
|
1237
1237
|
module2.exports = {
|
|
1238
1238
|
name: "braintrust",
|
|
1239
|
-
version: "0.0.
|
|
1239
|
+
version: "0.0.182",
|
|
1240
1240
|
description: "SDK for integrating Braintrust",
|
|
1241
1241
|
repository: {
|
|
1242
1242
|
type: "git",
|
|
@@ -1311,7 +1311,7 @@ var require_package = __commonJS({
|
|
|
1311
1311
|
},
|
|
1312
1312
|
dependencies: {
|
|
1313
1313
|
"@ai-sdk/provider": "^1.0.1",
|
|
1314
|
-
"@braintrust/core": "0.0.
|
|
1314
|
+
"@braintrust/core": "0.0.76",
|
|
1315
1315
|
"@next/env": "^14.2.3",
|
|
1316
1316
|
"@vercel/functions": "^1.0.2",
|
|
1317
1317
|
ai: "^3.2.16",
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -6190,16 +6190,11 @@ function wrapOpenAI(openai) {
|
|
|
6190
6190
|
}
|
|
6191
6191
|
globalThis.__inherited_braintrust_wrap_openai = wrapOpenAI;
|
|
6192
6192
|
function wrapOpenAIv4(openai) {
|
|
6193
|
-
|
|
6194
|
-
|
|
6195
|
-
|
|
6196
|
-
|
|
6197
|
-
|
|
6198
|
-
}
|
|
6199
|
-
return baseVal;
|
|
6200
|
-
}
|
|
6201
|
-
});
|
|
6202
|
-
let chatProxy = new Proxy(openai.chat, {
|
|
6193
|
+
const completionProxy = createEndpointProxy(
|
|
6194
|
+
openai.chat.completions,
|
|
6195
|
+
wrapChatCompletion
|
|
6196
|
+
);
|
|
6197
|
+
const chatProxy = new Proxy(openai.chat, {
|
|
6203
6198
|
get(target, name, receiver) {
|
|
6204
6199
|
if (name === "completions") {
|
|
6205
6200
|
return completionProxy;
|
|
@@ -6207,18 +6202,11 @@ function wrapOpenAIv4(openai) {
|
|
|
6207
6202
|
return Reflect.get(target, name, receiver);
|
|
6208
6203
|
}
|
|
6209
6204
|
});
|
|
6210
|
-
|
|
6211
|
-
|
|
6212
|
-
const baseVal = Reflect.get(target, name, receiver);
|
|
6213
|
-
if (name === "create") {
|
|
6214
|
-
return wrapEmbeddings(baseVal.bind(target));
|
|
6215
|
-
}
|
|
6216
|
-
return baseVal;
|
|
6217
|
-
}
|
|
6218
|
-
});
|
|
6205
|
+
const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
|
|
6206
|
+
const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
|
|
6219
6207
|
let betaProxy;
|
|
6220
6208
|
if (openai.beta?.chat?.completions?.stream) {
|
|
6221
|
-
|
|
6209
|
+
const betaChatCompletionProxy = new Proxy(openai?.beta?.chat.completions, {
|
|
6222
6210
|
get(target, name, receiver) {
|
|
6223
6211
|
const baseVal = Reflect.get(target, name, receiver);
|
|
6224
6212
|
if (name === "parse") {
|
|
@@ -6229,7 +6217,7 @@ function wrapOpenAIv4(openai) {
|
|
|
6229
6217
|
return baseVal;
|
|
6230
6218
|
}
|
|
6231
6219
|
});
|
|
6232
|
-
|
|
6220
|
+
const betaChatProxy = new Proxy(openai.beta.chat, {
|
|
6233
6221
|
get(target, name, receiver) {
|
|
6234
6222
|
if (name === "completions") {
|
|
6235
6223
|
return betaChatCompletionProxy;
|
|
@@ -6246,7 +6234,7 @@ function wrapOpenAIv4(openai) {
|
|
|
6246
6234
|
}
|
|
6247
6235
|
});
|
|
6248
6236
|
}
|
|
6249
|
-
|
|
6237
|
+
return new Proxy(openai, {
|
|
6250
6238
|
get(target, name, receiver) {
|
|
6251
6239
|
if (name === "chat") {
|
|
6252
6240
|
return chatProxy;
|
|
@@ -6254,13 +6242,15 @@ function wrapOpenAIv4(openai) {
|
|
|
6254
6242
|
if (name === "embeddings") {
|
|
6255
6243
|
return embeddingProxy;
|
|
6256
6244
|
}
|
|
6245
|
+
if (name === "moderations") {
|
|
6246
|
+
return moderationProxy;
|
|
6247
|
+
}
|
|
6257
6248
|
if (name === "beta" && betaProxy) {
|
|
6258
6249
|
return betaProxy;
|
|
6259
6250
|
}
|
|
6260
6251
|
return Reflect.get(target, name, receiver);
|
|
6261
6252
|
}
|
|
6262
6253
|
});
|
|
6263
|
-
return proxy;
|
|
6264
6254
|
}
|
|
6265
6255
|
function logCompletionResponse(startTime, response, span) {
|
|
6266
6256
|
span.log({
|
|
@@ -6409,7 +6399,7 @@ function wrapChatCompletion(completion) {
|
|
|
6409
6399
|
}
|
|
6410
6400
|
};
|
|
6411
6401
|
}
|
|
6412
|
-
function
|
|
6402
|
+
function parseBaseParams(allParams, inputField) {
|
|
6413
6403
|
const { span_info, ...params } = allParams;
|
|
6414
6404
|
const { metadata: spanInfoMetadata, ...spanInfoRest } = span_info ?? {};
|
|
6415
6405
|
let ret = {
|
|
@@ -6418,10 +6408,12 @@ function parseChatCompletionParams(allParams) {
|
|
|
6418
6408
|
metadata: spanInfoMetadata
|
|
6419
6409
|
}
|
|
6420
6410
|
};
|
|
6421
|
-
const
|
|
6422
|
-
|
|
6411
|
+
const input = params[inputField];
|
|
6412
|
+
const paramsRest = { ...params };
|
|
6413
|
+
delete paramsRest[inputField];
|
|
6414
|
+
return (0, import_core4.mergeDicts)(ret, { event: { input, metadata: paramsRest } });
|
|
6423
6415
|
}
|
|
6424
|
-
function
|
|
6416
|
+
function createApiWrapper(name, create, processResponse, parseParams) {
|
|
6425
6417
|
return async (allParams, options) => {
|
|
6426
6418
|
const { span_info: _, ...params } = allParams;
|
|
6427
6419
|
return traced(
|
|
@@ -6431,42 +6423,61 @@ function wrapEmbeddings(create) {
|
|
|
6431
6423
|
options
|
|
6432
6424
|
).withResponse();
|
|
6433
6425
|
logHeaders(response, span);
|
|
6434
|
-
|
|
6435
|
-
span.log({
|
|
6436
|
-
// TODO: Add a flag to control whether to log the full embedding vector,
|
|
6437
|
-
// possibly w/ JSON compression.
|
|
6438
|
-
output: { embedding_length },
|
|
6439
|
-
metrics: {
|
|
6440
|
-
tokens: result.usage?.total_tokens,
|
|
6441
|
-
prompt_tokens: result.usage?.prompt_tokens
|
|
6442
|
-
}
|
|
6443
|
-
});
|
|
6426
|
+
processResponse(result, span);
|
|
6444
6427
|
return result;
|
|
6445
6428
|
},
|
|
6446
6429
|
(0, import_core4.mergeDicts)(
|
|
6447
6430
|
{
|
|
6448
|
-
name
|
|
6431
|
+
name,
|
|
6449
6432
|
spanAttributes: {
|
|
6450
6433
|
type: import_core3.SpanTypeAttribute.LLM
|
|
6451
6434
|
}
|
|
6452
6435
|
},
|
|
6453
|
-
|
|
6436
|
+
parseParams(allParams)
|
|
6454
6437
|
)
|
|
6455
6438
|
);
|
|
6456
6439
|
};
|
|
6457
6440
|
}
|
|
6458
|
-
function
|
|
6459
|
-
|
|
6460
|
-
|
|
6461
|
-
|
|
6462
|
-
|
|
6463
|
-
|
|
6464
|
-
|
|
6441
|
+
function createEndpointProxy(target, wrapperFn) {
|
|
6442
|
+
return new Proxy(target, {
|
|
6443
|
+
get(target2, name, receiver) {
|
|
6444
|
+
const baseVal = Reflect.get(target2, name, receiver);
|
|
6445
|
+
if (name === "create") {
|
|
6446
|
+
return wrapperFn(baseVal.bind(target2));
|
|
6447
|
+
}
|
|
6448
|
+
return baseVal;
|
|
6465
6449
|
}
|
|
6466
|
-
};
|
|
6467
|
-
|
|
6468
|
-
|
|
6450
|
+
});
|
|
6451
|
+
}
|
|
6452
|
+
function parseChatCompletionParams(params) {
|
|
6453
|
+
return parseBaseParams(params, "messages");
|
|
6454
|
+
}
|
|
6455
|
+
function processEmbeddingResponse(result, span) {
|
|
6456
|
+
span.log({
|
|
6457
|
+
output: { embedding_length: result.data[0].embedding.length },
|
|
6458
|
+
metrics: {
|
|
6459
|
+
tokens: result.usage?.total_tokens,
|
|
6460
|
+
prompt_tokens: result.usage?.prompt_tokens
|
|
6461
|
+
}
|
|
6462
|
+
});
|
|
6463
|
+
}
|
|
6464
|
+
function processModerationResponse(result, span) {
|
|
6465
|
+
span.log({
|
|
6466
|
+
output: result.results
|
|
6467
|
+
});
|
|
6469
6468
|
}
|
|
6469
|
+
var wrapEmbeddings = (create) => createApiWrapper(
|
|
6470
|
+
"Embedding",
|
|
6471
|
+
create,
|
|
6472
|
+
processEmbeddingResponse,
|
|
6473
|
+
(params) => parseBaseParams(params, "input")
|
|
6474
|
+
);
|
|
6475
|
+
var wrapModerations = (create) => createApiWrapper(
|
|
6476
|
+
"Moderation",
|
|
6477
|
+
create,
|
|
6478
|
+
processModerationResponse,
|
|
6479
|
+
(params) => parseBaseParams(params, "input")
|
|
6480
|
+
);
|
|
6470
6481
|
function postprocessStreamingResults(allResults) {
|
|
6471
6482
|
let role = void 0;
|
|
6472
6483
|
let content = void 0;
|
package/dist/index.mjs
CHANGED
|
@@ -6126,16 +6126,11 @@ function wrapOpenAI(openai) {
|
|
|
6126
6126
|
}
|
|
6127
6127
|
globalThis.__inherited_braintrust_wrap_openai = wrapOpenAI;
|
|
6128
6128
|
function wrapOpenAIv4(openai) {
|
|
6129
|
-
|
|
6130
|
-
|
|
6131
|
-
|
|
6132
|
-
|
|
6133
|
-
|
|
6134
|
-
}
|
|
6135
|
-
return baseVal;
|
|
6136
|
-
}
|
|
6137
|
-
});
|
|
6138
|
-
let chatProxy = new Proxy(openai.chat, {
|
|
6129
|
+
const completionProxy = createEndpointProxy(
|
|
6130
|
+
openai.chat.completions,
|
|
6131
|
+
wrapChatCompletion
|
|
6132
|
+
);
|
|
6133
|
+
const chatProxy = new Proxy(openai.chat, {
|
|
6139
6134
|
get(target, name, receiver) {
|
|
6140
6135
|
if (name === "completions") {
|
|
6141
6136
|
return completionProxy;
|
|
@@ -6143,18 +6138,11 @@ function wrapOpenAIv4(openai) {
|
|
|
6143
6138
|
return Reflect.get(target, name, receiver);
|
|
6144
6139
|
}
|
|
6145
6140
|
});
|
|
6146
|
-
|
|
6147
|
-
|
|
6148
|
-
const baseVal = Reflect.get(target, name, receiver);
|
|
6149
|
-
if (name === "create") {
|
|
6150
|
-
return wrapEmbeddings(baseVal.bind(target));
|
|
6151
|
-
}
|
|
6152
|
-
return baseVal;
|
|
6153
|
-
}
|
|
6154
|
-
});
|
|
6141
|
+
const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
|
|
6142
|
+
const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
|
|
6155
6143
|
let betaProxy;
|
|
6156
6144
|
if (openai.beta?.chat?.completions?.stream) {
|
|
6157
|
-
|
|
6145
|
+
const betaChatCompletionProxy = new Proxy(openai?.beta?.chat.completions, {
|
|
6158
6146
|
get(target, name, receiver) {
|
|
6159
6147
|
const baseVal = Reflect.get(target, name, receiver);
|
|
6160
6148
|
if (name === "parse") {
|
|
@@ -6165,7 +6153,7 @@ function wrapOpenAIv4(openai) {
|
|
|
6165
6153
|
return baseVal;
|
|
6166
6154
|
}
|
|
6167
6155
|
});
|
|
6168
|
-
|
|
6156
|
+
const betaChatProxy = new Proxy(openai.beta.chat, {
|
|
6169
6157
|
get(target, name, receiver) {
|
|
6170
6158
|
if (name === "completions") {
|
|
6171
6159
|
return betaChatCompletionProxy;
|
|
@@ -6182,7 +6170,7 @@ function wrapOpenAIv4(openai) {
|
|
|
6182
6170
|
}
|
|
6183
6171
|
});
|
|
6184
6172
|
}
|
|
6185
|
-
|
|
6173
|
+
return new Proxy(openai, {
|
|
6186
6174
|
get(target, name, receiver) {
|
|
6187
6175
|
if (name === "chat") {
|
|
6188
6176
|
return chatProxy;
|
|
@@ -6190,13 +6178,15 @@ function wrapOpenAIv4(openai) {
|
|
|
6190
6178
|
if (name === "embeddings") {
|
|
6191
6179
|
return embeddingProxy;
|
|
6192
6180
|
}
|
|
6181
|
+
if (name === "moderations") {
|
|
6182
|
+
return moderationProxy;
|
|
6183
|
+
}
|
|
6193
6184
|
if (name === "beta" && betaProxy) {
|
|
6194
6185
|
return betaProxy;
|
|
6195
6186
|
}
|
|
6196
6187
|
return Reflect.get(target, name, receiver);
|
|
6197
6188
|
}
|
|
6198
6189
|
});
|
|
6199
|
-
return proxy;
|
|
6200
6190
|
}
|
|
6201
6191
|
function logCompletionResponse(startTime, response, span) {
|
|
6202
6192
|
span.log({
|
|
@@ -6345,7 +6335,7 @@ function wrapChatCompletion(completion) {
|
|
|
6345
6335
|
}
|
|
6346
6336
|
};
|
|
6347
6337
|
}
|
|
6348
|
-
function
|
|
6338
|
+
function parseBaseParams(allParams, inputField) {
|
|
6349
6339
|
const { span_info, ...params } = allParams;
|
|
6350
6340
|
const { metadata: spanInfoMetadata, ...spanInfoRest } = span_info ?? {};
|
|
6351
6341
|
let ret = {
|
|
@@ -6354,10 +6344,12 @@ function parseChatCompletionParams(allParams) {
|
|
|
6354
6344
|
metadata: spanInfoMetadata
|
|
6355
6345
|
}
|
|
6356
6346
|
};
|
|
6357
|
-
const
|
|
6358
|
-
|
|
6347
|
+
const input = params[inputField];
|
|
6348
|
+
const paramsRest = { ...params };
|
|
6349
|
+
delete paramsRest[inputField];
|
|
6350
|
+
return mergeDicts3(ret, { event: { input, metadata: paramsRest } });
|
|
6359
6351
|
}
|
|
6360
|
-
function
|
|
6352
|
+
function createApiWrapper(name, create, processResponse, parseParams) {
|
|
6361
6353
|
return async (allParams, options) => {
|
|
6362
6354
|
const { span_info: _, ...params } = allParams;
|
|
6363
6355
|
return traced(
|
|
@@ -6367,42 +6359,61 @@ function wrapEmbeddings(create) {
|
|
|
6367
6359
|
options
|
|
6368
6360
|
).withResponse();
|
|
6369
6361
|
logHeaders(response, span);
|
|
6370
|
-
|
|
6371
|
-
span.log({
|
|
6372
|
-
// TODO: Add a flag to control whether to log the full embedding vector,
|
|
6373
|
-
// possibly w/ JSON compression.
|
|
6374
|
-
output: { embedding_length },
|
|
6375
|
-
metrics: {
|
|
6376
|
-
tokens: result.usage?.total_tokens,
|
|
6377
|
-
prompt_tokens: result.usage?.prompt_tokens
|
|
6378
|
-
}
|
|
6379
|
-
});
|
|
6362
|
+
processResponse(result, span);
|
|
6380
6363
|
return result;
|
|
6381
6364
|
},
|
|
6382
6365
|
mergeDicts3(
|
|
6383
6366
|
{
|
|
6384
|
-
name
|
|
6367
|
+
name,
|
|
6385
6368
|
spanAttributes: {
|
|
6386
6369
|
type: SpanTypeAttribute3.LLM
|
|
6387
6370
|
}
|
|
6388
6371
|
},
|
|
6389
|
-
|
|
6372
|
+
parseParams(allParams)
|
|
6390
6373
|
)
|
|
6391
6374
|
);
|
|
6392
6375
|
};
|
|
6393
6376
|
}
|
|
6394
|
-
function
|
|
6395
|
-
|
|
6396
|
-
|
|
6397
|
-
|
|
6398
|
-
|
|
6399
|
-
|
|
6400
|
-
|
|
6377
|
+
function createEndpointProxy(target, wrapperFn) {
|
|
6378
|
+
return new Proxy(target, {
|
|
6379
|
+
get(target2, name, receiver) {
|
|
6380
|
+
const baseVal = Reflect.get(target2, name, receiver);
|
|
6381
|
+
if (name === "create") {
|
|
6382
|
+
return wrapperFn(baseVal.bind(target2));
|
|
6383
|
+
}
|
|
6384
|
+
return baseVal;
|
|
6401
6385
|
}
|
|
6402
|
-
};
|
|
6403
|
-
|
|
6404
|
-
|
|
6386
|
+
});
|
|
6387
|
+
}
|
|
6388
|
+
function parseChatCompletionParams(params) {
|
|
6389
|
+
return parseBaseParams(params, "messages");
|
|
6390
|
+
}
|
|
6391
|
+
function processEmbeddingResponse(result, span) {
|
|
6392
|
+
span.log({
|
|
6393
|
+
output: { embedding_length: result.data[0].embedding.length },
|
|
6394
|
+
metrics: {
|
|
6395
|
+
tokens: result.usage?.total_tokens,
|
|
6396
|
+
prompt_tokens: result.usage?.prompt_tokens
|
|
6397
|
+
}
|
|
6398
|
+
});
|
|
6399
|
+
}
|
|
6400
|
+
function processModerationResponse(result, span) {
|
|
6401
|
+
span.log({
|
|
6402
|
+
output: result.results
|
|
6403
|
+
});
|
|
6405
6404
|
}
|
|
6405
|
+
var wrapEmbeddings = (create) => createApiWrapper(
|
|
6406
|
+
"Embedding",
|
|
6407
|
+
create,
|
|
6408
|
+
processEmbeddingResponse,
|
|
6409
|
+
(params) => parseBaseParams(params, "input")
|
|
6410
|
+
);
|
|
6411
|
+
var wrapModerations = (create) => createApiWrapper(
|
|
6412
|
+
"Moderation",
|
|
6413
|
+
create,
|
|
6414
|
+
processModerationResponse,
|
|
6415
|
+
(params) => parseBaseParams(params, "input")
|
|
6416
|
+
);
|
|
6406
6417
|
function postprocessStreamingResults(allResults) {
|
|
6407
6418
|
let role = void 0;
|
|
6408
6419
|
let content = void 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "braintrust",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.182",
|
|
4
4
|
"description": "SDK for integrating Braintrust",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
77
|
"@ai-sdk/provider": "^1.0.1",
|
|
78
|
-
"@braintrust/core": "0.0.
|
|
78
|
+
"@braintrust/core": "0.0.76",
|
|
79
79
|
"@next/env": "^14.2.3",
|
|
80
80
|
"@vercel/functions": "^1.0.2",
|
|
81
81
|
"ai": "^3.2.16",
|