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

    15 Best Vibe Coding Tools in 2026 Compared: Pricing, Features, and Best Fit

    AI-first development is changing how software gets built. A new approach called “vibe coding” sits at the center of that shift. Developers describe what they want in plain language. An…

    Perplexity AI Introduces Hybrid Local-Server Inference Orchestrator for Personal Computer: Automatic On-Device and Cloud Task Routing

    Perplexity AI announced what it calls the first hybrid local-server inference orchestrator at Computex 2026. The system is designed to automatically route AI tasks between a user’s local device and…

    Leave a Reply

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