(program: Command)
| 16 | * 注册 LLM 相关命令 |
| 17 | */ |
| 18 | export function llmCommand(program: Command) { |
| 19 | // LLM 直接聊天命令 |
| 20 | program |
| 21 | .command('llm') |
| 22 | .alias('l') |
| 23 | .description('💬 LLM 直接聊天模式') |
| 24 | .option('-p, --provider <provider>', '选择 LLM 提供商 (volcengine|qwen)') |
| 25 | .option('-k, --api-key <key>', 'API 密钥') |
| 26 | .option('-m, --model <model>', '指定模型') |
| 27 | .option('-s, --stream', '启用流式输出', false) |
| 28 | .action(async options => { |
| 29 | try { |
| 30 | // 使用用户配置作为默认值 |
| 31 | const provider = options.provider || getCurrentProvider(); |
| 32 | |
| 33 | if (!isProviderSupported(provider)) { |
| 34 | console.log(chalk.red(`❌ 不支持的提供商: ${provider}`)); |
| 35 | console.log(chalk.gray('支持的提供商: qwen, volcengine')); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | // 验证API密钥 |
| 40 | let apiKey: string; |
| 41 | try { |
| 42 | const { validateApiKey } = await import('../config/defaults.js'); |
| 43 | apiKey = validateApiKey(provider, options.apiKey); |
| 44 | } catch (error) { |
| 45 | console.log(chalk.red('\n❌ API密钥配置错误')); |
| 46 | console.log(chalk.yellow('\n💡 配置API密钥的方法:')); |
| 47 | console.log(chalk.gray('1. 命令行参数: --api-key your-api-key')); |
| 48 | console.log( |
| 49 | chalk.gray( |
| 50 | '2. 环境变量: export QWEN_API_KEY=your-key 或 export VOLCENGINE_API_KEY=your-key' |
| 51 | ) |
| 52 | ); |
| 53 | console.log(chalk.gray('3. .env 文件: 复制 config.env.example 为 .env 并填入密钥')); |
| 54 | console.log(chalk.gray('\n📖 获取API密钥:')); |
| 55 | if (provider === 'qwen') { |
| 56 | console.log(chalk.gray('千问: https://dashscope.console.aliyun.com/apiKey')); |
| 57 | } else if (provider === 'volcengine') { |
| 58 | console.log( |
| 59 | chalk.gray( |
| 60 | '火山引擎: https://console.volcengine.com/ark/region:ark+cn-beijing/apiKey' |
| 61 | ) |
| 62 | ); |
| 63 | } |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | const providerConfig = getProviderConfig(provider); |
| 68 | const model = options.model || getCurrentModel(provider) || providerConfig.defaultModel; |
| 69 | const modelDescription = getModelDescription(provider, model); |
| 70 | |
| 71 | console.log(chalk.blue(`\n🤖 启动 ${provider.toUpperCase()} LLM 聊天`)); |
| 72 | console.log(chalk.green(`📱 模型: ${modelDescription}`)); |
| 73 | console.log(chalk.gray('💡 输入 "quit" 或 "exit" 退出聊天\n')); |
| 74 | |
| 75 | // 创建LLM实例 |
no test coverage detected