-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0e8738
commit b393c41
Showing
10 changed files
with
106 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Toolbox::GoogleSearch < Toolbox | ||
|
||
describe :google_search, <<~S | ||
Search Google for the indicated query. | ||
Use this to answer questions about current events, look up information, or find answers to questions. | ||
Try to use this sparingly; prefer to use the user's memories and the tools you have available to answer questions. | ||
When you do use this, try to use exact queries for which you expect to get a definitive answer. | ||
When you respond to the user, try to include an answer to the question rather than just a link. | ||
S | ||
def google_search(query_s:) | ||
encoded_query = URI.encode_www_form_component(query_s) | ||
response_body = get("https://www.google.com/search").param(q: encoded_query).body | ||
doc = Nokogiri::HTML(response_body) | ||
|
||
results = doc.css("div.BNeawe").map do |div| | ||
div.children.map do |node| | ||
if node.name == "a" | ||
anchor_text = node.text.strip | ||
href = node["href"] | ||
"#{anchor_text} (#{href})" | ||
else | ||
node.text.strip | ||
end | ||
end.join(" ") | ||
end.join("\n") | ||
|
||
{ | ||
message_to_user: "Web query: #{query_s}", | ||
query_results: results | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require "test_helper" | ||
|
||
class Toolbox::GoogleSearchTest < ActiveSupport::TestCase | ||
setup do | ||
@google_search = Toolbox::GoogleSearch.new | ||
WebMock.enable! | ||
end | ||
|
||
test "google_search returns the expected result" do | ||
expected_result = { | ||
message_to_user: "Web query: Sandi Metz POODR title", | ||
query_results: "Practical Object-Oriented Design in Ruby by Sandi Metz. Learn more (https://www.poodr.com) for more details." | ||
} | ||
html_content = <<-HTML | ||
<html> | ||
<body> | ||
<div class="BNeawe"> | ||
Practical Object-Oriented Design in Ruby by Sandi Metz.#{' '} | ||
<a href="https://www.poodr.com\">Learn more</a> for more details. | ||
</div> | ||
<div class="OtherClass"> | ||
Not relevant content. | ||
<a href="https://www.notrelevant.com">Ignore this</a>. | ||
</div> | ||
</body> | ||
</html> | ||
HTML | ||
|
||
stub_request(:get, /www.google.com/) | ||
.with( | ||
headers: { | ||
"Accept"=>"*/*", | ||
"Accept-Encoding"=>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3", | ||
"User-Agent"=>"Ruby" | ||
} | ||
) | ||
.to_return(status: 200, body: html_content, headers: {}) | ||
|
||
result = @google_search.google_search(query_s: "Sandi Metz POODR title") | ||
assert_equal expected_result, result | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters