discourse-ai/lib/inference/open_ai_embeddings.rb
Roman Rizzi f5cf1019fb
FEATURE: configurable embeddings (#1049)
* Use AR model for embeddings features

* endpoints

* Embeddings CRUD UI

* Add presets. Hide a couple more settings

* system specs

* Seed embedding definition from old settings

* Generate search bit index on the fly. cleanup orphaned data

* support for seeded models

* Fix run test for new embedding

* fix selected model not set correctly
2025-01-21 12:23:19 -03:00

45 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module ::DiscourseAi
module Inference
class OpenAiEmbeddings
def initialize(endpoint, api_key, model, dimensions)
@endpoint = endpoint
@api_key = api_key
@model = model
@dimensions = dimensions
end
attr_reader :endpoint, :api_key, :model, :dimensions
def perform!(content)
headers = { "Content-Type" => "application/json" }
if endpoint.include?("azure")
headers["api-key"] = api_key
else
headers["Authorization"] = "Bearer #{api_key}"
end
payload = { model: model, input: content }
payload[:dimensions] = dimensions if dimensions.present?
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
response = conn.post(endpoint, payload.to_json, headers)
case response.status
when 200
JSON.parse(response.body, symbolize_names: true).dig(:data, 0, :embedding)
when 429
# TODO add a AdminDashboard Problem?
else
Rails.logger.warn(
"OpenAI Embeddings failed with status: #{response.status} body: #{response.body}",
)
raise Net::HTTPBadResponse.new(response.body.to_s)
end
end
end
end
end