Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Integrated Oracle ChatOciRaw

Sandeepe2b1c3b668e3
diff --git a/browser_use/llm/oci_raw/chat.py b/browser_use/llm/oci_raw/chat.py
index afe6982e3..49ff83f49 100644
--- a/browser_use/llm/oci_raw/chat.py
+++ b/browser_use/llm/oci_raw/chat.py
@@ -15,6 +15,7 @@ from oci.generative_ai_inference import GenerativeAiInferenceClient
from oci.generative_ai_inference.models import (
ChatDetails,
GenericChatRequest,
+ CohereChatRequest,
OnDemandServingMode,
BaseChatRequest
)
@@ -42,12 +43,13 @@ class ChatOCIRaw(BaseChatModel):
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" ,"grok")
- temperature: Temperature for response generation (0.0-2.0)
- max_tokens: Maximum tokens in response
- frequency_penalty: Frequency penalty for response generation
- presence_penalty: Presence penalty for response generation
- top_p: Top-p sampling parameter
+ 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
@@ -65,6 +67,7 @@ class ChatOCIRaw(BaseChatModel):
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"
@@ -106,6 +109,51 @@ class ChatOCIRaw(BaseChatModel):
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'):
@@ -182,25 +230,31 @@ class ChatOCIRaw(BaseChatModel):
)
chat_response = response.data.chat_response
- if not hasattr(chat_response, 'choices') or not chat_response.choices:
+
+ # 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="No choices in response",
+ message=f"Unsupported response format: {type(chat_response).__name__}",
status_code=500,
model=self.name
)
- 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 ''
-
except Exception as e:
raise ModelProviderError(
message=f"Failed to extract content from response: {str(e)}",
@@ -210,18 +264,42 @@ class ChatOCIRaw(BaseChatModel):
async def _make_request(self, messages: list[BaseMessage]):
"""Make async request to OCI API using proper OCI SDK models."""
- # Serialize messages to OCI Message objects
- oci_messages = OCIRawMessageSerializer.serialize_messages(messages)
- # Create chat request using proper OCI SDK models (following your working example)
- 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.frequency_penalty = self.frequency_penalty
- chat_request.presence_penalty = self.presence_penalty
- chat_request.top_p = self.top_p
+ # 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)
diff --git a/browser_use/llm/oci_raw/serializer.py b/browser_use/llm/oci_raw/serializer.py
index 4ac3b90e9..150ca6a7e 100644
--- a/browser_use/llm/oci_raw/serializer.py
+++ b/browser_use/llm/oci_raw/serializer.py
@@ -21,6 +21,10 @@ 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
@@ -149,3 +153,71 @@ class OCIRawMessageSerializer:
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.py
index eedf6d663..9533e237a 100644
--- a/browser_use/llm/tests/test_chat_models.py
+++ b/browser_use/llm/tests/test_chat_models.py
@@ -6,6 +6,7 @@ from pydantic import BaseModel
from browser_use.llm import ChatAnthropic, ChatGoogle, ChatGroq, ChatOpenAI, ChatOpenRouter
from browser_use.llm.oci_raw.chat import ChatOCIRaw
from browser_use.llm.messages import ContentPartTextParam
+from examples.models.oci_models import meta_llm, cohere_llm, xai_llm
class CapitalResponse(BaseModel):
@@ -259,19 +260,7 @@ class TestChatModels:
except Exception:
pytest.skip('OCI credentials not available')
- return 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..aaaaaaaayeiis5uk2nuubznrekd6xsm56k3m4i7tyvkxmr2ftojqfkpx2ura',
- provider='meta',
- 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'
- )
+ return xai_llm #xai or cohere
@pytest.mark.asyncio
async def test_oci_raw_ainvoke_normal(self, oci_raw_chat):
diff --git a/browser_use/tokens/tests/test_cost.py b/browser_use/tokens/tests/test_cost.py
index e58885141..83f874edc 100644
--- a/browser_use/tokens/tests/test_cost.py
+++ b/browser_use/tokens/tests/test_cost.py
@@ -10,6 +10,7 @@ import logging
from browser_use.llm import ChatOpenAI
from browser_use.llm.messages import AssistantMessage, SystemMessage, UserMessage
from browser_use.tokens.service import TokenCost
+from examples.models.oci_models import cohere_llm, xai_llm, meta_llm
# Optional OCI import for testing
try:
@@ -30,19 +31,8 @@ def get_oci_model_if_available():
# Try to create OCI model with mock/test configuration
# These values should be replaced with real ones if testing with actual OCI
try:
- return 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..aaaaaaaayeiis5uk2nuubznrekd6xsm56k3m4i7tyvkxmr2ftojqfkpx2ura",
- provider="meta",
- 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"
- )
+ # get any of the llm xai_llm or cohere_llm
+ return meta_llm
except Exception as e:
logger.info(f"OCI model not available for testing: {e}")
@@ -64,7 +54,7 @@ Only output the country name, no numbers, no punctuation, just the name."""
# Test with different models
models = []
models.append(ChatOpenAI(model='gpt-4.1')) # Commented out - requires OPENAI_API_KEY
- # models.append(ChatGoogle(model='gemini-2.0-flash-exp'))
+ models.append(ChatGoogle(model='gemini-2.0-flash-exp'))
# Add OCI model if available
oci_model = get_oci_model_if_available()
diff --git a/examples/models/oci_models.py b/examples/models/oci_models.py
new file mode 100644
index 000000000..2883d8378
--- /dev/null
+++ b/examples/models/oci_models.py
@@ -0,0 +1,258 @@
+"""
+Oracle Cloud Infrastructure (OCI) Raw API Example
+
+This example demonstrates how to use OCI's Generative AI service with browser-use
+using the raw API integration (ChatOCIRaw) without Langchain dependencies.
+
+@dev You need to:
+1. Set up OCI configuration file at ~/.oci/config
+2. Have access to OCI Generative AI models in your tenancy
+3. Install the OCI Python SDK: uv add oci
+
+Requirements:
+- OCI account with Generative AI service access
+- Proper OCI configuration and authentication
+- Model deployment in your OCI compartment
+"""
+
+import asyncio
+import os
+import sys
+from pydantic import BaseModel
+
+sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
+
+from browser_use import Agent
+from browser_use.llm import ChatOCIRaw
+
+
+class SearchSummary(BaseModel):
+ query: str
+ results_found: int
+ top_result_title: str
+ summary: str
+ relevance_score: float
+
+
+
+# Configuration examples for different providers
+compartment_id = "ocid1.tenancy.oc1..aaaaaaaayeiis5uk2nuubznrekd6xsm56k3m4i7tyvkxmr2ftojqfkpx2ura"
+endpoint = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com"
+
+# Example 1: Meta Llama model (uses GenericChatRequest)
+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"
+)
+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"
+)
+
+# Example 3: xAI model (uses GenericChatRequest)
+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"
+)
+
+# Use Meta model by default for this example
+llm = xai_llm
+
+async def basic_example():
+ """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}')
+
+ # 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'")
+
+ # 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}')
+
+
+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',
+ 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}')
+
+
+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,
+ analyze the top 3 questions, and provide a detailed summary of common challenges""",
+ 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}')
+
+
+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}')
+
+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')
+
+
+if __name__ == '__main__':
+ asyncio.run(main())