|
| 1 | +# Copyright (C) 2024 Intel Corporation |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | + |
| 5 | +import os |
| 6 | + |
| 7 | +from browser_use import Agent, BrowserProfile |
| 8 | +from comps import opea_microservices, register_microservice |
| 9 | +from comps.cores.telemetry.opea_telemetry import opea_telemetry |
| 10 | +from fastapi import Request |
| 11 | +from langchain_openai import ChatOpenAI |
| 12 | +from pydantic import BaseModel, SecretStr |
| 13 | + |
| 14 | +LLM = None |
| 15 | +BROWSER_PROFILE = None |
| 16 | +LLM_ENDPOINT = os.getenv("LLM_ENDPOINT", "http://0.0.0.0:8008") |
| 17 | +LLM_MODEL = os.getenv("LLM_MODEL", "Qwen/Qwen2.5-VL-32B-Instruct") |
| 18 | + |
| 19 | + |
| 20 | +def initiate_llm_and_browser(llm_endpoint: str, model: str, secret_key: str = "sk-xxxxxx"): |
| 21 | + # Initialize global LLM and BrowserProfile if not already initialized |
| 22 | + global LLM, BROWSER_PROFILE |
| 23 | + if not LLM: |
| 24 | + LLM = ChatOpenAI(base_url=f"{llm_endpoint}/v1", model=model, api_key=SecretStr(secret_key), temperature=0.1) |
| 25 | + if not BROWSER_PROFILE: |
| 26 | + BROWSER_PROFILE = BrowserProfile( |
| 27 | + headless=True, |
| 28 | + chromium_sandbox=False, |
| 29 | + ) |
| 30 | + return LLM, BROWSER_PROFILE |
| 31 | + |
| 32 | + |
| 33 | +class BrowserUseRequest(BaseModel): |
| 34 | + task_prompt: str |
| 35 | + use_vision: bool = True |
| 36 | + secret_key: str = "sk-xxxxxx" |
| 37 | + llm_endpoint: str = LLM_ENDPOINT |
| 38 | + llm_model: str = LLM_MODEL |
| 39 | + agent_max_steps: int = 10 |
| 40 | + |
| 41 | + |
| 42 | +class BrowserUseResponse(BaseModel): |
| 43 | + is_success: bool = False |
| 44 | + model: str |
| 45 | + task_prompt: str |
| 46 | + use_vision: bool |
| 47 | + agent_researched_urls: list[str] = [] |
| 48 | + agent_actions: list[str] = [] |
| 49 | + agent_durations: float |
| 50 | + agent_steps: int |
| 51 | + final_result: str |
| 52 | + |
| 53 | + |
| 54 | +@register_microservice( |
| 55 | + name="opea_service@browser_use_agent", |
| 56 | + endpoint="/v1/browser_use_agent", |
| 57 | + host="0.0.0.0", |
| 58 | + port=8022, |
| 59 | +) |
| 60 | +@opea_telemetry |
| 61 | +async def run(request: Request): |
| 62 | + data = await request.json() |
| 63 | + chat_request = BrowserUseRequest.model_validate(data) |
| 64 | + llm, browser_profile = initiate_llm_and_browser( |
| 65 | + llm_endpoint=chat_request.llm_endpoint, model=chat_request.llm_model, secret_key=chat_request.secret_key |
| 66 | + ) |
| 67 | + agent = Agent( |
| 68 | + task=chat_request.task_prompt, |
| 69 | + llm=llm, |
| 70 | + use_vision=chat_request.use_vision, |
| 71 | + enable_memory=False, |
| 72 | + browser_profile=browser_profile, |
| 73 | + ) |
| 74 | + history = await agent.run(max_steps=chat_request.agent_max_steps) |
| 75 | + |
| 76 | + return BrowserUseResponse( |
| 77 | + is_success=history.is_successful() if history.is_successful() is not None else False, |
| 78 | + model=chat_request.llm_model, |
| 79 | + task_prompt=chat_request.task_prompt, |
| 80 | + use_vision=chat_request.use_vision, |
| 81 | + agent_researched_urls=history.urls(), |
| 82 | + agent_actions=history.action_names(), |
| 83 | + agent_durations=round(history.total_duration_seconds(), 3), |
| 84 | + agent_steps=history.number_of_steps(), |
| 85 | + final_result=history.final_result() if history.is_successful() else f"Task failed: {history.errors()}", |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + opea_microservices["opea_service@browser_use_agent"].start() |
0 commit comments