Spaces:
Sleeping
Sleeping
File size: 7,817 Bytes
d7b3d84 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
"""
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('β
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('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('β
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())
|