2023-05-11 10:03:03 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
|
|
|
class AnthropicBot < Bot
|
|
|
|
def self.can_reply_as?(bot_user)
|
2023-07-27 11:24:44 +10:00
|
|
|
bot_user.id == DiscourseAi::AiBot::EntryPoint::CLAUDE_V2_ID
|
2023-05-11 10:03:03 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
def bot_prompt_with_topic_context(post)
|
2023-06-20 08:45:31 +10:00
|
|
|
super(post).join("\n\n") + "\n\nAssistant:"
|
2023-05-11 10:03:03 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
def prompt_limit
|
2023-07-27 11:24:44 +10:00
|
|
|
50_000 # https://console.anthropic.com/docs/prompt-design#what-is-a-prompt
|
2023-05-11 10:03:03 -03:00
|
|
|
end
|
|
|
|
|
2023-06-20 08:45:31 +10:00
|
|
|
def title_prompt(post)
|
|
|
|
super(post).join("\n\n") + "\n\nAssistant:"
|
|
|
|
end
|
|
|
|
|
2023-05-23 23:08:17 +10:00
|
|
|
def get_delta(partial, context)
|
2023-07-27 11:24:44 +10:00
|
|
|
partial[:completion]
|
2023-05-23 23:08:17 +10:00
|
|
|
end
|
|
|
|
|
2023-05-11 10:03:03 -03:00
|
|
|
private
|
|
|
|
|
2023-06-20 08:45:31 +10:00
|
|
|
def build_message(poster_username, content, system: false, function: nil)
|
2023-05-11 10:03:03 -03:00
|
|
|
role = poster_username == bot_user.username ? "Assistant" : "Human"
|
|
|
|
|
|
|
|
"#{role}: #{content}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_for
|
2023-07-27 11:24:44 +10:00
|
|
|
"claude-2"
|
2023-05-11 10:03:03 -03:00
|
|
|
end
|
|
|
|
|
2023-05-16 14:38:21 -03:00
|
|
|
def get_updated_title(prompt)
|
|
|
|
DiscourseAi::Inference::AnthropicCompletions.perform!(
|
|
|
|
prompt,
|
|
|
|
model_for,
|
2023-08-24 07:20:24 +10:00
|
|
|
temperature: 0.4,
|
2023-05-16 14:38:21 -03:00
|
|
|
max_tokens: 40,
|
|
|
|
).dig(:completion)
|
|
|
|
end
|
|
|
|
|
2023-05-22 12:09:14 +10:00
|
|
|
def submit_prompt(prompt, prefer_low_cost: false, &blk)
|
2023-05-11 10:03:03 -03:00
|
|
|
DiscourseAi::Inference::AnthropicCompletions.perform!(
|
|
|
|
prompt,
|
|
|
|
model_for,
|
|
|
|
temperature: 0.4,
|
|
|
|
max_tokens: 3000,
|
|
|
|
&blk
|
|
|
|
)
|
|
|
|
end
|
2023-05-15 15:10:42 -03:00
|
|
|
|
2023-08-29 10:43:58 +10:00
|
|
|
def tokenizer
|
|
|
|
DiscourseAi::Tokenizer::AnthropicTokenizer
|
2023-05-15 15:10:42 -03:00
|
|
|
end
|
2023-05-11 10:03:03 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|