mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-09 11:48:47 +00:00
* FEATURE: introduce a more efficient formatter Previous formatting style was space inefficient given JSON consumes lots of tokens, the new format is now used consistently across commands Also fixes - search limited to 10 - search breaking on limit: non existent directive * Slight improvement to summarizer Stop blowing up context with custom prompts * ensure we include the guiding message * correct spec * langchain style summarizer ... much more accurate (albeit more expensive) * lint
57 lines
1.4 KiB
Ruby
57 lines
1.4 KiB
Ruby
#frozen_string_literal: true
|
|
|
|
module DiscourseAi::AiBot::Commands
|
|
class GoogleCommand < Command
|
|
class << self
|
|
def name
|
|
"google"
|
|
end
|
|
|
|
def desc
|
|
"!google SEARCH_QUERY - will search using Google (supports all Google search operators)"
|
|
end
|
|
end
|
|
|
|
def result_name
|
|
"results"
|
|
end
|
|
|
|
def description_args
|
|
{
|
|
count: @last_num_results || 0,
|
|
query: @last_query || "",
|
|
url: "https://google.com/search?q=#{CGI.escape(@last_query || "")}",
|
|
}
|
|
end
|
|
|
|
def process(search_string)
|
|
@last_query = search_string
|
|
api_key = SiteSetting.ai_google_custom_search_api_key
|
|
cx = SiteSetting.ai_google_custom_search_cx
|
|
query = CGI.escape(search_string)
|
|
uri =
|
|
URI("https://www.googleapis.com/customsearch/v1?key=#{api_key}&cx=#{cx}&q=#{query}&num=10")
|
|
body = Net::HTTP.get(uri)
|
|
|
|
parse_search_json(body).to_s
|
|
end
|
|
|
|
def parse_search_json(json_data)
|
|
parsed = JSON.parse(json_data)
|
|
results = parsed["items"]
|
|
|
|
@last_num_results = parsed.dig("searchInformation", "totalResults").to_i
|
|
|
|
format_results(results) do |result|
|
|
{
|
|
title: result["title"],
|
|
link: result["link"],
|
|
snippet: result["snippet"],
|
|
displayLink: result["displayLink"],
|
|
formattedUrl: result["formattedUrl"],
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|