Class: EasyAI::Command::GPT

Inherits:
EasyAI::Command show all
Defined in:
lib/easyai/command/gpt.rb

Constant Summary

Constants inherited from EasyAI::Command

DEFAULT_OPTIONS, DEFAULT_ROOT_OPTIONS

Instance Attribute Summary

Attributes inherited from EasyAI::Command

#args_help_flag

Instance Method Summary collapse

Methods inherited from EasyAI::Command

options, run

Constructor Details

#initialize(argv) ⇒ GPT

Returns a new instance of GPT.



25
26
27
28
# File 'lib/easyai/command/gpt.rb', line 25

def initialize(argv)
  super
  @gpt_args = @argv.remainder!
end

Instance Method Details

#runObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/easyai/command/gpt.rb', line 35

def run
  # 直接使用环境变量,不再依赖配置文件
  env = ENV.to_h
  
  # 如果环境变量中已经设置了 API KEY,直接使用
  # 用户可以通过 export OPENAI_API_KEY=xxx 来设置
  
  puts "正在运行: openai #{@gpt_args.join(' ')}".blue if verbose?
  exec(env, 'openai', *@gpt_args)
end

#validate!Object



30
31
32
33
# File 'lib/easyai/command/gpt.rb', line 30

def validate!
  super
  help! '未找到 OpenAI CLI。请安装:pip install openai' unless gpt_available?
end