From a3ca718131ce63ff50aa54ffb44c71a3e37045af Mon Sep 17 00:00:00 2001 From: martin legrand Date: Wed, 16 Apr 2025 13:59:11 +0200 Subject: [PATCH] feat : improve chrome path check + no query refusal on complex task --- sources/browser.py | 5 +++++ sources/llm_provider.py | 29 ++++++++++++++++------------- sources/router.py | 7 ------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/sources/browser.py b/sources/browser.py index 1621956..e9e181d 100644 --- a/sources/browser.py +++ b/sources/browser.py @@ -44,6 +44,11 @@ def get_chrome_path() -> str: for path in paths: if os.path.exists(path) and os.access(path, os.X_OK): # Check if executable return path + print("Looking for Google Chrome in these locations failed:") + print('\n'.join(paths)) + path = input("Google Chrome not found. Please enter the path to the Chrome executable: ") + if os.path.exists(path) and os.access(path, os.X_OK): + return path return None def create_driver(headless=False, stealth_mode=True, crx_path="./crx/nopecha.crx") -> webdriver.Chrome: diff --git a/sources/llm_provider.py b/sources/llm_provider.py index bfc54fc..1430d26 100644 --- a/sources/llm_provider.py +++ b/sources/llm_provider.py @@ -7,7 +7,9 @@ import requests import subprocess import ipaddress import httpx +import socket import platform +from urllib.parse import urlparse from dotenv import load_dotenv, set_key from openai import OpenAI from huggingface_hub import InferenceClient @@ -95,25 +97,26 @@ class Provider: raise Exception(f"Provider {self.provider_name} failed: {str(e)}") from e return thought - def is_ip_online(self, ip_address): + def is_ip_online(self, address: str, timeout: int = 10) -> bool: """ - Check if an IP address is online by sending a ping request. + Check if an address is online by sending a ping request. """ - if ip_address == "127.0.0.1": + if not address: + return False + if address.lower() in ["127.0.0.1", "localhost", "0.0.0.0"]: return True + hostname = urlparse(f'http://{address}' if not address.startswith(('http://', 'https://')) else address).hostname or address + try: + ip_address = socket.gethostbyname(hostname) + except socket.gaierror: + self.logger.error(f"Cannot resolve: {hostname}") + return False param = '-n' if platform.system().lower() == 'windows' else '-c' command = ['ping', param, '1', ip_address] try: - output = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=15) - if output.returncode == 0: - return True - else: - self.logger.error(f"Ping command returned code: {output.returncode}") - return False - except subprocess.TimeoutExpired: - return False - except Exception as e: - pretty_print(f"Error with ping request {str(e)}", color="failure") + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout) + return result.returncode == 0 + except (subprocess.TimeoutExpired, subprocess.SubprocessError) as e: return False def server_fn(self, history, verbose = False): diff --git a/sources/router.py b/sources/router.py index 793c752..b45b4c3 100644 --- a/sources/router.py +++ b/sources/router.py @@ -402,8 +402,6 @@ class AgentRouter: if confidence < 0.4: self.logger.info(f"Low confidence in complexity estimation: {confidence}") return "LOW" - if complexity == "HIGH" and len(text) < 64: - return None # ask for more info if complexity == "HIGH": return "HIGH" elif complexity == "LOW": @@ -440,11 +438,6 @@ class AgentRouter: text = self.lang_analysis.translate(text, lang) labels = [agent.role for agent in self.agents] complexity = self.estimate_complexity(text) - if complexity == None and self.asked_clarify == False: - self.asked_clarify = True - pretty_print(f"Humm, the task seem complex but you gave very little information. can you clarify?", color="info") - return None - self.asked_clarify = False if complexity == "HIGH": pretty_print(f"Complex task detected, routing to planner agent.", color="info") return self.find_planner_agent()