Fix : web search

This commit is contained in:
martin legrand
2025-03-11 12:27:19 +01:00
parent ed02a4dad0
commit 479bc2769b
7 changed files with 32 additions and 10 deletions
+10
View File
@@ -39,6 +39,7 @@ class Tools():
self.messages = []
self.config = configparser.ConfigParser()
self.current_dir = self.create_work_dir()
self.excutable_blocks_found = False
def check_config_dir_validity(self):
"""
@@ -129,6 +130,14 @@ class Tools():
print(f"Saving code block to: {save_path}")
with open(os.path.join(directory, save_path_file), 'w') as f:
f.write(block)
def found_executable_blocks(self):
"""
Check if executable blocks were found.
"""
tmp = self.excutable_blocks_found
self.excutable_blocks_found = False
return tmp
def load_exec_block(self, llm_text: str) -> tuple[list[str], str | None]:
"""
@@ -178,6 +187,7 @@ class Tools():
if ':' in content.split('\n')[0]:
save_path = content.split('\n')[0].split(':')[1]
content = content[content.find('\n')+1:]
self.excutable_blocks_found = True
code_blocks.append(content)
start_index = end_pos + len(end_tag)
return code_blocks, save_path
+14 -7
View File
@@ -6,9 +6,13 @@ import dotenv
dotenv.load_dotenv()
if __name__ == "__main__":
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utility import animate_thinking, pretty_print
from tools import Tools
else:
from sources.tools.tools import Tools
from sources.utility import animate_thinking, pretty_print
class webSearch(Tools):
def __init__(self, api_key: str = None):
@@ -24,6 +28,7 @@ class webSearch(Tools):
return "Error: No SerpApi key provided."
for block in blocks:
query = block.strip()
pretty_print(f"Searching for: {query}", color="status")
if not query:
return "Error: No search query provided."
@@ -32,19 +37,21 @@ class webSearch(Tools):
params = {
"q": query,
"api_key": self.api_key,
"num": 1,
"num": 100,
"output": "json"
}
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
results = []
if "organic_results" in data and len(data["organic_results"]) > 0:
first_result = data["organic_results"][0]
title = first_result.get("title", "No title")
snippet = first_result.get("snippet", "No snippet available")
link = first_result.get("link", "No link available")
return f"Title: {title}\nSnippet: {snippet}\nLink: {link}"
for result in data["organic_results"][:50]:
title = result.get("title", "No title")
snippet = result.get("snippet", "No snippet available")
link = result.get("link", "No link available")
results.append(f"Title: {title}\nSnippet: {snippet}\nLink: {link}")
return "\n\n".join(results)
else:
return "No results found for the query."
except requests.RequestException as e:
@@ -65,6 +72,6 @@ class webSearch(Tools):
if __name__ == "__main__":
search_tool = webSearch(api_key=os.getenv("SERPAPI_KEY"))
query = "when did covid start"
result = search_tool.execute(query, safety=True)
result = search_tool.execute([query], safety=True)
feedback = search_tool.interpreter_feedback(result)
print(feedback)