osr-mono/packages/osr-ai/commands/prompt.js
2025-01-31 14:45:54 +01:00

143 lines
5.4 KiB
JavaScript

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = void 0;
const path = __importStar(require("path"));
const exists_1 = require("@plastichub/fs/exists");
const osr_cli_commons_1 = require("@plastichub/osr-cli-commons");
const fs_1 = require("@plastichub/osr-commons");
const __1 = require("../");
const _cli_1 = require("../_cli");
const filters_1 = require("../lib/filters");
const openai_1 = require("../lib/openai");
const defaultOptions = (yargs) => {
yargs.parserConfiguration({
"camel-case-expansion": false
});
return yargs.option('append', {
default: false,
describe: 'append to file instead of overwriting',
type: 'boolean'
}).option('showPrompt', {
default: false,
describe: 'insert prompt in output',
type: 'boolean'
}).option('cache', {
default: true,
describe: 'Enable cache',
type: 'boolean'
}).option('alt', {
default: false,
describe: 'Use alternative path variable token separator',
type: 'boolean'
}).option('assistant', {
default: false,
describe: 'Use OpenAI Assistant API',
type: 'boolean'
}).option('query', {
description: 'query',
default: "list all towns of barcelona, as typescript enum, with gps coords, as strings"
}).option('dst', {
description: 'dst output path, supports XLS|CSV|HTML',
_default: './tests/chatgpt/last.ts'
}).option('env_key', {
default: 'OSR-CONFIG',
describe: 'Environment key to the config path'
}).option('api_key', {
describe: 'OpenAI key'
}).option('model', {
describe: 'OpenAI Model',
default: "gpt-4o"
}).option('system', {
describe: 'Path to system instructions'
}).option('source', {
describe: 'Path to a source file'
}).option('response_format', {
describe: 'Format of the response (for transcribe)',
default: "text"
}).option('filters', {
describe: `An array of filters to apply to the response: JSON : ${Object.keys(filters_1.Filters)} `,
default: ""
}).option('files', {
describe: `List of files to attach to the query: String|Glob`
}).option('logLevel', {
describe: `Log level: warn|info|trace|debug|error|fatal`,
default: 'info'
}).option('gui', {
describe: `GUI: electron|puppeteer`
}).option('prompts', {
describe: `Path to prompts file, JSON - Array`,
default: '${OSR_ROOT}/osr-ai-templates/prompts/documents.json',
type: 'string'
}).option('dump', {
describe: `Create a dump of the request and response as shell script`,
default: true,
type: 'boolean'
});
};
let options = (yargs) => defaultOptions(yargs);
const register = (cli) => {
return cli.command('prompt', 'Prompt ChatGPT - Ex', options, async (argv) => {
(0, _cli_1.defaults)();
if (argv.help) {
return;
}
const args = argv;
const config = (0, osr_cli_commons_1.CONFIG_DEFAULT)(args.env_key);
if (!config) {
__1.logger.warn('No config found!');
return;
}
if (config && !config.openai.key) {
__1.logger.warn('No OpenAI key found in config!');
return;
}
const opts = {
query: argv.query,
...argv,
dst: args.dst,
source: argv.source ? path.resolve((0, fs_1.resolve)(args.source)) : undefined,
system: argv.system ? path.resolve((0, fs_1.resolve)(args.system)) : undefined,
api_key: argv.openai_key || process.env.OPENAI_API_KEY || config.openai.key
};
__1.logger.setSettings({ minLevel: opts.logLevel });
__1.logger.info('Options', opts);
if (opts.cache === true && (0, exists_1.sync)(opts.dst)) {
__1.logger.debug('Output file already exists, skipping');
return;
}
if (!opts.api_key) {
__1.logger.error('No OpenAI key found in config or options!');
return;
}
if (!opts.query) {
__1.logger.error('No query specified');
return;
}
const ret = opts.assistant ? await (0, openai_1.assistant)(opts) : await (0, openai_1.prompt)(opts);
});
};
exports.register = register;
//# sourceMappingURL=prompt.js.map