A platform for community discussion. Free, open, simple.
DEV: Expose agent RAG as a standard tool (#38691)
Rafael dos Santos Silvafecaf8dbce48
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjsindex eeaa8fd51c0..654283be590 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs@@ -882,28 +882,6 @@ export default class AgentEditor extends Component {><field.Control step="any" lang="en" /></form.Field>-- <form.Field- @name="question_consolidator_llm_id"- @title={{i18n- "discourse_ai.ai_agent.question_consolidator_llm"- }}- @tooltip={{i18n- "discourse_ai.ai_agent.question_consolidator_llm_help"- }}- @format="large"- @type="custom"- as |field|- >- <field.Control>- <AiLlmSelector- @value={{field.value}}- @llms={{@agents.resultSetMeta.llms}}- @onChange={{field.set}}- class="ai-agent-editor__llms"- />- </field.Control>- </form.Field></RagOptionsFk></form.Section>{{/if}}diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rbindex f2c8c201857..e2ab6e7e2ed 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb@@ -427,7 +427,6 @@ module DiscourseAi:rag_chunk_overlap_tokens,:rag_conversation_chunks,:rag_llm_model_id,- :question_consolidator_llm_id,:allow_chat_channel_mentions,:allow_chat_direct_messages,:allow_topic_mentions,@@ -539,8 +538,6 @@ module DiscourseAi},rag_llm_model_id: {},- question_consolidator_llm_id: {- },allow_chat_channel_mentions: {},allow_chat_direct_messages: {diff --git a/plugins/discourse-ai/app/models/ai_agent.rb b/plugins/discourse-ai/app/models/ai_agent.rbindex 34f0cdf9a14..bbecf3b9289 100644--- a/plugins/discourse-ai/app/models/ai_agent.rb+++ b/plugins/discourse-ai/app/models/ai_agent.rb@@ -2,7 +2,9 @@class AiAgent < ActiveRecord::Base# TODO remove tool_details from ignored_columns 01-02-2027- self.ignored_columns = %i[tool_details]+ # question_consolidator_llm_id is intentionally ignored after the RAG tool+ # migration; the column can be dropped in a later schema cleanup.+ self.ignored_columns = %i[tool_details question_consolidator_llm_id]# Between the regular migration (which creates ai_agents as a VIEW over# ai_personas) and the post-migration (which does the actual rename_table),@@ -51,7 +53,6 @@ class AiAgent < ActiveRecord::Basebelongs_to :userbelongs_to :default_llm, class_name: "LlmModel"- belongs_to :question_consolidator_llm, class_name: "LlmModel"belongs_to :rag_llm_model, class_name: "LlmModel"has_many :upload_references, as: :target, dependent: :destroy@@ -208,7 +209,6 @@ class AiAgent < ActiveRecord::Basevision_enabledvision_max_pixelsrag_conversation_chunks- question_consolidator_llm_idallow_chat_channel_mentionsallow_chat_direct_messagesallow_topic_mentions@@ -433,43 +433,42 @@ end## Table name: ai_agents#-# id :bigint not null, primary key-# allow_chat_channel_mentions :boolean default(FALSE), not null-# allow_chat_direct_messages :boolean default(FALSE), not null-# allow_personal_messages :boolean default(TRUE), not null-# allow_topic_mentions :boolean default(FALSE), not null-# allowed_group_ids :integer default([]), not null, is an Array-# compression_threshold :integer-# description :string(2000) not null-# enabled :boolean default(TRUE), not null-# examples :jsonb-# execution_mode :string default("default"), not null-# force_default_llm :boolean default(FALSE), not null-# forced_tool_count :integer default(-1), not null-# max_context_posts :integer-# max_turn_tokens :integer-# name :string(100) not null-# priority :boolean default(FALSE), not null-# rag_chunk_overlap_tokens :integer default(10), not null-# rag_chunk_tokens :integer default(374), not null-# rag_conversation_chunks :integer default(10), not null-# require_approval :boolean default(FALSE), not null-# response_format :jsonb-# show_thinking :boolean default(TRUE), not null-# system :boolean default(FALSE), not null-# system_prompt :string(10000000) not null-# temperature :float-# tools :json not null-# top_p :float-# vision_enabled :boolean default(FALSE), not null-# vision_max_pixels :integer default(1048576), not null-# created_at :datetime not null-# updated_at :datetime not null-# created_by_id :integer-# default_llm_id :bigint-# question_consolidator_llm_id :bigint-# rag_llm_model_id :bigint-# user_id :integer+# id :bigint not null, primary key+# allow_chat_channel_mentions :boolean default(FALSE), not null+# allow_chat_direct_messages :boolean default(FALSE), not null+# allow_personal_messages :boolean default(TRUE), not null+# allow_topic_mentions :boolean default(FALSE), not null+# allowed_group_ids :integer default([]), not null, is an Array+# compression_threshold :integer+# description :string(2000) not null+# enabled :boolean default(TRUE), not null+# examples :jsonb+# execution_mode :string default("default"), not null+# force_default_llm :boolean default(FALSE), not null+# forced_tool_count :integer default(-1), not null+# max_context_posts :integer+# max_turn_tokens :integer+# name :string(100) not null+# priority :boolean default(FALSE), not null+# rag_chunk_overlap_tokens :integer default(10), not null+# rag_chunk_tokens :integer default(374), not null+# rag_conversation_chunks :integer default(10), not null+# require_approval :boolean default(FALSE), not null+# response_format :jsonb+# show_thinking :boolean default(TRUE), not null+# system :boolean default(FALSE), not null+# system_prompt :string(10000000) not null+# temperature :float+# tools :json not null+# top_p :float+# vision_enabled :boolean default(FALSE), not null+# vision_max_pixels :integer default(1048576), not null+# created_at :datetime not null+# updated_at :datetime not null+# created_by_id :integer+# default_llm_id :bigint+# rag_llm_model_id :bigint+# user_id :integer## Indexes#diff --git a/plugins/discourse-ai/app/models/rag_document_fragment.rb b/plugins/discourse-ai/app/models/rag_document_fragment.rbindex 747a6ee2882..20e023c5560 100644--- a/plugins/discourse-ai/app/models/rag_document_fragment.rb+++ b/plugins/discourse-ai/app/models/rag_document_fragment.rb@@ -1,6 +1,9 @@# frozen_string_literal: trueclass RagDocumentFragment < ActiveRecord::Base+ MAX_SEARCH_RESULTS = 200+ MAX_GET_FILE_CHARS = 500_000+belongs_to :uploadbelongs_to :target, polymorphic: true@@ -64,9 +67,98 @@ class RagDocumentFragment < ActiveRecord::Baseendend+ def search(target_id:, target_type:, query:, filenames: nil, limit: 10)+ return [] if !DiscourseAi::Embeddings.enabled?+ return [] if query.blank?++ limit = limit.to_i+ return [] if limit < 1++ limit = [MAX_SEARCH_RESULTS, limit].min+ upload_ids = upload_ids_for(target_id:, target_type:, filenames:)+ return [] if upload_ids.empty?++ query_vector = DiscourseAi::Embeddings::Vector.instance.vector_from(query)+ fragment_ids =+ DiscourseAi::Embeddings::Schema+ .for(self)+ .asymmetric_similarity_search(query_vector, limit: limit, offset: 0) do |builder|+ builder.join(<<~SQL, target_id: target_id, target_type: target_type)+ rag_document_fragments ON+ rag_document_fragments.id = rag_document_fragment_id AND+ rag_document_fragments.target_id = :target_id AND+ rag_document_fragments.target_type = :target_type+ SQL+ end+ .map(&:rag_document_fragment_id)++ uploads_by_id = Upload.where(id: upload_ids).pluck(:id, :original_filename).to_h+ fragments =+ where(+ id: fragment_ids,+ upload_id: upload_ids,+ target_id: target_id,+ target_type: target_type,+ ).pluck(:id, :fragment, :metadata, :upload_id, :fragment_number)++ fragments_by_id = {}+ fragments.each do |id, fragment, metadata, upload_id, fragment_number|+ fragments_by_id[id] = {+ fragment: fragment,+ metadata: metadata,+ filename: uploads_by_id[upload_id],+ fragment_number: fragment_number,+ }+ end++ fragment_ids.filter_map { |fragment_id| fragments_by_id[fragment_id] }.take(limit)+ end++ def read_file(target_id:, target_type:, filename:)+ return nil if filename.blank?++ upload_ids = upload_ids_for(target_id:, target_type:, filenames: [filename])+ return nil if upload_ids.empty?++ upload_id =+ Upload.where(id: upload_ids, original_filename: filename).order(id: :desc).pick(:id)+ return nil if upload_id.nil?++ fragments =+ where(target_id:, target_type:, upload_id: upload_id).order(:fragment_number).pluck(+ :fragment,+ )++ return nil if fragments.empty?++ result = +""+ fragments.each do |fragment|+ if result.length + fragment.length + 1 > MAX_GET_FILE_CHARS+ result << "\n[truncated]"+ break+ end+ result << "\n" unless result.empty?+ result << fragment+ end+ result+ end+def publish_status(upload, status)MessageBus.publish("/discourse-ai/rag/#{upload.id}", status, user_ids: [upload.user_id])end++ private++ def upload_ids_for(target_id:, target_type:, filenames: nil)+ upload_ids =+ UploadReference.where(target_id: target_id, target_type: target_type).pluck(:upload_id)+ return upload_ids if filenames.blank?++ normalized_filenames = Array(filenames).filter_map(&:presence)+ return [] if normalized_filenames.empty?++ Upload.where(id: upload_ids, original_filename: normalized_filenames).pluck(:id)+ endendenddiff --git a/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb b/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rbindex 8568ba76906..075cdd903ba 100644--- a/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb+++ b/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb@@ -23,7 +23,6 @@ class LocalizedAiAgentSerializer < ApplicationSerializer:rag_chunk_overlap_tokens,:rag_conversation_chunks,:rag_llm_model_id,- :question_consolidator_llm_id,:show_thinking,:forced_tool_count,:allow_chat_channel_mentions,diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.jsindex 78696373bba..3193d3f4c89 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js@@ -26,7 +26,6 @@ const CREATE_ATTRIBUTES = ["rag_chunk_overlap_tokens","rag_conversation_chunks","rag_llm_model_id",- "question_consolidator_llm_id","show_thinking","forced_tool_count","allow_personal_messages",@@ -61,7 +60,6 @@ const SYSTEM_ATTRIBUTES = ["rag_chunk_overlap_tokens","rag_conversation_chunks","rag_llm_model_id",- "question_consolidator_llm_id","show_thinking","allow_personal_messages","allow_topic_mentions",diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 7b2a7cdb997..a2a6811b9db 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -550,8 +550,6 @@ en:create_user_help: You can optionally attach a user to this agent. If you do, the AI will use this user to respond to requests.default_llm: Default language modeldefault_llm_help: The default language model to use for this agent. Required if you wish to mention the agent on public posts.- question_consolidator_llm: Language Model for Question Consolidator- question_consolidator_llm_help: The language model to use for the question consolidator, you may choose a less powerful model to save costs.system_prompt: System promptforced_tool_strategy: Forced tool strategyallow_chat_direct_messages: "Allow chat direct messages"diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex e008a26bfd8..dd34db6646c 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -520,6 +520,7 @@ en:description: "Controls whether the post is reviewed, hidden, deleted, or treated as spam."tool_summary:read_artifact: "Read a web artifact"+ search_uploaded_documents: "Search uploaded documents"update_artifact: "Update a web artifact"create_artifact: "Create web artifact"web_browser: "Browse Web"@@ -562,6 +563,7 @@ en:researcher_dry_run: "Preparing research"tool_help:read_artifact: "Read a web artifact using the AI Bot"+ search_uploaded_documents: "Search documents uploaded to this agent"update_artifact: "Update a web artifact using the AI Bot"create_artifact: "Create a web artifact using the AI Bot"web_browser: "Browse web page using the AI Bot"@@ -603,6 +605,9 @@ en:researcher: "Research forum information using the AI Bot"tool_description:read_artifact: "Read a web artifact using the AI Bot"+ search_uploaded_documents:+ one: "Found %{count} uploaded document excerpt for '%{query}'"+ other: "Found %{count} uploaded document excerpts for '%{query}'"update_artifact: "Updated a web artifact using the AI Bot"create_artifact: "Created a web artifact: %{name} - %{specification}"web_browser: "Reading <a href='%{url}'>%{url}</a>"diff --git a/plugins/discourse-ai/lib/agents/agent.rb b/plugins/discourse-ai/lib/agents/agent.rbindex 8f11098728e..07e549fc551 100644--- a/plugins/discourse-ai/lib/agents/agent.rb+++ b/plugins/discourse-ai/lib/agents/agent.rb@@ -20,10 +20,6 @@ module DiscourseAi1_048_576end- def question_consolidator_llm_id- nil- end-def execution_mode"default"end@@ -229,9 +225,21 @@ module DiscourseAi.all_available_tools.filter { |tool| tools.include?(tool) }.concat(tools.filter(&:custom?))+ .tap do |available_tools|+ next if !rag_tool_available?+ if available_tools.any? { |tool|+ tool.signature[:name] == Tools::SearchUploadedDocuments.name+ }+ next+ end++ available_tools << Tools::SearchUploadedDocuments+ end+ .uniqenddef craft_prompt(context, llm: nil)+ available_tools = self.available_toolssystem_insts = replace_placeholders(system_prompt, context)prompt_insts = <<~TEXT.strip@@ -239,28 +247,11 @@ module DiscourseAi#{available_tools.map(&:custom_system_message).compact_blank.join("\n")}TEXT- question_consolidator_llm = llm- if self.class.question_consolidator_llm_id.present?- question_consolidator_llm ||=- DiscourseAi::Completions::Llm.proxy(- LlmModel.find_by(id: self.class.question_consolidator_llm_id),- )- end-if context.custom_instructions.present?prompt_insts << "\n"prompt_insts << context.custom_instructionsend- fragments_guidance =- rag_fragments_prompt(- context.messages,- llm: question_consolidator_llm,- user: context.user,- )&.strip-- prompt_insts << fragments_guidance if fragments_guidance.present?-post_system_examples = []if examples.present?@@ -369,10 +360,18 @@ module DiscourseAillm: llm,context: context,provider_data: tool_call.provider_data,+ agent: self,)endend+ def rag_tool_available?+ return false if !DiscourseAi::Embeddings.enabled?+ return false if id.blank?++ UploadReference.where(target_id: id, target_type: "AiAgent").exists?+ end+def strip_quotes(value)if value.is_a?(String)if value.start_with?('"') && value.end_with?('"')@@ -386,104 +385,6 @@ module DiscourseAivalueendend-- def rag_fragments_prompt(conversation_context, llm:, user:)- upload_refs = UploadReference.where(target_id: id, target_type: "AiAgent").pluck(:upload_id)-- return nil if !DiscourseAi::Embeddings.enabled?- return nil if conversation_context.blank? || upload_refs.blank?-- latest_interactions =- conversation_context.select { |ctx| %i[model user].include?(ctx[:type]) }.last(10)-- return nil if latest_interactions.empty?-- # first response- if latest_interactions.length == 1- consolidated_question = DiscourseAi::Completions::Prompt.text_only(latest_interactions[0])- else- consolidated_question =- DiscourseAi::Agents::QuestionConsolidator.consolidate_question(- llm,- latest_interactions,- user,- )- end-- return nil if !consolidated_question-- vector = DiscourseAi::Embeddings::Vector.instance- reranker = DiscourseAi::Inference::HuggingFaceTextEmbeddings-- interactions_vector = vector.vector_from(consolidated_question)-- rag_conversation_chunks = self.class.rag_conversation_chunks- search_limit =- if reranker.reranker_configured?- rag_conversation_chunks * 5- else- rag_conversation_chunks- end-- schema = DiscourseAi::Embeddings::Schema.for(RagDocumentFragment)-- candidate_fragment_ids =- schema- .asymmetric_similarity_search(- interactions_vector,- limit: search_limit,- offset: 0,- ) { |builder| builder.join(<<~SQL, target_id: id, target_type: "AiAgent") }- rag_document_fragments ON- rag_document_fragments.id = rag_document_fragment_id AND- rag_document_fragments.target_id = :target_id AND- rag_document_fragments.target_type = :target_type- SQL- .map(&:rag_document_fragment_id)-- fragments =- RagDocumentFragment.where(upload_id: upload_refs, id: candidate_fragment_ids).pluck(- :fragment,- :metadata,- )-- if reranker.reranker_configured?- guidance = fragments.map { |fragment, _metadata| fragment }- ranks =- DiscourseAi::Inference::HuggingFaceTextEmbeddings- .rerank(conversation_context.last[:content], guidance)- .to_a- .take(rag_conversation_chunks)- .map { it[:index] }-- if ranks.empty?- fragments = fragments.take(rag_conversation_chunks)- else- fragments = ranks.map { |idx| fragments[idx] }- end- end-- <<~TEXT- <guidance>- The following texts will give you additional guidance for your response.- We included them because we believe they are relevant to this conversation topic.-- Texts:-- #{- fragments- .map do |fragment, metadata|- if metadata.present?- ["# #{metadata}", fragment].join("\n")- else- fragment- end- end- .join("\n")- }- </guidance>- TEXT- endendendenddiff --git a/plugins/discourse-ai/lib/agents/question_consolidator.rb b/plugins/discourse-ai/lib/agents/question_consolidator.rbdeleted file mode 100644index dd49b07e646..00000000000--- a/plugins/discourse-ai/lib/agents/question_consolidator.rb+++ /dev/null@@ -1,98 +0,0 @@-# frozen_string_literal: true--module DiscourseAi- module Agents- class QuestionConsolidator- attr_reader :llm, :messages, :user, :max_tokens-- def self.consolidate_question(llm, messages, user)- new(llm, messages, user).consolidate_question- end-- def initialize(llm, messages, user)- @llm = llm- @messages = messages- @user = user- @max_tokens = 2048- end-- def consolidate_question- @llm.generate(revised_prompt, user: @user, feature_name: "question_consolidator")- end-- def revised_prompt- max_tokens_per_model = @max_tokens / 5-- conversation_snippet = []- tokens = 0-- messages.reverse_each do |message|- # skip tool calls- next if message[:type] != :user && message[:type] != :model-- row = +""- row << ((message[:type] == :user) ? "user" : "model")-- content = DiscourseAi::Completions::Prompt.text_only(message)- current_tokens = @llm.tokenizer.tokenize(content).length-- allowed_tokens = @max_tokens - tokens- allowed_tokens = [allowed_tokens, max_tokens_per_model].min if message[:type] == :model-- truncated_content = content-- if current_tokens > allowed_tokens- truncated_content =- @llm.tokenizer.truncate(- content,- allowed_tokens,- strict: SiteSetting.ai_strict_token_counting,- )- current_tokens = allowed_tokens- end-- row << ": #{truncated_content}"- tokens += current_tokens- conversation_snippet << row-- break if tokens >= @max_tokens- end-- history = conversation_snippet.reverse.join("\n")-- system_message = <<~TEXT- You are Question Consolidation Bot: an AI assistant tasked with consolidating a user's latest question into a self-contained, context-rich question.-- - Your output will be used to query a vector database. DO NOT include superflous text such as "here is your consolidated question:".- - You interact with an API endpoint, not a user, you must never produce denials, nor conversations directed towards a non-existent user.- - You only produce automated responses to input, where a response is a consolidated question without further discussion.- - You only ever reply with consolidated questions. You never try to answer user queries.-- If for any reason there is no discernable question (Eg: thank you, or good job) reply with the text NO_QUESTION.- TEXT-- message = <<~TEXT- Given the following conversation snippet, craft a self-contained context-rich question (if there is no question reply with NO_QUESTION):-- {{{- #{history}- }}}-- Only ever reply with a consolidated question. Do not try to answer user queries.- TEXT-- response =- DiscourseAi::Completions::Prompt.new(- system_message,- messages: [{ type: :user, content: message }],- )-- if response == "NO_QUESTION"- nil- else- response- end- end- end- end-enddiff --git a/plugins/discourse-ai/lib/agents/tool_runner.rb b/plugins/discourse-ai/lib/agents/tool_runner.rbindex 040ebee7dbf..3ff500bb3e6 100644--- a/plugins/discourse-ai/lib/agents/tool_runner.rb+++ b/plugins/discourse-ai/lib/agents/tool_runner.rb@@ -374,79 +374,20 @@ module DiscourseAi@secrets_cache = secret_ids.present? ? AiSecret.where(id: secret_ids).index_by(&:id) : {}end- MAX_FRAGMENTS = 200- MAX_GET_FILE_CHARS = 500_000-def rag_search(query, filenames: nil, limit: 10)- limit = limit.to_i- return [] if limit < 1- limit = [MAX_FRAGMENTS, limit].min-- upload_refs =- UploadReference.where(target_id: tool.id, target_type: "AiTool").pluck(:upload_id)-- if filenames- upload_refs = Upload.where(id: upload_refs).where(original_filename: filenames).pluck(:id)- end-- return [] if upload_refs.empty?-- query_vector = DiscourseAi::Embeddings::Vector.instance.vector_from(query)- fragment_ids =- DiscourseAi::Embeddings::Schema- .for(RagDocumentFragment)- .asymmetric_similarity_search(query_vector, limit: limit, offset: 0) do |builder|- builder.join(<<~SQL, target_id: tool.id, target_type: "AiTool")- rag_document_fragments ON- rag_document_fragments.id = rag_document_fragment_id AND- rag_document_fragments.target_id = :target_id AND- rag_document_fragments.target_type = :target_type- SQL- end- .map(&:rag_document_fragment_id)-- fragments =- RagDocumentFragment.where(id: fragment_ids, upload_id: upload_refs).pluck(- :id,- :fragment,- :metadata,+ RagDocumentFragment+ .search(+ target_id: tool.id,+ target_type: "AiTool",+ query: query,+ filenames: filenames,+ limit: limit,)-- mapped = {}- fragments.each do |id, fragment, metadata|- mapped[id] = { fragment: fragment, metadata: metadata }- end-- fragment_ids.take(limit).map { |fragment_id| mapped[fragment_id] }+ .map { |fragment| { fragment: fragment[:fragment], metadata: fragment[:metadata] } }enddef rag_get_file(filename)- return nil if filename.blank?-- upload_refs =- UploadReference.where(target_id: tool.id, target_type: "AiTool").pluck(:upload_id)- upload_id =- Upload.where(id: upload_refs, original_filename: filename).order(id: :desc).pick(:id)- return nil if upload_id.nil?-- fragments =- RagDocumentFragment- .where(target_id: tool.id, target_type: "AiTool", upload_id: upload_id)- .order(:fragment_number)- .pluck(:fragment)-- return nil if fragments.empty?-- result = +""- fragments.each do |fragment|- if result.length + fragment.length + 1 > MAX_GET_FILE_CHARS- result << "\n[truncated]"- break- end- result << "\n" unless result.empty?- result << fragment- end- result+ RagDocumentFragment.read_file(target_id: tool.id, target_type: "AiTool", filename: filename)enddef attach_truncate(mini_racer_context)diff --git a/plugins/discourse-ai/lib/agents/tools/search_uploaded_documents.rb b/plugins/discourse-ai/lib/agents/tools/search_uploaded_documents.rbnew file mode 100644index 00000000000..599eac399e1--- /dev/null+++ b/plugins/discourse-ai/lib/agents/tools/search_uploaded_documents.rb@@ -0,0 +1,117 @@+# frozen_string_literal: true++module DiscourseAi+ module Agents+ module Tools+ class SearchUploadedDocuments < Tool+ DEFAULT_LIMIT = 10++ attr_reader :last_query, :result_count++ class << self+ def signature+ {+ name: name,+ description:+ "Search the documents uploaded to this agent and return the most relevant excerpts",+ parameters: [+ {+ name: "query",+ description:+ "What to look for in the uploaded documents. Use a focused search query.",+ type: "string",+ required: true,+ },+ {+ name: "filenames",+ description:+ "Optional list of filenames to restrict the search to when the user refers to specific uploaded documents",+ type: "array",+ item_type: "string",+ },+ {+ name: "limit",+ description:+ "Maximum number of excerpts to return. Prefer the default unless the user clearly asks for more or less context.",+ type: "integer",+ },+ ],+ }+ end++ def name+ "search_uploaded_documents"+ end++ def custom_system_message+ <<~TEXT+ Use the `search_uploaded_documents` tool when the answer may depend on documents uploaded to this agent.+ Do not assume uploaded document snippets are already in the prompt.+ TEXT+ end+ end++ def query+ parameters[:query].to_s.strip+ end++ def filenames+ Array(parameters[:filenames]).map(&:to_s).reject(&:blank?).uniq+ end++ def invoke+ if agent.blank? || agent.id.blank?+ return { error: "No uploaded documents are available for this agent." }+ end+ return { error: "A query is required." } if query.blank?++ @last_query = query++ yield("Searching uploaded documents for '#{query}'") if block_given?++ fragments =+ RagDocumentFragment.search(+ target_id: agent.id,+ target_type: "AiAgent",+ query: query,+ filenames: filenames,+ limit: result_limit,+ )++ @result_count = fragments.length++ result = {+ query: query,+ excerpts:+ fragments.map do |fragment|+ {+ filename: fragment[:filename],+ metadata: fragment[:metadata],+ fragment_number: fragment[:fragment_number],+ content: fragment[:fragment],+ }+ end,+ }+ result[:filenames] = filenames if filenames.present?+ result+ end++ protected++ def description_args+ { count: result_count || 0, query: last_query || "" }+ end++ private++ def result_limit+ requested_limit = parameters[:limit].to_i+ configured_limit = agent.class.rag_conversation_chunks || DEFAULT_LIMIT++ requested_limit = configured_limit if requested_limit <= 0+ [requested_limit, configured_limit].min+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/agents/tools/tool.rb b/plugins/discourse-ai/lib/agents/tools/tool.rbindex 3f8f334ca5d..b61b1abbf1a 100644--- a/plugins/discourse-ai/lib/agents/tools/tool.rb+++ b/plugins/discourse-ai/lib/agents/tools/tool.rb@@ -60,7 +60,7 @@ module DiscourseAi# llm being public makes it a bit easier to testattr_accessor :custom_raw, :parameters, :llm, :provider_data- attr_reader :tool_call_id, :agent_options, :bot_user, :context+ attr_reader :tool_call_id, :agent_options, :bot_user, :context, :agentdef initialize(parameters,@@ -69,7 +69,8 @@ module DiscourseAibot_user:,llm:,context: nil,- provider_data: {}+ provider_data: {},+ agent: nil)@parameters = parameters@tool_call_id = tool_call_id@@ -79,6 +80,7 @@ module DiscourseAi@llm = llm@context = context.nil? ? DiscourseAi::Agents::BotContext.new(messages: []) : context@provider_data = provider_data.is_a?(Hash) ? provider_data.deep_symbolize_keys : {}+ @agent = agentif [email protected]_a?(DiscourseAi::Agents::BotContext)raise ArgumentError, "context must be a DiscourseAi::Agents::Context"enddiff --git a/plugins/discourse-ai/spec/lib/agents/agent_spec.rb b/plugins/discourse-ai/spec/lib/agents/agent_spec.rbindex 11ec95ffc06..1af6ffbd337 100644--- a/plugins/discourse-ai/spec/lib/agents/agent_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/agent_spec.rb@@ -301,213 +301,70 @@ RSpec.describe DiscourseAi::Agents::Agent docontextend- context "when a agent has no uploads" do- it "doesn't include RAG guidance" do- guidance_fragment =- "The following texts will give you additional guidance to elaborate a response."-- expect(ai_agent.craft_prompt(with_cc).messages.first[:content]).not_to include(- guidance_fragment,- )- end+ def prompt_tool_names(prompt)+ prompt.tools.map(&:name)end- context "when RAG is running with a question consolidator" do- let(:consolidated_question) { "what is the time in france?" }-- fab!(:llm_model, :fake_model)-- fab!(:custom_ai_agent) do- Fabricate(- :ai_agent,- name: "custom",- rag_conversation_chunks: 3,- allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],- question_consolidator_llm_id: llm_model.id,+ context "when a agent has no uploads" do+ it "doesn't expose the uploaded document search tool" do+ expect(prompt_tool_names(ai_agent.craft_prompt(with_cc))).not_to include(+ "search_uploaded_documents",)end-- before do- context_embedding = vector_def.dimensions.times.map { rand(-1.0...1.0) }- EmbeddingsGenerationStubs.hugging_face_service(consolidated_question, context_embedding)-- UploadReference.ensure_exist!(target: custom_ai_agent, upload_ids: [upload.id])- end-- it "will run the question consolidator" do- custom_agent = DiscourseAi::Agents::Agent.find_by(id: custom_ai_agent.id, user: user).new-- # this means that we will consolidate- context.messages = [- { content: "Tell me the time", type: :user },- { content: "the time is 1", type: :model },- { content: "in france?", type: :user },- ]-- DiscourseAi::Completions::Endpoints::Fake.with_fake_content(consolidated_question) do- custom_agent.craft_prompt(context).messages.first[:content]- end-- message =- DiscourseAi::Completions::Endpoints::Fake.last_call[:dialect].prompt.messages.last[- :content- ]- expect(message).to include("Tell me the time")- expect(message).to include("the time is 1")- expect(message).to include("in france?")- end-- context "when there are messages with uploads" do- let(:image100x100) { plugin_file_from_fixtures("100x100.jpg") }- let(:image_upload) do- UploadCreator.new(image100x100, "image.jpg").create_for(Discourse.system_user.id)- end-- it "the question consolidator works" do- custom_agent = DiscourseAi::Agents::Agent.find_by(id: custom_ai_agent.id, user: user).new-- context.messages = [- { content: "Tell me the time", type: :user },- { content: "the time is 1", type: :model },- { content: ["in france?", { upload_id: image_upload.id }], type: :user },- ]-- DiscourseAi::Completions::Endpoints::Fake.with_fake_content(consolidated_question) do- custom_agent.craft_prompt(context).messages.first[:content]- end-- message =- DiscourseAi::Completions::Endpoints::Fake.last_call[:dialect].prompt.messages.last[- :content- ]- expect(message).to include("Tell me the time")- expect(message).to include("the time is 1")- expect(message).to include("in france?")- end- endendcontext "when a agent has RAG uploads" do- let(:embedding_value) { 0.04381 }- let(:prompt_cc_embeddings) { [embedding_value] * vector_def.dimensions }-- def stub_fragments(fragment_count, agent: ai_agent)- schema = DiscourseAi::Embeddings::Schema.for(RagDocumentFragment)-- fragment_count.times do |i|- fragment =- Fabricate(- :rag_document_fragment,- fragment: "fragment-n#{i}",- target_id: agent.id,- target_type: "AiAgent",- upload: upload,- )-- # Similarity is determined left-to-right.- embeddings = [embedding_value + "0.000#{i}".to_f] * vector_def.dimensions-- schema.store(fragment, embeddings, "test")- end- end-before dostored_ai_agent = AiAgent.find(ai_agent.id)UploadReference.ensure_exist!(target: stored_ai_agent, upload_ids: [upload.id])-- EmbeddingsGenerationStubs.hugging_face_service(- with_cc.messages.dig(0, :content),- prompt_cc_embeddings,- )end- context "when agent allows for less fragments" do- it "will only pick 3 fragments" do- custom_ai_agent =- Fabricate(- :ai_agent,- name: "custom",- rag_conversation_chunks: 3,- allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],- )-- stub_fragments(3, agent: custom_ai_agent)-- UploadReference.ensure_exist!(target: custom_ai_agent, upload_ids: [upload.id])-- custom_agent = DiscourseAi::Agents::Agent.find_by(id: custom_ai_agent.id, user: user).new-- expect(custom_agent.class.rag_conversation_chunks).to eq(3)+ it "exposes uploaded documents as a tool instead of injecting snippets into the prompt" do+ prompt = ai_agent.craft_prompt(with_cc)- crafted_system_prompt = custom_agent.craft_prompt(with_cc).messages.first[:content]-- expect(crafted_system_prompt).to include("fragment-n0")- expect(crafted_system_prompt).to include("fragment-n1")- expect(crafted_system_prompt).to include("fragment-n2")- expect(crafted_system_prompt).not_to include("fragment-n3")- end+ expect(prompt_tool_names(prompt)).to include("search_uploaded_documents")+ expect(prompt.messages.first[:content]).to include("search_uploaded_documents")+ expect(prompt.messages.first[:content]).not_to include(+ "The following texts will give you additional guidance",+ )end- context "when the reranker is available" do- before do- SiteSetting.ai_hugging_face_tei_reranker_endpoint = "https://test.reranker.com"- stub_fragments(15)- end-- it "uses the re-ranker to reorder the fragments and pick the top 10 candidates" do- skip "This test is flaky needs to be investigated ordering does not come back as expected"- # The re-ranker reverses the similarity search, but return less results- # to act as a limit for test-purposes.- expected_reranked = (4..14).to_a.reverse.map { |idx| { index: idx } }-- WebMock.stub_request(:post, "https://test.reranker.com/rerank").to_return(- status: 200,- body: JSON.dump(expected_reranked),+ it "finds the uploaded document search tool" do+ tool_call =+ DiscourseAi::Completions::ToolCall.new(+ name: "search_uploaded_documents",+ id: "call_uploaded_docs",+ parameters: {+ query: "time",+ },)- crafted_system_prompt = ai_agent.craft_prompt(with_cc).messages.first[:content]+ tool = ai_agent.find_tool(tool_call, bot_user: nil, llm: nil, context: with_cc)- expect(crafted_system_prompt).to include("fragment-n14")- expect(crafted_system_prompt).to include("fragment-n13")- expect(crafted_system_prompt).to include("fragment-n12")- expect(crafted_system_prompt).not_to include("fragment-n4") # Fragment #11 not included- end+ expect(tool).to be_a(DiscourseAi::Agents::Tools::SearchUploadedDocuments)+ expect(tool.agent).to eq(ai_agent)end+ end- context "when the reranker is not available" do- before { stub_fragments(10) }-- it "picks the first 10 candidates from the similarity search" do- crafted_system_prompt = ai_agent.craft_prompt(with_cc).messages.first[:content]-- expect(crafted_system_prompt).to include("fragment-n0")- expect(crafted_system_prompt).to include("fragment-n1")- expect(crafted_system_prompt).to include("fragment-n2")-- expect(crafted_system_prompt).not_to include("fragment-n10") # Fragment #10 not included- end+ context "when the agent has examples" do+ fab!(:examples_agent) do+ Fabricate(+ :ai_agent,+ examples: [["User message", "assistant response"]],+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],+ )end- context "when the agent has examples" do- fab!(:examples_agent) do- Fabricate(- :ai_agent,- examples: [["User message", "assistant response"]],- allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],- )- end-- it "includes them before the context messages" do- custom_agent = DiscourseAi::Agents::Agent.find_by(id: examples_agent.id, user: user).new+ it "includes them before the context messages" do+ custom_agent = DiscourseAi::Agents::Agent.find_by(id: examples_agent.id, user: user).new- post_system_prompt_msgs = custom_agent.craft_prompt(with_cc).messages.last(3)+ post_system_prompt_msgs = custom_agent.craft_prompt(with_cc).messages.last(3)- expect(post_system_prompt_msgs).to contain_exactly(- { content: "User message", type: :user },- { content: "assistant response", type: :model },- { content: "Tell me the time", type: :user },- )- end+ expect(post_system_prompt_msgs).to contain_exactly(+ { content: "User message", type: :user },+ { content: "assistant response", type: :model },+ { content: "Tell me the time", type: :user },+ )endendenddiff --git a/plugins/discourse-ai/spec/lib/agents/question_consolidator_spec.rb b/plugins/discourse-ai/spec/lib/agents/question_consolidator_spec.rbdeleted file mode 100644index 2a8a07e5056..00000000000--- a/plugins/discourse-ai/spec/lib/agents/question_consolidator_spec.rb+++ /dev/null@@ -1,35 +0,0 @@-# frozen_string_literal: true--RSpec.describe DiscourseAi::Agents::QuestionConsolidator do- let(:llm) { DiscourseAi::Completions::Llm.proxy(Fabricate(:fake_model)) }- let(:fake_endpoint) { DiscourseAi::Completions::Endpoints::Fake }-- fab!(:user)-- before { enable_current_plugin }-- describe ".consolidate_question" do- it "properly picks all the right messages and consolidates" do- messages = [- { type: :user, content: "What is the capital of France?" },- { type: :tool_call, content: "search:google", id: "123" },- { type: :tool, content: "some results from google", id: "123" },- { type: :model, content: "Paris" },- { type: :user, content: "What about Germany?" },- ]-- result = described_class.consolidate_question(llm, messages, user)- expect(result).to eq(fake_endpoint.fake_content)-- call = fake_endpoint.last_call-- prompt = call[:dialect].prompt- expect(prompt.messages.length).to eq(2)- content = prompt.messages[1][:content]- expect(content).to include("Germany")- expect(content).to include("France")- expect(content).to include("Paris")- expect(content).not_to include("google")- end- end-enddiff --git a/plugins/discourse-ai/spec/lib/agents/tools/search_uploaded_documents_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/search_uploaded_documents_spec.rbnew file mode 100644index 00000000000..f645a7d8275--- /dev/null+++ b/plugins/discourse-ai/spec/lib/agents/tools/search_uploaded_documents_spec.rb@@ -0,0 +1,95 @@+# frozen_string_literal: true++require "rails_helper"++RSpec.describe DiscourseAi::Agents::Tools::SearchUploadedDocuments do+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:vector_def, :embedding_definition)++ fab!(:ai_agent) do+ Group.refresh_automatic_groups!+ Fabricate(+ :ai_agent,+ name: "upload helper",+ rag_conversation_chunks: 3,+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],+ )+ end++ let(:upload) { Fabricate(:upload, original_filename: "guide.md") }+ let(:other_upload) { Fabricate(:upload, original_filename: "faq.md") }++ before do+ enable_current_plugin+ SiteSetting.authorized_extensions = "md|txt"+ SiteSetting.ai_embeddings_selected_model = vector_def.id+ SiteSetting.ai_embeddings_enabled = true++ UploadReference.ensure_exist!(target: ai_agent, upload_ids: [upload.id, other_upload.id])+ end++ let(:agent) { DiscourseAi::Agents::Agent.find_by(id: ai_agent.id, user: user).new }+ let(:embedding_value) { 0.04381 }+ let(:query_embeddings) { [embedding_value] * vector_def.dimensions }++ def store_fragment(text:, upload:, index:)+ fragment =+ Fabricate(+ :rag_document_fragment,+ fragment: text,+ target: ai_agent,+ upload: upload,+ fragment_number: index + 1,+ )++ embeddings = [embedding_value + "0.000#{index}".to_f] * vector_def.dimensions+ DiscourseAi::Embeddings::Schema.for(RagDocumentFragment).store(fragment, embeddings, "test")+ end++ it "returns no more excerpts than the agent rag setting allows" do+ store_fragment(text: "fragment-n0", upload: upload, index: 0)+ store_fragment(text: "fragment-n1", upload: upload, index: 1)+ store_fragment(text: "fragment-n2", upload: upload, index: 2)+ store_fragment(text: "fragment-n3", upload: upload, index: 3)++ EmbeddingsGenerationStubs.hugging_face_service("tell me the time", query_embeddings)++ tool =+ described_class.new(+ { query: "tell me the time", limit: 10 },+ bot_user: nil,+ llm: nil,+ agent: agent,+ )++ result = tool.invoke++ excerpt_contents = result[:excerpts].map { |excerpt| excerpt[:content] }++ expect(excerpt_contents.length).to eq(ai_agent.rag_conversation_chunks)+ expect(excerpt_contents).to all(be_in(%w[fragment-n0 fragment-n1 fragment-n2 fragment-n3]))+ expect(excerpt_contents.uniq.length).to eq(ai_agent.rag_conversation_chunks)+ end++ it "can restrict search to specific filenames" do+ store_fragment(text: "guide fragment", upload: upload, index: 0)+ store_fragment(text: "faq fragment", upload: other_upload, index: 1)++ EmbeddingsGenerationStubs.hugging_face_service("uploaded docs", query_embeddings)++ tool =+ described_class.new(+ { query: "uploaded docs", filenames: ["faq.md"] },+ bot_user: nil,+ llm: nil,+ agent: agent,+ )++ result = tool.invoke++ expect(result[:excerpts]).to contain_exactly(+ { filename: "faq.md", metadata: nil, fragment_number: 2, content: "faq fragment" },+ )+ expect(result[:filenames]).to eq(["faq.md"])+ end+enddiff --git a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rbindex d6891933a3d..0060de9c875 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb@@ -74,7 +74,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doallow_chat_channel_mentions: true,allow_chat_direct_messages: true,default_llm_id: llm_model.id,- question_consolidator_llm_id: llm_model.id,forced_tool_count: 2,)agent2.create_user!@@ -91,7 +90,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doexpect(serializer_agent2["allow_chat_direct_messages"]).to eq(true)expect(serializer_agent2["default_llm_id"]).to eq(llm_model.id)- expect(serializer_agent2["question_consolidator_llm_id"]).to eq(llm_model.id)+ expect(serializer_agent2).not_to have_key("question_consolidator_llm_id")expect(serializer_agent2["user_id"]).to eq(agent2.user_id)expect(serializer_agent2["user"]["id"]).to eq(agent2.user_id)expect(serializer_agent2["forced_tool_count"]).to eq(2)@@ -215,7 +214,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doallow_chat_channel_mentions: true,allow_chat_direct_messages: true,default_llm_id: llm_model.id,- question_consolidator_llm_id: llm_model.id,forced_tool_count: 2,execution_mode: "agentic",max_turn_tokens: 5000,@@ -248,7 +246,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doexpect(agent_json["allow_personal_messages"]).to eq(true)expect(agent_json["allow_chat_channel_mentions"]).to eq(true)expect(agent_json["allow_chat_direct_messages"]).to eq(true)- expect(agent_json["question_consolidator_llm_id"]).to eq(llm_model.id)+ expect(agent_json).not_to have_key("question_consolidator_llm_id")expect(agent_json["response_format"].map { |rf| rf["key"] }).to contain_exactly("summary")expect(agent_json["examples"]).to eq(valid_attributes[:examples])@@ -416,7 +414,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController dorag_chunk_overlap_tokens: "12",rag_conversation_chunks: "13",rag_llm_model_id: llm_model.id,- question_consolidator_llm_id: llm_model.id,},}@@ -427,7 +424,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doexpect(agent.rag_chunk_overlap_tokens).to eq(12)expect(agent.rag_conversation_chunks).to eq(13)expect(agent.rag_llm_model_id).to eq(llm_model.id)- expect(agent.question_consolidator_llm_id).to eq(llm_model.id)endit "supports updating agentic params" dodiff --git a/plugins/discourse-ai/test/javascripts/unit/models/ai-agent-test.js b/plugins/discourse-ai/test/javascripts/unit/models/ai-agent-test.jsindex 7b65ec89d28..eef3c43d444 100644--- a/plugins/discourse-ai/test/javascripts/unit/models/ai-agent-test.js+++ b/plugins/discourse-ai/test/javascripts/unit/models/ai-agent-test.js@@ -43,7 +43,6 @@ module("Discourse AI | Unit | Model | ai-agent", function () {rag_chunk_overlap_tokens: 10,rag_conversation_chunks: 10,rag_llm_model_id: 1,- question_consolidator_llm_id: 2,show_thinking: true,forced_tool_count: -1,allow_personal_messages: true,