ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
72 lines (71 loc) • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAICostCalculator = void 0;
const OpenAIImageGenerationModel_js_1 = require("./OpenAIImageGenerationModel.cjs");
const OpenAITextEmbeddingModel_js_1 = require("./OpenAITextEmbeddingModel.cjs");
const OpenAITextGenerationModel_js_1 = require("./OpenAITextGenerationModel.cjs");
const OpenAITranscriptionModel_js_1 = require("./OpenAITranscriptionModel.cjs");
const OpenAIChatModel_js_1 = require("./chat/OpenAIChatModel.cjs");
class OpenAICostCalculator {
constructor() {
Object.defineProperty(this, "provider", {
enumerable: true,
configurable: true,
writable: true,
value: "openai"
});
}
async calculateCostInMillicents(call) {
const type = call.type;
const model = call.model.modelName;
switch (type) {
case "image-generation": {
return (0, OpenAIImageGenerationModel_js_1.calculateOpenAIImageGenerationCostInMillicents)({
settings: call.settings,
});
}
case "text-embedding": {
if (model == null) {
return null;
}
if ((0, OpenAITextEmbeddingModel_js_1.isOpenAIEmbeddingModel)(model)) {
return (0, OpenAITextEmbeddingModel_js_1.calculateOpenAIEmbeddingCostInMillicents)({
model,
responses: call.response,
});
}
break;
}
case "json-generation":
case "text-generation": {
if (model == null) {
return null;
}
if ((0, OpenAIChatModel_js_1.isOpenAIChatModel)(model)) {
return (0, OpenAIChatModel_js_1.calculateOpenAIChatCostInMillicents)({
model,
response: call.response,
});
}
if ((0, OpenAITextGenerationModel_js_1.isOpenAITextGenerationModel)(model)) {
return (0, OpenAITextGenerationModel_js_1.calculateOpenAITextGenerationCostInMillicents)({
model,
response: call.response,
});
}
break;
}
case "transcription": {
if (model == null) {
return null;
}
return (0, OpenAITranscriptionModel_js_1.calculateOpenAITranscriptionCostInMillicents)({
model: model,
response: call.response,
});
}
}
return null;
}
}
exports.OpenAICostCalculator = OpenAICostCalculator;