mirror of
https://github.com/microsoft/fara.git
synced 2026-06-10 02:54:01 +08:00
use logging
This commit is contained in:
@@ -148,7 +148,7 @@ class FaraAgent:
|
||||
)
|
||||
return True # Captcha solved in time
|
||||
except asyncio.TimeoutError:
|
||||
print(f"Captcha timeout after {timeout_seconds} seconds!")
|
||||
self.logger.warning(f"Captcha timeout after {timeout_seconds} seconds!")
|
||||
# Force resume execution
|
||||
self.browser_manager._captcha_event.set()
|
||||
return False # Captcha timed out
|
||||
@@ -376,8 +376,9 @@ class FaraAgent:
|
||||
)
|
||||
assert isinstance(raw_response, str)
|
||||
all_actions.append(raw_response)
|
||||
# Print the model response
|
||||
print(f"\n[fara_agent] {raw_response}")
|
||||
thoughts, action_dict = self._parse_thoughts_and_action(raw_response)
|
||||
action_args = action_dict.get("arguments", {})
|
||||
self.logger.info(f"\nThought #{i+1}: {thoughts}\nAction #{i+1}: executing tool '{action_args["action"]}' with arguments {json.dumps(action_args)}")
|
||||
|
||||
(
|
||||
is_stop_action,
|
||||
@@ -385,10 +386,9 @@ class FaraAgent:
|
||||
action_description,
|
||||
) = await self.execute_action(function_call)
|
||||
all_observations.append(action_description)
|
||||
# Print action description
|
||||
print(f"\n[fara_agent] {action_description}")
|
||||
self.logger.info(f"Observation#{i+1}: {action_description}")
|
||||
if is_stop_action:
|
||||
final_answer = raw_response
|
||||
final_answer = thoughts
|
||||
break
|
||||
return final_answer, all_actions, all_observations
|
||||
|
||||
@@ -437,7 +437,7 @@ class FaraAgent:
|
||||
args = function_call[0].arguments
|
||||
action_description = ""
|
||||
assert self._page is not None
|
||||
self.logger.info(
|
||||
self.logger.debug(
|
||||
WebSurferEvent(
|
||||
source="FaraAgent",
|
||||
url=await self._playwright_controller.get_page_url(self._page),
|
||||
@@ -563,6 +563,7 @@ class FaraAgent:
|
||||
elif args["action"] == "pause_and_memorize_fact":
|
||||
fact = str(args.get("fact"))
|
||||
self._facts.append(fact)
|
||||
action_description= f"I memorized the following fact: {fact}"
|
||||
elif args["action"] == "stop" or args["action"] == "terminate":
|
||||
action_description = args.get("thoughts")
|
||||
is_stop_action = True
|
||||
|
||||
@@ -9,7 +9,24 @@ from pathlib import Path
|
||||
import json
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
# Configure logging to only show logs from fara.fara_agent
|
||||
# Disable all logging by default
|
||||
logging.basicConfig(
|
||||
level=logging.CRITICAL, # Set root logger to CRITICAL to suppress all logs
|
||||
format="%(message)s", # Simple format without logger name
|
||||
)
|
||||
|
||||
# Enable INFO level only for fara.fara_agent
|
||||
fara_agent_logger = logging.getLogger("fara.fara_agent")
|
||||
fara_agent_logger.setLevel(logging.INFO)
|
||||
|
||||
# Add a handler to ensure fara_agent logs are shown
|
||||
handler = logging.StreamHandler()
|
||||
handler.setLevel(logging.INFO)
|
||||
handler.setFormatter(logging.Formatter("%(message)s"))
|
||||
fara_agent_logger.addHandler(handler)
|
||||
fara_agent_logger.propagate = False # Don't propagate to root logger
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -29,11 +46,16 @@ async def run_fara_agent(
|
||||
save_screenshots: bool = True,
|
||||
max_rounds: int = 100,
|
||||
use_browser_base: bool = False,
|
||||
retries_on_failure: int = 3,
|
||||
retries_on_failure: int = 1,
|
||||
):
|
||||
# Create the FaraAgent instance
|
||||
print("##########################################")
|
||||
print(f"Task: {task}")
|
||||
print("##########################################")
|
||||
|
||||
for _ in range(retries_on_failure):
|
||||
# Initialize browser manager
|
||||
print("Initializing Browser...")
|
||||
browser_manager = BrowserBB(
|
||||
headless=headless,
|
||||
viewport_height=900,
|
||||
@@ -48,6 +70,7 @@ async def run_fara_agent(
|
||||
use_browser_base=use_browser_base,
|
||||
logger=logger,
|
||||
)
|
||||
print("Browser Running... Starting Fara Agent...")
|
||||
|
||||
agent = FaraAgent(
|
||||
browser_manager=browser_manager,
|
||||
@@ -60,8 +83,9 @@ async def run_fara_agent(
|
||||
|
||||
try:
|
||||
await agent.initialize()
|
||||
print(f"Running task: {task}")
|
||||
await agent.run(task)
|
||||
print("Running Fara...\n")
|
||||
final_answer, all_actions, all_observations = await agent.run(task)
|
||||
print(f"\nFinal Answer: {final_answer}")
|
||||
break # Exit the retry loop if successful
|
||||
except Exception as e:
|
||||
print(f"Error occurred: {e}")
|
||||
@@ -118,8 +142,12 @@ async def main():
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.browserbase:
|
||||
assert os.environ.get("BROWSERBASE_API_KEY"), "BROWSERBASE_API_KEY environment variable must be set to use browserbase"
|
||||
assert os.environ.get("BROWSERBASE_PROJECT_ID"), "BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID environment variables must be set to use browserbase"
|
||||
assert os.environ.get("BROWSERBASE_API_KEY"), (
|
||||
"BROWSERBASE_API_KEY environment variable must be set to use browserbase"
|
||||
)
|
||||
assert os.environ.get("BROWSERBASE_PROJECT_ID"), (
|
||||
"BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID environment variables must be set to use browserbase"
|
||||
)
|
||||
|
||||
endpoint_config = DEFAULT_ENDPOINT_CONFIG
|
||||
if args.endpoint_config:
|
||||
|
||||
Reference in New Issue
Block a user