2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Endpoints
|
|
|
|
class Anthropic < Base
|
|
|
|
def self.can_contact?(model_name)
|
|
|
|
%w[claude-instant-1 claude-2].include?(model_name)
|
|
|
|
end
|
|
|
|
|
2024-01-04 07:53:47 -05:00
|
|
|
def normalize_model_params(model_params)
|
|
|
|
model_params = model_params.dup
|
|
|
|
|
|
|
|
# temperature, stop_sequences are already supported
|
|
|
|
#
|
|
|
|
if model_params[:max_tokens]
|
|
|
|
model_params[:max_tokens_to_sample] = model_params.delete(:max_tokens)
|
|
|
|
end
|
|
|
|
|
|
|
|
model_params
|
|
|
|
end
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
def default_options
|
2024-01-04 07:53:47 -05:00
|
|
|
{
|
2024-01-09 12:10:20 -05:00
|
|
|
model: model == "claude-2" ? "claude-2.1" : model,
|
2024-01-04 08:44:07 -05:00
|
|
|
max_tokens_to_sample: 3_000,
|
2024-01-04 07:53:47 -05:00
|
|
|
stop_sequences: ["\n\nHuman:", "</function_calls>"],
|
|
|
|
}
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider_id
|
|
|
|
AiApiAuditLog::Provider::Anthropic
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def model_uri
|
|
|
|
@uri ||= URI("https://api.anthropic.com/v1/complete")
|
|
|
|
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
|
|
|
|
.merge(model_params)
|
|
|
|
.merge(prompt: prompt)
|
|
|
|
.tap { |payload| payload[:stream] = true if @streaming_mode }
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_request(payload)
|
|
|
|
headers = {
|
|
|
|
"anthropic-version" => "2023-06-01",
|
|
|
|
"x-api-key" => SiteSetting.ai_anthropic_api_key,
|
|
|
|
"content-type" => "application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
Net::HTTP::Post.new(model_uri, headers).tap { |r| r.body = payload }
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_completion_from(response_raw)
|
|
|
|
JSON.parse(response_raw, symbolize_names: true)[:completion].to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def partials_from(decoded_chunk)
|
|
|
|
decoded_chunk.split("\n").map { |line| line.split("data: ", 2)[1] }.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|