2023-12-26 12:49:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Endpoints
|
|
|
|
class Vllm < Base
|
2024-07-30 12:44:57 -04:00
|
|
|
def self.can_contact?(model_provider)
|
|
|
|
model_provider == "vllm"
|
2023-12-26 12:49:55 -05:00
|
|
|
end
|
|
|
|
|
2024-01-04 07:53:47 -05:00
|
|
|
def normalize_model_params(model_params)
|
|
|
|
model_params = model_params.dup
|
|
|
|
|
|
|
|
# max_tokens, temperature are already supported
|
|
|
|
if model_params[:stop_sequences]
|
|
|
|
model_params[:stop] = model_params.delete(:stop_sequences)
|
|
|
|
end
|
|
|
|
|
|
|
|
model_params
|
|
|
|
end
|
|
|
|
|
2023-12-26 12:49:55 -05:00
|
|
|
def default_options
|
2024-07-30 12:44:57 -04:00
|
|
|
{ max_tokens: 2000, model: llm_model.name }
|
2023-12-26 12:49:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider_id
|
|
|
|
AiApiAuditLog::Provider::Vllm
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def model_uri
|
2024-07-30 12:44:57 -04:00
|
|
|
if llm_model.url.to_s.starts_with?("srv://")
|
2024-08-02 08:52:33 -04:00
|
|
|
service = DiscourseAi::Utils::DnsSrv.lookup(llm_model.url.sub("srv://", ""))
|
2024-05-07 09:02:16 -04:00
|
|
|
api_endpoint = "https://#{service.target}:#{service.port}/v1/chat/completions"
|
2023-12-26 12:49:55 -05:00
|
|
|
else
|
2024-07-30 12:44:57 -04:00
|
|
|
api_endpoint = llm_model.url
|
2023-12-26 12:49:55 -05:00
|
|
|
end
|
2024-07-30 12:44:57 -04:00
|
|
|
|
2023-12-26 12:49:55 -05:00
|
|
|
@uri ||= URI(api_endpoint)
|
|
|
|
end
|
|
|
|
|
2024-08-02 08:52:33 -04:00
|
|
|
def prepare_payload(prompt, model_params, dialect)
|
|
|
|
payload = default_options.merge(model_params).merge(messages: prompt)
|
2024-11-11 16:14:30 -05:00
|
|
|
if @streaming_mode
|
|
|
|
payload[:stream] = true if @streaming_mode
|
|
|
|
payload[:stream_options] = { include_usage: true }
|
|
|
|
end
|
2024-08-02 08:52:33 -04:00
|
|
|
|
|
|
|
payload
|
2023-12-26 12:49:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_request(payload)
|
|
|
|
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
|
2024-01-10 17:23:07 -05:00
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
api_key = llm_model&.api_key || SiteSetting.ai_vllm_api_key
|
|
|
|
headers["X-API-KEY"] = api_key if api_key.present?
|
2024-01-10 17:23:07 -05:00
|
|
|
|
2023-12-26 12:49:55 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
def final_log_update(log)
|
|
|
|
log.request_tokens = @prompt_tokens if @prompt_tokens
|
|
|
|
log.response_tokens = @completion_tokens if @completion_tokens
|
2023-12-26 12:49:55 -05:00
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
def decode(response_raw)
|
|
|
|
json = JSON.parse(response_raw, symbolize_names: true)
|
|
|
|
@prompt_tokens = json.dig(:usage, :prompt_tokens)
|
|
|
|
@completion_tokens = json.dig(:usage, :completion_tokens)
|
|
|
|
[json.dig(:choices, 0, :message, :content)]
|
|
|
|
end
|
|
|
|
|
|
|
|
def decode_chunk(chunk)
|
|
|
|
@json_decoder ||= JsonStreamDecoder.new
|
|
|
|
(@json_decoder << chunk)
|
|
|
|
.map do |parsed|
|
|
|
|
# vLLM keeps sending usage over and over again
|
|
|
|
prompt_tokens = parsed.dig(:usage, :prompt_tokens)
|
|
|
|
completion_tokens = parsed.dig(:usage, :completion_tokens)
|
|
|
|
|
|
|
|
@prompt_tokens = prompt_tokens if prompt_tokens
|
2024-05-07 09:02:16 -04:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
@completion_tokens = completion_tokens if completion_tokens
|
2024-05-07 09:02:16 -04:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
text = parsed.dig(:choices, 0, :delta, :content)
|
|
|
|
if text.to_s.empty?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
.compact
|
2024-05-07 09:02:16 -04:00
|
|
|
end
|
2023-12-26 12:49:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|