mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-09 11:48:47 +00:00
FIX: Google command was including full payload (#128)
* FIX: Google command was including full payload Additionally there was no truncating happening meaning you could blow token budget easily on a single search. This made Google search mostly useless and it would mean that after using Google we would revert to a clean slate which is very confusing. * no need for nil there
This commit is contained in:
parent
7edb57c005
commit
03e689deb7
@ -48,22 +48,28 @@ module DiscourseAi::AiBot::Commands
|
|||||||
URI("https://www.googleapis.com/customsearch/v1?key=#{api_key}&cx=#{cx}&q=#{query}&num=10")
|
URI("https://www.googleapis.com/customsearch/v1?key=#{api_key}&cx=#{cx}&q=#{query}&num=10")
|
||||||
body = Net::HTTP.get(uri)
|
body = Net::HTTP.get(uri)
|
||||||
|
|
||||||
parse_search_json(body)
|
parse_search_json(body, query)
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_search_json(json_data)
|
def minimize_field(result, field, max_tokens: 100)
|
||||||
|
data = result[field].squish
|
||||||
|
data = ::DiscourseAi::Tokenizer::BertTokenizer.truncate(data, max_tokens).squish
|
||||||
|
data
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_search_json(json_data, query)
|
||||||
parsed = JSON.parse(json_data)
|
parsed = JSON.parse(json_data)
|
||||||
results = parsed["items"]
|
results = parsed["items"]
|
||||||
|
|
||||||
@last_num_results = parsed.dig("searchInformation", "totalResults").to_i
|
@last_num_results = parsed.dig("searchInformation", "totalResults").to_i
|
||||||
|
|
||||||
format_results(results, args: json_data) do |result|
|
format_results(results, args: query) do |result|
|
||||||
{
|
{
|
||||||
title: result["title"],
|
title: minimize_field(result, "title"),
|
||||||
link: result["link"],
|
link: minimize_field(result, "link"),
|
||||||
snippet: result["snippet"],
|
snippet: minimize_field(result, "snippet", max_tokens: 120),
|
||||||
displayLink: result["displayLink"],
|
displayLink: minimize_field(result, "displayLink"),
|
||||||
formattedUrl: result["formattedUrl"],
|
formattedUrl: minimize_field(result, "formattedUrl"),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -23,6 +23,7 @@ RSpec.describe DiscourseAi::AiBot::Commands::GoogleCommand do
|
|||||||
snippet: "snippet1",
|
snippet: "snippet1",
|
||||||
displayLink: "displayLink1",
|
displayLink: "displayLink1",
|
||||||
formattedUrl: "formattedUrl1",
|
formattedUrl: "formattedUrl1",
|
||||||
|
oops: "do no include me ... oops",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}.to_json
|
}.to_json
|
||||||
@ -38,6 +39,8 @@ RSpec.describe DiscourseAi::AiBot::Commands::GoogleCommand do
|
|||||||
expect(google.description_args[:count]).to eq(1)
|
expect(google.description_args[:count]).to eq(1)
|
||||||
expect(info).to include("title1")
|
expect(info).to include("title1")
|
||||||
expect(info).to include("snippet1")
|
expect(info).to include("snippet1")
|
||||||
|
expect(info).to include("some+search+term")
|
||||||
|
expect(info).to_not include("oops")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user