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