├── .github └── workflows │ └── main.yml ├── .gitignore ├── .rspec ├── .rubocop.yml ├── .ruby-version ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── Gemfile ├── Gemfile.lock ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── lib ├── open_router.rb └── open_router │ ├── client.rb │ ├── http.rb │ └── version.rb ├── open_router.gemspec ├── sig └── open_router.rbs └── spec ├── open_router_spec.rb └── spec_helper.rb /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Ruby 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | pull_request: 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | name: Ruby ${{ matrix.ruby }} 14 | strategy: 15 | matrix: 16 | ruby: 17 | - '3.2.2' 18 | 19 | steps: 20 | - uses: actions/checkout@v3 21 | - name: Set up Ruby 22 | uses: ruby/setup-ruby@v1 23 | with: 24 | ruby-version: ${{ matrix.ruby }} 25 | bundler-cache: true 26 | - name: Run the default task 27 | run: bundle exec rake 28 | env: 29 | ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | 10 | # rspec failure tracking 11 | .rspec_status 12 | *.gem 13 | .env 14 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | TargetRubyVersion: 3.2 3 | 4 | Layout/LineLength: 5 | Enabled: false 6 | 7 | Metrics/AbcSize: 8 | Enabled: false 9 | 10 | Metrics/BlockLength: 11 | Enabled: false 12 | 13 | Metrics/CyclomaticComplexity: 14 | Enabled: false 15 | 16 | Metrics/MethodLength: 17 | Enabled: false 18 | 19 | Metrics/ParameterLists: 20 | Enabled: false 21 | 22 | Metrics/PerceivedComplexity: 23 | Enabled: false 24 | 25 | Style/Documentation: 26 | Enabled: false 27 | 28 | Style/StringLiterals: 29 | Enabled: true 30 | EnforcedStyle: double_quotes 31 | 32 | Style/StringLiteralsInInterpolation: 33 | Enabled: true 34 | EnforcedStyle: double_quotes 35 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 3.2.2 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [0.3.0] - 2024-05-03 2 | 3 | - Uses Faraday's built-in JSON mode 4 | - Added support for configuring Faraday and its middleware 5 | - Spec creates a STDOUT logger by default (headers, bodies, errors) 6 | - Spec filters Bearer token from logs by default 7 | 8 | ## [0.1.0] - 2024-03-19 9 | 10 | - Initial release 11 | 12 | ## [Unreleased] 13 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. 8 | 9 | ## Our Standards 10 | 11 | Examples of behavior that contributes to a positive environment for our community include: 12 | 13 | * Demonstrating empathy and kindness toward other people 14 | * Being respectful of differing opinions, viewpoints, and experiences 15 | * Giving and gracefully accepting constructive feedback 16 | * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience 17 | * Focusing on what is best not just for us as individuals, but for the overall community 18 | 19 | Examples of unacceptable behavior include: 20 | 21 | * The use of sexualized language or imagery, and sexual attention or 22 | advances of any kind 23 | * Trolling, insulting or derogatory comments, and personal or political attacks 24 | * Public or private harassment 25 | * Publishing others' private information, such as a physical or email 26 | address, without their explicit permission 27 | * Other conduct which could reasonably be considered inappropriate in a 28 | professional setting 29 | 30 | ## Enforcement Responsibilities 31 | 32 | Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. 33 | 34 | Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. 35 | 36 | ## Scope 37 | 38 | This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. 39 | 40 | ## Enforcement 41 | 42 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at obiefernandez@gmail.com. All complaints will be reviewed and investigated promptly and fairly. 43 | 44 | All community leaders are obligated to respect the privacy and security of the reporter of any incident. 45 | 46 | ## Enforcement Guidelines 47 | 48 | Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: 49 | 50 | ### 1. Correction 51 | 52 | **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. 53 | 54 | **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. 55 | 56 | ### 2. Warning 57 | 58 | **Community Impact**: A violation through a single incident or series of actions. 59 | 60 | **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. 61 | 62 | ### 3. Temporary Ban 63 | 64 | **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. 65 | 66 | **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. 67 | 68 | ### 4. Permanent Ban 69 | 70 | **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. 71 | 72 | **Consequence**: A permanent ban from any sort of public interaction within the community. 73 | 74 | ## Attribution 75 | 76 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, 77 | available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 78 | 79 | Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). 80 | 81 | [homepage]: https://www.contributor-covenant.org 82 | 83 | For answers to common questions about this code of conduct, see the FAQ at 84 | https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. 85 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | # Specify your gem's dependencies in open_router.gemspec 6 | gemspec 7 | 8 | gem "activesupport", ">= 6.0" 9 | 10 | group :development do 11 | gem "dotenv", ">= 2" 12 | gem "pry", ">= 0.14" 13 | gem "rake", "~> 13.0" 14 | gem "rspec", "~> 3.0" 15 | gem "rubocop", "~> 1.21" 16 | gem "solargraph-rails", "~> 0.2.0.pre" 17 | gem "sorbet" 18 | gem "tapioca", require: false 19 | end 20 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | open_router (0.3.3) 5 | activesupport (>= 6.0) 6 | dotenv (>= 2) 7 | faraday (>= 1) 8 | faraday-multipart (>= 1) 9 | 10 | GEM 11 | remote: https://rubygems.org/ 12 | specs: 13 | activesupport (7.1.3.2) 14 | base64 15 | bigdecimal 16 | concurrent-ruby (~> 1.0, >= 1.0.2) 17 | connection_pool (>= 2.2.5) 18 | drb 19 | i18n (>= 1.6, < 2) 20 | minitest (>= 5.1) 21 | mutex_m 22 | tzinfo (~> 2.0) 23 | ast (2.4.2) 24 | backport (1.2.0) 25 | base64 (0.2.0) 26 | benchmark (0.3.0) 27 | bigdecimal (3.1.7) 28 | coderay (1.1.3) 29 | concurrent-ruby (1.2.3) 30 | connection_pool (2.4.1) 31 | diff-lcs (1.5.0) 32 | dotenv (3.1.0) 33 | drb (2.2.1) 34 | e2mmap (0.1.0) 35 | erubi (1.12.0) 36 | faraday (2.7.10) 37 | faraday-net_http (>= 2.0, < 3.1) 38 | ruby2_keywords (>= 0.0.4) 39 | faraday-multipart (1.0.4) 40 | multipart-post (~> 2) 41 | faraday-net_http (3.0.2) 42 | i18n (1.14.4) 43 | concurrent-ruby (~> 1.0) 44 | jaro_winkler (1.5.6) 45 | json (2.6.3) 46 | kramdown (2.4.0) 47 | rexml 48 | kramdown-parser-gfm (1.1.0) 49 | kramdown (~> 2.0) 50 | language_server-protocol (3.17.0.3) 51 | method_source (1.0.0) 52 | minitest (5.22.3) 53 | multipart-post (2.3.0) 54 | mutex_m (0.2.0) 55 | netrc (0.11.0) 56 | nokogiri (1.16.4-arm64-darwin) 57 | racc (~> 1.4) 58 | nokogiri (1.16.4-x86_64-linux) 59 | racc (~> 1.4) 60 | parallel (1.23.0) 61 | parser (3.2.2.3) 62 | ast (~> 2.4.1) 63 | racc 64 | prism (0.24.0) 65 | pry (0.14.2) 66 | coderay (~> 1.1) 67 | method_source (~> 1.0) 68 | racc (1.7.1) 69 | rainbow (3.1.1) 70 | rake (13.0.6) 71 | rbi (0.1.10) 72 | prism (>= 0.18.0, < 0.25) 73 | sorbet-runtime (>= 0.5.9204) 74 | rbs (2.8.4) 75 | regexp_parser (2.8.1) 76 | reverse_markdown (2.1.1) 77 | nokogiri 78 | rexml (3.2.6) 79 | rspec (3.12.0) 80 | rspec-core (~> 3.12.0) 81 | rspec-expectations (~> 3.12.0) 82 | rspec-mocks (~> 3.12.0) 83 | rspec-core (3.12.2) 84 | rspec-support (~> 3.12.0) 85 | rspec-expectations (3.12.3) 86 | diff-lcs (>= 1.2.0, < 2.0) 87 | rspec-support (~> 3.12.0) 88 | rspec-mocks (3.12.6) 89 | diff-lcs (>= 1.2.0, < 2.0) 90 | rspec-support (~> 3.12.0) 91 | rspec-support (3.12.1) 92 | rubocop (1.55.1) 93 | json (~> 2.3) 94 | language_server-protocol (>= 3.17.0) 95 | parallel (~> 1.10) 96 | parser (>= 3.2.2.3) 97 | rainbow (>= 2.2.2, < 4.0) 98 | regexp_parser (>= 1.8, < 3.0) 99 | rexml (>= 3.2.5, < 4.0) 100 | rubocop-ast (>= 1.28.1, < 2.0) 101 | ruby-progressbar (~> 1.7) 102 | unicode-display_width (>= 2.4.0, < 3.0) 103 | rubocop-ast (1.29.0) 104 | parser (>= 3.2.1.0) 105 | ruby-progressbar (1.13.0) 106 | ruby2_keywords (0.0.5) 107 | solargraph (0.50.0) 108 | backport (~> 1.2) 109 | benchmark 110 | bundler (~> 2.0) 111 | diff-lcs (~> 1.4) 112 | e2mmap 113 | jaro_winkler (~> 1.5) 114 | kramdown (~> 2.3) 115 | kramdown-parser-gfm (~> 1.1) 116 | parser (~> 3.0) 117 | rbs (~> 2.0) 118 | reverse_markdown (~> 2.0) 119 | rubocop (~> 1.38) 120 | thor (~> 1.0) 121 | tilt (~> 2.0) 122 | yard (~> 0.9, >= 0.9.24) 123 | solargraph-rails (0.2.2.pre.4) 124 | activesupport 125 | solargraph (>= 0.41.1) 126 | sorbet (0.5.11342) 127 | sorbet-static (= 0.5.11342) 128 | sorbet-runtime (0.5.11342) 129 | sorbet-static (0.5.11342-universal-darwin) 130 | sorbet-static (0.5.11342-x86_64-linux) 131 | sorbet-static-and-runtime (0.5.11342) 132 | sorbet (= 0.5.11342) 133 | sorbet-runtime (= 0.5.11342) 134 | spoom (1.3.0) 135 | erubi (>= 1.10.0) 136 | prism (>= 0.19.0) 137 | sorbet-static-and-runtime (>= 0.5.10187) 138 | thor (>= 0.19.2) 139 | tapioca (0.13.1) 140 | bundler (>= 2.2.25) 141 | netrc (>= 0.11.0) 142 | parallel (>= 1.21.0) 143 | rbi (>= 0.1.4, < 0.2) 144 | sorbet-static-and-runtime (>= 0.5.11087) 145 | spoom (>= 1.2.0) 146 | thor (>= 1.2.0) 147 | yard-sorbet 148 | thor (1.3.1) 149 | tilt (2.3.0) 150 | tzinfo (2.0.6) 151 | concurrent-ruby (~> 1.0) 152 | unicode-display_width (2.4.2) 153 | yard (0.9.36) 154 | yard-sorbet (0.8.1) 155 | sorbet-runtime (>= 0.5) 156 | yard (>= 0.9) 157 | 158 | PLATFORMS 159 | arm64-darwin-21 160 | x86_64-linux 161 | 162 | DEPENDENCIES 163 | activesupport (>= 6.0) 164 | dotenv (>= 2) 165 | open_router! 166 | pry (>= 0.14) 167 | rake (~> 13.0) 168 | rspec (~> 3.0) 169 | rubocop (~> 1.21) 170 | solargraph-rails (~> 0.2.0.pre) 171 | sorbet 172 | tapioca 173 | 174 | BUNDLED WITH 175 | 2.4.12 176 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2024 Obie Fernandez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenRouter 2 | 3 | The future will bring us hundreds of language models and dozens of providers for each. How will you choose the best? 4 | 5 | The [OpenRouter API](https://openrouter.ai/docs) is a single unified interface for all LLMs! And now you can easily use it with Ruby! 🤖🌌 6 | 7 | ## Features 8 | 9 | - **Prioritize price or performance**: OpenRouter scouts for the lowest prices and best latencies/throughputs across dozens of providers, and lets you choose how to prioritize them. 10 | - **Standardized API**: No need to change your code when switching between models or providers. You can even let users choose and pay for their own. 11 | - **Easy integration**: This Ruby gem provides a simple and intuitive interface to interact with the OpenRouter API, making it effortless to integrate AI capabilities into your Ruby applications. 12 | 13 | 👬 This Ruby library was originally bootstrapped from the [🤖 Anthropic](https://github.com/alexrudall/anthropic) gem by Alex Rudall, and subsequently extracted from the codebase of my fast-growing AI startup called [Olympia](https://olympia.chat?utm_source=open_router_gem&utm_medium=github) that lets you add AI-powered consultants to your startup! 14 | 15 | 🚢 Need someone to develop AI software for you using modern Ruby on Rails? My other company Magma Labs does exactly that: [magmalabs.io](https://www.magmalabs.io/?utm_source=open_router_gem&utm_medium=github). In fact, we also sell off-the-shelf solutions based on my early work on the field, via a platform called [MagmaChat](https://magmachat.ai?utm_source=open_router_gem&utm_medium=github) 16 | 17 | 18 | [🐦 Olympia's Twitter](https://twitter.com/OlympiaChat) | [🐦 Obie's Twitter](https://twitter.com/OlympiaChat) | [🎮 Ruby AI Builders Discord](https://discord.gg/k4Uc224xVD) 19 | 20 | ### Bundler 21 | 22 | Add this line to your application's Gemfile: 23 | 24 | ```ruby 25 | gem "open_router" 26 | ``` 27 | 28 | And then execute: 29 | 30 | $ bundle install 31 | 32 | ### Gem install 33 | 34 | Or install with: 35 | 36 | $ gem install open_router 37 | 38 | and require with: 39 | 40 | ```ruby 41 | require "open_router" 42 | ``` 43 | 44 | ## Usage 45 | 46 | - Get your API key from [https://openrouter.ai/keys](https://openrouter.ai/keys) 47 | 48 | ### Quickstart 49 | 50 | Configure the gem with your API keys, for example in an `open_router.rb` initializer file. Never hardcode secrets into your codebase - instead use `Rails.application.credentials` or something like [dotenv](https://github.com/motdotla/dotenv) to pass the keys safely into your environments. 51 | 52 | ```ruby 53 | OpenRouter.configure do |config| 54 | config.access_token = Rails.application.credentials.open_router[:access_token] 55 | config.site_name = 'Olympia' 56 | config.site_url = 'https://olympia.chat' 57 | end 58 | ``` 59 | 60 | Then you can create a client like this: 61 | 62 | ```ruby 63 | client = OpenRouter::Client.new 64 | ``` 65 | 66 | #### Configure Faraday 67 | 68 | The configuration object exposes a [`faraday`](https://github.com/lostisland/faraday-retry) method that you can pass a block to configure Faraday settings and middleware. 69 | 70 | This example adds `faraday-retry` and a logger that redacts the api key so it doesn't get leaked to logs. 71 | 72 | ```ruby 73 | require 'faraday/retry' 74 | 75 | retry_options = { 76 | max: 2, 77 | interval: 0.05, 78 | interval_randomness: 0.5, 79 | backoff_factor: 2 80 | } 81 | 82 | OpenRouter::Client.new(access_token: ENV["ACCESS_TOKEN"]) do |config| 83 | config.faraday do |f| 84 | f.request :retry, retry_options 85 | f.response :logger, ::Logger.new($stdout), { headers: true, bodies: true, errors: true } do |logger| 86 | logger.filter(/(Bearer) (\S+)/, '\1[REDACTED]') 87 | end 88 | end 89 | end 90 | ``` 91 | 92 | #### Change version or timeout 93 | 94 | The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the `request_timeout` when initializing the client. 95 | 96 | ```ruby 97 | client = OpenRouter::Client.new( 98 | access_token: "access_token_goes_here", 99 | request_timeout: 240 # Optional 100 | ) 101 | ``` 102 | 103 | ### Completions 104 | 105 | Hit the OpenRouter API for a completion: 106 | 107 | ```ruby 108 | messages = [ 109 | { role: "system", content: "You are a helpful assistant." }, 110 | { role: "user", content: "What is the color of the sky?" } 111 | ] 112 | 113 | response = client.complete(messages) 114 | puts response["choices"][0]["message"]["content"] 115 | # => "The sky is typically blue during the day due to a phenomenon called Rayleigh scattering. Sunlight..." 116 | ``` 117 | 118 | ### Models 119 | 120 | Pass an array to the `model` parameter to enable [explicit model routing](https://openrouter.ai/docs#model-routing). 121 | 122 | ```ruby 123 | OpenRouter::Client.new.complete( 124 | [ 125 | { role: "system", content: SYSTEM_PROMPT }, 126 | { role: "user", content: "Provide analysis of the data formatted as JSON:" } 127 | ], 128 | model: [ 129 | "mistralai/mixtral-8x7b-instruct:nitro", 130 | "mistralai/mixtral-8x7b-instruct" 131 | ], 132 | extras: { 133 | response_format: { 134 | type: "json_object" 135 | } 136 | } 137 | ) 138 | ``` 139 | 140 | [Browse full list of models available](https://openrouter.ai/models) or fetch from the OpenRouter API: 141 | 142 | ```ruby 143 | models = client.models 144 | puts models 145 | # => [{"id"=>"openrouter/auto", "object"=>"model", "created"=>1684195200, "owned_by"=>"openrouter", "permission"=>[], "root"=>"openrouter", "parent"=>nil}, ...] 146 | ``` 147 | 148 | ### Query Generation Stats 149 | 150 | Query the generation stats for a given generation ID: 151 | 152 | ```ruby 153 | generation_id = "generation-abcdefg" 154 | stats = client.query_generation_stats(generation_id) 155 | puts stats 156 | # => {"id"=>"generation-abcdefg", "object"=>"generation", "created"=>1684195200, "model"=>"openrouter/auto", "usage"=>{"prompt_tokens"=>10, "completion_tokens"=>50, "total_tokens"=>60}, "cost"=>0.0006} 157 | ``` 158 | 159 | ## Errors 160 | 161 | The client will raise an `OpenRouter::ServerError` in the case of an error returned from a completion (or empty response). 162 | 163 | ## Contributing 164 | 165 | Bug reports and pull requests are welcome on GitHub at . This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/OlympiaAI/open_router/blob/main/CODE_OF_CONDUCT.md). 166 | 167 | ## License 168 | 169 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 170 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "bundler/gem_tasks" 4 | require "rspec/core/rake_task" 5 | 6 | RSpec::Core::RakeTask.new(:spec) 7 | 8 | require "rubocop/rake_task" 9 | 10 | RuboCop::RakeTask.new 11 | 12 | task default: %i[spec rubocop] 13 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require "bundler/setup" 5 | require "open_router" 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | require "irb" 11 | IRB.start(__FILE__) 12 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /lib/open_router.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "faraday" 4 | require "faraday/multipart" 5 | 6 | require_relative "open_router/http" 7 | require_relative "open_router/client" 8 | require_relative "open_router/version" 9 | 10 | module OpenRouter 11 | class Error < StandardError; end 12 | class ConfigurationError < Error; end 13 | 14 | class Configuration 15 | attr_writer :access_token 16 | attr_accessor :api_version, :extra_headers, :faraday_config, :log_errors, :request_timeout, :uri_base 17 | 18 | DEFAULT_API_VERSION = "v1" 19 | DEFAULT_REQUEST_TIMEOUT = 120 20 | DEFAULT_URI_BASE = "https://openrouter.ai/api" 21 | 22 | def initialize 23 | self.access_token = nil 24 | self.api_version = DEFAULT_API_VERSION 25 | self.extra_headers = {} 26 | self.log_errors = false 27 | self.request_timeout = DEFAULT_REQUEST_TIMEOUT 28 | self.uri_base = DEFAULT_URI_BASE 29 | end 30 | 31 | def access_token 32 | return @access_token if @access_token 33 | 34 | raise ConfigurationError, "OpenRouter access token missing!" 35 | end 36 | 37 | def faraday(&block) 38 | self.faraday_config = block 39 | end 40 | 41 | def site_name=(value) 42 | @extra_headers["X-Title"] = value 43 | end 44 | 45 | def site_url=(value) 46 | @extra_headers["HTTP-Referer"] = value 47 | end 48 | end 49 | 50 | class << self 51 | attr_writer :configuration 52 | end 53 | 54 | def self.configuration 55 | @configuration ||= OpenRouter::Configuration.new 56 | end 57 | 58 | def self.configure 59 | yield(configuration) 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /lib/open_router/client.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "active_support/core_ext/object/blank" 4 | require "active_support/core_ext/hash/indifferent_access" 5 | 6 | require_relative "http" 7 | 8 | module OpenRouter 9 | class ServerError < StandardError; end 10 | 11 | class Client 12 | include OpenRouter::HTTP 13 | 14 | # Initializes the client with optional configurations. 15 | def initialize(access_token: nil, request_timeout: nil, uri_base: nil, extra_headers: {}) 16 | OpenRouter.configuration.access_token = access_token if access_token 17 | OpenRouter.configuration.request_timeout = request_timeout if request_timeout 18 | OpenRouter.configuration.uri_base = uri_base if uri_base 19 | OpenRouter.configuration.extra_headers = extra_headers if extra_headers.any? 20 | yield(OpenRouter.configuration) if block_given? 21 | end 22 | 23 | # Performs a chat completion request to the OpenRouter API. 24 | # @param messages [Array] Array of message hashes with role and content, like [{role: "user", content: "What is the meaning of life?"}] 25 | # @param model [String|Array] Model identifier, or array of model identifiers if you want to fallback to the next model in case of failure 26 | # @param providers [Array] Optional array of provider identifiers, ordered by priority 27 | # @param transforms [Array] Optional array of strings that tell OpenRouter to apply a series of transformations to the prompt before sending it to the model. Transformations are applied in-order 28 | # @param extras [Hash] Optional hash of model-specific parameters to send to the OpenRouter API 29 | # @param stream [Proc, nil] Optional callable object for streaming 30 | # @return [Hash] The completion response. 31 | def complete(messages, model: "openrouter/auto", providers: [], transforms: [], extras: {}, stream: nil) 32 | parameters = { messages: } 33 | if model.is_a?(String) 34 | parameters[:model] = model 35 | elsif model.is_a?(Array) 36 | parameters[:models] = model 37 | parameters[:route] = "fallback" 38 | end 39 | parameters[:provider] = { provider: { order: providers } } if providers.any? 40 | parameters[:transforms] = transforms if transforms.any? 41 | parameters[:stream] = stream if stream 42 | parameters.merge!(extras) 43 | 44 | post(path: "/chat/completions", parameters:).tap do |response| 45 | raise ServerError, response.dig("error", "message") if response.presence&.dig("error", "message").present? 46 | raise ServerError, "Empty response from OpenRouter. Might be worth retrying once or twice." if stream.blank? && response.blank? 47 | 48 | return response.with_indifferent_access if response.is_a?(Hash) 49 | end 50 | end 51 | 52 | # Fetches the list of available models from the OpenRouter API. 53 | # @return [Array] The list of models. 54 | def models 55 | get(path: "/models")["data"] 56 | end 57 | 58 | # Queries the generation stats for a given id. 59 | # @param generation_id [String] The generation id returned from a previous request. 60 | # @return [Hash] The stats including token counts and cost. 61 | def query_generation_stats(generation_id) 62 | response = get(path: "/generation?id=#{generation_id}") 63 | response["data"] 64 | end 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /lib/open_router/http.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module OpenRouter 4 | module HTTP 5 | def get(path:) 6 | conn.get(uri(path:)) do |req| 7 | req.headers = headers 8 | end&.body 9 | end 10 | 11 | def post(path:, parameters:) 12 | conn.post(uri(path:)) do |req| 13 | if parameters[:stream].respond_to?(:call) 14 | req.options.on_data = to_json_stream(user_proc: parameters[:stream]) 15 | parameters[:stream] = true # Necessary to tell OpenRouter to stream. 16 | end 17 | 18 | req.headers = headers 19 | req.body = parameters.to_json 20 | end&.body 21 | end 22 | 23 | def multipart_post(path:, parameters: nil) 24 | conn(multipart: true).post(uri(path:)) do |req| 25 | req.headers = headers.merge({ "Content-Type" => "multipart/form-data" }) 26 | req.body = multipart_parameters(parameters) 27 | end&.body 28 | end 29 | 30 | def delete(path:) 31 | conn.delete(uri(path:)) do |req| 32 | req.headers = headers 33 | end&.body 34 | end 35 | 36 | private 37 | 38 | # Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks. 39 | # For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could 40 | # be a data object or an error object as described in the OpenRouter API documentation. 41 | # 42 | # If the JSON object for a given data or error message is invalid, it is ignored. 43 | # 44 | # @param user_proc [Proc] The inner proc to call for each JSON object in the chunk. 45 | # @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON. 46 | def to_json_stream(user_proc:) 47 | proc do |chunk, _| 48 | chunk.scan(/(?:data|error): (\{.*\})/i).flatten.each do |data| 49 | user_proc.call(JSON.parse(data)) 50 | rescue JSON::ParserError 51 | # Ignore invalid JSON. 52 | end 53 | end 54 | end 55 | 56 | def conn(multipart: false) 57 | Faraday.new do |f| 58 | f.options[:timeout] = OpenRouter.configuration.request_timeout 59 | f.request(:multipart) if multipart 60 | f.use MiddlewareErrors if @log_errors 61 | f.response :raise_error 62 | f.response :json 63 | 64 | OpenRouter.configuration.faraday_config&.call(f) 65 | end 66 | end 67 | 68 | def uri(path:) 69 | File.join(OpenRouter.configuration.uri_base, OpenRouter.configuration.api_version, path) 70 | end 71 | 72 | def headers 73 | { 74 | "Authorization" => "Bearer #{OpenRouter.configuration.access_token}", 75 | "Content-Type" => "application/json", 76 | "X-Title" => "OpenRouter Ruby Client", 77 | "HTTP-Referer" => "https://github.com/OlympiaAI/open_router" 78 | }.merge(OpenRouter.configuration.extra_headers) 79 | end 80 | 81 | def multipart_parameters(parameters) 82 | parameters&.transform_values do |value| 83 | next value unless value.is_a?(File) 84 | 85 | # Doesn't seem like OpenRouter needs mime_type yet, so not worth 86 | # the library to figure this out. Hence the empty string 87 | # as the second argument. 88 | Faraday::UploadIO.new(value, "", value.path) 89 | end 90 | end 91 | end 92 | end 93 | -------------------------------------------------------------------------------- /lib/open_router/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module OpenRouter 4 | VERSION = "0.3.3" 5 | end 6 | -------------------------------------------------------------------------------- /open_router.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "lib/open_router/version" 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "open_router" 7 | spec.version = OpenRouter::VERSION 8 | spec.authors = ["Obie Fernandez"] 9 | spec.email = ["obiefernandez@gmail.com"] 10 | 11 | spec.summary = "Ruby library for OpenRouter API." 12 | spec.homepage = "https://github.com/OlympiaAI/open_router" 13 | spec.license = "MIT" 14 | spec.required_ruby_version = ">= 3.2.2" 15 | 16 | spec.metadata["homepage_uri"] = spec.homepage 17 | spec.metadata["source_code_uri"] = "https://github.com/OlympiaAI/open_router" 18 | spec.metadata["changelog_uri"] = "https://github.com/OlympiaAI/open_router/blob/main/CHANGELOG.md" 19 | 20 | spec.files = Dir.chdir(__dir__) do 21 | `git ls-files -z`.split("\x0").reject do |f| 22 | (File.expand_path(f) == __FILE__) || f.end_with?(".gem") || f.start_with?(*%w[bin/ test/ spec/ features/ .git 23 | .circleci appveyor]) 24 | end 25 | end 26 | 27 | spec.bindir = "exe" 28 | spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } 29 | spec.require_paths = ["lib"] 30 | 31 | spec.add_dependency "activesupport", ">= 6.0" 32 | spec.add_dependency "dotenv", ">= 2" 33 | spec.add_dependency "faraday", ">= 1" 34 | spec.add_dependency "faraday-multipart", ">= 1" 35 | end 36 | -------------------------------------------------------------------------------- /sig/open_router.rbs: -------------------------------------------------------------------------------- 1 | module OpenRouter 2 | class Client 3 | include OpenRouter::HTTP 4 | 5 | def initialize: () { (OpenRouter::Configuration) -> void } -> void 6 | 7 | def complete: ( 8 | messages: Array[Hash[Symbol, String]], 9 | ?model: String, 10 | ?providers: Array[String], 11 | ?transforms: Array[String], 12 | ?extras: Hash[Symbol, untyped], 13 | ?stream: Proc 14 | ) -> Hash[String, untyped] 15 | 16 | def models: () -> Array[Hash[String, untyped]] 17 | 18 | def query_generation_stats: (generation_id: String) -> Hash[String, untyped] 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /spec/open_router_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe OpenRouter do 4 | it "has a version number" do 5 | expect(OpenRouter::VERSION).not_to be nil 6 | end 7 | 8 | describe OpenRouter::Client do 9 | let(:client) do 10 | OpenRouter::Client.new(access_token: ENV["ACCESS_TOKEN"]) do |config| 11 | config.faraday do |f| 12 | f.response :logger, ::Logger.new($stdout), { headers: true, bodies: true, errors: true } do |logger| 13 | logger.filter(/(Bearer) (\S+)/, '\1[REDACTED]') 14 | end 15 | end 16 | end 17 | end 18 | 19 | describe "#initialize" do 20 | it "yields the configuration" do 21 | expect { |b| OpenRouter::Client.new(&b) }.to yield_with_args(OpenRouter.configuration) 22 | end 23 | end 24 | 25 | describe "#complete" do 26 | let(:messages) { [{ role: "user", content: "What is the meaning of life?" }] } 27 | let(:extras) { { max_tokens: 100 } } 28 | 29 | it "sends a POST request to the completions endpoint with the correct parameters" do 30 | # let the call execute 31 | expect(client).to receive(:post).with( 32 | path: "/chat/completions", 33 | parameters: { 34 | model: "mistralai/mistral-7b-instruct:free", 35 | messages:, 36 | max_tokens: 100 37 | } 38 | ).and_call_original 39 | puts client.complete(messages, model: "mistralai/mistral-7b-instruct:free", extras:) 40 | end 41 | end 42 | 43 | describe "#models" do 44 | it "sends a GET request to the models endpoint" do 45 | expect(client).to receive(:get).with(path: "/models").and_return({ "data" => [] }) 46 | client.models 47 | end 48 | 49 | it "returns the data from the response" do 50 | allow(client).to receive(:get).and_return({ "data" => [{ "id" => "model1" }, { "id" => "model2" }] }) 51 | expect(client.models).to eq([{ "id" => "model1" }, { "id" => "model2" }]) 52 | end 53 | end 54 | 55 | describe "#query_generation_stats" do 56 | let(:generation_id) { "generation_123" } 57 | 58 | it "sends a GET request to the generation endpoint with the generation ID" do 59 | expect(client).to receive(:get).with(path: "/generation?id=#{generation_id}").and_return({ "data" => {} }) 60 | client.query_generation_stats(generation_id) 61 | end 62 | 63 | it "returns the data from the response" do 64 | allow(client).to receive(:get).and_return({ "data" => { "tokens" => 100, "cost" => 0.01 } }) 65 | expect(client.query_generation_stats(generation_id)).to eq({ "tokens" => 100, "cost" => 0.01 }) 66 | end 67 | end 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "dotenv" 4 | require "open_router" 5 | require "pry" 6 | 7 | Dotenv.load 8 | 9 | RSpec.configure do |config| 10 | # Enable flags like --only-failures and --next-failure 11 | config.example_status_persistence_file_path = ".rspec_status" 12 | 13 | # Disable RSpec exposing methods globally on `Module` and `main` 14 | config.disable_monkey_patching! 15 | 16 | config.expect_with :rspec do |c| 17 | c.syntax = :expect 18 | end 19 | end 20 | --------------------------------------------------------------------------------