From e84339d9af85cde62baefa4c38ebb12ff4f82766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mugnolo?= Date: Wed, 7 May 2025 21:39:58 -0300 Subject: [PATCH 1/2] Handle OpenAI organization and project IDs This commit adds support for OpenAI organization and project IDs. Closes #160 --- docs/configuration.md | 11 +++++++++++ lib/ruby_llm/configuration.rb | 2 ++ lib/ruby_llm/providers/openai.rb | 6 ++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index f1b0dcc8c..9f7f41045 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -46,6 +46,8 @@ RubyLLM.configure do |config| # Provide keys ONLY for the providers you intend to use. # Using environment variables (ENV.fetch) is highly recommended. config.openai_api_key = ENV.fetch('OPENAI_API_KEY', nil) + config.openai_organization_id = ENV.fetch('OPENAI_ORGANIZATION_ID', nil) + config.openai_project_id = ENV.fetch('OPENAI_PROJECT_ID', nil) config.anthropic_api_key = ENV.fetch('ANTHROPIC_API_KEY', nil) config.gemini_api_key = ENV.fetch('GEMINI_API_KEY', nil) config.deepseek_api_key = ENV.fetch('DEEPSEEK_API_KEY', nil) @@ -117,6 +119,15 @@ end This setting redirects requests made with `provider: :openai` to your specified base URL. See the [Working with Models Guide]({% link guides/models.md %}#connecting-to-custom-endpoints--using-unlisted-models) for more details on using custom models with this setting. +## Optional OpenAI Headers + +OpenAI supports additional headers for organization and project management: + +* `openai_organization_id`: Specifies the billing organization for API usage when multiple organizations are accessible. +* `openai_project_id`: Tracks API usage for a project. + +These headers are optional and only need to be set if you want to use organization or project-specific billing. + ## Default Models These settings determine which models are used by the top-level helper methods (`RubyLLM.chat`, `RubyLLM.embed`, `RubyLLM.paint`) when no specific `model:` argument is provided. diff --git a/lib/ruby_llm/configuration.rb b/lib/ruby_llm/configuration.rb index 06abbec5e..3becaa4cb 100644 --- a/lib/ruby_llm/configuration.rb +++ b/lib/ruby_llm/configuration.rb @@ -13,6 +13,8 @@ class Configuration # Provider-specific configuration attr_accessor :openai_api_key, :openai_api_base, + :openai_organization_id, + :openai_project_id, :anthropic_api_key, :gemini_api_key, :deepseek_api_key, diff --git a/lib/ruby_llm/providers/openai.rb b/lib/ruby_llm/providers/openai.rb index 3dde96b9a..7ad39d9c1 100644 --- a/lib/ruby_llm/providers/openai.rb +++ b/lib/ruby_llm/providers/openai.rb @@ -34,8 +34,10 @@ def api_base(config) def headers(config) { - 'Authorization' => "Bearer #{config.openai_api_key}" - } + 'Authorization' => "Bearer #{config.openai_api_key}", + 'OpenAI-Organization' => config.openai_organization_id, + 'OpenAI-Project' => config.openai_project_id + }.compact end def capabilities From d1afd99c40771cbaddc8e3cfd327f6d5b8b38024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mugnolo?= Date: Thu, 8 May 2025 10:15:41 -0300 Subject: [PATCH 2/2] Filter out keys ending in _id --- lib/ruby_llm/configuration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_llm/configuration.rb b/lib/ruby_llm/configuration.rb index 3becaa4cb..3e605dd3f 100644 --- a/lib/ruby_llm/configuration.rb +++ b/lib/ruby_llm/configuration.rb @@ -58,7 +58,7 @@ def initialize def inspect redacted = lambda do |name, value| - if name.match?(/_key|_secret|_token$/) + if name.match?(/_id|_key|_secret|_token$/) value.nil? ? 'nil' : '[FILTERED]' else value