summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rw-r--r--components/provider.rb6
-rw-r--r--components/providers/base.rb4
-rw-r--r--components/providers/openai.rb48
-rw-r--r--components/storage.rb44
4 files changed, 67 insertions, 35 deletions
diff --git a/components/provider.rb b/components/provider.rb
index 3138cc4..163b099 100644
--- a/components/provider.rb
+++ b/components/provider.rb
@@ -8,11 +8,11 @@ module NanoBot
module Components
class Provider
def self.new(provider, environment: {})
- case provider[:name]
+ case provider[:id]
when 'openai'
- Providers::OpenAI.new(provider[:settings], environment:)
+ Providers::OpenAI.new(provider[:settings], provider[:credentials], environment:)
else
- raise "Unsupported provider #{provider[:name]}"
+ raise "Unsupported provider \"#{provider[:id]}\""
end
end
end
diff --git a/components/providers/base.rb b/components/providers/base.rb
index 011c5dd..7a99833 100644
--- a/components/providers/base.rb
+++ b/components/providers/base.rb
@@ -6,6 +6,10 @@ module NanoBot
module Components
module Providers
class Base
+ def initialize(_settings, _credentials, _environment: {})
+ raise NoMethodError, "The 'initialize' method is not implemented for the current provider."
+ end
+
def evaluate(_payload)
raise NoMethodError, "The 'evaluate' method is not implemented for the current provider."
end
diff --git a/components/providers/openai.rb b/components/providers/openai.rb
index c64a588..ce6fb33 100644
--- a/components/providers/openai.rb
+++ b/components/providers/openai.rb
@@ -9,6 +9,8 @@ module NanoBot
module Components
module Providers
class OpenAI < Base
+ DEFAULT_ADDRESS = 'https://api.openai.com'
+
CHAT_SETTINGS = %i[
model stream temperature top_p n stop max_tokens
presence_penalty frequency_penalty logit_bias
@@ -16,14 +18,18 @@ module NanoBot
attr_reader :settings
- def initialize(settings, environment: {})
+ def initialize(settings, credentials, environment: {})
@settings = settings
+ @credentials = credentials
@environment = environment
- @client = ::OpenAI::Client.new(
- uri_base: "#{@settings[:credentials][:address].sub(%r{/$}, '')}/",
- access_token: @settings[:credentials][:'access-token']
- )
+ uri_base = if @credentials[:address].nil? || @credentials[:address].to_s.strip.empty?
+ "#{DEFAULT_ADDRESS}/"
+ else
+ "#{@credentials[:address].to_s.sub(%r{/$}, '')}/"
+ end
+
+ @client = ::OpenAI::Client.new(uri_base:, access_token: @credentials[:'access-token'])
end
def stream(input)
@@ -48,16 +54,7 @@ module NanoBot
)
end
- user = @settings[:credentials][:'user-identifier']
-
- user_suffix = @environment && (
- @environment['NANO_BOTS_USER_IDENTIFIER'] ||
- @environment[:NANO_BOTS_USER_IDENTIFIER]
- )
-
- user = "#{user}/#{user_suffix}" if user_suffix && user_suffix != ''
-
- payload = { model: @settings[:model], user: Crypto.encrypt(user, soft: true), messages: }
+ payload = { user: OpenAI.end_user(@settings, @environment), messages: }
CHAT_SETTINGS.each do |key|
payload[key] = @settings[key] if @settings.key?(key)
@@ -87,6 +84,27 @@ module NanoBot
block.call({ who: 'AI', message: result.dig('choices', 0, 'message', 'content') }, true)
end
end
+
+ def self.end_user(settings, environment)
+ user = ENV.fetch('NANO_BOTS_END_USER', nil)
+
+ user = settings[:user] if !settings[:user].nil? && !settings[:user].to_s.strip.empty?
+
+ candidate = environment && (
+ environment['NANO_BOTS_END_USER'] ||
+ environment[:NANO_BOTS_END_USER]
+ )
+
+ user = candidate if !candidate.nil? && !candidate.to_s.strip.empty?
+
+ user = if user.nil? || user.to_s.strip.empty?
+ 'unknown'
+ else
+ user.to_s.strip
+ end
+
+ Crypto.encrypt(user, soft: true)
+ end
end
end
end
diff --git a/components/storage.rb b/components/storage.rb
index b6d8910..577ce67 100644
--- a/components/storage.rb
+++ b/components/storage.rb
@@ -8,6 +8,31 @@ require_relative './crypto'
module NanoBot
module Components
class Storage
+ def self.end_user(cartridge, environment)
+ user = ENV.fetch('NANO_BOTS_END_USER', nil)
+
+ if cartridge[:provider][:id] == 'openai' &&
+ !cartridge[:provider][:settings][:user].nil? &&
+ !cartridge[:provider][:settings][:user].to_s.strip.empty?
+ user = cartridge[:provider][:settings][:user]
+ end
+
+ candidate = environment && (
+ environment['NANO_BOTS_END_USER'] ||
+ environment[:NANO_BOTS_END_USER]
+ )
+
+ user = candidate if !candidate.nil? && !candidate.to_s.strip.empty?
+
+ user = if user.nil? || user.to_s.strip.empty?
+ 'unknown'
+ else
+ user.to_s.strip
+ end
+
+ Crypto.encrypt(user, soft: true)
+ end
+
def self.build_path_and_ensure_state_file!(key, cartridge, environment: {})
path = [
Logic::Helpers::Hash.fetch(cartridge, %i[state directory]),
@@ -18,27 +43,12 @@ module NanoBot
path = "#{user_home!.sub(%r{/$}, '')}/.local/state/nano-bots" if path.nil?
- prefix = environment && (
- environment['NANO_BOTS_USER_IDENTIFIER'] ||
- environment[:NANO_BOTS_USER_IDENTIFIER]
- )
-
- path = "#{path.sub(%r{/$}, '')}/ruby-nano-bots/vault"
-
- if prefix
- normalized = prefix.split('/').map do |part|
- Crypto.encrypt(
- part.to_s.gsub('.', '-').force_encoding('UTF-8').to_slug.normalize,
- soft: true
- )
- end.join('/')
-
- path = "#{path}/#{normalized}"
- end
+ path = "#{path.sub(%r{/$}, '')}/ruby-nano-bots"
path = "#{path}/#{cartridge[:meta][:author].to_slug.normalize}"
path = "#{path}/#{cartridge[:meta][:name].to_slug.normalize}"
path = "#{path}/#{cartridge[:meta][:version].to_s.gsub('.', '-').to_slug.normalize}"
+ path = "#{path}/#{end_user(cartridge, environment)}"
path = "#{path}/#{Crypto.encrypt(key, soft: true)}"
path = "#{path}/state.json"