mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-02-15 07:54:43 +00:00
The new site settings: ai_openai_gpt35_url : distribution for GPT 16k ai_openai_gpt4_url: distribution for GPT 4 ai_openai_embeddings_url: distribution for ada2 If untouched we will simply use OpenAI endpoints. Azure requires 1 URL per model, OpenAI allows a single URL to serve multiple models. Hence the new settings.
35 lines
966 B
Ruby
35 lines
966 B
Ruby
# frozen_string_literal: true
|
|
|
|
module ::DiscourseAi
|
|
module Inference
|
|
class OpenAiEmbeddings
|
|
def self.perform!(content, model = 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
|
|
|
|
model ||= "text-embedding-ada-002"
|
|
|
|
response =
|
|
Faraday.post(
|
|
SiteSetting.ai_openai_embeddings_url,
|
|
{ model: model, input: content }.to_json,
|
|
headers,
|
|
)
|
|
if response.status != 200
|
|
Rails.logger.warn(
|
|
"OpenAI Embeddings failed with status: #{response.status} body: #{response.body}",
|
|
)
|
|
raise Net::HTTPBadResponse
|
|
end
|
|
|
|
JSON.parse(response.body, symbolize_names: true)
|
|
end
|
|
end
|
|
end
|
|
end
|