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
This commit is contained in:
parent
059d3b6fd2
commit
f1283e156d
|
@ -205,6 +205,10 @@ en:
|
||||||
summarizing: "Summarizing topic"
|
summarizing: "Summarizing topic"
|
||||||
searching: "Searching for: '%{query}'"
|
searching: "Searching for: '%{query}'"
|
||||||
tool_options:
|
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:
|
||||||
read_private:
|
read_private:
|
||||||
name: "Read Private"
|
name: "Read Private"
|
||||||
|
|
|
@ -23,15 +23,24 @@ module DiscourseAi
|
||||||
"google"
|
"google"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.accepted_options
|
||||||
|
[option(:base_query, type: :string)]
|
||||||
|
end
|
||||||
|
|
||||||
def query
|
def query
|
||||||
parameters[:query].to_s.strip
|
parameters[:query].to_s.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoke
|
def invoke
|
||||||
|
query = self.query
|
||||||
|
|
||||||
yield(query)
|
yield(query)
|
||||||
|
|
||||||
api_key = SiteSetting.ai_google_custom_search_api_key
|
api_key = SiteSetting.ai_google_custom_search_api_key
|
||||||
cx = SiteSetting.ai_google_custom_search_cx
|
cx = SiteSetting.ai_google_custom_search_cx
|
||||||
|
|
||||||
|
query = "#{options[:base_query]} #{query}" if options[:base_query].present?
|
||||||
|
|
||||||
escaped_query = CGI.escape(query)
|
escaped_query = CGI.escape(query)
|
||||||
uri =
|
uri =
|
||||||
URI(
|
URI(
|
||||||
|
|
|
@ -7,13 +7,14 @@ RSpec.describe DiscourseAi::AiBot::Tools::Google do
|
||||||
let(:progress_blk) { Proc.new {} }
|
let(:progress_blk) { Proc.new {} }
|
||||||
let(:search) { described_class.new({ query: "some search term" }, bot_user: bot_user, llm: llm) }
|
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
|
describe "#process" do
|
||||||
it "will not explode if there are no results" 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
|
json_text = { searchInformation: { totalResults: "0" } }.to_json
|
||||||
|
|
||||||
stub_request(
|
stub_request(
|
||||||
|
@ -27,10 +28,30 @@ RSpec.describe DiscourseAi::AiBot::Tools::Google do
|
||||||
expect(info).to_not include("oops")
|
expect(info).to_not include("oops")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can generate correct info" do
|
it "supports base_query" do
|
||||||
SiteSetting.ai_google_custom_search_api_key = "abc"
|
base_query = "site:discourse.org"
|
||||||
SiteSetting.ai_google_custom_search_cx = "cx"
|
|
||||||
|
|
||||||
|
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 = {
|
json_text = {
|
||||||
searchInformation: {
|
searchInformation: {
|
||||||
totalResults: "2",
|
totalResults: "2",
|
||||||
|
|
Loading…
Reference in New Issue