FEATURE: support OpenAI-Organization header (#245)
Per: https://platform.openai.com/docs/api-reference/authentication There is an organization option which is useful for large orgs > For users who belong to multiple organizations, you can pass a header to specify which organization is used for an API request. Usage from these API requests will count against the specified organization's subscription quota.
This commit is contained in:
parent
d87adcebea
commit
9242da545e
|
@ -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_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_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_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_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_openai_api_key: "API key for OpenAI API"
|
||||||
ai_anthropic_api_key: "API key for Anthropic API"
|
ai_anthropic_api_key: "API key for Anthropic API"
|
||||||
|
|
|
@ -93,6 +93,7 @@ discourse_ai:
|
||||||
ai_openai_gpt4_url: "https://api.openai.com/v1/chat/completions"
|
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_gpt4_32k_url: "https://api.openai.com/v1/chat/completions"
|
||||||
ai_openai_embeddings_url: "https://api.openai.com/v1/embeddings"
|
ai_openai_embeddings_url: "https://api.openai.com/v1/embeddings"
|
||||||
|
ai_openai_organization: ""
|
||||||
ai_openai_api_key:
|
ai_openai_api_key:
|
||||||
default: ""
|
default: ""
|
||||||
secret: true
|
secret: true
|
||||||
|
|
|
@ -48,6 +48,10 @@ module ::DiscourseAi
|
||||||
headers["Authorization"] = "Bearer #{SiteSetting.ai_openai_api_key}"
|
headers["Authorization"] = "Bearer #{SiteSetting.ai_openai_api_key}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if SiteSetting.ai_openai_organization.present?
|
||||||
|
headers["OpenAI-Organization"] = SiteSetting.ai_openai_organization
|
||||||
|
end
|
||||||
|
|
||||||
payload = { model: model, messages: messages }
|
payload = { model: model, messages: messages }
|
||||||
|
|
||||||
payload[:temperature] = temperature if temperature
|
payload[:temperature] = temperature if temperature
|
||||||
|
|
|
@ -6,6 +6,35 @@ require_relative "../../support/openai_completions_inference_stubs"
|
||||||
describe DiscourseAi::Inference::OpenAiCompletions do
|
describe DiscourseAi::Inference::OpenAiCompletions do
|
||||||
before { SiteSetting.ai_openai_api_key = "abc-123" }
|
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
|
context "when configured using Azure" do
|
||||||
it "Supports custom Azure endpoints for completions" do
|
it "Supports custom Azure endpoints for completions" do
|
||||||
gpt_url_base =
|
gpt_url_base =
|
||||||
|
|
Loading…
Reference in New Issue