Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add AWS Bedrock as an embeddings provider (#45519)

Rafael dos Santos Silva7e78be5af675
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-embedding-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-embedding-editor.gjs
index 5bbf0482895..328a39cf051 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-embedding-editor.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-embedding-editor.gjs
@@ -135,6 +135,10 @@ export default class AiEmbeddingEditor extends Component {
return this.args.model.id < 0;
}
+ get isBedrock() {
+ return this.currentProvider === "aws_bedrock";
+ }
+
get providerParams() {
const normalizeParam = (value) => {
if (!value) {
@@ -149,6 +153,7 @@ export default class AiEmbeddingEditor extends Component {
type: value.type || "text",
values: (value.values || []).map((v) => ({ id: v, name: v })),
default: value.default,
+ optional: value.optional ?? false,
};
};
@@ -167,6 +172,8 @@ export default class AiEmbeddingEditor extends Component {
return "select";
case "checkbox":
return "checkbox";
+ case "secret":
+ return "custom";
default:
return `input-${type}`;
}
@@ -402,21 +409,27 @@ export default class AiEmbeddingEditor extends Component {
</field.Control>
</form.Field>
- <form.Field
- class="ai-embedding-editor__url"
- @format="large"
- @name="url"
- @title={{i18n "discourse_ai.embeddings.url"}}
- @type="input"
- @validation="required"
- as |field|
- >
- <field.Control />
- </form.Field>
+ {{#unless this.isBedrock}}
+ <form.Field
+ class="ai-embedding-editor__url"
+ @format="large"
+ @name="url"
+ @title={{i18n "discourse_ai.embeddings.url"}}
+ @type="input"
+ @validation="required"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ {{/unless}}
<form.Field
class="ai-embedding-editor__api-key"
@format="large"
+ @helpText={{if
+ this.isBedrock
+ (i18n "discourse_ai.embeddings.hints.bedrock_api_key")
+ }}
@name="ai_secret_id"
@title={{i18n "discourse_ai.embeddings.api_key"}}
@type="custom"
@@ -552,7 +565,7 @@ export default class AiEmbeddingEditor extends Component {
(concat "discourse_ai.embeddings.provider_fields." name)
}}
@type={{this.fieldTypeForProviderParam params.type}}
- @validation="required"
+ @validation={{unless params.optional "required"}}
as |field|
>
{{#if (eq params.type "enum")}}
@@ -565,6 +578,14 @@ export default class AiEmbeddingEditor extends Component {
</field.Control>
{{else if (eq params.type "checkbox")}}
<field.Control />
+ {{else if (eq params.type "secret")}}
+ <field.Control>
+ <AiSecretSelector
+ @onChange={{field.set}}
+ @secrets={{this.availableSecrets}}
+ @value={{field.value}}
+ />
+ </field.Control>
{{else}}
<field.Control />
{{/if}}
diff --git a/plugins/discourse-ai/app/models/ai_secret.rb b/plugins/discourse-ai/app/models/ai_secret.rb
index b71b08948e8..90ab0e63a7e 100644
--- a/plugins/discourse-ai/app/models/ai_secret.rb
+++ b/plugins/discourse-ai/app/models/ai_secret.rb
@@ -28,7 +28,7 @@ class AiSecret < ActiveRecord::Base
decision_models.each do |decision_model|
usage << { type: "decision_model", name: decision_model.display_name, id: decision_model.id }
end
- embedding_definitions.each do |ed|
+ (embedding_definitions.to_a | provider_param_embedding_definitions.to_a).each do |ed|
usage << { type: "embedding", name: ed.display_name, id: ed.id }
end
provider_param_llms.each do |llm|
@@ -57,7 +57,7 @@ class AiSecret < ActiveRecord::Base
private
def used_by_provider_params?
- provider_param_llms.exists?
+ provider_param_llms.exists? || provider_param_embedding_definitions.exists?
end
def provider_param_llms
@@ -78,6 +78,26 @@ class AiSecret < ActiveRecord::Base
LlmModel.where(sql_conditions.join(" OR "), *bind_values).where("llm_models.id > 0")
end
+
+ def provider_param_embedding_definitions
+ secret_keys =
+ EmbeddingDefinition.provider_params.flat_map do |provider, params|
+ params.keys.filter_map do |key|
+ [provider, key] if EmbeddingDefinition.secret_param?(provider, key)
+ end
+ end
+
+ conditions =
+ secret_keys.map do |provider, key|
+ EmbeddingDefinition.sanitize_sql_array(
+ ["(provider = ? AND provider_params ->> ? = ?)", provider.to_s, key.to_s, id.to_s],
+ )
+ end
+
+ return EmbeddingDefinition.none if conditions.empty?
+
+ EmbeddingDefinition.where(conditions.join(" OR "))
+ end
end
# == Schema Information
diff --git a/plugins/discourse-ai/app/models/embedding_definition.rb b/plugins/discourse-ai/app/models/embedding_definition.rb
index 19bacd4ec7a..d6aba29ca5c 100644
--- a/plugins/discourse-ai/app/models/embedding_definition.rb
+++ b/plugins/discourse-ai/app/models/embedding_definition.rb
@@ -5,10 +5,11 @@ class EmbeddingDefinition < ActiveRecord::Base
HUGGING_FACE = "hugging_face"
OPEN_AI = "open_ai"
GOOGLE = "google"
+ AWS_BEDROCK = "aws_bedrock"
class << self
def provider_names
- [CLOUDFLARE, HUGGING_FACE, OPEN_AI, GOOGLE]
+ [CLOUDFLARE, HUGGING_FACE, OPEN_AI, GOOGLE, AWS_BEDROCK]
end
def distance_functions
@@ -30,11 +31,48 @@ class EmbeddingDefinition < ActiveRecord::Base
end
def provider_params
- { open_ai: { model_name: :text } }
+ {
+ open_ai: {
+ model_name: :text,
+ },
+ aws_bedrock: {
+ model_id: :text,
+ region: :text,
+ access_key_id: {
+ type: :secret,
+ optional: true,
+ },
+ role_arn: {
+ type: :text,
+ optional: true,
+ },
+ },
+ }
+ end
+
+ def secret_param?(provider, key)
+ definition = provider_params.dig(provider&.to_sym, key.to_sym)
+ definition == :secret || (definition.is_a?(Hash) && definition[:type] == :secret)
end
def presets
@presets ||= [
+ {
+ preset_id: "cohere-embed-v4",
+ display_name: "Cohere Embed v4 (AWS Bedrock)",
+ dimensions: 1536,
+ max_sequence_length: 8192,
+ pg_function: "<=>",
+ tokenizer_class: "DiscourseAi::Tokenizer::OpenAiCl100kTokenizer",
+ provider: AWS_BEDROCK,
+ matryoshka_dimensions: true,
+ provider_params: {
+ model_id: "cohere.embed-v4:0",
+ region: "us-east-1",
+ access_key_id: nil,
+ role_arn: nil,
+ },
+ },
{
preset_id: "bge-large-en",
display_name: "bge-large-en",
@@ -126,9 +164,13 @@ class EmbeddingDefinition < ActiveRecord::Base
validates :provider, presence: true, inclusion: provider_names
validates :display_name, presence: true, length: { maximum: 100 }
validates :tokenizer_class, presence: true, inclusion: tokenizer_names
- validates :url, :dimensions, :max_sequence_length, presence: true
+ validates :dimensions, :max_sequence_length, presence: true
+ validates :url, presence: true, unless: :aws_bedrock?
validates :pg_function, presence: true, inclusion: distance_functions
validate :api_key_or_secret_present
+ validate :bedrock_params_present, if: :aws_bedrock?
+
+ before_validation :assign_bedrock_url, if: :aws_bedrock?
after_create :create_indexes
after_update_commit :clear_provider_pause
@@ -156,13 +198,26 @@ class EmbeddingDefinition < ActiveRecord::Base
open_ai_client
when GOOGLE
gemini_client
+ when AWS_BEDROCK
+ bedrock_client
else
raise "Uknown embeddings provider"
end
end
def lookup_custom_param(key)
- provider_params&.dig(key)
+ value = provider_params&.dig(key)
+ return value if value.nil?
+
+ if self.class.secret_param?(provider, key) && value.to_s.match?(/\A\d+\z/)
+ return AiSecret.find_by(id: value.to_i)&.secret
+ end
+
+ value
+ end
+
+ def aws_bedrock?
+ provider == AWS_BEDROCK
end
def endpoint_url
@@ -203,6 +258,8 @@ class EmbeddingDefinition < ActiveRecord::Base
def api_key_or_secret_present
return if seeded?
+ # The AWS SDK can resolve credentials from the environment or instance role.
+ return if aws_bedrock?
if ai_secret_id.present?
unless AiSecret.exists?(ai_secret_id)
errors.add(:ai_secret_id, I18n.t("discourse_ai.embeddings.secret_not_found"))
@@ -213,6 +270,20 @@ class EmbeddingDefinition < ActiveRecord::Base
errors.add(:base, I18n.t("discourse_ai.embeddings.secret_required"))
end
+ def bedrock_params_present
+ %w[model_id region].each do |param|
+ next if lookup_custom_param(param).present?
+ errors.add(:base, I18n.t("discourse_ai.embeddings.missing_provider_param", param: param))
+ end
+ end
+
+ # Bedrock requests are addressed by region and model ID, so the URL is only
+ # informational, but the column is required.
+ def assign_bedrock_url
+ region = lookup_custom_param("region")
+ self.url = "https://bedrock-runtime.#{region}.amazonaws.com" if region.present?
+ end
+
def strategy
@strategy ||= DiscourseAi::Embeddings::Strategies::Truncation.new
end
@@ -241,6 +312,17 @@ class EmbeddingDefinition < ActiveRecord::Base
DiscourseAi::Inference::GeminiEmbeddings.new(endpoint_url, api_key, client_dimensions)
end
+
+ def bedrock_client
+ DiscourseAi::Inference::BedrockEmbeddings.new(
+ region: lookup_custom_param("region"),
+ model_id: lookup_custom_param("model_id"),
+ dimensions: matryoshka_dimensions ? dimensions : nil,
+ api_key: api_key,
+ access_key_id: lookup_custom_param("access_key_id"),
+ role_arn: lookup_custom_param("role_arn"),
+ )
+ end
end
# == Schema Information
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 7401cf4c696..a2182194248 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -1646,6 +1646,7 @@ en:
bge-large-en: "Open-source English-only model, self-hosted"
bge-m3: "Open-source multilingual model for longer content, self-hosted"
multilingual-e5-large: "Open-source multilingual model, self-hosted"
+ cohere-embed-v4: "Cohere's multilingual model, hosted on AWS Bedrock"
gemini-embedding-001: "Google's hosted multilingual model"
text-embedding-3-large: "OpenAI's most accurate embedding model"
text-embedding-3-small: "OpenAI's faster, lower-cost embedding model"
@@ -1659,6 +1660,7 @@ en:
failure: "Attempting to generate an embedding resulted in: %{error}"
hints:
dimensions_warning: "Once saved, this value can't be changed."
+ bedrock_api_key: "A Bedrock API key, or the secret access key when an access key ID is set. Leave empty to use an IAM role or the server's AWS credentials."
matryoshka_dimensions: "Defines the size of nested embeddings used for hierarchical or multi-layered representation of data, similar to how nested dolls fit within each other."
embed_prompt: "Task instruction prefix when generating embeddings of forum content. ONLY for embeddings models that require prefixes, like nomic-embed or stella. Not needed for most models."
search_prompt: "Task instruction prefix when generating embeddings of search queries. ONLY for embeddings models that require prefixes, like nomic-embed or stella. Not needed for most models."
@@ -1684,10 +1686,15 @@ en:
open_ai: "OpenAI"
google: "Google"
cloudflare: "Cloudflare"
+ aws_bedrock: "AWS Bedrock"
CDCK: "CDCK"
fake: "Custom"
provider_fields:
model_name: "Model name"
+ model_id: "Model ID or inference profile"
+ region: "AWS region"
+ access_key_id: "AWS access key ID"
+ role_arn: "IAM role ARN"
semantic_search: "Topics (Semantic)"
semantic_search_results:
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index d249ef658ff..fd4a7730bbf 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -1416,6 +1416,7 @@ en:
cannot_edit_builtin: "You can't edit a built-in model."
secret_required: "A credential must be selected"
secret_not_found: "The selected credential does not exist"
+ missing_provider_param: "%{param} can't be blank"
configuration:
disable_embeddings: "You have to disable 'ai embeddings enabled' first."
choose_model: "Set 'ai embeddings selected model' first."
diff --git a/plugins/discourse-ai/lib/inference/bedrock_embeddings.rb b/plugins/discourse-ai/lib/inference/bedrock_embeddings.rb
new file mode 100644
index 00000000000..c359b466451
--- /dev/null
+++ b/plugins/discourse-ai/lib/inference/bedrock_embeddings.rb
@@ -0,0 +1,183 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Inference
+ class BedrockEmbeddings
+ TIMEOUT = 60
+
+ CATEGORIES_BY_ERROR_CODE = {
+ "AccessDeniedException" => :authorization_failed,
+ "UnrecognizedClientException" => :authentication_failed,
+ "ExpiredTokenException" => :authentication_failed,
+ "ResourceNotFoundException" => :invalid_configuration,
+ "ThrottlingException" => :rate_limited,
+ "ServiceQuotaExceededException" => :rate_limited,
+ "ServiceUnavailableException" => :provider_unavailable,
+ "InternalServerException" => :provider_unavailable,
+ "ModelNotReadyException" => :provider_unavailable,
+ "ModelTimeoutException" => :provider_unavailable,
+ }.freeze
+
+ @role_credentials = {}
+ @role_credentials_mutex = Mutex.new
+
+ class << self
+ # AssumeRoleCredentials calls STS when built and refreshes itself before
+ # expiry, so share one per role instead of assuming it on every request.
+ def role_credentials(role_arn, region)
+ @role_credentials_mutex.synchronize do
+ @role_credentials[[role_arn, region]] ||= begin
+ require "aws-sdk-sts" unless defined?(Aws::STS)
+
+ Aws::AssumeRoleCredentials.new(
+ role_arn: role_arn,
+ role_session_name: "discourse-bedrock-embeddings-#{Process.pid}",
+ client: Aws::STS::Client.new(region: region),
+ )
+ end
+ end
+ end
+
+ def reset_role_credentials!
+ @role_credentials_mutex.synchronize { @role_credentials.clear }
+ end
+ end
+
+ # Authentication is resolved in this order: an IAM role to assume, a static
+ # access key pair (`api_key` holds the secret access key), a Bedrock API key
+ # sent as a bearer token, then the AWS SDK default chain (environment,
+ # shared config, instance or task role).
+ def initialize(
+ region:,
+ model_id:,
+ dimensions: nil,
+ api_key: nil,
+ access_key_id: nil,
+ role_arn: nil
+ )
+ @region = region
+ @model_id = model_id
+ @dimensions = dimensions
+ @api_key = api_key
+ @access_key_id = access_key_id
+ @role_arn = role_arn
+ end
+
+ attr_reader :region, :model_id, :dimensions
+
+ def perform!(content)
+ response =
+ client.invoke_model(
+ model_id: model_id,
+ body: payload_for(content).to_json,
+ content_type: "application/json",
+ accept: "application/json",
+ )
+
+ extract_embedding(JSON.parse(response.body.read))
+ rescue Aws::Errors::ServiceError => e
+ raise error_from_service(e)
+ rescue Aws::Errors::MissingCredentialsError, Aws::Sigv4::Errors::MissingCredentialsError => e
+ raise EmbeddingInferenceError.new(
+ provider: EmbeddingDefinition::AWS_BEDROCK,
+ category: :authentication_failed,
+ message: e.message,
+ )
+ rescue Seahorse::Client::NetworkingError => e
+ raise EmbeddingInferenceError.new(
+ provider: EmbeddingDefinition::AWS_BEDROCK,
+ category: :provider_unavailable,
+ message: e.message,
+ )
+ end
+
+ private
+
+ def model_family
+ @model_family ||=
+ if model_id.include?("cohere.embed")
+ :cohere
+ elsif model_id.include?("titan-embed-text")
+ :titan
+ else
+ raise EmbeddingInferenceError.new(
+ provider: EmbeddingDefinition::AWS_BEDROCK,
+ category: :invalid_configuration,
+ message: "Unsupported Bedrock embedding model: #{model_id}",
+ )
+ end
+ end
+
+ def payload_for(content)
+ case model_family
+ when :cohere
+ payload = {
+ texts: [content],
+ input_type: "search_document",
+ embedding_types: ["float"],
+ truncate: "END",
+ }
+ payload[:output_dimension] = dimensions if dimensions.present?
+ payload
+ when :titan
+ payload = { inputText: content, normalize: true }
+ payload[:dimensions] = dimensions if dimensions.present?
+ payload
+ end
+ end
+
+ def extract_embedding(body)
+ case model_family
+ when :cohere
+ body.dig("embeddings", "float", 0)
+ when :titan
+ body["embedding"]
+ end
+ end
+
+ def client
+ require "aws-sdk-bedrockruntime" unless defined?(Aws::BedrockRuntime)
+
+ options = { region: region, http_read_timeout: TIMEOUT }
+
+ if @role_arn.present?
+ options[:credentials] = self.class.role_credentials(@role_arn, region)
+ elsif @access_key_id.present?
+ options[:credentials] = Aws::Credentials.new(@access_key_id, @api_key)
+ elsif @api_key.present?
+ # An explicit nil skips the default credential chain, whose instance
+ # metadata probe stalls every request on hosts outside AWS.
+ options[:credentials] = nil
+ options[:token_provider] = Aws::StaticTokenProvider.new(@api_key)
+ options[:auth_scheme_preference] = ["httpBearerAuth"]
+ end
+
+ Aws::BedrockRuntime::Client.new(options)
+ end
+
+ def error_from_service(error)
+ status = error.context&.http_response&.status_code.to_i
+ code = error.code.to_s
+
+ EmbeddingInferenceError.new(
+ provider: EmbeddingDefinition::AWS_BEDROCK,
+ category: CATEGORIES_BY_ERROR_CODE[code] || category_for_status(status),
+ http_status: status.nonzero?,
+ provider_error_code: code,
+ message: error.message,
+ )
+ end
+
+ def category_for_status(status)
+ return :authentication_failed if status == 401
+ return :authorization_failed if status == 403
+ return :rate_limited if status == 429
+ return :provider_unavailable if status == 408 || status >= 500
+
+ # Validation errors can be specific to one input, so they must not pause
+ # the provider for every other request.
+ :request_rejected
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/fabricators/embedding_definition_fabricator.rb b/plugins/discourse-ai/spec/fabricators/embedding_definition_fabricator.rb
index 5daee9777c8..c0103978564 100644
--- a/plugins/discourse-ai/spec/fabricators/embedding_definition_fabricator.rb
+++ b/plugins/discourse-ai/spec/fabricators/embedding_definition_fabricator.rb
@@ -38,3 +38,14 @@ Fabricator(:gemini_embedding_def, from: :embedding_definition) do
tokenizer_class "DiscourseAi::Tokenizer::OpenAiTokenizer"
url "https://generativelanguage.googleapis.com/v1beta/models/embedding-001:embedContent"
end
+
+Fabricator(:bedrock_embedding_def, from: :embedding_definition) do
+ display_name "Cohere Embed v4"
+ provider "aws_bedrock"
+ api_key nil
+ provider_params { { model_id: "cohere.embed-v4:0", region: "us-east-1" } }
+ tokenizer_class "DiscourseAi::Tokenizer::OpenAiCl100kTokenizer"
+ max_sequence_length 8192
+ dimensions 1536
+ matryoshka_dimensions true
+end
diff --git a/plugins/discourse-ai/spec/lib/inference/bedrock_embeddings_spec.rb b/plugins/discourse-ai/spec/lib/inference/bedrock_embeddings_spec.rb
new file mode 100644
index 00000000000..19c067bf991
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/inference/bedrock_embeddings_spec.rb
@@ -0,0 +1,244 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Inference::BedrockEmbeddings do
+ subject(:client) do
+ described_class.new(
+ region: "us-east-1",
+ model_id: model_id,
+ dimensions: dimensions,
+ api_key: api_key,
+ access_key_id: access_key_id,
+ role_arn: role_arn,
+ )
+ end
+
+ let(:model_id) { "cohere.embed-v4:0" }
+ let(:dimensions) { nil }
+ let(:api_key) { "bedrock-api-key" }
+ let(:access_key_id) { nil }
+ let(:role_arn) { nil }
+ let(:invoke_url) { "https://bedrock-runtime.us-east-1.amazonaws.com/model/#{model_id}/invoke" }
+
+ before { enable_current_plugin }
+
+ after { described_class.reset_role_credentials! }
+
+ def stub_invoke(body: { embeddings: { float: [[0.1, 0.2, 0.3]] } }, status: 200, headers: {})
+ stub_request(:post, invoke_url).to_return(
+ status: status,
+ body: body.to_json,
+ headers: { "Content-Type" => "application/json" }.merge(headers),
+ )
+ end
+
+ describe "#perform!" do
+ it "requests a Cohere document embedding and returns the float vector" do
+ stub_invoke
+
+ expect(client.perform!("hello world")).to eq([0.1, 0.2, 0.3])
+ expect(WebMock).to have_requested(:post, invoke_url).with(
+ body: {
+ texts: ["hello world"],
+ input_type: "search_document",
+ embedding_types: ["float"],
+ truncate: "END",
+ },
+ )
+ end
+
+ context "with dimensions on a Cohere model" do
+ let(:dimensions) { 512 }
+
+ it "requests that output dimension" do
+ stub_invoke
+
+ client.perform!("hello world")
+
+ expect(WebMock).to have_requested(:post, invoke_url).with(
+ body: hash_including(output_dimension: 512),
+ )
+ end
+ end
+
+ context "with a Titan text model" do
+ let(:model_id) { "amazon.titan-embed-text-v2:0" }
+ let(:dimensions) { 256 }
+
+ it "sends the Titan payload and returns its embedding" do
+ stub_invoke(body: { embedding: [0.4, 0.5] })
+
+ expect(client.perform!("hello world")).to eq([0.4, 0.5])
+ expect(WebMock).to have_requested(:post, invoke_url).with(
+ body: {
+ inputText: "hello world",
+ normalize: true,
+ dimensions: 256,
+ },
+ )
+ end
+ end
+
+ context "with a model family it can't build a payload for" do
+ let(:model_id) { "amazon.nova-2-multimodal-embeddings-v1:0" }
+
+ it "raises an invalid configuration error without calling Bedrock" do
+ stub_invoke
+
+ expect { client.perform!("hello world") }.to raise_error(
+ DiscourseAi::Inference::EmbeddingInferenceError,
+ ) { |error| expect(error.category).to eq(:invalid_configuration) }
+ expect(WebMock).not_to have_requested(:post, invoke_url)
+ end
+ end
+
+ it "sends a Bedrock API key as a bearer token" do
+ stub_invoke
+
+ client.perform!("hello world")
+
+ expect(WebMock).to have_requested(:post, invoke_url).with(
+ headers: {
+ "Authorization" => "Bearer bedrock-api-key",
+ },
+ )
+ end
+
+ context "with an access key ID" do
+ let(:access_key_id) { "AKIAEXAMPLE" }
+ let(:api_key) { "secret-access-key" }
+
+ it "signs the request with that key pair" do
+ stub_invoke
+
+ client.perform!("hello world")
+
+ expect(WebMock).to have_requested(:post, invoke_url).with { |request|
+ request.headers["Authorization"].start_with?(
+ "AWS4-HMAC-SHA256 Credential=AKIAEXAMPLE/",
+ ) && request.headers["Authorization"].include?("/us-east-1/bedrock/aws4_request")
+ }
+ end
+ end
+
+ context "with a role ARN" do
+ let(:role_arn) { "arn:aws:iam::123456789012:role/bedrock" }
+ let(:api_key) { nil }
+
+ it "signs the request with the assumed role's temporary credentials" do
+ allow(Aws::STS::Client).to receive(:new).and_return(instance_double(Aws::STS::Client))
+ allow(Aws::AssumeRoleCredentials).to receive(:new).and_return(
+ Aws::Credentials.new("ASIAROLE", "role-secret", "role-session-token"),
+ )
+ stub_invoke
+
+ client.perform!("hello world")
+
+ expect(WebMock).to have_requested(:post, invoke_url).with { |request|
+ request.headers["Authorization"].start_with?("AWS4-HMAC-SHA256 Credential=ASIAROLE/") &&
+ request.headers["X-Amz-Security-Token"] == "role-session-token"
+ }
+ end
+ end
+
+ context "with a role ARN that can't be assumed" do
+ let(:role_arn) { "arn:aws:iam::123456789012:role/bedrock" }
+ let(:api_key) { nil }
+
+ around do |example|
+ previous = ENV.to_h.slice("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY")
+ ENV["AWS_ACCESS_KEY_ID"] = "AKIAFROMENV"
+ ENV["AWS_SECRET_ACCESS_KEY"] = "env-secret"
+ example.run
+ ensure
+ %w[AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY].each { |name| ENV[name] = previous[name] }
+ end
+
+ it "raises a terminal authorization error" do
+ stub_request(:post, "https://sts.us-east-1.amazonaws.com/").to_return(
+ status: 403,
+ body: <<~XML,
+ <ErrorResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
+ <Error>
+ <Type>Sender</Type>
+ <Code>AccessDenied</Code>
+ <Message>User is not authorized to perform: sts:AssumeRole</Message>
+ </Error>
+ </ErrorResponse>
+ XML
+ headers: {
+ "Content-Type" => "text/xml",
+ },
+ )
+ stub_invoke
+
+ expect { client.perform!("hello world") }.to raise_error(
+ DiscourseAi::Inference::EmbeddingInferenceError,
+ ) { |error|
+ expect([error.category, error.http_status, error.terminal?]).to eq(
+ [:authorization_failed, 403, true],
+ )
+ }
+ expect(WebMock).not_to have_requested(:post, invoke_url)
+ end
+ end
+
+ context "without configured credentials" do
+ let(:api_key) { nil }
+
+ around do |example|
+ previous = ENV.to_h.slice("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY")
+ ENV["AWS_ACCESS_KEY_ID"] = "AKIAFROMENV"
+ ENV["AWS_SECRET_ACCESS_KEY"] = "env-secret"
+ example.run
+ ensure
+ %w[AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY].each { |name| ENV[name] = previous[name] }
+ end
+
+ it "signs the request with the AWS SDK's default credentials" do
+ stub_invoke
+
+ client.perform!("hello world")
+
+ expect(WebMock).to have_requested(:post, invoke_url).with { |request|
+ request.headers["Authorization"].start_with?("AWS4-HMAC-SHA256 Credential=AKIAFROMENV/")
+ }
+ end
+ end
+
+ it "raises a terminal authorization error when access is denied" do
+ stub_invoke(
+ status: 403,
+ body: {
+ message: "You don't have access to the model",
+ },
+ headers: {
+ "x-amzn-ErrorType" => "AccessDeniedException",
+ },
+ )
+
+ expect { client.perform!("hello world") }.to raise_error(
+ DiscourseAi::Inference::EmbeddingInferenceError,
+ ) { |error|
+ expect([error.category, error.http_status, error.terminal?]).to eq(
+ [:authorization_failed, 403, true],
+ )
+ }
+ end
+
+ it "raises a non-terminal error when Bedrock rejects one input" do
+ stub_invoke(
+ status: 400,
+ body: {
+ message: "Malformed input request",
+ },
+ headers: {
+ "x-amzn-ErrorType" => "ValidationException",
+ },
+ )
+
+ expect { client.perform!("hello world") }.to raise_error(
+ DiscourseAi::Inference::EmbeddingInferenceError,
+ ) { |error| expect([error.category, error.terminal?]).to eq([:request_rejected, false]) }
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/models/ai_secret_spec.rb b/plugins/discourse-ai/spec/models/ai_secret_spec.rb
index f62861fa37b..fc6e910bce5 100644
--- a/plugins/discourse-ai/spec/models/ai_secret_spec.rb
+++ b/plugins/discourse-ai/spec/models/ai_secret_spec.rb
@@ -57,6 +57,18 @@ RSpec.describe AiSecret do
expect(ai_secret.in_use?).to eq(true)
end
+ it "returns true when used as a Bedrock embedding access key ID" do
+ Fabricate(
+ :bedrock_embedding_def,
+ provider_params: {
+ model_id: "cohere.embed-v4:0",
+ region: "us-east-1",
+ access_key_id: ai_secret.id.to_s,
+ },
+ )
+ expect(ai_secret.in_use?).to eq(true)
+ end
+
it "returns true when referenced in provider_params as numeric" do
llm = Fabricate(:bedrock_model)
DB.exec(
@@ -164,6 +176,24 @@ RSpec.describe AiSecret do
expect(tool_usage[:name]).to include("Weather Tool")
end
+ it "lists an embedding once when it uses this secret as API key and access key ID" do
+ embedding =
+ Fabricate(
+ :bedrock_embedding_def,
+ display_name: "Cohere",
+ ai_secret: ai_secret,
+ provider_params: {
+ model_id: "cohere.embed-v4:0",
+ region: "us-east-1",
+ access_key_id: ai_secret.id.to_s,
+ },
+ )
+
+ expect(ai_secret.used_by).to contain_exactly(
+ { type: "embedding", name: "Cohere", id: embedding.id },
+ )
+ end
+
it "lists decision_models using this secret" do
decision_model = Fabricate(:decision_model, display_name: "Jev", ai_secret: ai_secret)
diff --git a/plugins/discourse-ai/spec/models/embedding_definition_spec.rb b/plugins/discourse-ai/spec/models/embedding_definition_spec.rb
index 35f815a50b1..eeab7ccc0b6 100644
--- a/plugins/discourse-ai/spec/models/embedding_definition_spec.rb
+++ b/plugins/discourse-ai/spec/models/embedding_definition_spec.rb
@@ -17,6 +17,59 @@ describe EmbeddingDefinition do
expect(embedding_definition).not_to be_valid
expect(embedding_definition.errors[:pg_function]).to be_present
end
+
+ it "accepts a Bedrock definition without an API key and derives its URL from the region" do
+ definition =
+ Fabricate.build(
+ :bedrock_embedding_def,
+ url: nil,
+ provider_params: {
+ model_id: "cohere.embed-v4:0",
+ region: "eu-west-1",
+ },
+ )
+
+ expect(definition).to be_valid
+ expect(definition.url).to eq("https://bedrock-runtime.eu-west-1.amazonaws.com")
+ end
+
+ it "requires a model ID and region for Bedrock definitions" do
+ definition = Fabricate.build(:bedrock_embedding_def, url: nil, provider_params: {})
+
+ expect(definition).not_to be_valid
+ expect(definition.errors.full_messages).to contain_exactly(
+ "model_id can't be blank",
+ "region can't be blank",
+ )
+ end
+ end
+
+ describe "#inference_client" do
+ before { enable_current_plugin }
+
+ it "signs Bedrock requests with an access key ID stored as a credential" do
+ access_key = Fabricate(:ai_secret, secret: "AKIASTORED")
+ definition =
+ Fabricate(
+ :bedrock_embedding_def,
+ api_key: "secret-access-key",
+ provider_params: {
+ model_id: "cohere.embed-v4:0",
+ region: "us-east-1",
+ access_key_id: access_key.id.to_s,
+ },
+ )
+ invoke_url = "https://bedrock-runtime.us-east-1.amazonaws.com/model/cohere.embed-v4:0/invoke"
+ stub_request(:post, invoke_url).to_return(
+ status: 200,
+ body: { embeddings: { float: [[0.1, 0.2]] } }.to_json,
+ )
+
+ expect(definition.inference_client.perform!("hello")).to eq([0.1, 0.2])
+ expect(WebMock).to have_requested(:post, invoke_url).with { |request|
+ request.headers["Authorization"].start_with?("AWS4-HMAC-SHA256 Credential=AKIASTORED/")
+ }
+ end
end
describe "#prepare_query_text" do
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_embeddings_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_embeddings_controller_spec.rb
index 87cbfc7b108..ed5060a2cf1 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_embeddings_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_embeddings_controller_spec.rb
@@ -80,6 +80,31 @@ RSpec.describe DiscourseAi::Admin::AiEmbeddingsController do
expect(created_def.provider_params["model_name"]).to eq("embeddings-v1")
end
+ it "creates a Bedrock definition without a URL or API key" do
+ post "/admin/plugins/discourse-ai/ai-embeddings.json",
+ params: {
+ ai_embedding:
+ valid_attrs.except(:url, :api_key).merge(
+ provider: "aws_bedrock",
+ provider_params: {
+ model_id: "cohere.embed-v4:0",
+ region: "us-west-2",
+ role_arn: "arn:aws:iam::123456789012:role/bedrock",
+ },
+ ),
+ }
+
+ created_def = EmbeddingDefinition.last
+
+ expect(response.status).to eq(201)
+ expect(created_def.url).to eq("https://bedrock-runtime.us-west-2.amazonaws.com")
+ expect(created_def.provider_params).to eq(
+ "model_id" => "cohere.embed-v4:0",
+ "region" => "us-west-2",
+ "role_arn" => "arn:aws:iam::123456789012:role/bedrock",
+ )
+ end
+
it "ignores parameters not associated with that provider" do
post "/admin/plugins/discourse-ai/ai-embeddings.json",
params: {