2023-04-10 10:04:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ::DiscourseAi
|
|
|
|
module Inference
|
|
|
|
class AnthropicCompletions
|
|
|
|
CompletionFailed = Class.new(StandardError)
|
2023-05-11 09:03:03 -04:00
|
|
|
TIMEOUT = 60
|
2023-04-10 10:04:42 -04:00
|
|
|
|
2023-05-11 09:03:03 -04:00
|
|
|
def self.perform!(
|
|
|
|
prompt,
|
|
|
|
model = "claude-v1",
|
|
|
|
temperature: nil,
|
|
|
|
top_p: nil,
|
|
|
|
max_tokens: nil,
|
|
|
|
user_id: nil
|
|
|
|
)
|
|
|
|
url = URI("https://api.anthropic.com/v1/complete")
|
2023-04-10 10:04:42 -04:00
|
|
|
headers = {
|
|
|
|
"x-api-key" => SiteSetting.ai_anthropic_api_key,
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
}
|
|
|
|
|
2023-05-11 09:03:03 -04:00
|
|
|
payload = { model: model, prompt: prompt }
|
2023-04-10 10:04:42 -04:00
|
|
|
|
2023-05-11 09:03:03 -04:00
|
|
|
payload[:temperature] = temperature if temperature
|
|
|
|
payload[:top_p] = top_p if top_p
|
|
|
|
payload[:max_tokens_to_sample] = max_tokens || 300
|
|
|
|
payload[:stream] = true if block_given?
|
2023-04-10 10:04:42 -04:00
|
|
|
|
2023-05-11 09:03:03 -04:00
|
|
|
Net::HTTP.start(
|
|
|
|
url.host,
|
|
|
|
url.port,
|
|
|
|
use_ssl: true,
|
|
|
|
read_timeout: TIMEOUT,
|
|
|
|
open_timeout: TIMEOUT,
|
|
|
|
write_timeout: TIMEOUT,
|
|
|
|
) do |http|
|
|
|
|
request = Net::HTTP::Post.new(url, headers)
|
|
|
|
request_body = payload.to_json
|
|
|
|
request.body = request_body
|
2023-04-10 10:04:42 -04:00
|
|
|
|
2023-05-11 09:03:03 -04:00
|
|
|
http.request(request) do |response|
|
|
|
|
if response.code.to_i != 200
|
|
|
|
Rails.logger.error(
|
|
|
|
"AnthropicCompletions: status: #{response.code.to_i} - body: #{response.body}",
|
|
|
|
)
|
|
|
|
raise CompletionFailed
|
|
|
|
end
|
|
|
|
|
|
|
|
log =
|
|
|
|
AiApiAuditLog.create!(
|
|
|
|
provider_id: AiApiAuditLog::Provider::Anthropic,
|
|
|
|
raw_request_payload: request_body,
|
|
|
|
user_id: user_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
if !block_given?
|
|
|
|
response_body = response.read_body
|
|
|
|
parsed_response = JSON.parse(response_body, symbolize_names: true)
|
|
|
|
|
|
|
|
log.update!(
|
|
|
|
raw_response_payload: response_body,
|
2023-05-15 14:10:42 -04:00
|
|
|
request_tokens: DiscourseAi::Tokenizer::AnthropicTokenizer.size(prompt),
|
|
|
|
response_tokens:
|
|
|
|
DiscourseAi::Tokenizer::AnthropicTokenizer.size(parsed_response[:completion]),
|
2023-05-11 09:03:03 -04:00
|
|
|
)
|
|
|
|
return parsed_response
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
cancelled = false
|
|
|
|
cancel = lambda { cancelled = true }
|
|
|
|
response_data = +""
|
|
|
|
response_raw = +""
|
2023-04-10 10:04:42 -04:00
|
|
|
|
2023-05-11 09:03:03 -04:00
|
|
|
response.read_body do |chunk|
|
|
|
|
if cancelled
|
|
|
|
http.finish
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
response_raw << chunk
|
|
|
|
|
|
|
|
chunk
|
|
|
|
.split("\n")
|
|
|
|
.each do |line|
|
|
|
|
data = line.split("data: ", 2)[1]
|
|
|
|
next if !data || data.squish == "[DONE]"
|
|
|
|
|
|
|
|
if !cancelled && partial = JSON.parse(data, symbolize_names: true)
|
|
|
|
response_data << partial[:completion].to_s
|
|
|
|
|
|
|
|
yield partial, cancel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue IOError
|
|
|
|
raise if !cancelled
|
|
|
|
ensure
|
|
|
|
log.update!(
|
|
|
|
raw_response_payload: response_raw,
|
2023-05-15 14:10:42 -04:00
|
|
|
request_tokens: DiscourseAi::Tokenizer::AnthropicTokenizer.size(prompt),
|
|
|
|
response_tokens: DiscourseAi::Tokenizer::AnthropicTokenizer.size(response_data),
|
2023-05-11 09:03:03 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-04-10 10:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|