2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Endpoints
|
2023-11-28 23:17:46 -05:00
|
|
|
class HuggingFace < Base
|
2024-07-30 12:44:57 -04:00
|
|
|
def self.can_contact?(model_provider)
|
|
|
|
model_provider == "hugging_face"
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-01-04 07:53:47 -05:00
|
|
|
def normalize_model_params(model_params)
|
|
|
|
model_params = model_params.dup
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
# max_tokens, temperature are already supported
|
2024-01-04 07:53:47 -05:00
|
|
|
if model_params[:stop_sequences]
|
|
|
|
model_params[:stop] = model_params.delete(:stop_sequences)
|
|
|
|
end
|
|
|
|
|
|
|
|
model_params
|
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
def default_options
|
2024-07-30 12:44:57 -04:00
|
|
|
{ model: llm_model.name, temperature: 0.7 }
|
2024-05-07 09:02:16 -04:00
|
|
|
end
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
def provider_id
|
|
|
|
AiApiAuditLog::Provider::HuggingFaceTextGeneration
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def model_uri
|
2024-07-30 12:44:57 -04:00
|
|
|
URI(llm_model.url)
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def prepare_payload(prompt, model_params, _dialect)
|
2023-11-23 10:58:54 -05:00
|
|
|
default_options
|
2024-05-07 09:02:16 -04:00
|
|
|
.merge(model_params)
|
|
|
|
.merge(messages: prompt)
|
2023-11-23 10:58:54 -05:00
|
|
|
.tap do |payload|
|
2024-05-07 09:02:16 -04:00
|
|
|
if !payload[:max_tokens]
|
2024-07-30 12:44:57 -04:00
|
|
|
token_limit = llm_model.max_prompt_tokens
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
payload[:max_tokens] = token_limit - prompt_size(prompt)
|
|
|
|
end
|
2023-12-06 09:22:42 -05:00
|
|
|
|
|
|
|
payload[:stream] = true if @streaming_mode
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_request(payload)
|
2024-07-30 12:44:57 -04:00
|
|
|
api_key = llm_model.api_key
|
2024-05-16 08:50:22 -04:00
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
headers =
|
|
|
|
{ "Content-Type" => "application/json" }.tap do |h|
|
2024-05-16 08:50:22 -04:00
|
|
|
h["Authorization"] = "Bearer #{api_key}" if api_key.present?
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Net::HTTP::Post.new(model_uri, headers).tap { |r| r.body = payload }
|
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def xml_tools_enabled?
|
|
|
|
true
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def decode(response_raw)
|
|
|
|
parsed = JSON.parse(response_raw, symbolize_names: true)
|
|
|
|
text = parsed.dig(:choices, 0, :message, :content)
|
|
|
|
if text.to_s.empty?
|
|
|
|
[""]
|
|
|
|
else
|
|
|
|
[text]
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def decode_chunk(chunk)
|
|
|
|
@json_decoder ||= JsonStreamDecoder.new
|
|
|
|
(@json_decoder << chunk)
|
|
|
|
.map do |parsed|
|
|
|
|
text = parsed.dig(:choices, 0, :delta, :content)
|
|
|
|
if text.to_s.empty?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
text
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|