UNPKG

venice-ai-sdk-apl

Version:

A comprehensive SDK for the Venice AI API with CLI support, programmatic CLI usage, CLI-style interface, and interactive demo

296 lines 11.5 kB
"use strict"; /** * Venice AI API Client * * This is the main client class for interacting with the Venice AI API. * It provides access to all API resources and handles configuration. * * @example * ```typescript * import { VeniceAI } from 'venice-ai-sdk-apl'; * * const venice = new VeniceAI({ * apiKey: 'your-api-key', * }); * * // Now you can use the client to access API resources * const models = await venice.models.list(); * ``` */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LogLevel = exports.VeniceAI = void 0; const chat_1 = require("./resources/chat"); const image_1 = require("./resources/image"); const models_1 = require("./resources/models"); const api_keys_1 = require("./resources/api-keys"); const characters_1 = require("./resources/characters"); const vvv_1 = require("./resources/vvv"); const http_1 = require("./utils/http"); const logger_1 = require("./utils/logger"); Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return logger_1.LogLevel; } }); const cli_1 = require("./cli"); /** * Default configuration values */ const DEFAULT_CONFIG = { baseUrl: 'https://api.venice.ai/api/v1', timeout: 30000, // 30 seconds logLevel: logger_1.LogLevel.NONE, }; /** * Main Venice AI API client class */ class VeniceAI { /** * Creates a new Venice AI API client * * @param config - Client configuration */ constructor(config) { // Merge provided config with defaults this.config = { ...DEFAULT_CONFIG, ...config, }; // Validate required config if (!this.config.apiKey) { throw new Error('API key is required'); } // Configure logger if (this.config.logLevel !== undefined) { logger_1.Logger.setLevel(this.config.logLevel); } // Initialize HTTP client this.httpClient = new http_1.HttpClient(this.config); // Initialize resources this.chat = new chat_1.ChatResource(this.httpClient); this.image = new image_1.ImageResource(this.httpClient); this.models = new models_1.ModelsResource(this.httpClient); this.apiKeys = new api_keys_1.ApiKeysResource(this.httpClient); this.characters = new characters_1.CharactersResource(this.httpClient); this.vvv = new vvv_1.VVVResource(this.httpClient); } /** * Get the current configuration */ getConfig() { return { ...this.config }; } /** * Set the log level * * @param level - Log level */ setLogLevel(level) { this.config.logLevel = level; logger_1.Logger.setLevel(level); } /** * Enable debug logging */ enableDebugLogging() { this.setLogLevel(logger_1.LogLevel.DEBUG); } /** * Disable logging */ disableLogging() { this.setLogLevel(logger_1.LogLevel.NONE); } /** * CLI-style interface that mirrors the command-line syntax * * This method allows you to use the same commands you're familiar with from the CLI * directly in your code. It supports both string-based CLI-style arguments and * object-based options. * * @example * ```typescript * // CLI-style with string arguments * const styles = await venice.cli('list-styles --limit 5'); * * // CLI-style with object arguments * const models = await venice.cli('list-models', { limit: 5, raw: true }); * * // Generate an image * const image = await venice.cli('generate-image "A beautiful sunset" --style Photographic --output sunset.png'); * ``` * * @param command - The command to execute (e.g., 'list-keys', 'chat', 'generate-image') * @param options - Options for the command (can be a string of CLI arguments or an object) * @returns The result of the command */ async cli(command, options = {}) { // Parse the command to extract the base command and any arguments const parts = command.trim().split(/\s+/); const baseCommand = parts[0]; // If options is a string, parse it as CLI arguments if (typeof options === 'string') { options = this._parseCliArgs(options); } // If there are arguments in the command string, extract them if (parts.length > 1) { // Extract quoted arguments (for prompts, etc.) let remainingArgs = parts.slice(1).join(' '); const quotedArgs = []; // Extract text in quotes const quoteRegex = /"([^"]*)"|'([^']*)'/g; let match; while ((match = quoteRegex.exec(remainingArgs)) !== null) { quotedArgs.push(match[1] || match[2]); // Remove the quoted text from remainingArgs remainingArgs = remainingArgs.replace(match[0], ''); } // Parse the remaining arguments const parsedArgs = this._parseCliArgs(remainingArgs); // Merge the parsed arguments with the options options = { ...parsedArgs, ...options }; // Add the quoted arguments as positional arguments if (quotedArgs.length > 0) { options._args = quotedArgs; } } // Map commands to functions switch (baseCommand) { case 'list-keys': return cli_1.commands.listKeys(options); case 'create-key': if (!options.name && options._args && options._args.length > 0) { return cli_1.commands.createKey(options._args[0], options); } return cli_1.commands.createKey(options.name, options); case 'delete-key': if (!options.id && options._args && options._args.length > 0) { return cli_1.commands.deleteKey(options._args[0], options); } return cli_1.commands.deleteKey(options.id, options); case 'rate-limits': return cli_1.commands.rateLimits(options); case 'list-models': return cli_1.commands.listModels(options); case 'chat': if (options._args && options._args.length > 0) { return cli_1.commands.chat(options._args[0], options); } throw new Error('Chat command requires a prompt'); case 'generate-image': if (options._args && options._args.length > 0) { return cli_1.commands.generateImage(options._args[0], options); } throw new Error('Generate image command requires a prompt'); case 'list-styles': return cli_1.commands.listStyles(options); case 'list-characters': return cli_1.commands.listCharacters(options); case 'vvv-supply': return cli_1.commands.vvvCirculatingSupply(options); case 'vvv-utilization': return cli_1.commands.vvvUtilization(options); case 'vvv-yield': return cli_1.commands.vvvStakingYield(options); case 'configure': if (options._args && options._args.length > 0) { return cli_1.commands.configure(options._args[0]); } if (options.apiKey) { return cli_1.commands.configure(options.apiKey); } throw new Error('Configure command requires an API key'); default: throw new Error(`Unknown command: ${baseCommand}`); } } /** * Parse CLI-style arguments into an options object * * @example * ```typescript * // "--limit 5 --raw" becomes { limit: "5", raw: true } * const options = venice._parseCliArgs("--limit 5 --raw"); * ``` * * @param argsString - The CLI arguments string to parse * @returns An object with the parsed options * @private */ _parseCliArgs(argsString) { const options = {}; // Simple parsing logic for --flag and --key value const args = argsString.trim().split(/\s+/).filter(arg => arg.length > 0); for (let i = 0; i < args.length; i++) { if (args[i].startsWith('--')) { const key = args[i].substring(2).replace(/-([a-z])/g, (_, char) => char.toUpperCase()); if (i + 1 < args.length && !args[i + 1].startsWith('--')) { // Handle numeric values const value = args[i + 1]; if (/^\d+$/.test(value)) { options[key] = parseInt(value, 10); } else if (/^\d+\.\d+$/.test(value)) { options[key] = parseFloat(value); } else { options[key] = value; } i++; } else { options[key] = true; } } else if (args[i].startsWith('-')) { // Handle short flags (-d, -r, etc.) const flags = args[i].substring(1).split(''); for (const flag of flags) { // Map short flags to long flags switch (flag) { case 'd': options.debug = true; break; case 'r': options.raw = true; break; case 'w': options.webSearch = true; break; case 'm': if (i + 1 < args.length && !args[i + 1].startsWith('-')) { options.model = args[i + 1]; i++; } break; case 's': if (i + 1 < args.length && !args[i + 1].startsWith('-')) { options.system = args[i + 1]; i++; } break; case 'n': if (i + 1 < args.length && !args[i + 1].startsWith('-')) { options.negative = args[i + 1]; i++; } break; case 'h': if (i + 1 < args.length && !args[i + 1].startsWith('-')) { options.height = parseInt(args[i + 1], 10); i++; } break; case 'o': if (i + 1 < args.length && !args[i + 1].startsWith('-')) { options.output = args[i + 1]; i++; } break; default: options[flag] = true; } } } } return options; } } exports.VeniceAI = VeniceAI; //# sourceMappingURL=client.js.map