Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ en:
summarizing: "Summarizing topic"
searching: "Searching for: '%{query}'"
tool_options:
google:
base_query:
name: "Base Search Query"
description: "Base query to use when searching. Examples: 'site:example.com' will only include results from example.com, before:2022-01-01 will only includes results from 2021 and earlier. This text is prepended to the search query."
read:
read_private:
name: "Read Private"
Expand Down
9 changes: 9 additions & 0 deletions lib/ai_bot/tools/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ def self.name
"google"
end

def self.accepted_options
[option(:base_query, type: :string)]
end

def query
parameters[:query].to_s.strip
end

def invoke
query = self.query

yield(query)

api_key = SiteSetting.ai_google_custom_search_api_key
cx = SiteSetting.ai_google_custom_search_cx

query = "#{options[:base_query]} #{query}" if options[:base_query].present?

escaped_query = CGI.escape(query)
uri =
URI(
Expand Down
35 changes: 28 additions & 7 deletions spec/lib/modules/ai_bot/tools/google_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
let(:progress_blk) { Proc.new {} }
let(:search) { described_class.new({ query: "some search term" }, bot_user: bot_user, llm: llm) }

before { SiteSetting.ai_bot_enabled = true }
before do
SiteSetting.ai_bot_enabled = true
SiteSetting.ai_google_custom_search_api_key = "abc"
SiteSetting.ai_google_custom_search_cx = "cx"
end

describe "#process" do
it "will not explode if there are no results" do
SiteSetting.ai_google_custom_search_api_key = "abc"
SiteSetting.ai_google_custom_search_cx = "cx"

json_text = { searchInformation: { totalResults: "0" } }.to_json

stub_request(
Expand All @@ -27,10 +28,30 @@
expect(info).to_not include("oops")
end

it "can generate correct info" do
SiteSetting.ai_google_custom_search_api_key = "abc"
SiteSetting.ai_google_custom_search_cx = "cx"
it "supports base_query" do
base_query = "site:discourse.org"

search =
described_class.new(
{ query: "some search term" },
bot_user: bot_user,
llm: llm,
persona_options: {
"base_query" => base_query,
},
)

json_text = { searchInformation: { totalResults: "0" } }.to_json

stub_request(
:get,
"https://www.googleapis.com/customsearch/v1?cx=cx&key=abc&num=10&q=site:discourse.org%20some%20search%20term",
).to_return(status: 200, body: json_text, headers: {})

search.invoke(&progress_blk)
end

it "can generate correct info" do
json_text = {
searchInformation: {
totalResults: "2",
Expand Down