Skip to content

Commit 4aaf202

Browse files
committed
fix: improved browser tool search output, if python tool had result then skip stdout in msg
1 parent e88d426 commit 4aaf202

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

gptme/tools/_browser_playwright.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,20 @@ def _list_clickable_elements(page, selector=None) -> list[Element]:
121121
return elements
122122

123123

124-
def titleurl_to_list(results: list[tuple[str, str]]) -> str:
125-
s = "```results"
126-
for i, (title, url) in enumerate(results):
127-
s += f"\n{i+1}. {title} ({url})"
128-
return s + "\n```"
124+
@dataclass
125+
class SearchResult:
126+
title: str
127+
url: str
128+
description: str | None = None
129+
130+
131+
def titleurl_to_list(results: list[SearchResult]) -> str:
132+
s = ""
133+
for i, r in enumerate(results):
134+
s += f"\n{i + 1}. {r.title} ({r.url})"
135+
if r.description:
136+
s += f"\n {r.description}"
137+
return s.strip()
129138

130139

131140
def _list_results_google(page) -> str:
@@ -141,8 +150,11 @@ def _list_results_google(page) -> str:
141150
h3 = result.query_selector("h3")
142151
if h3:
143152
title = h3.inner_text()
144-
result.query_selector("span").inner_text()
145-
hits.append((title, url))
153+
# desc has data-sncf attribute
154+
desc = (
155+
result.query_selector("[data-sncf]").inner_text().strip().split("\n")[0]
156+
)
157+
hits.append(SearchResult(title, url, desc))
146158
return titleurl_to_list(hits)
147159

148160

@@ -163,6 +175,6 @@ def _list_results_duckduckgo(page) -> str:
163175
h2 = result.query_selector("h2")
164176
if h2:
165177
title = h2.inner_text()
166-
result.query_selector("span").inner_text()
167-
hits.append((title, url))
178+
desc = result.query_selector("span").inner_text().strip().split("\n")[0]
179+
hits.append(SearchResult(title, url, desc))
168180
return titleurl_to_list(hits)

gptme/tools/python.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,20 @@ def execute_python(code: str, ask: bool, args=None) -> Generator[Message, None,
114114
result = _ipython.run_cell(code, silent=False, store_history=False)
115115

116116
output = ""
117-
if captured.stdout:
118-
# remove one occurrence of the result if present, to avoid repeating the result in the output
119-
stdout = (
120-
captured.stdout.replace(str(result.result), "", 1)
121-
if result.result
122-
else captured.stdout
123-
)
124-
if stdout:
125-
output += f"\n```stdout\n{stdout.rstrip()}\n```\n\n"
117+
if result.result is not None:
118+
output += f"Result:\n```\n{result.result}\n```\n\n"
119+
# only show stdout if there is no result
120+
elif captured.stdout:
121+
output += f"```stdout\n{captured.stdout.rstrip()}\n```\n\n"
122+
126123
if captured.stderr:
127-
output += f"stderr:\n```\n{captured.stderr.rstrip()}\n```\n\n"
124+
output += f"```stderr\n{captured.stderr.rstrip()}\n```\n\n"
128125
if result.error_in_exec:
129126
tb = result.error_in_exec.__traceback__
130127
while tb.tb_next: # type: ignore
131128
tb = tb.tb_next # type: ignore
132129
# type: ignore
133130
output += f"Exception during execution on line {tb.tb_lineno}:\n {result.error_in_exec.__class__.__name__}: {result.error_in_exec}"
134-
if result.result is not None:
135-
output += f"Result:\n```\n{result.result}\n```\n\n"
136131

137132
# strip ANSI escape sequences
138133
# TODO: better to signal to the terminal that we don't want colors?

0 commit comments

Comments
 (0)