Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fixed ruff linter issues
Sandeepbbb23f58cca7
diff --git a/browser_use/cli.py b/browser_use/cli.pyindex aeb6dd527..0cac1f1d6 100644--- a/browser_use/cli.py+++ b/browser_use/cli.py@@ -23,7 +23,6 @@ from dotenv import load_dotenvfrom browser_use.llm.anthropic.chat import ChatAnthropicfrom browser_use.llm.google.chat import ChatGoogle-from browser_use.llm.oci_raw.chat import ChatOCIRawfrom browser_use.llm.openai.chat import ChatOpenAIload_dotenv()@@ -248,7 +247,9 @@ def get_llm(config: dict[str, Any]):return ChatGoogle(model=model_name, temperature=temperature)elif model_name.startswith('oci'):# OCI models require additional configuration- print('⚠️ OCI models require manual configuration. Please use the ChatOCIRaw class directly with your OCI credentials.')+ print(+ '⚠️ OCI models require manual configuration. Please use the ChatOCIRaw class directly with your OCI credentials.'+ )sys.exit(1)# Auto-detect based on available API keysdiff --git a/browser_use/llm/models.py b/browser_use/llm/models.pyindex 66ffdf386..fbc4a0bfc 100644--- a/browser_use/llm/models.py+++ b/browser_use/llm/models.py@@ -113,7 +113,7 @@ def get_llm_by_name(model_name: str):elif provider == 'oci':# OCI requires more complex configuration that can't be easily inferred from env vars# Users should use ChatOCIRaw directly with proper configuration- raise ValueError(f"OCI models require manual configuration. Use ChatOCIRaw directly with your OCI credentials.")+ raise ValueError('OCI models require manual configuration. Use ChatOCIRaw directly with your OCI credentials.')else:available_providers = ['openai', 'azure', 'google', 'oci']diff --git a/browser_use/llm/oci_raw/chat.py b/browser_use/llm/oci_raw/chat.pyindex 49ff83f49..a0e60739a 100644--- a/browser_use/llm/oci_raw/chat.py+++ b/browser_use/llm/oci_raw/chat.py@@ -5,372 +5,358 @@ This module provides direct integration with Oracle Cloud Infrastructure'sGenerative AI service using raw API calls without Langchain dependencies."""-import jsonimport asyncio+import jsonfrom dataclasses import dataclass-from typing import Any, Dict, TypeVar, overload+from typing import TypeVar, overloadimport ocifrom oci.generative_ai_inference import GenerativeAiInferenceClientfrom oci.generative_ai_inference.models import (- ChatDetails,- GenericChatRequest,- CohereChatRequest,- OnDemandServingMode,- BaseChatRequest+ BaseChatRequest,+ ChatDetails,+ CohereChatRequest,+ GenericChatRequest,+ OnDemandServingMode,)from pydantic import BaseModelfrom browser_use.llm.base import BaseChatModelfrom browser_use.llm.exceptions import ModelProviderError, ModelRateLimitErrorfrom browser_use.llm.messages import BaseMessage-from .serializer import OCIRawMessageSerializerfrom browser_use.llm.schema import SchemaOptimizerfrom browser_use.llm.views import ChatInvokeCompletion, ChatInvokeUsage+from .serializer import OCIRawMessageSerializer+T = TypeVar('T', bound=BaseModel)@dataclassclass ChatOCIRaw(BaseChatModel):- """- A direct OCI Raw API integration for browser-use that bypasses Langchain.-- This class provides a browser-use compatible interface for OCI GenAI models- using direct API calls to Oracle Cloud Infrastructure.-- Args:- model_id: The OCI GenAI model OCID- service_endpoint: The OCI service endpoint URL- compartment_id: The OCI compartment OCID- provider: The model provider (e.g., "meta", "cohere", "xai")- temperature: Temperature for response generation (0.0-2.0) - supported by all providers- max_tokens: Maximum tokens in response - supported by all providers- frequency_penalty: Frequency penalty for response generation - supported by Meta and Cohere only- presence_penalty: Presence penalty for response generation - supported by Meta only- top_p: Top-p sampling parameter - supported by all providers- top_k: Top-k sampling parameter - supported by Cohere and xAI only- auth_type: Authentication type (e.g., "API_KEY")- auth_profile: Authentication profile name- timeout: Request timeout in seconds- """-- # Model configuration- model_id: str- service_endpoint: str- compartment_id: str- provider: str = "meta"-- # Model parameters- temperature: float | None = 1.0- max_tokens: int | None = 600- frequency_penalty: float | None = 0.0- presence_penalty: float | None = 0.0- top_p: float | None = 0.75- top_k: int | None = 0 # Used by Cohere models-- # Authentication- auth_type: str = "API_KEY"- auth_profile: str = "DEFAULT"-- # Client configuration- timeout: float = 60.0-- # Static properties- @property- def provider_name(self) -> str:- return 'oci-raw'-- @property- def name(self) -> str:- # Return a shorter name for telemetry (max 100 chars)- if len(self.model_id) > 90:- # Extract the model name from the OCID- parts = self.model_id.split('.')- if len(parts) >= 4:- return f"oci-{self.provider}-{parts[3]}" # e.g., "oci-meta-us-chicago-1"- else:- return f"oci-{self.provider}-model"- return self.model_id-- @property- def model(self) -> str:- return self.model_id-- @property- def model_name(self) -> str:- # Override for telemetry - return shorter name (max 100 chars)- if len(self.model_id) > 90:- # Extract the model name from the OCID- parts = self.model_id.split('.')- if len(parts) >= 4:- return f"oci-{self.provider}-{parts[3]}" # e.g., "oci-meta-us-chicago-1"- else:- return f"oci-{self.provider}-model"- return self.model_id-- def _uses_cohere_format(self) -> bool:- """Check if the provider uses Cohere chat request format."""- return self.provider.lower() == "cohere"-- def _get_supported_parameters(self) -> dict[str, bool]:- """Get which parameters are supported by the current provider."""- provider = self.provider.lower()- if provider == "meta":- return {- "temperature": True,- "max_tokens": True,- "frequency_penalty": True,- "presence_penalty": True,- "top_p": True,- "top_k": False- }- elif provider == "cohere":- return {- "temperature": True,- "max_tokens": True,- "frequency_penalty": True,- "presence_penalty": False,- "top_p": True,- "top_k": True- }- elif provider == "xai":- return {- "temperature": True,- "max_tokens": True,- "frequency_penalty": False,- "presence_penalty": False,- "top_p": True,- "top_k": True- }- else:- # Default: assume all parameters are supported- return {- "temperature": True,- "max_tokens": True,- "frequency_penalty": True,- "presence_penalty": True,- "top_p": True,- "top_k": True- }-- def _get_oci_client(self) -> GenerativeAiInferenceClient:- """Get the OCI GenerativeAiInferenceClient following your working example."""- if not hasattr(self, '_client'):- # Configure OCI client based on auth_type (following your working example)- if self.auth_type == "API_KEY":- config = oci.config.from_file('~/.oci/config', self.auth_profile)- self._client = GenerativeAiInferenceClient(- config=config,- service_endpoint=self.service_endpoint,- retry_strategy=oci.retry.NoneRetryStrategy(),- timeout=(10, 240) # Following your working example- )- elif self.auth_type == "INSTANCE_PRINCIPAL":- config = {}- signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner()- self._client = GenerativeAiInferenceClient(- config=config,- signer=signer,- service_endpoint=self.service_endpoint,- retry_strategy=oci.retry.NoneRetryStrategy(),- timeout=(10, 240)- )- elif self.auth_type == "RESOURCE_PRINCIPAL":- config = {}- signer = oci.auth.signers.get_resource_principals_signer()- self._client = GenerativeAiInferenceClient(- config=config,- signer=signer,- service_endpoint=self.service_endpoint,- retry_strategy=oci.retry.NoneRetryStrategy(),- timeout=(10, 240)- )- else:- # Fallback to API_KEY- config = oci.config.from_file('~/.oci/config', self.auth_profile)- self._client = GenerativeAiInferenceClient(- config=config,- service_endpoint=self.service_endpoint,- retry_strategy=oci.retry.NoneRetryStrategy(),- timeout=(10, 240)- )-- return self._client-- def _extract_usage(self, response) -> ChatInvokeUsage | None:- """Extract usage information from OCI response."""- try:- # The response is the direct OCI response object, not a dict- if hasattr(response, 'data') and hasattr(response.data, 'chat_response'):- chat_response = response.data.chat_response- if hasattr(chat_response, 'usage'):- usage = chat_response.usage- return ChatInvokeUsage(- prompt_tokens=getattr(usage, 'prompt_tokens', 0),- prompt_cached_tokens=None,- prompt_cache_creation_tokens=None,- prompt_image_tokens=None,- completion_tokens=getattr(usage, 'completion_tokens', 0),- total_tokens=getattr(usage, 'total_tokens', 0),- )- return None- except Exception:- return None-- def _extract_content(self, response) -> str:- """Extract text content from OCI response."""- try:- # The response is the direct OCI response object, not a dict- if not hasattr(response, 'data'):- raise ModelProviderError(- message="Invalid response format: no data attribute",- status_code=500,- model=self.name- )-- chat_response = response.data.chat_response-- # Handle different response types based on provider- if hasattr(chat_response, 'text'):- # Cohere response format - has direct text attribute- return chat_response.text or ''- elif hasattr(chat_response, 'choices') and chat_response.choices:- # Generic response format - has choices array (Meta, xAI)- choice = chat_response.choices[0]- message = choice.message- content_parts = message.content-- # Extract text from content parts- text_parts = []- for part in content_parts:- if hasattr(part, 'text'):- text_parts.append(part.text)-- return '\n'.join(text_parts) if text_parts else ''- else:- raise ModelProviderError(- message=f"Unsupported response format: {type(chat_response).__name__}",- status_code=500,- model=self.name- )-- except Exception as e:- raise ModelProviderError(- message=f"Failed to extract content from response: {str(e)}",- status_code=500,- model=self.name- ) from e-- async def _make_request(self, messages: list[BaseMessage]):- """Make async request to OCI API using proper OCI SDK models."""-- # Create chat request based on provider type- if self._uses_cohere_format():- # Cohere models use CohereChatRequest with single message string- message_text = OCIRawMessageSerializer.serialize_messages_for_cohere(messages)-- chat_request = CohereChatRequest()- chat_request.message = message_text- chat_request.max_tokens = self.max_tokens- chat_request.temperature = self.temperature- chat_request.frequency_penalty = self.frequency_penalty- chat_request.top_p = self.top_p- chat_request.top_k = self.top_k- else:- # Meta, xAI and other models use GenericChatRequest with messages array- oci_messages = OCIRawMessageSerializer.serialize_messages(messages)-- chat_request = GenericChatRequest()- chat_request.api_format = BaseChatRequest.API_FORMAT_GENERIC- chat_request.messages = oci_messages- chat_request.max_tokens = self.max_tokens- chat_request.temperature = self.temperature- chat_request.top_p = self.top_p-- # Provider-specific parameters- if self.provider.lower() == "meta":- # Meta models support frequency_penalty and presence_penalty- chat_request.frequency_penalty = self.frequency_penalty- chat_request.presence_penalty = self.presence_penalty- elif self.provider.lower() == "xai":- # xAI models support top_k but not frequency_penalty or presence_penalty- chat_request.top_k = self.top_k- else:- # Default: include all parameters for unknown providers- chat_request.frequency_penalty = self.frequency_penalty- chat_request.presence_penalty = self.presence_penalty-- # Create serving mode- serving_mode = OnDemandServingMode(model_id=self.model_id)-- # Create chat details- chat_details = ChatDetails()- chat_details.serving_mode = serving_mode- chat_details.chat_request = chat_request- chat_details.compartment_id = self.compartment_id-- # Make the request in a thread to avoid blocking- def _sync_request():- try:- client = self._get_oci_client()- response = client.chat(chat_details)- return response # Return the raw response object- except Exception as e:- # Handle OCI-specific exceptions- status_code = getattr(e, 'status', 500)- if status_code == 429:- raise ModelRateLimitError(- message=f"Rate limit exceeded: {str(e)}",- model=self.name- ) from e- else:- raise ModelProviderError(- message=str(e),- status_code=status_code,- model=self.name- ) from e-- # Run in thread pool to make it async- loop = asyncio.get_event_loop()- return await loop.run_in_executor(None, _sync_request)-- @overload- async def ainvoke(self, messages: list[BaseMessage], output_format: None = None) -> ChatInvokeCompletion[str]: ...-- @overload- async def ainvoke(self, messages: list[BaseMessage], output_format: type[T]) -> ChatInvokeCompletion[T]: ...-- async def ainvoke(- self, messages: list[BaseMessage], output_format: type[T] | None = None- ) -> ChatInvokeCompletion[T] | ChatInvokeCompletion[str]:- """- Invoke the OCI GenAI model with the given messages using raw API.-- Args:- messages: List of chat messages- output_format: Optional Pydantic model class for structured output-- Returns:- Either a string response or an instance of output_format- """- try:- if output_format is None:- # Return string response- response = await self._make_request(messages)- content = self._extract_content(response)- usage = self._extract_usage(response)-- return ChatInvokeCompletion(- completion=content,- usage=usage,- )- else:- # For structured output, add JSON schema instructions- optimized_schema = SchemaOptimizer.create_optimized_json_schema(output_format)-- # Add JSON schema instruction to messages- system_instruction = f"""+ """+ A direct OCI Raw API integration for browser-use that bypasses Langchain.++ This class provides a browser-use compatible interface for OCI GenAI models+ using direct API calls to Oracle Cloud Infrastructure.++ Args:+ model_id: The OCI GenAI model OCID+ service_endpoint: The OCI service endpoint URL+ compartment_id: The OCI compartment OCID+ provider: The model provider (e.g., "meta", "cohere", "xai")+ temperature: Temperature for response generation (0.0-2.0) - supported by all providers+ max_tokens: Maximum tokens in response - supported by all providers+ frequency_penalty: Frequency penalty for response generation - supported by Meta and Cohere only+ presence_penalty: Presence penalty for response generation - supported by Meta only+ top_p: Top-p sampling parameter - supported by all providers+ top_k: Top-k sampling parameter - supported by Cohere and xAI only+ auth_type: Authentication type (e.g., "API_KEY")+ auth_profile: Authentication profile name+ timeout: Request timeout in seconds+ """++ # Model configuration+ model_id: str+ service_endpoint: str+ compartment_id: str+ provider: str = 'meta'++ # Model parameters+ temperature: float | None = 1.0+ max_tokens: int | None = 600+ frequency_penalty: float | None = 0.0+ presence_penalty: float | None = 0.0+ top_p: float | None = 0.75+ top_k: int | None = 0 # Used by Cohere models++ # Authentication+ auth_type: str = 'API_KEY'+ auth_profile: str = 'DEFAULT'++ # Client configuration+ timeout: float = 60.0++ # Static properties+ @property+ def provider_name(self) -> str:+ return 'oci-raw'++ @property+ def name(self) -> str:+ # Return a shorter name for telemetry (max 100 chars)+ if len(self.model_id) > 90:+ # Extract the model name from the OCID+ parts = self.model_id.split('.')+ if len(parts) >= 4:+ return f'oci-{self.provider}-{parts[3]}' # e.g., "oci-meta-us-chicago-1"+ else:+ return f'oci-{self.provider}-model'+ return self.model_id++ @property+ def model(self) -> str:+ return self.model_id++ @property+ def model_name(self) -> str:+ # Override for telemetry - return shorter name (max 100 chars)+ if len(self.model_id) > 90:+ # Extract the model name from the OCID+ parts = self.model_id.split('.')+ if len(parts) >= 4:+ return f'oci-{self.provider}-{parts[3]}' # e.g., "oci-meta-us-chicago-1"+ else:+ return f'oci-{self.provider}-model'+ return self.model_id++ def _uses_cohere_format(self) -> bool:+ """Check if the provider uses Cohere chat request format."""+ return self.provider.lower() == 'cohere'++ def _get_supported_parameters(self) -> dict[str, bool]:+ """Get which parameters are supported by the current provider."""+ provider = self.provider.lower()+ if provider == 'meta':+ return {+ 'temperature': True,+ 'max_tokens': True,+ 'frequency_penalty': True,+ 'presence_penalty': True,+ 'top_p': True,+ 'top_k': False,+ }+ elif provider == 'cohere':+ return {+ 'temperature': True,+ 'max_tokens': True,+ 'frequency_penalty': True,+ 'presence_penalty': False,+ 'top_p': True,+ 'top_k': True,+ }+ elif provider == 'xai':+ return {+ 'temperature': True,+ 'max_tokens': True,+ 'frequency_penalty': False,+ 'presence_penalty': False,+ 'top_p': True,+ 'top_k': True,+ }+ else:+ # Default: assume all parameters are supported+ return {+ 'temperature': True,+ 'max_tokens': True,+ 'frequency_penalty': True,+ 'presence_penalty': True,+ 'top_p': True,+ 'top_k': True,+ }++ def _get_oci_client(self) -> GenerativeAiInferenceClient:+ """Get the OCI GenerativeAiInferenceClient following your working example."""+ if not hasattr(self, '_client'):+ # Configure OCI client based on auth_type (following your working example)+ if self.auth_type == 'API_KEY':+ config = oci.config.from_file('~/.oci/config', self.auth_profile)+ self._client = GenerativeAiInferenceClient(+ config=config,+ service_endpoint=self.service_endpoint,+ retry_strategy=oci.retry.NoneRetryStrategy(),+ timeout=(10, 240), # Following your working example+ )+ elif self.auth_type == 'INSTANCE_PRINCIPAL':+ config = {}+ signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner()+ self._client = GenerativeAiInferenceClient(+ config=config,+ signer=signer,+ service_endpoint=self.service_endpoint,+ retry_strategy=oci.retry.NoneRetryStrategy(),+ timeout=(10, 240),+ )+ elif self.auth_type == 'RESOURCE_PRINCIPAL':+ config = {}+ signer = oci.auth.signers.get_resource_principals_signer()+ self._client = GenerativeAiInferenceClient(+ config=config,+ signer=signer,+ service_endpoint=self.service_endpoint,+ retry_strategy=oci.retry.NoneRetryStrategy(),+ timeout=(10, 240),+ )+ else:+ # Fallback to API_KEY+ config = oci.config.from_file('~/.oci/config', self.auth_profile)+ self._client = GenerativeAiInferenceClient(+ config=config,+ service_endpoint=self.service_endpoint,+ retry_strategy=oci.retry.NoneRetryStrategy(),+ timeout=(10, 240),+ )++ return self._client++ def _extract_usage(self, response) -> ChatInvokeUsage | None:+ """Extract usage information from OCI response."""+ try:+ # The response is the direct OCI response object, not a dict+ if hasattr(response, 'data') and hasattr(response.data, 'chat_response'):+ chat_response = response.data.chat_response+ if hasattr(chat_response, 'usage'):+ usage = chat_response.usage+ return ChatInvokeUsage(+ prompt_tokens=getattr(usage, 'prompt_tokens', 0),+ prompt_cached_tokens=None,+ prompt_cache_creation_tokens=None,+ prompt_image_tokens=None,+ completion_tokens=getattr(usage, 'completion_tokens', 0),+ total_tokens=getattr(usage, 'total_tokens', 0),+ )+ return None+ except Exception:+ return None++ def _extract_content(self, response) -> str:+ """Extract text content from OCI response."""+ try:+ # The response is the direct OCI response object, not a dict+ if not hasattr(response, 'data'):+ raise ModelProviderError(message='Invalid response format: no data attribute', status_code=500, model=self.name)++ chat_response = response.data.chat_response++ # Handle different response types based on provider+ if hasattr(chat_response, 'text'):+ # Cohere response format - has direct text attribute+ return chat_response.text or ''+ elif hasattr(chat_response, 'choices') and chat_response.choices:+ # Generic response format - has choices array (Meta, xAI)+ choice = chat_response.choices[0]+ message = choice.message+ content_parts = message.content++ # Extract text from content parts+ text_parts = []+ for part in content_parts:+ if hasattr(part, 'text'):+ text_parts.append(part.text)++ return '\n'.join(text_parts) if text_parts else ''+ else:+ raise ModelProviderError(+ message=f'Unsupported response format: {type(chat_response).__name__}', status_code=500, model=self.name+ )++ except Exception as e:+ raise ModelProviderError(+ message=f'Failed to extract content from response: {str(e)}', status_code=500, model=self.name+ ) from e++ async def _make_request(self, messages: list[BaseMessage]):+ """Make async request to OCI API using proper OCI SDK models."""++ # Create chat request based on provider type+ if self._uses_cohere_format():+ # Cohere models use CohereChatRequest with single message string+ message_text = OCIRawMessageSerializer.serialize_messages_for_cohere(messages)++ chat_request = CohereChatRequest()+ chat_request.message = message_text+ chat_request.max_tokens = self.max_tokens+ chat_request.temperature = self.temperature+ chat_request.frequency_penalty = self.frequency_penalty+ chat_request.top_p = self.top_p+ chat_request.top_k = self.top_k+ else:+ # Meta, xAI and other models use GenericChatRequest with messages array+ oci_messages = OCIRawMessageSerializer.serialize_messages(messages)++ chat_request = GenericChatRequest()+ chat_request.api_format = BaseChatRequest.API_FORMAT_GENERIC+ chat_request.messages = oci_messages+ chat_request.max_tokens = self.max_tokens+ chat_request.temperature = self.temperature+ chat_request.top_p = self.top_p++ # Provider-specific parameters+ if self.provider.lower() == 'meta':+ # Meta models support frequency_penalty and presence_penalty+ chat_request.frequency_penalty = self.frequency_penalty+ chat_request.presence_penalty = self.presence_penalty+ elif self.provider.lower() == 'xai':+ # xAI models support top_k but not frequency_penalty or presence_penalty+ chat_request.top_k = self.top_k+ else:+ # Default: include all parameters for unknown providers+ chat_request.frequency_penalty = self.frequency_penalty+ chat_request.presence_penalty = self.presence_penalty++ # Create serving mode+ serving_mode = OnDemandServingMode(model_id=self.model_id)++ # Create chat details+ chat_details = ChatDetails()+ chat_details.serving_mode = serving_mode+ chat_details.chat_request = chat_request+ chat_details.compartment_id = self.compartment_id++ # Make the request in a thread to avoid blocking+ def _sync_request():+ try:+ client = self._get_oci_client()+ response = client.chat(chat_details)+ return response # Return the raw response object+ except Exception as e:+ # Handle OCI-specific exceptions+ status_code = getattr(e, 'status', 500)+ if status_code == 429:+ raise ModelRateLimitError(message=f'Rate limit exceeded: {str(e)}', model=self.name) from e+ else:+ raise ModelProviderError(message=str(e), status_code=status_code, model=self.name) from e++ # Run in thread pool to make it async+ loop = asyncio.get_event_loop()+ return await loop.run_in_executor(None, _sync_request)++ @overload+ async def ainvoke(self, messages: list[BaseMessage], output_format: None = None) -> ChatInvokeCompletion[str]: ...++ @overload+ async def ainvoke(self, messages: list[BaseMessage], output_format: type[T]) -> ChatInvokeCompletion[T]: ...++ async def ainvoke(+ self, messages: list[BaseMessage], output_format: type[T] | None = None+ ) -> ChatInvokeCompletion[T] | ChatInvokeCompletion[str]:+ """+ Invoke the OCI GenAI model with the given messages using raw API.++ Args:+ messages: List of chat messages+ output_format: Optional Pydantic model class for structured output++ Returns:+ Either a string response or an instance of output_format+ """+ try:+ if output_format is None:+ # Return string response+ response = await self._make_request(messages)+ content = self._extract_content(response)+ usage = self._extract_usage(response)++ return ChatInvokeCompletion(+ completion=content,+ usage=usage,+ )+ else:+ # For structured output, add JSON schema instructions+ optimized_schema = SchemaOptimizer.create_optimized_json_schema(output_format)++ # Add JSON schema instruction to messages+ system_instruction = f"""You must respond with ONLY a valid JSON object that matches this exact schema:{json.dumps(optimized_schema, indent=2)}@@ -381,76 +367,77 @@ IMPORTANT:- No extra fields are allowed- Use proper JSON syntax with double quotes"""-- # Clone messages and add system instruction- modified_messages = messages.copy()-- # Add or modify system message- from browser_use.llm.messages import SystemMessage- if modified_messages and hasattr(modified_messages[0], 'role') and modified_messages[0].role == 'system':- # Modify existing system message- existing_content = modified_messages[0].content- if isinstance(existing_content, str):- modified_messages[0].content = existing_content + "\n\n" + system_instruction- else:- # Handle list content- modified_messages[0].content = str(existing_content) + "\n\n" + system_instruction- else:- # Insert new system message at the beginning- modified_messages.insert(0, SystemMessage(content=system_instruction))-- response = await self._make_request(modified_messages)- response_text = self._extract_content(response)-- # Clean and parse the JSON response- try:- # Clean the response text- cleaned_text = response_text.strip()-- # Remove markdown code blocks if present- if cleaned_text.startswith('```json'):- cleaned_text = cleaned_text[7:]- if cleaned_text.startswith('```'):- cleaned_text = cleaned_text[3:]- if cleaned_text.endswith('```'):- cleaned_text = cleaned_text[:-3]-- cleaned_text = cleaned_text.strip()-- # Try to find JSON object in the response- if not cleaned_text.startswith('{'):- start_idx = cleaned_text.find('{')- end_idx = cleaned_text.rfind('}')- if start_idx != -1 and end_idx != -1 and end_idx > start_idx:- cleaned_text = cleaned_text[start_idx:end_idx + 1]-- # Parse the JSON- parsed_data = json.loads(cleaned_text)- parsed = output_format.model_validate(parsed_data)-- usage = self._extract_usage(response)- return ChatInvokeCompletion(- completion=parsed,- usage=usage,- )-- except (json.JSONDecodeError, ValueError) as e:- raise ModelProviderError(- message=f'Failed to parse structured output: {str(e)}. Response was: {response_text[:200]}...',- status_code=500,- model=self.name,- ) from e-- except ModelRateLimitError:- # Re-raise rate limit errors as-is- raise- except ModelProviderError:- # Re-raise provider errors as-is- raise- except Exception as e:- # Handle any other exceptions- raise ModelProviderError(- message=f"Unexpected error: {str(e)}",- status_code=500,- model=self.name,- ) from e++ # Clone messages and add system instruction+ modified_messages = messages.copy()++ # Add or modify system message+ from browser_use.llm.messages import SystemMessage++ if modified_messages and hasattr(modified_messages[0], 'role') and modified_messages[0].role == 'system':+ # Modify existing system message+ existing_content = modified_messages[0].content+ if isinstance(existing_content, str):+ modified_messages[0].content = existing_content + '\n\n' + system_instruction+ else:+ # Handle list content+ modified_messages[0].content = str(existing_content) + '\n\n' + system_instruction+ else:+ # Insert new system message at the beginning+ modified_messages.insert(0, SystemMessage(content=system_instruction))++ response = await self._make_request(modified_messages)+ response_text = self._extract_content(response)++ # Clean and parse the JSON response+ try:+ # Clean the response text+ cleaned_text = response_text.strip()++ # Remove markdown code blocks if present+ if cleaned_text.startswith('```json'):+ cleaned_text = cleaned_text[7:]+ if cleaned_text.startswith('```'):+ cleaned_text = cleaned_text[3:]+ if cleaned_text.endswith('```'):+ cleaned_text = cleaned_text[:-3]++ cleaned_text = cleaned_text.strip()++ # Try to find JSON object in the response+ if not cleaned_text.startswith('{'):+ start_idx = cleaned_text.find('{')+ end_idx = cleaned_text.rfind('}')+ if start_idx != -1 and end_idx != -1 and end_idx > start_idx:+ cleaned_text = cleaned_text[start_idx : end_idx + 1]++ # Parse the JSON+ parsed_data = json.loads(cleaned_text)+ parsed = output_format.model_validate(parsed_data)++ usage = self._extract_usage(response)+ return ChatInvokeCompletion(+ completion=parsed,+ usage=usage,+ )++ except (json.JSONDecodeError, ValueError) as e:+ raise ModelProviderError(+ message=f'Failed to parse structured output: {str(e)}. Response was: {response_text[:200]}...',+ status_code=500,+ model=self.name,+ ) from e++ except ModelRateLimitError:+ # Re-raise rate limit errors as-is+ raise+ except ModelProviderError:+ # Re-raise provider errors as-is+ raise+ except Exception as e:+ # Handle any other exceptions+ raise ModelProviderError(+ message=f'Unexpected error: {str(e)}',+ status_code=500,+ model=self.name,+ ) from ediff --git a/browser_use/llm/oci_raw/serializer.py b/browser_use/llm/oci_raw/serializer.pyindex 150ca6a7e..a0a5dd02c 100644--- a/browser_use/llm/oci_raw/serializer.py+++ b/browser_use/llm/oci_raw/serializer.py@@ -7,217 +7,218 @@ and the OCI Raw API message format using proper OCI SDK models.from typing import List-from oci.generative_ai_inference.models import Message, TextContent, ImageContent, ImageUrl+from oci.generative_ai_inference.models import ImageContent, ImageUrl, Message, TextContent+from browser_use.llm.messages import (- BaseMessage,- UserMessage,- SystemMessage,- AssistantMessage,- ContentPartImageParam,+ AssistantMessage,+ BaseMessage,+ ContentPartImageParam,+ SystemMessage,+ UserMessage,)class OCIRawMessageSerializer:- """- Serializer for converting between browser-use message types and OCI Raw API message formats.- Uses proper OCI SDK model objects as shown in the working example.-- Supports both:- - GenericChatRequest (Meta, xAI models) - uses messages array- - CohereChatRequest (Cohere models) - uses single message string- """-- @staticmethod- def _is_base64_image(url: str) -> bool:- """Check if the URL is a base64 encoded image."""- return url.startswith('data:image/')-- @staticmethod- def _parse_base64_url(url: str) -> str:- """Parse base64 URL and return the base64 data."""- if not OCIRawMessageSerializer._is_base64_image(url):- raise ValueError(f"Not a base64 image URL: {url}")-- # Extract the base64 data from data:image/png;base64,<data>- try:- header, data = url.split(',', 1)- return data- except ValueError:- raise ValueError(f"Invalid base64 image URL format: {url}")-- @staticmethod- def _create_image_content(part: ContentPartImageParam) -> ImageContent:- """Convert ContentPartImageParam to OCI ImageContent."""- url = part.image_url.url-- if OCIRawMessageSerializer._is_base64_image(url):- # Handle base64 encoded images - OCI expects data URLs as-is- image_url = ImageUrl(url=url)- else:- # Handle regular URLs- image_url = ImageUrl(url=url)-- return ImageContent(image_url=image_url)-- @staticmethod- def serialize_messages(messages: List[BaseMessage]) -> List[Message]:- """- Serialize a list of browser-use messages to OCI Raw API Message objects.-- Args:- messages: List of browser-use messages-- Returns:- List of OCI Message objects- """- oci_messages = []-- for message in messages:- oci_message = Message()-- if isinstance(message, UserMessage):- oci_message.role = "USER"- content = message.content- if isinstance(content, str):- text_content = TextContent()- text_content.text = content- oci_message.content = [text_content]- elif isinstance(content, list):- # Handle content parts - text and images- contents = []- for part in content:- if part.type == 'text':- text_content = TextContent()- text_content.text = part.text- contents.append(text_content)- elif part.type == 'image_url':- image_content = OCIRawMessageSerializer._create_image_content(part)- contents.append(image_content)- if contents:- oci_message.content = contents-- elif isinstance(message, SystemMessage):- oci_message.role = "SYSTEM"- content = message.content- if isinstance(content, str):- text_content = TextContent()- text_content.text = content- oci_message.content = [text_content]- elif isinstance(content, list):- # Handle content parts - typically just text for system messages- contents = []- for part in content:- if part.type == 'text':- text_content = TextContent()- text_content.text = part.text- contents.append(text_content)- elif part.type == 'image_url':- # System messages can theoretically have images too- image_content = OCIRawMessageSerializer._create_image_content(part)- contents.append(image_content)- if contents:- oci_message.content = contents-- elif isinstance(message, AssistantMessage):- oci_message.role = "ASSISTANT"- content = message.content- if isinstance(content, str):- text_content = TextContent()- text_content.text = content- oci_message.content = [text_content]- elif isinstance(content, list):- # Handle content parts - text, images, and refusals- contents = []- for part in content:- if part.type == 'text':- text_content = TextContent()- text_content.text = part.text- contents.append(text_content)- elif part.type == 'image_url':- # Assistant messages can have images in responses- image_content = OCIRawMessageSerializer._create_image_content(part)- contents.append(image_content)- elif part.type == 'refusal':- text_content = TextContent()- text_content.text = f"[Refusal] {part.refusal}"- contents.append(text_content)- if contents:- oci_message.content = contents- else:- # Fallback for any message format issues- oci_message.role = "USER"- text_content = TextContent()- text_content.text = str(message)- oci_message.content = [text_content]-- oci_messages.append(oci_message)-- return oci_messages-- @staticmethod- def serialize_messages_for_cohere(messages: List[BaseMessage]) -> str:- """- Serialize messages for Cohere models which expect a single message string.-- Cohere models use CohereChatRequest.message (string) instead of messages array.- We combine all messages into a single conversation string.-- Args:- messages: List of browser-use messages-- Returns:- Single string containing the conversation- """- conversation_parts = []-- for message in messages:- content = ""-- if isinstance(message, UserMessage):- if isinstance(message.content, str):- content = message.content- elif isinstance(message.content, list):- # Extract text from content parts- text_parts = []- for part in message.content:- if part.type == 'text':- text_parts.append(part.text)- elif part.type == 'image_url':- # Cohere may not support images in all models, but include a placeholder- text_parts.append(f"[Image: {part.image_url.url}]")- content = ' '.join(text_parts)-- conversation_parts.append(f"User: {content}")-- elif isinstance(message, SystemMessage):- if isinstance(message.content, str):- content = message.content- elif isinstance(message.content, list):- # Extract text from content parts- text_parts = []- for part in message.content:- if part.type == 'text':- text_parts.append(part.text)- content = ' '.join(text_parts)-- conversation_parts.append(f"System: {content}")-- elif isinstance(message, AssistantMessage):- if isinstance(message.content, str):- content = message.content- elif isinstance(message.content, list):- # Extract text from content parts- text_parts = []- for part in message.content:- if part.type == 'text':- text_parts.append(part.text)- elif part.type == 'refusal':- text_parts.append(f"[Refusal] {part.refusal}")- content = ' '.join(text_parts)-- conversation_parts.append(f"Assistant: {content}")- else:- # Fallback- conversation_parts.append(f"User: {str(message)}")-- return '\n\n'.join(conversation_parts)+ """+ Serializer for converting between browser-use message types and OCI Raw API message formats.+ Uses proper OCI SDK model objects as shown in the working example.++ Supports both:+ - GenericChatRequest (Meta, xAI models) - uses messages array+ - CohereChatRequest (Cohere models) - uses single message string+ """++ @staticmethod+ def _is_base64_image(url: str) -> bool:+ """Check if the URL is a base64 encoded image."""+ return url.startswith('data:image/')++ @staticmethod+ def _parse_base64_url(url: str) -> str:+ """Parse base64 URL and return the base64 data."""+ if not OCIRawMessageSerializer._is_base64_image(url):+ raise ValueError(f'Not a base64 image URL: {url}')++ # Extract the base64 data from data:image/png;base64,<data>+ try:+ header, data = url.split(',', 1)+ return data+ except ValueError:+ raise ValueError(f'Invalid base64 image URL format: {url}')++ @staticmethod+ def _create_image_content(part: ContentPartImageParam) -> ImageContent:+ """Convert ContentPartImageParam to OCI ImageContent."""+ url = part.image_url.url++ if OCIRawMessageSerializer._is_base64_image(url):+ # Handle base64 encoded images - OCI expects data URLs as-is+ image_url = ImageUrl(url=url)+ else:+ # Handle regular URLs+ image_url = ImageUrl(url=url)++ return ImageContent(image_url=image_url)++ @staticmethod+ def serialize_messages(messages: List[BaseMessage]) -> List[Message]:+ """+ Serialize a list of browser-use messages to OCI Raw API Message objects.++ Args:+ messages: List of browser-use messages++ Returns:+ List of OCI Message objects+ """+ oci_messages = []++ for message in messages:+ oci_message = Message()++ if isinstance(message, UserMessage):+ oci_message.role = 'USER'+ content = message.content+ if isinstance(content, str):+ text_content = TextContent()+ text_content.text = content+ oci_message.content = [text_content]+ elif isinstance(content, list):+ # Handle content parts - text and images+ contents = []+ for part in content:+ if part.type == 'text':+ text_content = TextContent()+ text_content.text = part.text+ contents.append(text_content)+ elif part.type == 'image_url':+ image_content = OCIRawMessageSerializer._create_image_content(part)+ contents.append(image_content)+ if contents:+ oci_message.content = contents++ elif isinstance(message, SystemMessage):+ oci_message.role = 'SYSTEM'+ content = message.content+ if isinstance(content, str):+ text_content = TextContent()+ text_content.text = content+ oci_message.content = [text_content]+ elif isinstance(content, list):+ # Handle content parts - typically just text for system messages+ contents = []+ for part in content:+ if part.type == 'text':+ text_content = TextContent()+ text_content.text = part.text+ contents.append(text_content)+ elif part.type == 'image_url':+ # System messages can theoretically have images too+ image_content = OCIRawMessageSerializer._create_image_content(part)+ contents.append(image_content)+ if contents:+ oci_message.content = contents++ elif isinstance(message, AssistantMessage):+ oci_message.role = 'ASSISTANT'+ content = message.content+ if isinstance(content, str):+ text_content = TextContent()+ text_content.text = content+ oci_message.content = [text_content]+ elif isinstance(content, list):+ # Handle content parts - text, images, and refusals+ contents = []+ for part in content:+ if part.type == 'text':+ text_content = TextContent()+ text_content.text = part.text+ contents.append(text_content)+ elif part.type == 'image_url':+ # Assistant messages can have images in responses+ image_content = OCIRawMessageSerializer._create_image_content(part)+ contents.append(image_content)+ elif part.type == 'refusal':+ text_content = TextContent()+ text_content.text = f'[Refusal] {part.refusal}'+ contents.append(text_content)+ if contents:+ oci_message.content = contents+ else:+ # Fallback for any message format issues+ oci_message.role = 'USER'+ text_content = TextContent()+ text_content.text = str(message)+ oci_message.content = [text_content]++ oci_messages.append(oci_message)++ return oci_messages++ @staticmethod+ def serialize_messages_for_cohere(messages: List[BaseMessage]) -> str:+ """+ Serialize messages for Cohere models which expect a single message string.++ Cohere models use CohereChatRequest.message (string) instead of messages array.+ We combine all messages into a single conversation string.++ Args:+ messages: List of browser-use messages++ Returns:+ Single string containing the conversation+ """+ conversation_parts = []++ for message in messages:+ content = ''++ if isinstance(message, UserMessage):+ if isinstance(message.content, str):+ content = message.content+ elif isinstance(message.content, list):+ # Extract text from content parts+ text_parts = []+ for part in message.content:+ if part.type == 'text':+ text_parts.append(part.text)+ elif part.type == 'image_url':+ # Cohere may not support images in all models, but include a placeholder+ text_parts.append(f'[Image: {part.image_url.url}]')+ content = ' '.join(text_parts)++ conversation_parts.append(f'User: {content}')++ elif isinstance(message, SystemMessage):+ if isinstance(message.content, str):+ content = message.content+ elif isinstance(message.content, list):+ # Extract text from content parts+ text_parts = []+ for part in message.content:+ if part.type == 'text':+ text_parts.append(part.text)+ content = ' '.join(text_parts)++ conversation_parts.append(f'System: {content}')++ elif isinstance(message, AssistantMessage):+ if isinstance(message.content, str):+ content = message.content+ elif isinstance(message.content, list):+ # Extract text from content parts+ text_parts = []+ for part in message.content:+ if part.type == 'text':+ text_parts.append(part.text)+ elif part.type == 'refusal':+ text_parts.append(f'[Refusal] {part.refusal}')+ content = ' '.join(text_parts)++ conversation_parts.append(f'Assistant: {content}')+ else:+ # Fallback+ conversation_parts.append(f'User: {str(message)}')++ return '\n\n'.join(conversation_parts)diff --git a/browser_use/llm/tests/test_chat_models.py b/browser_use/llm/tests/test_chat_models.pyindex 9533e237a..e2a4d65d6 100644--- a/browser_use/llm/tests/test_chat_models.py+++ b/browser_use/llm/tests/test_chat_models.py@@ -4,9 +4,8 @@ import pytestfrom pydantic import BaseModelfrom browser_use.llm import ChatAnthropic, ChatGoogle, ChatGroq, ChatOpenAI, ChatOpenRouter-from browser_use.llm.oci_raw.chat import ChatOCIRawfrom browser_use.llm.messages import ContentPartTextParam-from examples.models.oci_models import meta_llm, cohere_llm, xai_llm+from examples.models.oci_models import xai_llmclass CapitalResponse(BaseModel):@@ -256,11 +255,12 @@ class TestChatModels:# Skip if OCI credentials not available - check for config file existencetry:import oci+oci.config.from_file('~/.oci/config', 'DEFAULT')except Exception:pytest.skip('OCI credentials not available')-- return xai_llm #xai or cohere++ return xai_llm # xai or cohere@pytest.mark.asyncioasync def test_oci_raw_ainvoke_normal(self, oci_raw_chat):diff --git a/browser_use/llm/tests/test_single_step.py b/browser_use/llm/tests/test_single_step.pyindex 1254b1801..271326dd9 100644--- a/browser_use/llm/tests/test_single_step.py+++ b/browser_use/llm/tests/test_single_step.py@@ -25,6 +25,7 @@ def _check_oci_credentials() -> bool:"""Check if OCI credentials are available."""try:import oci+oci.config.from_file('~/.oci/config', 'DEFAULT')return Trueexcept Exception:@@ -124,11 +125,11 @@ def create_mock_state_message(temp_dir: str):(ChatAnthropic, 'claude-3-5-sonnet-latest'),(ChatAzureOpenAI, 'gpt-4.1-mini'),pytest.param(- ChatOCIRaw,+ ChatOCIRaw,{'model_id': 'ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyarojgfh6msa452vziycwfymle5gxdvpwwxzara53topmq','service_endpoint': 'https://inference.generativeai.us-chicago-1.oci.oraclecloud.com',- 'compartment_id': 'ocid1.tenancy.oc1..aaaaaaaayeiis5uk2..your_compartment_id',+ 'compartment_id': 'ocid1.tenancy.oc1..aaaaaaaayeiis5uk2nuubznrekd6xsm56k3m4i7tyvkxmr2ftojqfkpx2ura','provider': 'meta','temperature': 0.7,'max_tokens': 800,@@ -136,12 +137,9 @@ def create_mock_state_message(temp_dir: str):'presence_penalty': 0.0,'top_p': 0.9,'auth_type': 'API_KEY',- 'auth_profile': 'DEFAULT'+ 'auth_profile': 'DEFAULT',},- marks=pytest.mark.skipif(- not _check_oci_credentials(),- reason="OCI credentials not available"- )+ marks=pytest.mark.skipif(not _check_oci_credentials(), reason='OCI credentials not available'),),],)diff --git a/browser_use/tokens/tests/test_cost.py b/browser_use/tokens/tests/test_cost.pyindex 18a8af782..dc9c403e4 100644--- a/browser_use/tokens/tests/test_cost.py+++ b/browser_use/tokens/tests/test_cost.py@@ -11,12 +11,13 @@ from browser_use import ChatGooglefrom browser_use.llm import ChatOpenAIfrom browser_use.llm.messages import AssistantMessage, SystemMessage, UserMessagefrom browser_use.tokens.service import TokenCost-from examples.models.oci_models import cohere_llm, xai_llm, meta_llm+from examples.models.oci_models import meta_llm# Optional OCI import for testingtry:- from browser_use.llm.oci_raw.chat import ChatOCIRaw- OCI_AVAILABLE = True+ import importlib.util++ OCI_AVAILABLE = importlib.util.find_spec('browser_use.llm.oci_raw.chat') is not Noneexcept ImportError:OCI_AVAILABLE = False@@ -28,7 +29,7 @@ def get_oci_model_if_available():"""Create OCI model for testing if credentials are available."""if not OCI_AVAILABLE:return None-+# Try to create OCI model with mock/test configuration# These values should be replaced with real ones if testing with actual OCItry:@@ -36,7 +37,7 @@ def get_oci_model_if_available():return meta_llmexcept Exception as e:- logger.info(f"OCI model not available for testing: {e}")+ logger.info(f'OCI model not available for testing: {e}')return None@@ -56,7 +57,7 @@ Only output the country name, no numbers, no punctuation, just the name."""models = []models.append(ChatOpenAI(model='gpt-4.1')) # Commented out - requires OPENAI_API_KEYmodels.append(ChatGoogle(model='gemini-2.0-flash-exp'))-+# Add OCI model if availableoci_model = get_oci_model_if_available()if oci_model:diff --git a/examples/models/oci_models.py b/examples/models/oci_models.pyindex f97bc9127..148c12838 100644--- a/examples/models/oci_models.py+++ b/examples/models/oci_models.py@@ -18,6 +18,7 @@ Requirements:import asyncioimport osimport sys+from pydantic import BaseModelsys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))@@ -27,235 +28,226 @@ from browser_use.llm import ChatOCIRawclass SearchSummary(BaseModel):- query: str- results_found: int- top_result_title: str- summary: str- relevance_score: float-+ query: str+ results_found: int+ top_result_title: str+ summary: str+ relevance_score: float# Configuration examples for different providers-# Replace these with your actual OCI values-compartment_id = "ocid1.tenancy.oc1..aaaaaaaayour-compartment-id-here"-endpoint = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com"+compartment_id = 'ocid1.tenancy.oc1..aaaaaaaayeiis5uk2nuubznrekd6xsm56k3m4i7tyvkxmr2ftojqfkpx2ura'+endpoint = 'https://inference.generativeai.us-chicago-1.oci.oraclecloud.com'# Example 1: Meta Llama model (uses GenericChatRequest)-# Replace with your actual Meta model OCID-meta_model_id = "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyayour-meta-model-id-here"+meta_model_id = 'ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyarojgfh6msa452vziycwfymle5gxdvpwwxzara53topmq'meta_llm = ChatOCIRaw(- model_id=meta_model_id,- service_endpoint=endpoint,- compartment_id=compartment_id,- provider="meta", # Meta Llama model- temperature=0.7,- max_tokens=800,- frequency_penalty=0.0,- presence_penalty=0.0,- top_p=0.9,- auth_type="API_KEY",- auth_profile="DEFAULT"+ model_id=meta_model_id,+ service_endpoint=endpoint,+ compartment_id=compartment_id,+ provider='meta', # Meta Llama model+ temperature=0.7,+ max_tokens=800,+ frequency_penalty=0.0,+ presence_penalty=0.0,+ top_p=0.9,+ auth_type='API_KEY',+ auth_profile='DEFAULT',)-# Replace with your actual Cohere model OCID-cohere_model_id = "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyayour-cohere-model-id-here"+cohere_model_id = 'ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyanrlpnq5ybfu5hnzarg7jomak3q6kyhkzjsl4qj24fyoq'# Example 2: Cohere model (uses CohereChatRequest)+# cohere_model_id = "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyapnibwg42qjhwaxrlqfpreueirtwghiwvv2whsnwmnlva"cohere_llm = ChatOCIRaw(- model_id=cohere_model_id,- service_endpoint=endpoint,- compartment_id=compartment_id,- provider="cohere", # Cohere model- temperature=1.0,- max_tokens=600,- frequency_penalty=0.0,- top_p=0.75,- top_k=0, # Cohere-specific parameter- auth_type="API_KEY",- auth_profile="DEFAULT"+ model_id=cohere_model_id,+ service_endpoint=endpoint,+ compartment_id=compartment_id,+ provider='cohere', # Cohere model+ temperature=1.0,+ max_tokens=600,+ frequency_penalty=0.0,+ top_p=0.75,+ top_k=0, # Cohere-specific parameter+ auth_type='API_KEY',+ auth_profile='DEFAULT',)# Example 3: xAI model (uses GenericChatRequest)-# Replace with your actual xAI model OCID-xai_model_id = "ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceyayour-xai-model-id-here"+xai_model_id = 'ocid1.generativeaimodel.oc1.us-chicago-1.amaaaaaask7dceya3bsfz4ogiuv3yc7gcnlry7gi3zzx6tnikg6jltqszm2q'xai_llm = ChatOCIRaw(- model_id=xai_model_id,- service_endpoint=endpoint,- compartment_id=compartment_id,- provider="xai", # xAI model- temperature=1.0,- max_tokens=20000,- top_p=1.0,- top_k=0,- auth_type="API_KEY",- auth_profile="DEFAULT"+ model_id=xai_model_id,+ service_endpoint=endpoint,+ compartment_id=compartment_id,+ provider='xai', # xAI model+ temperature=1.0,+ max_tokens=20000,+ top_p=1.0,+ top_k=0,+ auth_type='API_KEY',+ auth_profile='DEFAULT',)# Use Meta model by default for this examplellm = xai_llm+async def basic_example():- """Basic example using ChatOCIRaw with a simple task."""- print('🔹 Basic ChatOCIRaw Example')- print('=' * 40)+ """Basic example using ChatOCIRaw with a simple task."""+ print('🔹 Basic ChatOCIRaw Example')+ print('=' * 40)+ print(f'Model: {llm.name}')+ print(f'Provider: {llm.provider_name}')- print(f'Model: {llm.name}')- print(f'Provider: {llm.provider_name}')+ # Create agent with a simple task+ agent = Agent(+ task="Go to google.com and search for 'Oracle Cloud Infrastructure pricing'",+ llm=llm,+ )- # Create agent with a simple task- agent = Agent(- task="Go to google.com and search for 'Oracle Cloud Infrastructure pricing'",- llm=llm,- )+ print("Task: Go to google.com and search for 'Oracle Cloud Infrastructure pricing'")- print("Task: Go to google.com and search for 'Oracle Cloud Infrastructure pricing'")-- # Run the agent- try:- result = await agent.run(max_steps=5)- print(f'✅ Task completed successfully!')- print(f'Final result: {result}')- except Exception as e:- print(f'❌ Error: {e}')+ # Run the agent+ try:+ result = await agent.run(max_steps=5)+ print('✅ Task completed successfully!')+ print(f'Final result: {result}')+ except Exception as e:+ print(f'❌ Error: {e}')async def structured_output_example():- """Example demonstrating structured output with Pydantic models."""- print('\n🔹 Structured Output Example')- print('=' * 40)--- # Create agent that will return structured data- agent = Agent(- task="""Go to github.com, search for 'browser automation python',+ """Example demonstrating structured output with Pydantic models."""+ print('\n🔹 Structured Output Example')+ print('=' * 40)++ # Create agent that will return structured data+ agent = Agent(+ task="""Go to github.com, search for 'browser automation python',find the most popular repository, and return structured information about it""",- llm=llm,- output_format=SearchSummary # This will enforce structured output- )-- print("Task: Search GitHub for browser automation and return structured data")-- try:- result = await agent.run(max_steps=5)-- if isinstance(result, SearchSummary):- print('✅ Structured output received!')- print(f'Query: {result.query}')- print(f'Results Found: {result.results_found}')- print(f'Top Result: {result.top_result_title}')- print(f'Summary: {result.summary}')- print(f'Relevance Score: {result.relevance_score}')- else:- print(f'Result: {result}')-- except Exception as e:- print(f'❌ Error: {e}')+ llm=llm,+ output_format=SearchSummary, # This will enforce structured output+ )++ print('Task: Search GitHub for browser automation and return structured data')++ try:+ result = await agent.run(max_steps=5)++ if isinstance(result, SearchSummary):+ print('✅ Structured output received!')+ print(f'Query: {result.query}')+ print(f'Results Found: {result.results_found}')+ print(f'Top Result: {result.top_result_title}')+ print(f'Summary: {result.summary}')+ print(f'Relevance Score: {result.relevance_score}')+ else:+ print(f'Result: {result}')++ except Exception as e:+ print(f'❌ Error: {e}')async def advanced_configuration_example():- """Example showing advanced configuration options."""- print('\n🔹 Advanced Configuration Example')- print('=' * 40)--- print(f'Model: {llm.name}')- print(f'Provider: {llm.provider_name}')- print(f'Configuration: Cohere model with instance principal auth')-- # Create agent with a more complex task- agent = Agent(- task="""Navigate to stackoverflow.com, search for questions about 'python web scraping' and tap search help,+ """Example showing advanced configuration options."""+ print('\n🔹 Advanced Configuration Example')+ print('=' * 40)++ print(f'Model: {llm.name}')+ print(f'Provider: {llm.provider_name}')+ print('Configuration: Cohere model with instance principal auth')++ # Create agent with a more complex task+ agent = Agent(+ task="""Navigate to stackoverflow.com, search for questions about 'python web scraping' and tap search help,analyze the top 3 questions, and provide a detailed summary of common challenges""",- llm=llm,- )+ llm=llm,+ )- print("Task: Analyze StackOverflow questions about Python web scraping")-- try:- result = await agent.run(max_steps=8)- print(f'✅ Advanced task completed!')- print(f'Analysis result: {result}')- except Exception as e:- print(f'❌ Error: {e}')+ print('Task: Analyze StackOverflow questions about Python web scraping')++ try:+ result = await agent.run(max_steps=8)+ print('✅ Advanced task completed!')+ print(f'Analysis result: {result}')+ except Exception as e:+ print(f'❌ Error: {e}')async def provider_compatibility_test():- """Test different provider formats to verify compatibility."""- print('\n🔹 Provider Compatibility Test')- print('=' * 40)-- providers_to_test = [- ("Meta", meta_llm),- ("Cohere", cohere_llm),- ("xAI", xai_llm)- ]-- for provider_name, model in providers_to_test:- print(f'\nTesting {provider_name} model...')- print(f'Model ID: {model.model_id}')- print(f'Provider: {model.provider}')- print(f'Uses Cohere format: {model._uses_cohere_format()}')-- # Create a simple agent to test the model- agent = Agent(- task="Go to google.com and tell me what you see",- llm=model,- )-- try:- result = await agent.run(max_steps=3)- print(f'✅ {provider_name} model works correctly!')- print(f'Result: {str(result)[:100]}...')- except Exception as e:- print(f'❌ {provider_name} model failed: {e}')+ """Test different provider formats to verify compatibility."""+ print('\n🔹 Provider Compatibility Test')+ print('=' * 40)++ providers_to_test = [('Meta', meta_llm), ('Cohere', cohere_llm), ('xAI', xai_llm)]++ for provider_name, model in providers_to_test:+ print(f'\nTesting {provider_name} model...')+ print(f'Model ID: {model.model_id}')+ print(f'Provider: {model.provider}')+ print(f'Uses Cohere format: {model._uses_cohere_format()}')++ # Create a simple agent to test the model+ agent = Agent(+ task='Go to google.com and tell me what you see',+ llm=model,+ )++ try:+ result = await agent.run(max_steps=3)+ print(f'✅ {provider_name} model works correctly!')+ print(f'Result: {str(result)[:100]}...')+ except Exception as e:+ print(f'❌ {provider_name} model failed: {e}')+async def main():- """Run all OCI Raw examples."""- print('🚀 Oracle Cloud Infrastructure (OCI) Raw API Examples')- print('=' * 60)-- print('\n📋 Prerequisites:')- print('1. OCI account with Generative AI service access')- print('2. OCI configuration file at ~/.oci/config')- print('3. Model deployed in your OCI compartment')- print('4. Proper IAM permissions for Generative AI')- print('5. OCI Python SDK installed: uv add oci')- print('=' * 60)-- print('\n⚙️ Configuration Notes:')- print('• Update model_id, service_endpoint, and compartment_id with your values')- print('• Supported providers: "meta", "cohere", "xai"')- print('• Auth types: "API_KEY", "INSTANCE_PRINCIPAL", "RESOURCE_PRINCIPAL"')- print('• Default OCI config profile: "DEFAULT"')- print('=' * 60)-- print('\n🔧 Provider-Specific API Formats:')- print('• Meta/xAI models: Use GenericChatRequest with messages array')- print('• Cohere models: Use CohereChatRequest with single message string')- print('• The integration automatically detects and uses the correct format')- print('=' * 60)-- try:- # Run all examples- await basic_example()- await structured_output_example()- await advanced_configuration_example()- # await provider_compatibility_test()-- print('\n🎉 All examples completed successfully!')-- except Exception as e:- print(f'\n❌ Example failed: {e}')- print('\n🔧 Troubleshooting:')- print('• Verify OCI configuration: oci setup config')- print('• Check model OCID and availability')- print('• Ensure compartment access and IAM permissions')- print('• Verify service endpoint URL')- print('• Check OCI Python SDK installation')- print('• Ensure you\'re using the correct provider name in ChatOCIRaw')+ """Run all OCI Raw examples."""+ print('🚀 Oracle Cloud Infrastructure (OCI) Raw API Examples')+ print('=' * 60)++ print('\n📋 Prerequisites:')+ print('1. OCI account with Generative AI service access')+ print('2. OCI configuration file at ~/.oci/config')+ print('3. Model deployed in your OCI compartment')+ print('4. Proper IAM permissions for Generative AI')+ print('5. OCI Python SDK installed: uv add oci')+ print('=' * 60)++ print('\n⚙️ Configuration Notes:')+ print('• Update model_id, service_endpoint, and compartment_id with your values')+ print('• Supported providers: "meta", "cohere", "xai"')+ print('• Auth types: "API_KEY", "INSTANCE_PRINCIPAL", "RESOURCE_PRINCIPAL"')+ print('• Default OCI config profile: "DEFAULT"')+ print('=' * 60)++ print('\n🔧 Provider-Specific API Formats:')+ print('• Meta/xAI models: Use GenericChatRequest with messages array')+ print('• Cohere models: Use CohereChatRequest with single message string')+ print('• The integration automatically detects and uses the correct format')+ print('=' * 60)++ try:+ # Run all examples+ await basic_example()+ await structured_output_example()+ await advanced_configuration_example()+ # await provider_compatibility_test()++ print('\n🎉 All examples completed successfully!')++ except Exception as e:+ print(f'\n❌ Example failed: {e}')+ print('\n🔧 Troubleshooting:')+ print('• Verify OCI configuration: oci setup config')+ print('• Check model OCID and availability')+ print('• Ensure compartment access and IAM permissions')+ print('• Verify service endpoint URL')+ print('• Check OCI Python SDK installation')+ print("• Ensure you're using the correct provider name in ChatOCIRaw")if __name__ == '__main__':- asyncio.run(main())+ asyncio.run(main())