mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-02-13 15:04:43 +00:00
* FEATURE: add support for new OpenAI embedding models This adds support for just released text_embedding_3_small and large Note, we have not yet implemented truncation support which is a new API feature. (triggered using dimensions) * Tiny side fix, recalc bots when ai is enabled or disabled * FIX: downsample to 2000 items per vector which is a pgvector limitation
35 lines
1.0 KiB
Ruby
35 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ::DiscourseAi
|
|
module Inference
|
|
class OpenAiEmbeddings
|
|
def self.perform!(content, model:, dimensions: nil)
|
|
headers = { "Content-Type" => "application/json" }
|
|
|
|
if SiteSetting.ai_openai_embeddings_url.include?("azure")
|
|
headers["api-key"] = SiteSetting.ai_openai_api_key
|
|
else
|
|
headers["Authorization"] = "Bearer #{SiteSetting.ai_openai_api_key}"
|
|
end
|
|
|
|
payload = { model: model, input: content }
|
|
payload[:dimensions] = dimensions if dimensions.present?
|
|
|
|
response = Faraday.post(SiteSetting.ai_openai_embeddings_url, payload.to_json, headers)
|
|
|
|
case response.status
|
|
when 200
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
when 429
|
|
# TODO add a AdminDashboard Problem?
|
|
else
|
|
Rails.logger.warn(
|
|
"OpenAI Embeddings failed with status: #{response.status} body: #{response.body}",
|
|
)
|
|
raise Net::HTTPBadResponse
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|