Microsoft Fara Tutorial: Run a Browser-Use Agent in Google Colab with a Mock OpenAI-Compatible Endpoint


mock_server_code = r'''
from fastapi import FastAPI, Request
import time
app = FastAPI()
STATE = {"calls": 0}
@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
   payload = await request.json()
   STATE["calls"] += 1
   model_name = payload.get("model", "mock-fara-7b")
   if STATE["calls"] == 1:
       content = (
           "I will open a stable public test page so the browser-control loop can be demonstrated.\n"
           "{\"name\":\"computer\",\"arguments\":{\"action\":\"visit_url\",\"url\":\"https://example.com\"}}"
       )
   else:
       content = (
           "The browser has opened Example Domain, a stable demonstration page used for documentation and examples.\n"
           "{\"name\":\"computer\",\"arguments\":{\"action\":\"terminate\",\"status\":\"success\"}}"
       )
   return {
       "id": f"chatcmpl-mock-{STATE['calls']}",
       "object": "chat.completion",
       "created": int(time.time()),
       "model": model_name,
       "choices": [
           {
               "index": 0,
               "message": {
                   "role": "assistant",
                   "content": content
               },
               "finish_reason": "stop"
           }
       ],
       "usage": {
           "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 = {
       "model": REAL_FARA_MODEL,
       "base_url": REAL_FARA_BASE_URL,
       "api_key": REAL_FARA_API_KEY,
   }
else:
   endpoint_config = {
       "model": "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,
       text=True,
   )
   if not wait_for_port("127.0.0.1", 8001, timeout=60):
       if mock_process and mock_process.stdout:
           print(mock_process.stdout.read())
       raise RuntimeError("Mock endpoint did not start on port 8001.")
   print("Mock endpoint is running at http://127.0.0.1:8001/v1")
else:
   print("\nUsing real Fara endpoint. Make sure it is reachable.")



Source link

  • Related Posts

    KwaiKAT Team Releases KAT-Coder-V2.5: An Agentic Coding Model Trained on 100,000+ Verifiable Repository Environments

    The KwaiKAT Team at Kuaishou has introduced the KAT-Coder-V2.5. It is a coding model trained to operate inside real, executable repositories rather than emit single-turn code. The served model is…

    Induction Labs Photon-1 Simulates Desktops, Plays Checkers, and Models Billiard Physics From One Pretraining Run

    Most agents that learn from video need to know what action produced each frame. Induction Labs is arguing that this requirement is the bottleneck. Last week, they released imagination models,…

    Leave a Reply

    Your email address will not be published. Required fields are marked *