Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit f1283e1

Browse files
authored
FEATURE: allow scoping of google tool queries (#852)
This allows to simply scope search results to specific domains and prepend arbitrary snippets to searches made
1 parent 059d3b6 commit f1283e1

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

config/locales/server.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ en:
205205
summarizing: "Summarizing topic"
206206
searching: "Searching for: '%{query}'"
207207
tool_options:
208+
google:
209+
base_query:
210+
name: "Base Search Query"
211+
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."
208212
read:
209213
read_private:
210214
name: "Read Private"

lib/ai_bot/tools/google.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,24 @@ def self.name
2323
"google"
2424
end
2525

26+
def self.accepted_options
27+
[option(:base_query, type: :string)]
28+
end
29+
2630
def query
2731
parameters[:query].to_s.strip
2832
end
2933

3034
def invoke
35+
query = self.query
36+
3137
yield(query)
3238

3339
api_key = SiteSetting.ai_google_custom_search_api_key
3440
cx = SiteSetting.ai_google_custom_search_cx
41+
42+
query = "#{options[:base_query]} #{query}" if options[:base_query].present?
43+
3544
escaped_query = CGI.escape(query)
3645
uri =
3746
URI(

spec/lib/modules/ai_bot/tools/google_spec.rb

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
let(:progress_blk) { Proc.new {} }
88
let(:search) { described_class.new({ query: "some search term" }, bot_user: bot_user, llm: llm) }
99

10-
before { SiteSetting.ai_bot_enabled = true }
10+
before do
11+
SiteSetting.ai_bot_enabled = true
12+
SiteSetting.ai_google_custom_search_api_key = "abc"
13+
SiteSetting.ai_google_custom_search_cx = "cx"
14+
end
1115

1216
describe "#process" do
1317
it "will not explode if there are no results" do
14-
SiteSetting.ai_google_custom_search_api_key = "abc"
15-
SiteSetting.ai_google_custom_search_cx = "cx"
16-
1718
json_text = { searchInformation: { totalResults: "0" } }.to_json
1819

1920
stub_request(
@@ -27,10 +28,30 @@
2728
expect(info).to_not include("oops")
2829
end
2930

30-
it "can generate correct info" do
31-
SiteSetting.ai_google_custom_search_api_key = "abc"
32-
SiteSetting.ai_google_custom_search_cx = "cx"
31+
it "supports base_query" do
32+
base_query = "site:discourse.org"
33+
34+
search =
35+
described_class.new(
36+
{ query: "some search term" },
37+
bot_user: bot_user,
38+
llm: llm,
39+
persona_options: {
40+
"base_query" => base_query,
41+
},
42+
)
43+
44+
json_text = { searchInformation: { totalResults: "0" } }.to_json
3345

46+
stub_request(
47+
:get,
48+
"https://www.googleapis.com/customsearch/v1?cx=cx&key=abc&num=10&q=site:discourse.org%20some%20search%20term",
49+
).to_return(status: 200, body: json_text, headers: {})
50+
51+
search.invoke(&progress_blk)
52+
end
53+
54+
it "can generate correct info" do
3455
json_text = {
3556
searchInformation: {
3657
totalResults: "2",

0 commit comments

Comments
 (0)