2024-01-12 14:36:44 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
|
|
|
class Prompt
|
|
|
|
INVALID_TURN = Class.new(StandardError)
|
|
|
|
|
2025-06-27 10:35:47 -03:00
|
|
|
attr_reader :messages, :tools, :system_message_text
|
2025-05-15 17:32:39 +10:00
|
|
|
attr_accessor :topic_id, :post_id, :max_pixels, :tool_choice
|
2024-03-02 07:53:21 +11:00
|
|
|
|
|
|
|
def initialize(
|
|
|
|
system_message_text = nil,
|
|
|
|
messages: [],
|
|
|
|
tools: [],
|
|
|
|
topic_id: nil,
|
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-27 14:30:11 +11:00
|
|
|
post_id: nil,
|
2024-10-05 08:46:57 +09:00
|
|
|
max_pixels: nil,
|
|
|
|
tool_choice: nil
|
2024-03-02 07:53:21 +11:00
|
|
|
)
|
2024-01-12 14:36:44 -03:00
|
|
|
raise ArgumentError, "messages must be an array" if !messages.is_a?(Array)
|
|
|
|
raise ArgumentError, "tools must be an array" if !tools.is_a?(Array)
|
|
|
|
|
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-27 14:30:11 +11:00
|
|
|
@max_pixels = max_pixels || 1_048_576
|
|
|
|
|
2024-03-02 07:53:21 +11:00
|
|
|
@topic_id = topic_id
|
|
|
|
@post_id = post_id
|
|
|
|
|
2024-01-15 18:51:14 +11:00
|
|
|
@messages = []
|
|
|
|
|
|
|
|
if system_message_text
|
2025-06-27 10:35:47 -03:00
|
|
|
@system_message_text = system_message_text
|
|
|
|
@messages << { type: :system, content: @system_message_text }
|
|
|
|
else
|
|
|
|
@system_message_text = messages.find { |m| m[:type] == :system }&.dig(:content)
|
2024-01-15 18:51:14 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
@messages.concat(messages)
|
2024-01-12 14:36:44 -03:00
|
|
|
|
|
|
|
@messages.each { |message| validate_message(message) }
|
|
|
|
@messages.each_cons(2) { |last_turn, new_turn| validate_turn(last_turn, new_turn) }
|
|
|
|
|
2025-05-15 17:32:39 +10:00
|
|
|
self.tools = tools
|
2024-10-05 08:46:57 +09:00
|
|
|
@tool_choice = tool_choice
|
2024-01-12 14:36:44 -03:00
|
|
|
end
|
|
|
|
|
2025-05-15 17:32:39 +10:00
|
|
|
def tools=(tools)
|
|
|
|
raise ArgumentError, "tools must be an array" if !tools.is_a?(Array) && !tools.nil?
|
|
|
|
|
|
|
|
@tools =
|
|
|
|
tools.map do |tool|
|
|
|
|
if tool.is_a?(Hash)
|
|
|
|
ToolDefinition.from_hash(tool)
|
|
|
|
elsif tool.is_a?(ToolDefinition)
|
|
|
|
tool
|
|
|
|
else
|
|
|
|
raise ArgumentError, "tool must be a hash or a ToolDefinition was #{tool.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-03-17 15:14:09 +11:00
|
|
|
# this new api tries to create symmetry between responses and prompts
|
|
|
|
# this means anything we get back from the model via endpoint can be easily appended
|
|
|
|
def push_model_response(response)
|
|
|
|
response = [response] if !response.is_a? Array
|
|
|
|
|
|
|
|
thinking, thinking_signature, redacted_thinking_signature = nil
|
|
|
|
|
|
|
|
response.each do |message|
|
|
|
|
if message.is_a?(Thinking)
|
|
|
|
# we can safely skip partials here
|
|
|
|
next if message.partial?
|
|
|
|
if message.redacted
|
|
|
|
redacted_thinking_signature = message.signature
|
|
|
|
else
|
|
|
|
thinking = message.message
|
|
|
|
thinking_signature = message.signature
|
|
|
|
end
|
|
|
|
elsif message.is_a?(ToolCall)
|
|
|
|
next if message.partial?
|
|
|
|
# this is a bit surprising about the API
|
|
|
|
# needing to add arguments is not ideal
|
|
|
|
push(
|
|
|
|
type: :tool_call,
|
|
|
|
content: { arguments: message.parameters }.to_json,
|
|
|
|
id: message.id,
|
|
|
|
name: message.name,
|
|
|
|
)
|
|
|
|
elsif message.is_a?(String)
|
|
|
|
push(type: :model, content: message)
|
|
|
|
else
|
|
|
|
raise ArgumentError, "response must be an array of strings, ToolCalls, or Thinkings"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# anthropic rules are that we attach thinking to last for the response
|
|
|
|
# it is odd, I wonder if long term we just keep thinking as a separate object
|
|
|
|
if thinking || redacted_thinking_signature
|
|
|
|
messages.last[:thinking] = thinking
|
|
|
|
messages.last[:thinking_signature] = thinking_signature
|
|
|
|
messages.last[:redacted_thinking_signature] = redacted_thinking_signature
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-03-04 12:22:30 +11:00
|
|
|
def push(
|
|
|
|
type:,
|
|
|
|
content:,
|
|
|
|
id: nil,
|
|
|
|
name: nil,
|
|
|
|
thinking: nil,
|
|
|
|
thinking_signature: nil,
|
|
|
|
redacted_thinking_signature: nil
|
|
|
|
)
|
2024-01-12 14:36:44 -03:00
|
|
|
return if type == :system
|
|
|
|
new_message = { type: type, content: content }
|
2024-03-09 08:46:40 +11:00
|
|
|
new_message[:name] = name.to_s if name
|
2024-01-16 13:48:00 +11:00
|
|
|
new_message[:id] = id.to_s if id
|
2025-03-04 12:22:30 +11:00
|
|
|
new_message[:thinking] = thinking if thinking
|
|
|
|
new_message[:thinking_signature] = thinking_signature if thinking_signature
|
|
|
|
new_message[
|
|
|
|
:redacted_thinking_signature
|
|
|
|
] = redacted_thinking_signature if redacted_thinking_signature
|
2024-01-12 14:36:44 -03:00
|
|
|
|
|
|
|
validate_message(new_message)
|
|
|
|
validate_turn(messages.last, new_message)
|
|
|
|
|
|
|
|
messages << new_message
|
|
|
|
end
|
|
|
|
|
2024-03-08 06:37:23 +11:00
|
|
|
def has_tools?
|
|
|
|
tools.present?
|
|
|
|
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-27 14:30:11 +11:00
|
|
|
def encoded_uploads(message)
|
2025-04-01 02:39:07 +11:00
|
|
|
if message[:content].is_a?(Array)
|
|
|
|
upload_ids =
|
|
|
|
message[:content]
|
|
|
|
.map do |content|
|
|
|
|
content[:upload_id] if content.is_a?(Hash) && content.key?(:upload_id)
|
|
|
|
end
|
|
|
|
.compact
|
|
|
|
if !upload_ids.empty?
|
|
|
|
return UploadEncoder.encode(upload_ids: upload_ids, max_pixels: max_pixels)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def text_only(message)
|
|
|
|
if message[:content].is_a?(Array)
|
|
|
|
message[:content].map { |element| element if element.is_a?(String) }.compact.join
|
|
|
|
else
|
|
|
|
message[:content]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_upload(upload_id)
|
|
|
|
UploadEncoder.encode(upload_ids: [upload_id], max_pixels: max_pixels).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_with_encoded_uploads(content)
|
|
|
|
return [content] unless content.is_a?(Array)
|
|
|
|
|
|
|
|
content.map do |c|
|
|
|
|
if c.is_a?(Hash) && c.key?(:upload_id)
|
|
|
|
encode_upload(c[:upload_id])
|
|
|
|
else
|
|
|
|
c
|
|
|
|
end
|
|
|
|
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-27 14:30:11 +11:00
|
|
|
end
|
|
|
|
|
2025-03-06 09:41:09 +11:00
|
|
|
def ==(other)
|
|
|
|
return false unless other.is_a?(Prompt)
|
|
|
|
messages == other.messages && tools == other.tools && topic_id == other.topic_id &&
|
|
|
|
post_id == other.post_id && max_pixels == other.max_pixels &&
|
|
|
|
tool_choice == other.tool_choice
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other)
|
|
|
|
self == other
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash
|
|
|
|
[messages, tools, topic_id, post_id, max_pixels, tool_choice].hash
|
|
|
|
end
|
|
|
|
|
2024-01-12 14:36:44 -03:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_message(message)
|
|
|
|
valid_types = %i[system user model tool tool_call]
|
|
|
|
if !valid_types.include?(message[:type])
|
|
|
|
raise ArgumentError, "message type must be one of #{valid_types}"
|
|
|
|
end
|
|
|
|
|
2025-03-04 12:22:30 +11:00
|
|
|
valid_keys = %i[
|
|
|
|
type
|
|
|
|
content
|
|
|
|
id
|
|
|
|
name
|
|
|
|
thinking
|
|
|
|
thinking_signature
|
|
|
|
redacted_thinking_signature
|
|
|
|
]
|
2024-01-12 14:36:44 -03:00
|
|
|
if (invalid_keys = message.keys - valid_keys).any?
|
|
|
|
raise ArgumentError, "message contains invalid keys: #{invalid_keys}"
|
|
|
|
end
|
|
|
|
|
2025-04-01 02:39:07 +11:00
|
|
|
if message[:content].is_a?(Array)
|
|
|
|
message[:content].each do |content|
|
|
|
|
if !content.is_a?(String) && !(content.is_a?(Hash) && content.keys == [:upload_id])
|
|
|
|
raise ArgumentError, "Array message content must be a string or {upload_id: ...} "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if !message[:content].is_a?(String)
|
|
|
|
raise ArgumentError, "Message content must be a string or an array"
|
|
|
|
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-27 14:30:11 +11:00
|
|
|
end
|
2024-01-12 14:36:44 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_turn(last_turn, new_turn)
|
|
|
|
valid_types = %i[tool tool_call model user]
|
|
|
|
raise INVALID_TURN if !valid_types.include?(new_turn[:type])
|
|
|
|
|
|
|
|
if last_turn[:type] == :system && %i[tool tool_call model].include?(new_turn[:type])
|
|
|
|
raise INVALID_TURN
|
|
|
|
end
|
|
|
|
|
|
|
|
raise INVALID_TURN if new_turn[:type] == :tool && last_turn[:type] != :tool_call
|
|
|
|
raise INVALID_TURN if new_turn[:type] == :model && last_turn[:type] == :model
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|