0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-14 13:58:53 +08:00
discourse/plugins/discourse-ai/lib/completions/dialects/claude.rb
Sam fa54f62348
FEATURE: extract text from document uploads for LLM prompts (#39634)
Document attachments (doc, docx, xls, xlsx, rtf, csv, md, txt) are now
converted to text before being included in LLM prompts, instead of
being forwarded as raw base64 payloads. PDFs remain the only format
sent as a raw upload, capped at 10MB.

New converters under lib/completions:

- DocToText shells out to antiword
- DocxToText parses OOXML directly with size and depth limits
- XlsToText shells out to xls2csv
- XlsxToText parses OOXML and shared strings into CSV-style text
- RtfToText is a custom RTF tokenizer with destination/group handling

Plain text formats (csv, md, txt) are read with a 1MB byte cap and
UTF-8 normalization. Extracted text is truncated to 100k characters,
with a preamble noting the original filename and size.

Dialect trimming now uses token-aware truncation against a per-message
budget so large extracted documents collapse cleanly under the prompt
limit, rather than the previous step-based slicing of raw content.

Other changes:

- LlmModel.normalize_attachment_types is shared with UploadEncoder and
  collapses "markdown" to "md" so the canonical extension is consistent
  across model config, UI defaults, and encoder output
- ai-llm-attachment-types adds csv, xls, xlsx to the default choices
- Locale strings clarify that vision controls images and
  allowed_attachment_types controls documents

---------

Co-authored-by: Rafael Silva <xfalcox@gmail.com>
2026-05-05 08:16:23 +10:00

196 lines
5.9 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module Completions
module Dialects
class Claude < Dialect
class << self
def can_translate?(llm_model)
llm_model.provider == "anthropic" ||
(llm_model.provider == "aws_bedrock") &&
(llm_model.name.include?("anthropic") || llm_model.name.include?("claude"))
end
end
class ClaudePrompt
attr_reader :system_prompt, :messages, :tools, :tool_choice
def initialize(system_prompt, messages, tools, tool_choice)
@system_prompt = system_prompt
@messages = messages
@tools = tools
@tool_choice = tool_choice
end
def has_tools?
tools.present?
end
end
def translate
messages = super
system_prompt = messages.shift[:content] if messages.first[:role] == "system"
if !system_prompt && !native_tool_support?
system_prompt = tools_dialect.instructions.presence
end
interleving_messages = []
previous_message = nil
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
tools = nil
tools = tools_dialect.translated_tools if native_tool_support?
ClaudePrompt.new(system_prompt.presence, interleving_messages, tools, tool_choice)
end
def max_prompt_tokens
llm_model.max_prompt_tokens
end
def native_tool_support?
!llm_model.lookup_custom_param("disable_native_tools")
end
private
def tools_dialect
if native_tool_support?
@tools_dialect ||= DiscourseAi::Completions::Dialects::ClaudeTools.new(prompt.tools)
else
super
end
end
def tool_call_msg(msg)
translated = tools_dialect.from_raw_tool_call(msg)
{ role: "assistant", content: translated }
end
def tool_msg(msg)
translated = tools_dialect.from_raw_tool(msg)
{ role: "user", content: translated }
end
def model_msg(msg)
content_array = []
anthropic = anthropic_reasoning(msg)
if anthropic.present?
if msg[:thinking] && anthropic[:signature]
content_array << {
type: "thinking",
thinking: msg[:thinking],
signature: anthropic[:signature],
}
end
if anthropic[:redacted_signature]
content_array << { type: "redacted_thinking", data: anthropic[:redacted_signature] }
end
end
# other encoder is used to pass through thinking
content_array =
to_encoded_content_array(
content: [content_array, msg[:content]].flatten,
upload_encoder: ->(_details) {},
text_encoder: ->(text) { { type: "text", text: text } },
other_encoder: ->(details) { details },
allow_images: false,
allow_documents: false,
)
{ role: "assistant", content: no_array_if_only_text(content_array) }
end
def anthropic_reasoning(message)
info = message[:thinking_provider_info]
return if info.blank?
info[:anthropic] || info["anthropic"]
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_array = []
content_array << "#{msg[:id]}: " if msg[:id]
content_array.concat([msg[:content]].flatten)
content_array =
to_encoded_content_array(
content: content_array,
upload_encoder: ->(details) { upload_node(details) },
text_encoder: ->(text) { { type: "text", text: text } },
allow_images: vision_support?,
allow_documents: true,
allowed_attachment_types: llm_model.allowed_attachment_types,
upload_filter: ->(encoded) { document_allowed?(encoded) },
)
{ role: "user", content: no_array_if_only_text(content_array) }
end
# keeping our payload as backward compatible as possible
def no_array_if_only_text(content_array)
if content_array.length == 1 && content_array.first[:type] == "text"
content_array.first[:text]
else
content_array
end
end
def image_node(details)
{
source: {
type: "base64",
data: details[:base64],
media_type: details[:mime_type],
},
type: "image",
}
end
def upload_node(details)
return if details.blank?
return { type: "text", text: details[:text] } if details[:text].present?
if details[:kind] == :document || details[:mime_type] == "application/pdf"
{
type: "document",
source: {
type: "base64",
data: details[:base64],
media_type: details[:mime_type],
},
}
else
image_node(details)
end
end
end
end
end
end