diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 767d3d30..c5dccc51 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -41,6 +41,7 @@ en: ai_openai_gpt35_16k_url: "Custom URL used for GPT 3.5 16k chat completions. (for Azure support)" ai_openai_gpt4_url: "Custom URL used for GPT 4 chat completions. (for Azure support)" ai_openai_gpt4_32k_url: "Custom URL used for GPT 4 32k chat completions. (for Azure support)" + ai_openai_organization: "(Optional, leave empty to omit) Organization id used for the OpenAI API. Passed in using the OpenAI-Organization header." ai_openai_embeddings_url: "Custom URL used for the OpenAI embeddings API. (in the case of Azure it can be: https://COMPANY.openai.azure.com/openai/deployments/DEPLOYMENT/embeddings?api-version=2023-05-15)" ai_openai_api_key: "API key for OpenAI API" ai_anthropic_api_key: "API key for Anthropic API" diff --git a/config/settings.yml b/config/settings.yml index b1a37ffd..e0dca0c2 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -93,6 +93,7 @@ discourse_ai: ai_openai_gpt4_url: "https://api.openai.com/v1/chat/completions" ai_openai_gpt4_32k_url: "https://api.openai.com/v1/chat/completions" ai_openai_embeddings_url: "https://api.openai.com/v1/embeddings" + ai_openai_organization: "" ai_openai_api_key: default: "" secret: true diff --git a/lib/shared/inference/openai_completions.rb b/lib/shared/inference/openai_completions.rb index 3492068a..2bfa0ca0 100644 --- a/lib/shared/inference/openai_completions.rb +++ b/lib/shared/inference/openai_completions.rb @@ -48,6 +48,10 @@ module ::DiscourseAi headers["Authorization"] = "Bearer #{SiteSetting.ai_openai_api_key}" end + if SiteSetting.ai_openai_organization.present? + headers["OpenAI-Organization"] = SiteSetting.ai_openai_organization + end + payload = { model: model, messages: messages } payload[:temperature] = temperature if temperature diff --git a/spec/shared/inference/openai_completions_spec.rb b/spec/shared/inference/openai_completions_spec.rb index 6e4b56a9..92aa22d8 100644 --- a/spec/shared/inference/openai_completions_spec.rb +++ b/spec/shared/inference/openai_completions_spec.rb @@ -6,6 +6,35 @@ require_relative "../../support/openai_completions_inference_stubs" describe DiscourseAi::Inference::OpenAiCompletions do before { SiteSetting.ai_openai_api_key = "abc-123" } + it "supports sending an organization id" do + SiteSetting.ai_openai_organization = "org_123" + + stub_request(:post, "https://api.openai.com/v1/chat/completions").with( + body: + "{\"model\":\"gpt-3.5-turbo-0613\",\"messages\":[{\"role\":\"system\",\"content\":\"hello\"}]}", + headers: { + "Authorization" => "Bearer abc-123", + "Content-Type" => "application/json", + "Host" => "api.openai.com", + "User-Agent" => "Ruby", + "OpenAI-Organization" => "org_123", + }, + ).to_return( + status: 200, + body: { choices: [message: { content: "world" }] }.to_json, + headers: { + }, + ) + + result = + DiscourseAi::Inference::OpenAiCompletions.perform!( + [{ role: "system", content: "hello" }], + "gpt-3.5-turbo-0613", + ) + + expect(result.dig(:choices, 0, :message, :content)).to eq("world") + end + context "when configured using Azure" do it "Supports custom Azure endpoints for completions" do gpt_url_base =