Microsoft Fara Tutorial: Run a Browser-Use Agent in Google Colab with a Mock OpenAI-Compatible Endpoint
In this tutorial, we arrange Microsoft Fara in Google Colab and run a browser-use workflow from begin to end. We start by cloning the repository, putting in the package deal, getting ready Playwright, and verifying that the put in Fara recordsdata work even when the package deal format modifications. Instead of instantly counting on a heavy Fara-7B deployment, we create a small mock OpenAI-compatible endpoint that returns legitimate browser actions. This lets us take a look at the identical agent loop that Fara makes use of for actual duties, together with sending a activity, receiving model-style motion responses, and executing these actions via the browser. We additionally hold the endpoint configuration versatile, so the identical pocket book can later hook up with Azure Foundry, vLLM, LM Studio, or Ollama after we wish to use the true Fara-7B mannequin.
import os
import sys
import json
import time
import socket
import subprocess
import importlib
from pathlib import Path
USE_REAL_FARA_ENDPOINT = False
REAL_FARA_BASE_URL = "http://localhost:5000/v1"
REAL_FARA_API_KEY = "not-needed"
REAL_FARA_MODEL = "microsoft/Fara-7B"
TASK = "Open instance.com and inform me what the web page is."
WORKDIR = Path("/content material/fara_tutorial")
REPO_DIR = Path("/content material/fara")
REPO_SRC = REPO_DIR / "src"
OUTPUT_DIR = WORKDIR / "outputs"
ENDPOINT_CONFIG_PATH = WORKDIR / "endpoint_config.json"
MOCK_SERVER_FILE = WORKDIR / "mock_fara_endpoint.py"
WORKDIR.mkdir(dad and mom=True, exist_ok=True)
OUTPUT_DIR.mkdir(dad and mom=True, exist_ok=True)
We import the required Python libraries and outline the principle configuration values for the tutorial. We determine whether or not to make use of the mock endpoint or hook up with a actual Fara endpoint, and we set the browser activity that the agent must carry out. We additionally create the working folders and file paths for the repository, endpoint configuration, mock server, and output recordsdata.
def run_cmd(cmd, cwd=None, test=True, env=None):
print(f"n$ {cmd}")
consequence = subprocess.run(
cmd,
shell=True,
cwd=str(cwd) if cwd else None,
textual content=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=env,
)
print(consequence.stdout)
if test and consequence.returncode != 0:
increase RuntimeError(f"Command failed with exit code {consequence.returncode}: {cmd}")
return consequence
def wait_for_port(host, port, timeout=60):
begin = time.time()
whereas time.time() - begin < timeout:
attempt:
with socket.create_connection((host, port), timeout=2):
return True
besides OSError:
time.sleep(1)
return False
print("Python:", sys.model)
print("Working listing:", WORKDIR)
We outline a command runner that lets us execute shell instructions in the pocket book and think about the output clearly. We additionally create a helper operate that checks whether or not a server is lively on a given port earlier than the subsequent step runs. We then print the Python model and dealing listing to verify that the Colab atmosphere is ready up accurately.
os.chdir("/content material")
if REPO_DIR.exists():
print("Fara repo already exists. Pulling newest modifications...")
run_cmd("git pull", cwd=REPO_DIR, test=False)
else:
run_cmd("git clone https://github.com/microsoft/fara.git /content material/fara")
print("nInstalling Fara and tutorial dependencies...")
run_cmd(
f'{sys.executable} -m pip set up -q "setuptools<82" wheel pip',
test=True,
)
run_cmd(
f'{sys.executable} -m pip set up -q -e /content material/fara fastapi uvicorn requests pillow',
test=True,
)
if str(REPO_SRC) not in sys.path:
sys.path.insert(0, str(REPO_SRC))
print("nInstalling Playwright Firefox browser and system dependencies...")
run_cmd(
f"{sys.executable} -m playwright set up --with-deps firefox",
test=True,
)
We clone the Microsoft Fara repository into Colab, or pull the most recent model if the repository already exists. We set up Fara alongside with the supporting packages wanted for the mock endpoint and browser workflow. We additionally set up Playwright Firefox help so the agent can management a browser throughout execution.
print("nInspecting Fara package deal recordsdata...")
attempt:
import fara
print("Imported fara from:", getattr(fara, "__file__", "unknown"))
besides Exception as e:
print("Could not import fara:", repr(e))
print("nAvailable recordsdata inside /content material/fara/src/fara:")
if (REPO_SRC / "fara").exists():
for p in sorted((REPO_SRC / "fara").glob("*.py")):
print("-", p.title)
else:
print("Could not discover /content material/fara/src/fara")
print("nTrying to examine Fara motion definitions...")
attempt:
fara_agent = importlib.import_module("fara.fara_agent")
action_defs = getattr(fara_agent, "FARA_ACTION_DEFINITIONS", None)
if action_defs:
print("nFara motion house:")
for action_name, arg_names in action_defs.gadgets():
args = ", ".be a part of(sorted(arg_names)) if arg_names else "no arguments"
print(f"- {action_name}: {args}")
else:
print("FARA_ACTION_DEFINITIONS was not discovered. Continuing as a result of this step is non-obligatory.")
besides Exception as e:
print("Could not import fara.fara_agent immediately:", repr(e))
print("Continuing as a result of this inspection step is non-obligatory.")
We examine the put in Fara package deal and test the place it’s being imported from in the pocket book. We checklist the Python recordsdata in the Fara supply folder to grasp the present package deal construction. We then attempt to load the Fara motion definitions safely, whereas preserving the tutorial working even when the import path modifications.
mock_server_code = r'''
from fastapi import FastAPI, Request
import time
app = FastAPI()
STATE = {"calls": 0}
@app.publish("/v1/chat/completions")
async def chat_completions(request: Request):
payload = await request.json()
STATE["calls"] += 1
model_name = payload.get("mannequin", "mock-fara-7b")
if STATE["calls"] == 1:
content material = (
"I'll open a steady public take a look at web page so the browser-control loop may be demonstrated.n"
"{"title":"pc","arguments":{"motion":"visit_url","url":"https://instance.com"}}"
)
else:
content material = (
"The browser has opened Example Domain, a steady demonstration web page used for documentation and examples.n"
"{"title":"pc","arguments":{"motion":"terminate","standing":"success"}}"
)
return {
"id": f"chatcmpl-mock-{STATE['calls']}",
"object": "chat.completion",
"created": int(time.time()),
"mannequin": model_name,
"decisions": [
{
"index": 0,
"message": {
"role": "assistant",
"content": content
},
"finish_reason": "stop"
}
],
"utilization": {
"prompt_tokens": 100,
"completion_tokens": 50,
"total_tokens": 150
}
}
'''
MOCK_SERVER_FILE.write_text(mock_server_code)
print(f"nMock endpoint written to: {MOCK_SERVER_FILE}")
if USE_REAL_FARA_ENDPOINT:
endpoint_config = {
"mannequin": REAL_FARA_MODEL,
"base_url": REAL_FARA_BASE_URL,
"api_key": REAL_FARA_API_KEY,
}
else:
endpoint_config = {
"mannequin": "mock-fara-7b",
"base_url": "http://127.0.0.1:8001/v1",
"api_key": "not-needed",
}
ENDPOINT_CONFIG_PATH.write_text(json.dumps(endpoint_config, indent=2))
print("nEndpoint config:")
print(ENDPOINT_CONFIG_PATH.read_text())
mock_process = None
if not USE_REAL_FARA_ENDPOINT:
print("nStarting mock OpenAI-compatible endpoint...")
mock_process = subprocess.Popen(
[
sys.executable,
"-m",
"uvicorn",
"mock_fara_endpoint:app",
"--host",
"127.0.0.1",
"--port",
"8001",
],
cwd=str(WORKDIR),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
textual content=True,
)
if not wait_for_port("127.0.0.1", 8001, timeout=60):
if mock_process and mock_process.stdout:
print(mock_process.stdout.learn())
increase RuntimeError("Mock endpoint didn't begin on port 8001.")
print("Mock endpoint is working at http://127.0.0.1:8001/v1")
else:
print("nUsing actual Fara endpoint. Make positive it's reachable.")
We create a mock OpenAI-compatible endpoint that returns legitimate Fara-style browser actions. We write the endpoint configuration file so Fara is aware of whether or not to name the mock server or a actual Fara-7B endpoint. We then begin the mock server in the background and wait till it is able to obtain requests.
print("nRunning Fara browser agent...")
fara_command = (
f'fara-cli '
f'--task "{TASK}" '
f'--endpoint_config "{ENDPOINT_CONFIG_PATH}"'
)
agent_result = run_cmd(fara_command, cwd=REPO_DIR, test=False)
if agent_result.returncode != 0:
print("nfara-cli failed, making an attempt module type...")
fara_command = (
f'{sys.executable} -m fara.run_fara '
f'--task "{TASK}" '
f'--endpoint_config "{ENDPOINT_CONFIG_PATH}"'
)
agent_result = run_cmd(fara_command, cwd=REPO_DIR, test=False)
print("nFara command exit code:", agent_result.returncode)
print("nSaved tutorial outputs:")
if OUTPUT_DIR.exists():
recordsdata = sorted(OUTPUT_DIR.glob("*"))
if recordsdata:
for path in recordsdata:
print("-", path)
else:
print("No recordsdata saved in output listing.")
else:
print("Output listing doesn't exist.")
real_usage_notes = """
============================================================================
How to modify this pocket book from mock mode to actual Fara-7B
============================================================================
Option A: Azure Foundry endpoint
Set:
USE_REAL_FARA_ENDPOINT = True
REAL_FARA_BASE_URL = "https://your-endpoint.inference.ml.azure.com/"
REAL_FARA_API_KEY = "YOUR_AZURE_FOUNDRY_KEY"
REAL_FARA_MODEL = "Fara-7B"
Option B: self-host with vLLM on a GPU machine
Run this individually on the GPU machine:
vllm serve "microsoft/Fara-7B" --port 5000 --dtype auto
Then set:
USE_REAL_FARA_ENDPOINT = True
REAL_FARA_BASE_URL = "http://localhost:5000/v1"
REAL_FARA_API_KEY = "not-needed"
REAL_FARA_MODEL = "microsoft/Fara-7B"
Option C: LM Studio or Ollama
Load a suitable Fara-7B mannequin and allow an OpenAI-compatible native server.
Example base URLs:
LM Studio: http://localhost:1234/v1
Ollama-style OpenAI server: http://localhost:11434/v1
Important:
Browser brokers must be examined in sandboxed environments solely.
Avoid non-public accounts, funds, credentials, and high-risk web sites.
============================================================================
"""
print(real_usage_notes)
if mock_process is just not None:
print("nStopping mock endpoint...")
mock_process.terminate()
attempt:
mock_process.wait(timeout=10)
besides subprocess.TimeoutExpired:
mock_process.kill()
print("Mock endpoint stopped.")
print("nTutorial full.")
We run the Fara browser agent utilizing fara-cli and use the module command as a fallback if the CLI name fails. We print the consequence, test any generated output recordsdata, and embrace the settings wanted to modify from mock mode to a actual Fara-7B deployment. We lastly cease the mock endpoint and shut the tutorial cleanly.
In conclusion, now we have a Colab-ready Fara setup that demonstrates the principle browser-control pipeline with out requiring GPU assets or a reside mannequin server. We put in the framework, ready the browser runtime, safely inspected the motion setup, began a native mock endpoint, and ran the Fara CLI on a easy internet activity. The mock endpoint helps us confirm that the agent loop, endpoint format, and browser execution circulation are working earlier than shifting to a actual mannequin deployment. We additionally left the code structured so switching from mock mode to Fara-7B solely requires altering the endpoint settings.
Check out the Full Codes here. Also, be happy to comply with us on Twitter and don’t overlook to affix our 150k+ ML SubReddit and Subscribe to our Newsletter. Wait! are you on telegram? now you can join us on telegram as well.
Need to accomplice with us for selling your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar and many others.? Connect with us
The publish Microsoft Fara Tutorial: Run a Browser-Use Agent in Google Colab with a Mock OpenAI-Compatible Endpoint appeared first on MarkTechPost.
