UNPKG

auto-gpt-ts

Version:

my take of Auto-GPT in typescript

122 lines 5.45 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiManager = void 0; const logging_1 = require("../logging"); const config_1 = require("../config/config"); const singelton_1 = require("../singelton"); const models_info_1 = require("./models-info"); const openai_1 = require("openai"); const configuration = new openai_1.Configuration({ apiKey: new config_1.Config().openaiApiKey }); const openai = new openai_1.OpenAIApi(configuration); let ApiManager = class ApiManager extends logging_1.Loggable { constructor() { super(...arguments); this.totalTost = 0; this.totalBudget = 0; this.totalCompletionTokens = 0; this.totalPromptTokens = 0; } reset() { this.totalPromptTokens = 0; this.totalCompletionTokens = 0; this.totalTost = 0; this.totalBudget = 0.0; } /** * Create a chat completion and update the cost. * @param {Array<Message>} messages - The list of messages to send to the API. * @param {string} - The model to use for the API call. * @param {number} - The temperature to use for the API call. * @param {number} - The maximum number of tokens for the API call. * @returns {Promise<Object>} - The AI's response. */ createChatCompletion(options) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { let { messages, max_tokens, model, temperature } = options; const cfg = new config_1.Config(); if (!temperature) { temperature = cfg.temperature; } this.logger.debug(`Creating chat completion with model ${model}, temperature ${temperature}, max_tokens ${max_tokens}`); const response = (yield openai.createChatCompletion({ model: model, messages: messages, temperature: temperature, max_tokens: max_tokens, })).data; const prompt_tokens = (_b = (_a = response.usage) === null || _a === void 0 ? void 0 : _a.prompt_tokens) !== null && _b !== void 0 ? _b : 0; const completion_tokens = (_d = (_c = response.usage) === null || _c === void 0 ? void 0 : _c.completion_tokens) !== null && _d !== void 0 ? _d : 0; this.updateCost(prompt_tokens, completion_tokens, model); return response; }); } /** * Update the total cost, prompt tokens, and completion tokens. * @param {number} promptTokens - The number of tokens used in the prompt. * @param {number} completionTokens - The number of tokens used in the completion. * @param {string} model - The model used for the API call. */ updateCost(promptTokens, completionTokens, model) { this.totalPromptTokens += promptTokens; this.totalCompletionTokens += completionTokens; this.totalTost += (promptTokens * models_info_1.COSTS[model]["prompt"] + completionTokens * models_info_1.COSTS[model]["completion"]) / 1000; this.logger.debug(`Total running cost: $${this.totalTost.toFixed(3)}`); } /** * Sets the total user-defined budget for API calls. * @param {number} total_budget - The total budget for API calls. */ setTotalBudget(total_budget) { this.totalBudget = total_budget; } /** * Get the total number of prompt tokens. * @returns {number} - The total number of prompt tokens. */ getTotalPromptTokens() { return this.totalPromptTokens; } /** * Get the total number of completion tokens. * @returns {number} - The total number of completion tokens. */ getTotalCompletionTokens() { return this.totalCompletionTokens; } /** * Get the total cost of API calls. * @returns {number} - The total cost of API calls. */ getTotalCost() { return this.totalTost; } getTotalBudget() { return this.totalBudget; } }; ApiManager = __decorate([ singelton_1.Singleton ], ApiManager); exports.ApiManager = ApiManager; //# sourceMappingURL=api-manages.js.map