mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-04 08:20:03 +00:00
* 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
41 lines
1.1 KiB
Ruby
41 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ::DiscourseAi
|
|
module Inference
|
|
class CloudflareWorkersAi
|
|
def initialize(endpoint, api_token, referer = Discourse.base_url)
|
|
@endpoint = endpoint
|
|
@api_token = api_token
|
|
@referer = referer
|
|
end
|
|
|
|
attr_reader :endpoint, :api_token, :referer
|
|
|
|
def perform!(content)
|
|
headers = {
|
|
"Referer" => Discourse.base_url,
|
|
"Content-Type" => "application/json",
|
|
"Authorization" => "Bearer #{api_token}",
|
|
}
|
|
|
|
payload = { text: [content] }
|
|
|
|
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(:result, :data).first
|
|
when 429
|
|
# TODO add a AdminDashboard Problem?
|
|
else
|
|
Rails.logger.warn(
|
|
"Cloudflare Workers AI Embeddings failed with status: #{response.status} body: #{response.body}",
|
|
)
|
|
raise Net::HTTPBadResponse.new(response.body.to_s)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|