mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-11-01 06:58:39 +00:00
* DEV: Remove the summarization feature Instead, we'll register summarization implementations for OpenAI, Anthropic, and Discourse AI using the API defined in discourse/discourse#21813. Core and chat will implement features on top of these implementations instead of this plugin extending them. * Register instances that contain the model, requiring less site settings
58 lines
1.3 KiB
Ruby
58 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseAi
|
|
module Summarization
|
|
module Strategies
|
|
class Anthropic < ::Summarization::Base
|
|
def display_name
|
|
"Anthropic's #{model}"
|
|
end
|
|
|
|
def correctly_configured?
|
|
SiteSetting.ai_anthropic_api_key.present?
|
|
end
|
|
|
|
def configuration_hint
|
|
I18n.t(
|
|
"discourse_ai.summarization.configuration_hint",
|
|
count: 1,
|
|
setting: "ai_anthropic_api_key",
|
|
)
|
|
end
|
|
|
|
def summarize(content_text)
|
|
response =
|
|
::DiscourseAi::Inference::AnthropicCompletions.perform!(
|
|
prompt(content_text),
|
|
model,
|
|
).dig(:completion)
|
|
|
|
Nokogiri::HTML5.fragment(response).at("ai").text
|
|
end
|
|
|
|
def prompt(content)
|
|
truncated_content =
|
|
::DiscourseAi::Tokenizer::AnthropicTokenizer.truncate(content, max_length - 50)
|
|
|
|
"Human: Summarize the following article that is inside <input> tags.
|
|
Please include only the summary inside <ai> tags.
|
|
|
|
<input>##{truncated_content}</input>
|
|
|
|
|
|
Assistant:
|
|
"
|
|
end
|
|
|
|
private
|
|
|
|
def max_length
|
|
lengths = { "claude-v1" => 9000, "claude-v1-100k" => 100_000 }
|
|
|
|
lengths[model]
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|