From ca4d93c59572c3bbfa03aa9270c1fca04b79cd9d Mon Sep 17 00:00:00 2001 From: Obie Fernandez Date: Thu, 28 Mar 2024 17:46:35 -0600 Subject: [PATCH] full update to 0.2.1 with passing specs --- .github/workflows/main.yml | 2 ++ .gitignore | 1 + Gemfile | 5 +++-- Gemfile.lock | 32 ++++++++++++++++++++++++++++++++ lib/open_router/client.rb | 13 +++++++++---- open_router.gemspec | 2 ++ spec/open_router_spec.rb | 21 +++++++++------------ 7 files changed, 58 insertions(+), 18 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index df96d01..361f492 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,3 +25,5 @@ jobs: bundler-cache: true - name: Run the default task run: bundle exec rake + env: + ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} diff --git a/.gitignore b/.gitignore index 783440f..726ddde 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # rspec failure tracking .rspec_status *.gem +.env diff --git a/Gemfile b/Gemfile index 75f0b59..d6cc10b 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,9 @@ source "https://rubygems.org" # Specify your gem's dependencies in open_router.gemspec gemspec +gem "activesupport", ">= 6.0" +gem "dotenv", ">= 2" +gem "pry", ">= 0.14" gem "rake", "~> 13.0" - gem "rspec", "~> 3.0" - gem "rubocop", "~> 1.21" diff --git a/Gemfile.lock b/Gemfile.lock index 59b9479..9c958bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,27 +2,54 @@ PATH remote: . specs: open_router (0.2.1) + activesupport (>= 6.0) + dotenv (>= 2) faraday (>= 1) faraday-multipart (>= 1) GEM remote: https://rubygems.org/ specs: + activesupport (7.1.3.2) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) ast (2.4.2) + base64 (0.2.0) + bigdecimal (3.1.7) + coderay (1.1.3) + concurrent-ruby (1.2.3) + connection_pool (2.4.1) diff-lcs (1.5.0) + dotenv (3.1.0) + drb (2.2.1) faraday (2.7.10) faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) faraday-multipart (1.0.4) multipart-post (~> 2) faraday-net_http (3.0.2) + i18n (1.14.4) + concurrent-ruby (~> 1.0) json (2.6.3) language_server-protocol (3.17.0.3) + method_source (1.0.0) + minitest (5.22.3) multipart-post (2.3.0) + mutex_m (0.2.0) parallel (1.23.0) parser (3.2.2.3) ast (~> 2.4.1) racc + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) racc (1.7.1) rainbow (3.1.1) rake (13.0.6) @@ -56,6 +83,8 @@ GEM parser (>= 3.2.1.0) ruby-progressbar (1.13.0) ruby2_keywords (0.0.5) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) unicode-display_width (2.4.2) PLATFORMS @@ -63,7 +92,10 @@ PLATFORMS x86_64-linux DEPENDENCIES + activesupport (>= 6.0) + dotenv (>= 2) open_router! + pry (>= 0.14) rake (~> 13.0) rspec (~> 3.0) rubocop (~> 1.21) diff --git a/lib/open_router/client.rb b/lib/open_router/client.rb index 781b82d..970ea67 100644 --- a/lib/open_router/client.rb +++ b/lib/open_router/client.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require "active_support/core_ext/object/blank" +require "active_support/core_ext/hash/indifferent_access" + require_relative "http" module OpenRouter @@ -9,7 +12,11 @@ class Client include OpenRouter::HTTP # Initializes the client with optional configurations. - def initialize + def initialize(access_token: nil, request_timeout: nil, uri_base: nil, extra_headers: {}) + OpenRouter.configuration.access_token = access_token if access_token + OpenRouter.configuration.request_timeout = request_timeout if request_timeout + OpenRouter.configuration.uri_base = uri_base if uri_base + OpenRouter.configuration.extra_headers = extra_headers if extra_headers.any? yield(OpenRouter.configuration) if block_given? end @@ -34,12 +41,10 @@ def complete(messages, model: "openrouter/auto", providers: [], transforms: [], parameters[:stream] = stream if stream parameters.merge!(extras) - puts parameters if Rails.env.local? - json_post(path: "/chat/completions", parameters:).tap do |response| raise ServerError, "Empty response from OpenRouter. Might be worth retrying once or twice." if response.blank? raise ServerError, response.dig("error", "message") if response.dig("error", "message").present? - end + end.with_indifferent_access end # Fetches the list of available models from the OpenRouter API. diff --git a/open_router.gemspec b/open_router.gemspec index f4526d5..e07fde9 100644 --- a/open_router.gemspec +++ b/open_router.gemspec @@ -28,6 +28,8 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] + spec.add_dependency "activesupport", ">= 6.0" + spec.add_dependency "dotenv", ">= 2" spec.add_dependency "faraday", ">= 1" spec.add_dependency "faraday-multipart", ">= 1" end diff --git a/spec/open_router_spec.rb b/spec/open_router_spec.rb index fae9e6a..f6a812f 100644 --- a/spec/open_router_spec.rb +++ b/spec/open_router_spec.rb @@ -1,6 +1,9 @@ # frozen_string_literal: true -# frozen_string_literal: true +require "dotenv" +require "pry" + +Dotenv.load RSpec.describe OpenRouter do it "has a version number" do @@ -8,7 +11,7 @@ end describe OpenRouter::Client do - let(:client) { OpenRouter::Client.new } + let(:client) { OpenRouter::Client.new(access_token: ENV["ACCESS_TOKEN"]) } describe "#initialize" do it "yields the configuration" do @@ -18,25 +21,19 @@ describe "#complete" do let(:messages) { [{ role: "user", content: "What is the meaning of life?" }] } - let(:model) { "openrouter/auto" } - let(:providers) { %w[provider1 provider2] } - let(:transforms) { %w[transform1 transform2] } let(:extras) { { max_tokens: 100 } } - let(:stream) { proc { |response| } } it "sends a POST request to the completions endpoint with the correct parameters" do + # let the call execute expect(client).to receive(:json_post).with( path: "/chat/completions", parameters: { - model:, + model: "mistralai/mistral-7b-instruct:free", messages:, - provider: { provider: { order: providers } }, - transforms:, - stream:, max_tokens: 100 } - ) - client.complete(messages, model:, providers:, transforms:, extras:, stream:) + ).and_call_original + puts client.complete(messages, model: "mistralai/mistral-7b-instruct:free", extras:) end end