Skip to content

Commit

Permalink
fix: fixed search result output format
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Aug 8, 2024
1 parent 9d2907b commit eda4f79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 15 additions & 8 deletions gptme/tools/_browser_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,29 @@ def _list_clickable_elements(page, selector=None) -> list[Element]:
return elements


def titleurl_to_list(results: list[tuple[str, str]]) -> str:
s = "```results"
for i, (title, url) in enumerate(results):
s += f"\n{i+1}. {title} ({url})"
return s + "\n```"


def _list_results_google(page) -> str:
# fetch the results (elements with .g class)
results = page.query_selector_all(".g")
if not results:
return "Error: something went wrong with the search."

# list results
s = "Results:"
for i, result in enumerate(results):
hits = []
for result in results:
url = result.query_selector("a").evaluate("el => el.href")
h3 = result.query_selector("h3")
if h3:
title = h3.inner_text()
result.query_selector("span").inner_text()
s += f"\n{i+1}. {title} ({url})"
return s
hits.append((title, url))
return titleurl_to_list(hits)


def _list_results_duckduckgo(page) -> str:
Expand All @@ -146,12 +153,12 @@ def _list_results_duckduckgo(page) -> str:
return "Error: something went wrong with the search."

# list results
s = "Results:"
for i, result in enumerate(results):
hits = []
for result in results:
url = result.query_selector("a").evaluate("el => el.href")
h2 = result.query_selector("h2")
if h2:
title = h2.inner_text()
result.query_selector("span").inner_text()
s += f"\n{i+1}. {title} ({url})"
return s
hits.append((title, url))
return titleurl_to_list(hits)
4 changes: 2 additions & 2 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ def test_browser():
# def test_search_duckduckgo():
# results = search("test", "duckduckgo")
# print(results)
# assert "Results:" in results
# assert "```results" in results


@pytest.mark.slow
def test_search_google():
results = search("test", "google")
print(results)
assert "Results:" in results
assert "```results" in results


@pytest.mark.slow
Expand Down

0 comments on commit eda4f79

Please sign in to comment.