2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
module Dialects
|
2023-12-18 16:06:01 -05:00
|
|
|
class Claude < Dialect
|
|
|
|
class << self
|
|
|
|
def can_translate?(model_name)
|
2024-06-20 16:51:46 -04:00
|
|
|
model_name.start_with?("claude") || model_name.start_with?("anthropic")
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
class ClaudePrompt
|
|
|
|
attr_reader :system_prompt
|
|
|
|
attr_reader :messages
|
2024-06-05 18:34:23 -04:00
|
|
|
attr_reader :tools
|
2024-03-18 15:48:46 -04:00
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
def initialize(system_prompt, messages, tools)
|
2024-03-18 15:48:46 -04:00
|
|
|
@system_prompt = system_prompt
|
|
|
|
@messages = messages
|
2024-06-05 18:34:23 -04:00
|
|
|
@tools = tools
|
2024-03-18 15:48:46 -04:00
|
|
|
end
|
2024-06-07 09:52:01 -04:00
|
|
|
|
|
|
|
def has_tools?
|
|
|
|
tools.present?
|
|
|
|
end
|
2024-03-18 15:48:46 -04:00
|
|
|
end
|
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
def tokenizer
|
|
|
|
llm_model&.tokenizer_class || DiscourseAi::Tokenizer::AnthropicTokenizer
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def translate
|
2024-05-07 09:02:16 -04:00
|
|
|
messages = super
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
system_prompt = messages.shift[:content] if messages.first[:role] == "system"
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-06-07 09:52:01 -04:00
|
|
|
if !system_prompt && !native_tool_support?
|
|
|
|
system_prompt = tools_dialect.instructions.presence
|
|
|
|
end
|
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
interleving_messages = []
|
|
|
|
previous_message = nil
|
2024-05-07 09:02:16 -04:00
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
messages.each do |message|
|
|
|
|
if previous_message
|
|
|
|
if previous_message[:role] == "user" && message[:role] == "user"
|
|
|
|
interleving_messages << { role: "assistant", content: "OK" }
|
|
|
|
elsif previous_message[:role] == "assistant" && message[:role] == "assistant"
|
|
|
|
interleving_messages << { role: "user", content: "OK" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
interleving_messages << message
|
|
|
|
previous_message = message
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-06-07 09:52:01 -04:00
|
|
|
tools = nil
|
|
|
|
tools = tools_dialect.translated_tools if native_tool_support?
|
|
|
|
|
|
|
|
ClaudePrompt.new(system_prompt.presence, interleving_messages, tools)
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
def max_prompt_tokens
|
2024-05-16 08:50:22 -04:00
|
|
|
return llm_model.max_prompt_tokens if llm_model&.max_prompt_tokens
|
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
# Longer term it will have over 1 million
|
|
|
|
200_000 # Claude-3 has a 200k context window for now
|
2024-01-08 08:28:03 -05:00
|
|
|
end
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
|
2024-06-07 09:52:01 -04:00
|
|
|
def native_tool_support?
|
|
|
|
SiteSetting.ai_anthropic_native_tool_call_models_map.include?(model_name)
|
|
|
|
end
|
|
|
|
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
private
|
|
|
|
|
2024-06-05 18:34:23 -04:00
|
|
|
def tools_dialect
|
2024-06-07 09:52:01 -04:00
|
|
|
if native_tool_support?
|
|
|
|
@tools_dialect ||= DiscourseAi::Completions::Dialects::ClaudeTools.new(prompt.tools)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2024-06-05 18:34:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tool_call_msg(msg)
|
2024-06-07 09:52:01 -04:00
|
|
|
translated = tools_dialect.from_raw_tool_call(msg)
|
|
|
|
{ role: "assistant", content: translated }
|
2024-06-05 18:34:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tool_msg(msg)
|
2024-06-07 09:52:01 -04:00
|
|
|
translated = tools_dialect.from_raw_tool(msg)
|
|
|
|
{ role: "user", content: translated }
|
2024-06-05 18:34:23 -04:00
|
|
|
end
|
|
|
|
|
2024-05-07 09:02:16 -04:00
|
|
|
def model_msg(msg)
|
|
|
|
{ role: "assistant", content: msg[:content] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def system_msg(msg)
|
|
|
|
msg = { role: "system", content: msg[:content] }
|
|
|
|
|
|
|
|
if tools_dialect.instructions.present?
|
|
|
|
msg[:content] = msg[:content].dup << "\n\n#{tools_dialect.instructions}"
|
|
|
|
end
|
|
|
|
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_msg(msg)
|
|
|
|
content = +""
|
|
|
|
content << "#{msg[:id]}: " if msg[:id]
|
|
|
|
content << msg[:content]
|
2024-07-24 15:29:47 -04:00
|
|
|
content = inline_images(content, msg) if vision_support?
|
2024-05-07 09:02:16 -04:00
|
|
|
|
|
|
|
{ role: "user", content: content }
|
|
|
|
end
|
|
|
|
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
def inline_images(content, message)
|
2024-07-24 15:29:47 -04:00
|
|
|
encoded_uploads = prompt.encoded_uploads(message)
|
|
|
|
return content if encoded_uploads.blank?
|
|
|
|
|
|
|
|
content_w_imgs =
|
|
|
|
encoded_uploads.reduce([]) do |memo, details|
|
|
|
|
memo << {
|
|
|
|
source: {
|
|
|
|
type: "base64",
|
|
|
|
data: details[:base64],
|
|
|
|
media_type: details[:mime_type],
|
|
|
|
},
|
|
|
|
type: "image",
|
|
|
|
}
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
end
|
|
|
|
|
2024-07-24 15:29:47 -04:00
|
|
|
content_w_imgs << { type: "text", text: content }
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|