@wrcb/cb-common 1.0.838 → 1.0.840
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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import OpenAI from 'openai';
|
|
2
2
|
import { Stan } from 'node-nats-streaming';
|
|
3
3
|
import { AIProvider } from '../../events/ai/aiRequestMadeEvent';
|
|
4
|
-
interface ChatParams {
|
|
4
|
+
export interface ChatParams {
|
|
5
5
|
provider: AIProvider;
|
|
6
6
|
model: string;
|
|
7
7
|
messages: OpenAI.ChatCompletionMessageParam[];
|
|
@@ -10,38 +10,32 @@ interface ChatParams {
|
|
|
10
10
|
responseFormat?: {
|
|
11
11
|
type: 'json_object' | 'text';
|
|
12
12
|
};
|
|
13
|
-
openaiClient: OpenAI;
|
|
14
|
-
cerebrasClient?: OpenAI;
|
|
15
13
|
}
|
|
16
|
-
interface TTSParams {
|
|
14
|
+
export interface TTSParams {
|
|
17
15
|
model: string;
|
|
18
16
|
voice: string;
|
|
19
17
|
input: string;
|
|
20
18
|
speed?: number;
|
|
21
19
|
instructions?: string;
|
|
22
20
|
responseFormat?: string;
|
|
23
|
-
openaiClient: OpenAI;
|
|
24
21
|
}
|
|
25
|
-
interface TranscribeParams {
|
|
22
|
+
export interface TranscribeParams {
|
|
26
23
|
audioBuffer: Buffer;
|
|
27
24
|
mimeType?: string;
|
|
28
|
-
openaiClient: OpenAI;
|
|
29
25
|
}
|
|
30
|
-
interface ImageGenerateParams {
|
|
26
|
+
export interface ImageGenerateParams {
|
|
31
27
|
model: string;
|
|
32
28
|
prompt: string;
|
|
33
29
|
size: string;
|
|
34
30
|
quality?: string;
|
|
35
31
|
n?: number;
|
|
36
|
-
openaiClient: OpenAI;
|
|
37
32
|
}
|
|
38
|
-
interface ImageEditParams {
|
|
33
|
+
export interface ImageEditParams {
|
|
39
34
|
model: string;
|
|
40
35
|
imageBuffer: Buffer;
|
|
41
36
|
prompt: string;
|
|
42
37
|
size: string;
|
|
43
38
|
n?: number;
|
|
44
|
-
openaiClient: OpenAI;
|
|
45
39
|
}
|
|
46
40
|
export interface ChatResult {
|
|
47
41
|
content: string;
|
|
@@ -52,7 +46,9 @@ export interface ChatResult {
|
|
|
52
46
|
export declare class AIClient {
|
|
53
47
|
private readonly natsClient;
|
|
54
48
|
private readonly service;
|
|
55
|
-
|
|
49
|
+
private readonly openai;
|
|
50
|
+
private readonly cerebras;
|
|
51
|
+
constructor(natsClient: Stan, service: string, openaiApiKey: string, cerebrasApiKey?: string);
|
|
56
52
|
private emit;
|
|
57
53
|
chat(params: ChatParams): Promise<ChatResult>;
|
|
58
54
|
tts(params: TTSParams): Promise<Buffer>;
|
|
@@ -60,4 +56,3 @@ export declare class AIClient {
|
|
|
60
56
|
generateImage(params: ImageGenerateParams): Promise<string>;
|
|
61
57
|
editImage(params: ImageEditParams): Promise<string>;
|
|
62
58
|
}
|
|
63
|
-
export {};
|
|
@@ -33,20 +33,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
33
33
|
};
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
35
|
exports.AIClient = void 0;
|
|
36
|
-
const openai_1 = require("openai");
|
|
36
|
+
const openai_1 = __importStar(require("openai"));
|
|
37
37
|
const aiRequestMadePublisher_1 = require("../../events/ai/aiRequestMadePublisher");
|
|
38
|
-
// Preços em USD por 1K tokens (ou por unidade para TTS/imagens)
|
|
39
38
|
const COST_PER_1K = {
|
|
40
39
|
'gpt-4o': { input: 0.0025, output: 0.010 },
|
|
41
40
|
'gpt-4o-mini': { input: 0.00015, output: 0.0006 },
|
|
42
41
|
'gpt-oss-120b': { input: 0, output: 0 },
|
|
43
|
-
'whisper-1': { input: 0.006, output: 0 },
|
|
44
|
-
'tts-1': { input: 0.015, output: 0 },
|
|
42
|
+
'whisper-1': { input: 0.006, output: 0 },
|
|
43
|
+
'tts-1': { input: 0.015, output: 0 },
|
|
45
44
|
'tts-1-hd': { input: 0.030, output: 0 },
|
|
46
45
|
'gpt-4o-mini-tts': { input: 0.012, output: 0 },
|
|
47
|
-
'dall-e-3': { input: 0.040, output: 0 },
|
|
46
|
+
'dall-e-3': { input: 0.040, output: 0 },
|
|
48
47
|
'dall-e-2': { input: 0.020, output: 0 },
|
|
49
|
-
'gpt-image-1': { input: 0.067, output: 0 },
|
|
48
|
+
'gpt-image-1': { input: 0.067, output: 0 },
|
|
50
49
|
};
|
|
51
50
|
function calcCost(model, promptTokens, completionTokens) {
|
|
52
51
|
var _a;
|
|
@@ -54,9 +53,14 @@ function calcCost(model, promptTokens, completionTokens) {
|
|
|
54
53
|
return (promptTokens / 1000) * rates.input + (completionTokens / 1000) * rates.output;
|
|
55
54
|
}
|
|
56
55
|
class AIClient {
|
|
57
|
-
constructor(natsClient, service) {
|
|
56
|
+
constructor(natsClient, service, openaiApiKey, cerebrasApiKey) {
|
|
58
57
|
this.natsClient = natsClient;
|
|
59
58
|
this.service = service;
|
|
59
|
+
this.openai = new openai_1.default({ apiKey: openaiApiKey });
|
|
60
|
+
this.cerebras = new openai_1.default({
|
|
61
|
+
apiKey: cerebrasApiKey !== null && cerebrasApiKey !== void 0 ? cerebrasApiKey : '',
|
|
62
|
+
baseURL: 'https://api.cerebras.ai/v1',
|
|
63
|
+
});
|
|
60
64
|
}
|
|
61
65
|
emit(data) {
|
|
62
66
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -87,8 +91,8 @@ class AIClient {
|
|
|
87
91
|
chat(params) {
|
|
88
92
|
return __awaiter(this, void 0, void 0, function* () {
|
|
89
93
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
90
|
-
const { provider, model, messages, maxTokens, temperature, responseFormat
|
|
91
|
-
const client = provider === 'cerebras' ?
|
|
94
|
+
const { provider, model, messages, maxTokens, temperature, responseFormat } = params;
|
|
95
|
+
const client = provider === 'cerebras' ? this.cerebras : this.openai;
|
|
92
96
|
const requestedAt = new Date().toISOString();
|
|
93
97
|
const t0 = Date.now();
|
|
94
98
|
const promptSummary = messages.map(m => { var _a; return `[${m.role}] ${String((_a = m.content) !== null && _a !== void 0 ? _a : '').slice(0, 200)}`; }).join(' | ').slice(0, 500);
|
|
@@ -98,8 +102,7 @@ class AIClient {
|
|
|
98
102
|
const promptTokens = (_b = (_a = response.usage) === null || _a === void 0 ? void 0 : _a.prompt_tokens) !== null && _b !== void 0 ? _b : 0;
|
|
99
103
|
const completionTokens = (_d = (_c = response.usage) === null || _c === void 0 ? void 0 : _c.completion_tokens) !== null && _d !== void 0 ? _d : 0;
|
|
100
104
|
const costUSD = calcCost(model, promptTokens, completionTokens);
|
|
101
|
-
|
|
102
|
-
yield this.emit({ provider, type: 'chat', model, promptTokens, completionTokens, costUSD, durationMs, requestedAt, prompt: promptSummary, success: true });
|
|
105
|
+
yield this.emit({ provider, type: 'chat', model, promptTokens, completionTokens, costUSD, durationMs: Date.now() - t0, requestedAt, prompt: promptSummary, success: true });
|
|
103
106
|
return {
|
|
104
107
|
content: (_g = (_f = (_e = response.choices[0]) === null || _e === void 0 ? void 0 : _e.message) === null || _f === void 0 ? void 0 : _f.content) !== null && _g !== void 0 ? _g : '',
|
|
105
108
|
promptTokens,
|
|
@@ -117,15 +120,14 @@ class AIClient {
|
|
|
117
120
|
tts(params) {
|
|
118
121
|
return __awaiter(this, void 0, void 0, function* () {
|
|
119
122
|
var _a, _b;
|
|
120
|
-
const { model, voice, input, speed, instructions, responseFormat
|
|
123
|
+
const { model, voice, input, speed, instructions, responseFormat } = params;
|
|
121
124
|
const requestedAt = new Date().toISOString();
|
|
122
125
|
const t0 = Date.now();
|
|
123
126
|
const costUSD = (input.length / 1000) * ((_b = (_a = COST_PER_1K[model]) === null || _a === void 0 ? void 0 : _a.input) !== null && _b !== void 0 ? _b : 0.030);
|
|
124
127
|
try {
|
|
125
|
-
const response = yield
|
|
128
|
+
const response = yield this.openai.audio.speech.create(Object.assign(Object.assign({ model, voice: voice, input, response_format: (responseFormat !== null && responseFormat !== void 0 ? responseFormat : 'mp3') }, (speed !== undefined && { speed })), (instructions !== undefined && { instructions })));
|
|
126
129
|
const buffer = Buffer.from(yield response.arrayBuffer());
|
|
127
|
-
|
|
128
|
-
yield this.emit({ provider: 'openai', type: 'tts', model, promptTokens: 0, completionTokens: 0, costUSD, durationMs, requestedAt, prompt: input.slice(0, 200), inputChars: input.length, success: true });
|
|
130
|
+
yield this.emit({ provider: 'openai', type: 'tts', model, promptTokens: 0, completionTokens: 0, costUSD, durationMs: Date.now() - t0, requestedAt, prompt: input.slice(0, 200), inputChars: input.length, success: true });
|
|
129
131
|
return buffer;
|
|
130
132
|
}
|
|
131
133
|
catch (err) {
|
|
@@ -137,16 +139,15 @@ class AIClient {
|
|
|
137
139
|
}
|
|
138
140
|
transcribe(params) {
|
|
139
141
|
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
-
const { audioBuffer, mimeType
|
|
142
|
+
const { audioBuffer, mimeType } = params;
|
|
141
143
|
const requestedAt = new Date().toISOString();
|
|
142
144
|
const t0 = Date.now();
|
|
143
145
|
const estimatedMinutes = audioBuffer.length / 40000 / 60;
|
|
144
146
|
const costUSD = estimatedMinutes * 0.006;
|
|
145
147
|
try {
|
|
146
148
|
const file = yield (0, openai_1.toFile)(audioBuffer, 'audio.m4a', { type: mimeType !== null && mimeType !== void 0 ? mimeType : 'audio/m4a' });
|
|
147
|
-
const response = yield
|
|
148
|
-
|
|
149
|
-
yield this.emit({ provider: 'openai', type: 'transcribe', model: 'whisper-1', promptTokens: 0, completionTokens: 0, costUSD, durationMs, requestedAt, prompt: `audio ${audioBuffer.length}B`, success: true });
|
|
149
|
+
const response = yield this.openai.audio.transcriptions.create({ model: 'whisper-1', file });
|
|
150
|
+
yield this.emit({ provider: 'openai', type: 'transcribe', model: 'whisper-1', promptTokens: 0, completionTokens: 0, costUSD, durationMs: Date.now() - t0, requestedAt, prompt: `audio ${audioBuffer.length}B`, success: true });
|
|
150
151
|
return response.text;
|
|
151
152
|
}
|
|
152
153
|
catch (err) {
|
|
@@ -159,16 +160,15 @@ class AIClient {
|
|
|
159
160
|
generateImage(params) {
|
|
160
161
|
return __awaiter(this, void 0, void 0, function* () {
|
|
161
162
|
var _a, _b;
|
|
162
|
-
const { model, prompt, size, quality, n
|
|
163
|
+
const { model, prompt, size, quality, n } = params;
|
|
163
164
|
const requestedAt = new Date().toISOString();
|
|
164
165
|
const t0 = Date.now();
|
|
165
166
|
const costUSD = (_b = (_a = COST_PER_1K[model]) === null || _a === void 0 ? void 0 : _a.input) !== null && _b !== void 0 ? _b : 0.040;
|
|
166
167
|
try {
|
|
167
|
-
const response = yield
|
|
168
|
+
const response = yield this.openai.images.generate(Object.assign(Object.assign({ model,
|
|
168
169
|
prompt, size: size }, (quality !== undefined && { quality: quality })), { n: n !== null && n !== void 0 ? n : 1, response_format: 'b64_json' }));
|
|
169
170
|
const b64 = response.data[0].b64_json;
|
|
170
|
-
|
|
171
|
-
yield this.emit({ provider: 'openai', type: 'image-generate', model, promptTokens: 0, completionTokens: 0, costUSD, durationMs, requestedAt, prompt: prompt.slice(0, 300), imageSize: size, success: true });
|
|
171
|
+
yield this.emit({ provider: 'openai', type: 'image-generate', model, promptTokens: 0, completionTokens: 0, costUSD, durationMs: Date.now() - t0, requestedAt, prompt: prompt.slice(0, 300), imageSize: size, success: true });
|
|
172
172
|
return b64;
|
|
173
173
|
}
|
|
174
174
|
catch (err) {
|
|
@@ -181,7 +181,7 @@ class AIClient {
|
|
|
181
181
|
editImage(params) {
|
|
182
182
|
return __awaiter(this, void 0, void 0, function* () {
|
|
183
183
|
var _a, _b;
|
|
184
|
-
const { model, imageBuffer, prompt, size, n
|
|
184
|
+
const { model, imageBuffer, prompt, size, n } = params;
|
|
185
185
|
const requestedAt = new Date().toISOString();
|
|
186
186
|
const t0 = Date.now();
|
|
187
187
|
const costUSD = (_b = (_a = COST_PER_1K[model]) === null || _a === void 0 ? void 0 : _a.input) !== null && _b !== void 0 ? _b : 0.020;
|
|
@@ -191,7 +191,7 @@ class AIClient {
|
|
|
191
191
|
globalThis.File = NodeFile;
|
|
192
192
|
}
|
|
193
193
|
const imageFile = yield (0, openai_1.toFile)(imageBuffer, 'image.png', { type: 'image/png' });
|
|
194
|
-
const response = yield
|
|
194
|
+
const response = yield this.openai.images.edit({
|
|
195
195
|
model,
|
|
196
196
|
image: imageFile,
|
|
197
197
|
prompt,
|
|
@@ -200,8 +200,7 @@ class AIClient {
|
|
|
200
200
|
response_format: 'b64_json',
|
|
201
201
|
});
|
|
202
202
|
const b64 = response.data[0].b64_json;
|
|
203
|
-
|
|
204
|
-
yield this.emit({ provider: 'openai', type: 'image-edit', model, promptTokens: 0, completionTokens: 0, costUSD, durationMs, requestedAt, prompt: prompt.slice(0, 300), imageSize: size, success: true });
|
|
203
|
+
yield this.emit({ provider: 'openai', type: 'image-edit', model, promptTokens: 0, completionTokens: 0, costUSD, durationMs: Date.now() - t0, requestedAt, prompt: prompt.slice(0, 300), imageSize: size, success: true });
|
|
205
204
|
return b64;
|
|
206
205
|
}
|
|
207
206
|
catch (err) {
|