2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Endpoints
|
|
|
|
class Base
|
2024-11-13 14:58:24 -05:00
|
|
|
attr_reader :partial_tool_calls
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
CompletionFailed = Class.new(StandardError)
|
|
|
|
TIMEOUT = 60
|
|
|
|
|
2024-01-29 14:04:25 -05:00
|
|
|
class << self
|
2024-05-13 14:54:42 -04:00
|
|
|
def endpoint_for(provider_name)
|
2024-01-29 14:04:25 -05:00
|
|
|
endpoints = [
|
|
|
|
DiscourseAi::Completions::Endpoints::AwsBedrock,
|
|
|
|
DiscourseAi::Completions::Endpoints::OpenAi,
|
|
|
|
DiscourseAi::Completions::Endpoints::HuggingFace,
|
|
|
|
DiscourseAi::Completions::Endpoints::Gemini,
|
|
|
|
DiscourseAi::Completions::Endpoints::Vllm,
|
2024-03-18 15:48:46 -04:00
|
|
|
DiscourseAi::Completions::Endpoints::Anthropic,
|
2024-04-10 17:24:17 -04:00
|
|
|
DiscourseAi::Completions::Endpoints::Cohere,
|
2024-09-11 21:28:08 -04:00
|
|
|
DiscourseAi::Completions::Endpoints::SambaNova,
|
2024-11-19 01:28:09 -05:00
|
|
|
DiscourseAi::Completions::Endpoints::Mistral,
|
2024-01-29 14:04:25 -05:00
|
|
|
]
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
endpoints << DiscourseAi::Completions::Endpoints::Ollama if Rails.env.development?
|
|
|
|
|
2024-01-29 14:04:25 -05:00
|
|
|
if Rails.env.test? || Rails.env.development?
|
|
|
|
endpoints << DiscourseAi::Completions::Endpoints::Fake
|
|
|
|
end
|
|
|
|
|
|
|
|
endpoints.detect(-> { raise DiscourseAi::Completions::Llm::UNKNOWN_MODEL }) do |ek|
|
2024-05-13 14:54:42 -04:00
|
|
|
ek.can_contact?(provider_name)
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
2024-01-10 23:56:40 -05:00
|
|
|
end
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
def can_contact?(_model_provider)
|
2024-01-29 14:04:25 -05:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
def initialize(llm_model)
|
2024-05-16 08:50:22 -04:00
|
|
|
@llm_model = llm_model
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
def use_ssl?
|
2024-05-16 08:50:22 -04:00
|
|
|
if model_uri&.scheme.present?
|
|
|
|
model_uri.scheme == "https"
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
end
|
|
|
|
|
2024-06-07 09:52:01 -04:00
|
|
|
def xml_tags_to_strip(dialect)
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2024-10-23 01:49:56 -04:00
|
|
|
def perform_completion!(
|
|
|
|
dialect,
|
|
|
|
user,
|
|
|
|
model_params = {},
|
|
|
|
feature_name: nil,
|
|
|
|
feature_context: nil,
|
2024-11-13 14:58:24 -05:00
|
|
|
partial_tool_calls: false,
|
2024-10-23 01:49:56 -04:00
|
|
|
&blk
|
|
|
|
)
|
2024-11-13 14:58:24 -05:00
|
|
|
@partial_tool_calls = partial_tool_calls
|
2024-01-04 07:53:47 -05:00
|
|
|
model_params = normalize_model_params(model_params)
|
2024-06-07 09:52:01 -04:00
|
|
|
orig_blk = blk
|
2024-01-04 07:53:47 -05:00
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
@streaming_mode = block_given?
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
prompt = dialect.translate
|
|
|
|
|
2024-02-21 15:14:50 -05:00
|
|
|
FinalDestination::HTTP.start(
|
2023-11-23 10:58:54 -05:00
|
|
|
model_uri.host,
|
|
|
|
model_uri.port,
|
2024-05-07 09:02:16 -04:00
|
|
|
use_ssl: use_ssl?,
|
2023-11-23 10:58:54 -05:00
|
|
|
read_timeout: TIMEOUT,
|
|
|
|
open_timeout: TIMEOUT,
|
|
|
|
write_timeout: TIMEOUT,
|
|
|
|
) do |http|
|
|
|
|
response_data = +""
|
|
|
|
response_raw = +""
|
2023-12-18 16:06:01 -05:00
|
|
|
|
|
|
|
# Needed to response token calculations. Cannot rely on response_data due to function buffering.
|
|
|
|
partials_raw = +""
|
|
|
|
request_body = prepare_payload(prompt, model_params, dialect).to_json
|
2023-11-23 10:58:54 -05:00
|
|
|
|
|
|
|
request = prepare_request(request_body)
|
|
|
|
|
|
|
|
http.request(request) do |response|
|
|
|
|
if response.code.to_i != 200
|
|
|
|
Rails.logger.error(
|
|
|
|
"#{self.class.name}: status: #{response.code.to_i} - body: #{response.body}",
|
|
|
|
)
|
2024-05-21 12:35:50 -04:00
|
|
|
raise CompletionFailed, response.body
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-11-18 17:22:39 -05:00
|
|
|
xml_tool_processor =
|
|
|
|
XmlToolProcessor.new(
|
|
|
|
partial_tool_calls: partial_tool_calls,
|
|
|
|
) if xml_tools_enabled? && dialect.prompt.has_tools?
|
2024-11-11 16:14:30 -05:00
|
|
|
|
|
|
|
to_strip = xml_tags_to_strip(dialect)
|
|
|
|
xml_stripper =
|
|
|
|
DiscourseAi::Completions::XmlTagStripper.new(to_strip) if to_strip.present?
|
|
|
|
|
|
|
|
if @streaming_mode && xml_stripper
|
|
|
|
blk =
|
|
|
|
lambda do |partial, cancel|
|
|
|
|
partial = xml_stripper << partial if partial.is_a?(String)
|
|
|
|
orig_blk.call(partial, cancel) if partial
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
log =
|
2024-11-11 16:14:30 -05:00
|
|
|
start_log(
|
2023-11-23 10:58:54 -05:00
|
|
|
provider_id: provider_id,
|
2024-11-11 16:14:30 -05:00
|
|
|
request_body: request_body,
|
|
|
|
dialect: dialect,
|
|
|
|
prompt: prompt,
|
|
|
|
user: user,
|
2024-05-13 23:28:46 -04:00
|
|
|
feature_name: feature_name,
|
2024-11-11 16:14:30 -05:00
|
|
|
feature_context: feature_context,
|
2023-11-23 10:58:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if !@streaming_mode
|
2024-11-11 16:14:30 -05:00
|
|
|
return(
|
|
|
|
non_streaming_response(
|
|
|
|
response: response,
|
|
|
|
xml_tool_processor: xml_tool_processor,
|
|
|
|
xml_stripper: xml_stripper,
|
|
|
|
partials_raw: partials_raw,
|
|
|
|
response_raw: response_raw,
|
|
|
|
)
|
|
|
|
)
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
cancelled = false
|
2024-11-21 01:51:45 -05:00
|
|
|
cancel = -> do
|
|
|
|
cancelled = true
|
2024-11-11 16:14:30 -05:00
|
|
|
http.finish
|
2024-04-18 16:54:54 -04:00
|
|
|
end
|
|
|
|
|
2024-11-21 01:51:45 -05:00
|
|
|
break if cancelled
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
response.read_body do |chunk|
|
2024-11-21 01:51:45 -05:00
|
|
|
break if cancelled
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
response_raw << chunk
|
2024-11-21 01:51:45 -05:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
decode_chunk(chunk).each do |partial|
|
2024-11-21 01:51:45 -05:00
|
|
|
break if cancelled
|
2024-11-11 16:14:30 -05:00
|
|
|
partials_raw << partial.to_s
|
|
|
|
response_data << partial if partial.is_a?(String)
|
|
|
|
partials = [partial]
|
|
|
|
if xml_tool_processor && partial.is_a?(String)
|
|
|
|
partials = (xml_tool_processor << partial)
|
|
|
|
if xml_tool_processor.should_cancel?
|
|
|
|
cancel.call
|
|
|
|
break
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
2024-11-11 16:14:30 -05:00
|
|
|
partials.each { |inner_partial| blk.call(inner_partial, cancel) }
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue IOError, StandardError
|
|
|
|
raise if !cancelled
|
|
|
|
end
|
2024-11-11 16:14:30 -05:00
|
|
|
if xml_stripper
|
|
|
|
stripped = xml_stripper.finish
|
|
|
|
if stripped.present?
|
|
|
|
response_data << stripped
|
|
|
|
result = []
|
|
|
|
result = (xml_tool_processor << stripped) if xml_tool_processor
|
|
|
|
result.each { |partial| blk.call(partial, cancel) }
|
2024-01-02 09:21:13 -05:00
|
|
|
end
|
|
|
|
end
|
2024-11-11 16:14:30 -05:00
|
|
|
if xml_tool_processor
|
|
|
|
xml_tool_processor.finish.each { |partial| blk.call(partial, cancel) }
|
2024-06-07 09:52:01 -04:00
|
|
|
end
|
2024-11-11 16:14:30 -05:00
|
|
|
decode_chunk_finish.each { |partial| blk.call(partial, cancel) }
|
2023-11-23 10:58:54 -05:00
|
|
|
return response_data
|
|
|
|
ensure
|
2023-11-28 09:15:12 -05:00
|
|
|
if log
|
|
|
|
log.raw_response_payload = response_raw
|
2024-03-05 14:04:37 -05:00
|
|
|
final_log_update(log)
|
2024-11-11 16:14:30 -05:00
|
|
|
|
|
|
|
log.response_tokens = tokenizer.size(partials_raw) if log.response_tokens.blank?
|
2023-11-28 09:15:12 -05:00
|
|
|
log.save!
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2023-11-28 09:15:12 -05:00
|
|
|
if Rails.env.development?
|
|
|
|
puts "#{self.class.name}: request_tokens #{log.request_tokens} response_tokens #{log.response_tokens}"
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-05 14:04:37 -05:00
|
|
|
def final_log_update(log)
|
|
|
|
# for people that need to override
|
|
|
|
end
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
def default_options
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def provider_id
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def prompt_size(prompt)
|
|
|
|
tokenizer.size(extract_prompt_for_tokenizer(prompt))
|
|
|
|
end
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
attr_reader :llm_model
|
2023-11-23 10:58:54 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
def tokenizer
|
|
|
|
llm_model.tokenizer_class
|
|
|
|
end
|
|
|
|
|
2024-01-04 07:53:47 -05:00
|
|
|
# should normalize temperature, max_tokens, stop_words to endpoint specific values
|
|
|
|
def normalize_model_params(model_params)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
def model_uri
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_payload(_prompt, _model_params)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_request(_payload)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def decode(_response_raw)
|
2023-11-23 10:58:54 -05:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def decode_chunk_finish
|
|
|
|
[]
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def decode_chunk(_chunk)
|
2023-11-23 10:58:54 -05:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_prompt_for_tokenizer(prompt)
|
2024-05-07 09:02:16 -04:00
|
|
|
prompt.map { |message| message[:content] || message["content"] || "" }.join("\n")
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def xml_tools_enabled?
|
|
|
|
raise NotImplementedError
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
private
|
2024-03-01 15:53:21 -05:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def start_log(
|
|
|
|
provider_id:,
|
|
|
|
request_body:,
|
|
|
|
dialect:,
|
|
|
|
prompt:,
|
|
|
|
user:,
|
|
|
|
feature_name:,
|
|
|
|
feature_context:
|
|
|
|
)
|
|
|
|
AiApiAuditLog.new(
|
|
|
|
provider_id: provider_id,
|
|
|
|
user_id: user&.id,
|
|
|
|
raw_request_payload: request_body,
|
|
|
|
request_tokens: prompt_size(prompt),
|
|
|
|
topic_id: dialect.prompt.topic_id,
|
|
|
|
post_id: dialect.prompt.post_id,
|
|
|
|
feature_name: feature_name,
|
|
|
|
language_model: llm_model.name,
|
|
|
|
feature_context: feature_context.present? ? feature_context.as_json : nil,
|
|
|
|
)
|
2024-06-05 18:34:23 -04:00
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def non_streaming_response(
|
|
|
|
response:,
|
|
|
|
xml_tool_processor:,
|
|
|
|
xml_stripper:,
|
|
|
|
partials_raw:,
|
|
|
|
response_raw:
|
|
|
|
)
|
|
|
|
response_raw << response.read_body
|
|
|
|
response_data = decode(response_raw)
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
response_data.each { |partial| partials_raw << partial.to_s }
|
|
|
|
|
|
|
|
if xml_tool_processor
|
|
|
|
response_data.each do |partial|
|
|
|
|
processed = (xml_tool_processor << partial)
|
|
|
|
processed << xml_tool_processor.finish
|
|
|
|
response_data = []
|
|
|
|
processed.flatten.compact.each { |inner| response_data << inner }
|
|
|
|
end
|
2024-05-08 22:11:50 -04:00
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
if xml_stripper
|
|
|
|
response_data.map! do |partial|
|
|
|
|
stripped = (xml_stripper << partial) if partial.is_a?(String)
|
|
|
|
if stripped.present?
|
|
|
|
stripped
|
|
|
|
else
|
|
|
|
partial
|
|
|
|
end
|
|
|
|
end
|
|
|
|
response_data << xml_stripper.finish
|
2024-03-07 14:37:23 -05:00
|
|
|
end
|
2023-12-26 12:49:55 -05:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
response_data.reject!(&:blank?)
|
|
|
|
|
|
|
|
# this is to keep stuff backwards compatible
|
|
|
|
response_data = response_data.first if response_data.length == 1
|
|
|
|
|
|
|
|
response_data
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|