diff options
author | icebaker <icebaker@proton.me> | 2023-12-15 20:44:11 -0300 |
---|---|---|
committer | icebaker <icebaker@proton.me> | 2023-12-15 20:44:11 -0300 |
commit | 4c0182e088fc1ff53a4d49c88814cd4f0e89c72a (patch) | |
tree | 9c16b56e49b3fd3fbce3a6862739b04de8c90ce3 | |
parent | 37a39d9f74a47d3e11cb04669fd00168b2cdb68e (diff) |
upgrading gemini and fixing directive
-rw-r--r-- | Gemfile.lock | 4 | ||||
-rw-r--r-- | README.md | 75 | ||||
-rw-r--r-- | components/provider.rb | 2 | ||||
-rw-r--r-- | components/providers/base.rb | 2 | ||||
-rw-r--r-- | components/providers/google.rb | 36 | ||||
-rw-r--r-- | components/providers/openai.rb | 7 | ||||
-rw-r--r-- | docker-compose.example.yml | 9 | ||||
-rw-r--r-- | nano-bots.gemspec | 2 |
8 files changed, 119 insertions, 18 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 87eea48..aa661a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,7 +5,7 @@ PATH babosa (~> 2.0) concurrent-ruby (~> 1.2, >= 1.2.2) dotenv (~> 2.8, >= 2.8.1) - gemini-ai (~> 1.0) + gemini-ai (~> 2.0) pry (~> 0.14.2) rainbow (~> 3.1, >= 3.1.1) rbnacl (~> 7.1, >= 7.1.1) @@ -34,7 +34,7 @@ GEM multipart-post (~> 2) faraday-net_http (3.0.2) ffi (1.16.3) - gemini-ai (1.0.0) + gemini-ai (2.0.0) event_stream_parser (~> 1.0) faraday (~> 2.7, >= 2.7.12) googleauth (~> 1.9, >= 1.9.1) @@ -81,6 +81,32 @@ NANO_BOTS_END_USER=your-user Click [here](https://github.com/gbaptista/gemini-ai#credentials) to learn how to obtain your credentials. +#### Option 1: API Key (Generative Language API) + +```sh +export GOOGLE_API_KEY=your-api-key + +export NANO_BOTS_ENCRYPTION_PASSWORD=UNSAFE +export NANO_BOTS_END_USER=your-user + +# export NANO_BOTS_STATE_DIRECTORY=/home/user/.local/state/nano-bots +# export NANO_BOTS_CARTRIDGES_DIRECTORY=/home/user/.local/share/nano-bots/cartridges +``` + +Alternatively, if your current directory has a `.env` file with the environment variables, they will be automatically loaded: + +```sh +GOOGLE_API_KEY=your-api-key + +NANO_BOTS_ENCRYPTION_PASSWORD=UNSAFE +NANO_BOTS_END_USER=your-user + +# NANO_BOTS_STATE_DIRECTORY=/home/user/.local/state/nano-bots +# NANO_BOTS_CARTRIDGES_DIRECTORY=/home/user/.local/share/nano-bots/cartridges +``` + +#### Option 2: Service Account (Vertex AI API) + ```sh export GOOGLE_CREDENTIALS_FILE_PATH=google-credentials.json export GOOGLE_PROJECT_ID=your-project-id @@ -139,6 +165,25 @@ services: ### Google Gemini +#### Option 1: API Key (Generative Language API) + +```yaml +--- +services: + nano-bots: + image: ruby:3.2.2-slim-bookworm + command: sh -c "apt-get update && apt-get install -y --no-install-recommends build-essential libffi-dev libsodium-dev lua5.4-dev curl && curl -s https://raw.githubusercontent.com/babashka/babashka/master/install | bash && gem install nano-bots -v 1.2.0 && bash" + environment: + GOOGLE_API_KEY: your-api-key + NANO_BOTS_ENCRYPTION_PASSWORD: UNSAFE + NANO_BOTS_END_USER: your-user + volumes: + - ./your-cartridges:/root/.local/share/nano-bots/cartridges + - ./your-state-path:/root/.local/state/nano-bots +``` + +#### Option 2: Service Account (Vertex AI API) + ```yaml --- services: @@ -346,6 +391,8 @@ provider: Read the [full specification](https://spec.nbots.io/#/README?id=google-gemini) for Google Gemini. +#### Option 1: API Key (Generative Language API) + ```yaml --- meta: @@ -363,8 +410,34 @@ behaviors: provider: id: google credentials: - project-id: ENV/GOOGLE_PROJECT_ID + service: generative-language-api + api-key: ENV/GOOGLE_API_KEY + options: + model: gemini-pro +``` + +#### Option 2: Service Account (Vertex AI API) + +```yaml +--- +meta: + symbol: 🤖 + name: Nano Bot Name + author: Your Name + version: 1.0.0 + license: CC0-1.0 + description: A helpful assistant. + +behaviors: + interaction: + directive: You are a helpful assistant. + +provider: + id: google + credentials: + service: vertex-ai-api file-path: ENV/GOOGLE_CREDENTIALS_FILE_PATH + project-id: ENV/GOOGLE_PROJECT_ID region: ENV/GOOGLE_REGION options: model: gemini-pro diff --git a/components/provider.rb b/components/provider.rb index d83319f..57f1cca 100644 --- a/components/provider.rb +++ b/components/provider.rb @@ -9,7 +9,7 @@ module NanoBot def self.new(provider, environment: {}) case provider[:id] when 'openai' - Providers::OpenAI.new(provider[:settings], provider[:credentials], environment:) + Providers::OpenAI.new(nil, provider[:settings], provider[:credentials], environment:) when 'google' Providers::Google.new(provider[:options], provider[:settings], provider[:credentials], environment:) else diff --git a/components/providers/base.rb b/components/providers/base.rb index 7a99833..0bea758 100644 --- a/components/providers/base.rb +++ b/components/providers/base.rb @@ -6,7 +6,7 @@ module NanoBot module Components module Providers class Base - def initialize(_settings, _credentials, _environment: {}) + def initialize(_options, _settings, _credentials, _environment: {}) raise NoMethodError, "The 'initialize' method is not implemented for the current provider." end diff --git a/components/providers/google.rb b/components/providers/google.rb index f847677..b15dd1b 100644 --- a/components/providers/google.rb +++ b/components/providers/google.rb @@ -13,26 +13,38 @@ module NanoBot module Components module Providers class Google < Base + SAFETY_SETTINGS = %i[category threshold].freeze + SETTINGS = { generationConfig: %i[ temperature topP topK candidateCount maxOutputTokens stopSequences ].freeze }.freeze - SAFETY_SETTINGS = %i[category threshold].freeze - attr_reader :settings def initialize(options, settings, credentials, _environment) @settings = settings + gemini_credentials = if credentials[:'api-key'] + { + service: credentials[:service], + api_key: credentials[:'api-key'], + project_id: credentials[:'project-id'], + region: credentials[:region] + } + else + { + service: credentials[:service], + file_path: credentials[:'file-path'], + project_id: credentials[:'project-id'], + region: credentials[:region] + } + end + @client = Gemini.new( - credentials: { - file_path: credentials[:'file-path'], - project_id: credentials[:'project-id'], - region: credentials[:region] - }, - settings: { model: options[:model], stream: options[:stream] } + credentials: gemini_credentials, + options: { model: options[:model], stream: options[:stream] } ) end @@ -61,9 +73,15 @@ module NanoBot %i[backdrop directive].each do |key| next unless input[:behavior][key] + messages.prepend( + { role: 'model', + parts: { text: 'Understood.' }, + _meta: { at: Time.now } } + ) + # TODO: Does Gemini have system messages? messages.prepend( - { role: key == :directive ? 'user' : 'user', + { role: 'user', parts: { text: input[:behavior][key] }, _meta: { at: Time.now } } ) diff --git a/components/providers/openai.rb b/components/providers/openai.rb index f6eafd4..e71f143 100644 --- a/components/providers/openai.rb +++ b/components/providers/openai.rb @@ -17,13 +17,14 @@ module NanoBot DEFAULT_ADDRESS = 'https://api.openai.com' CHAT_SETTINGS = %i[ - model stream temperature top_p n stop max_tokens - presence_penalty frequency_penalty logit_bias seed response_format + model stream frequency_penalty logit_bias logprobs top_logprobs + max_tokens n presence_penalty response_format seed stop temperature + top_p tool_choice ].freeze attr_reader :settings - def initialize(settings, credentials, environment: {}) + def initialize(_options, settings, credentials, environment: {}) @settings = settings @credentials = credentials @environment = environment diff --git a/docker-compose.example.yml b/docker-compose.example.yml index ea0d28c..2d397e7 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -6,8 +6,17 @@ services: environment: OPENAI_API_ADDRESS: https://api.openai.com OPENAI_API_KEY: your-access-token + + GOOGLE_API_KEY: your-api-key + + GOOGLE_CREDENTIALS_FILE_PATH: /root/.config/google-credentials.json + GOOGLE_PROJECT_ID: your-project-id + GOOGLE_REGION: us-east4 + NANO_BOTS_ENCRYPTION_PASSWORD: UNSAFE NANO_BOTS_END_USER: your-user + volumes: + - ./google-credentials.json:/root/.config/google-credentials.json - ./your-cartridges:/root/.local/share/nano-bots/cartridges - ./your-state-path:/root/.local/state/nano-bots diff --git a/nano-bots.gemspec b/nano-bots.gemspec index f11fd58..ff466aa 100644 --- a/nano-bots.gemspec +++ b/nano-bots.gemspec @@ -34,7 +34,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'babosa', '~> 2.0' spec.add_dependency 'concurrent-ruby', '~> 1.2', '>= 1.2.2' spec.add_dependency 'dotenv', '~> 2.8', '>= 2.8.1' - spec.add_dependency 'gemini-ai', '~> 1.0' + spec.add_dependency 'gemini-ai', '~> 2.0' spec.add_dependency 'pry', '~> 0.14.2' spec.add_dependency 'rainbow', '~> 3.1', '>= 3.1.1' spec.add_dependency 'rbnacl', '~> 7.1', '>= 7.1.1' |