Module: LightGptProxy

Extended by:
Forwardable
Defined in:
lib/light_gpt_proxy.rb,
lib/light_gpt_proxy/guard.rb,
lib/light_gpt_proxy/config.rb,
lib/light_gpt_proxy/logger.rb,
lib/light_gpt_proxy/version.rb,
lib/light_gpt_proxy/endpoint.rb,
lib/light_gpt_proxy/provider.rb,
lib/light_gpt_proxy/templates.rb,
lib/light_gpt_proxy/proxy_server.rb,
lib/light_gpt_proxy/schema_validator.rb,
lib/light_gpt_proxy/templates/template.rb,
lib/light_gpt_proxy/schema_defaults_applier.rb,
lib/light_gpt_proxy/templates/ollama_template.rb,
lib/light_gpt_proxy/templates/copilot_template.rb,
lib/light_gpt_proxy/templates/open_ai_template.rb,
lib/light_gpt_proxy/helpers/hash_deep_stringify_helper.rb

Defined Under Namespace

Modules: Helpers, Templates Classes: Config, Endpoint, Guard, Logger, Provider, ProxyServer, SchemaDefaultsApplier, SchemaValidator, Template

Constant Summary collapse

CONFIG_FILE =
'.light_gpt_proxy.yml'
ENCRYPTED_CONFIG_FILE =
'.light_gpt_proxy.yml.enc'
DEFAULT_PORT =
3030
DEFAULT_CONFIG =
{ 'default_port' => DEFAULT_PORT, 'verbose' => true, 'default_provider' => nil }.freeze
Error =
Class.new(StandardError)
VERSION =
'0.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.passwdObject

Returns the value of attribute passwd.



27
28
29
# File 'lib/light_gpt_proxy.rb', line 27

def passwd
  @passwd
end

.verbObject

Returns the value of attribute verb.



27
28
29
# File 'lib/light_gpt_proxy.rb', line 27

def verb
  @verb
end

Class Method Details

.configObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/light_gpt_proxy.rb', line 41

def config
  @config ||= Config.new(
    if config?
      if encrypted_config?
        raise Error, 'Please provide a password to decrypt the config' unless passwd

        guard(passwd).decode(config_path)
      else
        YAML.load_file(config_path)
      end
    else
      DEFAULT_CONFIG
    end
  )
end

.config?Boolean

Returns:

  • (Boolean)


29
# File 'lib/light_gpt_proxy.rb', line 29

def config? = File.exist?(config_path)

.config_pathObject



32
33
34
35
36
37
38
39
# File 'lib/light_gpt_proxy.rb', line 32

def config_path
  @config_path ||= [
    File.join(Dir.pwd, CONFIG_FILE),
    File.join(Dir.pwd, ENCRYPTED_CONFIG_FILE),
    File.join(Dir.home, CONFIG_FILE),
    File.join(Dir.home, ENCRYPTED_CONFIG_FILE)
  ].find { |path| File.exist?(path) }
end

.encrypted_config?Boolean

Returns:

  • (Boolean)


30
# File 'lib/light_gpt_proxy.rb', line 30

def encrypted_config? = config_path&.end_with?('.enc')

.guard(pass = passwd) ⇒ Object



57
# File 'lib/light_gpt_proxy.rb', line 57

def guard(pass = passwd) = Guard.new(pass)

.loggerObject



59
# File 'lib/light_gpt_proxy.rb', line 59

def logger = @logger ||= Logger.new($stdout, verbose: verb)

.provider(name) ⇒ Object



58
# File 'lib/light_gpt_proxy.rb', line 58

def provider(name) = config.provider(name)

.serverObject



60
# File 'lib/light_gpt_proxy.rb', line 60

def server = @server ||= LightGptProxy::ProxyServer

.start(port: nil, password: passwd, verbose: true) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/light_gpt_proxy.rb', line 62

def start(port: nil, password: passwd, verbose: true)
  @passwd = password
  @verb = verbose
  verify_password! if encrypted_config?
  port ||= config.default_port
  logger.info("Starting LightGPT Proxy server on port #{port} with Puma")
  @server_thread = Thread.new do
    Rack::Handler::Puma.run(server, Port: port)
  end
  @server_thread.join # Keep the server running by joining the thread indefinitely
rescue => e # rubocop:disable Style/RescueStandardError
  logger.error("Failed to start server: #{e.message}")
end

.stopObject



80
81
82
83
84
85
86
87
88
# File 'lib/light_gpt_proxy.rb', line 80

def stop
  logger.info('Stopping LightGPT Proxy server')
  if @server_thread
    Thread.kill(@server_thread)
    logger.info('Server stopped successfully')
  else
    logger.warn('Server is not running')
  end
end

.verify_password!Object



76
77
78
# File 'lib/light_gpt_proxy.rb', line 76

def verify_password!
  config
end