2023-11-23 12:58:54 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Dialects
|
2023-12-18 18:06:01 -03:00
|
|
|
class Claude < Dialect
|
|
|
|
class << self
|
|
|
|
def can_translate?(model_name)
|
|
|
|
%w[claude-instant-1 claude-2].include?(model_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tokenizer
|
|
|
|
DiscourseAi::Tokenizer::AnthropicTokenizer
|
|
|
|
end
|
2023-11-23 12:58:54 -03:00
|
|
|
end
|
|
|
|
|
2023-12-18 18:06:01 -03:00
|
|
|
def translate
|
2024-01-12 14:36:44 -03:00
|
|
|
messages = prompt.messages
|
|
|
|
|
|
|
|
trimmed_messages = trim_messages(messages)
|
|
|
|
|
|
|
|
# Need to include this differently
|
|
|
|
last_message = trimmed_messages.last[:type] == :assistant ? trimmed_messages.pop : nil
|
|
|
|
|
|
|
|
claude_prompt =
|
|
|
|
trimmed_messages.reduce(+"") do |memo, msg|
|
2024-03-09 08:46:40 +11:00
|
|
|
if msg[:type] == :tool_call
|
|
|
|
memo << "\n\nAssistant: #{tool_call_to_xml(msg)}"
|
|
|
|
elsif msg[:type] == :system
|
2024-01-12 14:36:44 -03:00
|
|
|
memo << "Human: " unless uses_system_message?
|
|
|
|
memo << msg[:content]
|
2024-01-16 13:48:00 +11:00
|
|
|
if prompt.tools.present?
|
2024-01-12 14:36:44 -03:00
|
|
|
memo << "\n"
|
|
|
|
memo << build_tools_prompt
|
|
|
|
end
|
|
|
|
elsif msg[:type] == :model
|
|
|
|
memo << "\n\nAssistant: #{msg[:content]}"
|
|
|
|
elsif msg[:type] == :tool
|
2024-03-09 08:46:40 +11:00
|
|
|
memo << "\n\nHuman:\n"
|
|
|
|
memo << tool_result_to_xml(msg)
|
2024-01-12 14:36:44 -03:00
|
|
|
else
|
2024-01-16 13:48:00 +11:00
|
|
|
memo << "\n\nHuman: "
|
|
|
|
memo << "#{msg[:id]}: " if msg[:id]
|
|
|
|
memo << msg[:content]
|
2024-01-12 14:36:44 -03:00
|
|
|
end
|
2023-12-18 18:06:01 -03:00
|
|
|
|
2024-01-12 14:36:44 -03:00
|
|
|
memo
|
|
|
|
end
|
2023-11-23 12:58:54 -03:00
|
|
|
|
2024-01-12 14:36:44 -03:00
|
|
|
claude_prompt << "\n\nAssistant:"
|
|
|
|
claude_prompt << " #{last_message[:content]}:" if last_message
|
2024-01-11 15:56:40 +11:00
|
|
|
|
|
|
|
claude_prompt
|
2023-11-23 12:58:54 -03:00
|
|
|
end
|
|
|
|
|
2023-12-18 18:06:01 -03:00
|
|
|
def max_prompt_tokens
|
2024-01-09 14:10:20 -03:00
|
|
|
100_000 # Claude-2.1 has a 200k context window.
|
2023-12-18 18:06:01 -03:00
|
|
|
end
|
|
|
|
|
2023-11-23 12:58:54 -03:00
|
|
|
private
|
|
|
|
|
2024-01-09 00:28:03 +11:00
|
|
|
def uses_system_message?
|
|
|
|
model_name == "claude-2"
|
|
|
|
end
|
2023-11-23 12:58:54 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|