components/providers/openai.rb in nano-bots-0.0.9 vs components/providers/openai.rb in nano-bots-0.0.10

- old
+ new

@@ -1,10 +1,11 @@ # frozen_string_literal: true require 'openai' require_relative './base' +require_relative '../crypto' module NanoBot module Components module Providers class OpenAI < Base @@ -13,12 +14,13 @@ presence_penalty frequency_penalty logit_bias ].freeze attr_reader :settings - def initialize(settings) + def initialize(settings, environment: {}) @settings = settings + @environment = environment @client = ::OpenAI::Client.new( uri_base: "#{@settings[:credentials][:address].sub(%r{/$}, '')}/", access_token: @settings[:credentials][:'access-token'] ) @@ -44,14 +46,19 @@ { role: key == :directive ? 'system' : 'user', content: input[:behavior][key] } ) end - payload = { - model: @settings[:model], - user: @settings[:credentials][:'user-identifier'], - messages: - } + 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: } CHAT_SETTINGS.each do |key| payload[key] = @settings[key] if @settings.key?(key) end