Skip to content

Commit 0ad4d5c

Browse files
committed
Check responding api_key is it loaded properly with load dotenv
1 parent 0ed9e52 commit 0ad4d5c

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

examples/ui/command_line.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
"""
2+
To Use it:
3+
4+
Example 1: Using OpenAI (default), with default task: 'go to reddit and search for posts about browser-use'
5+
python command_line.py
6+
7+
Example 2: Using OpenAI with a Custom Query
8+
python command_line.py --query "go to google and search for browser-use"
9+
10+
Example 3: Using Anthropic's Claude Model with a Custom Query
11+
python command_line.py --query "find latest Python tutorials on Medium" --provider anthropic
12+
13+
"""
114
import os
215
import sys
316
import argparse
@@ -6,22 +19,31 @@
619
# Ensure local repository (browser_use) is accessible
720
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
821

9-
# Third-party imports
10-
from langchain_openai import ChatOpenAI
22+
from dotenv import load_dotenv
1123

12-
# Local module imports
1324
from browser_use import Agent
1425
from browser_use.browser.browser import Browser, BrowserConfig
1526
from browser_use.controller.service import Controller
1627

1728

29+
load_dotenv()
30+
1831
def get_llm(provider: str):
1932
if provider == 'anthropic':
2033
from langchain_anthropic import ChatAnthropic
34+
api_key = os.getenv("ANTHROPIC_API_KEY")
35+
if not api_key:
36+
raise ValueError("Error: ANTHROPIC_API_KEY is not set. Please provide a valid API key.")
37+
2138
return ChatAnthropic(
2239
model_name='claude-3-5-sonnet-20240620', timeout=25, stop=None, temperature=0.0
2340
)
2441
elif provider == 'openai':
42+
from langchain_openai import ChatOpenAI
43+
api_key = os.getenv("OPENAI_API_KEY")
44+
if not api_key:
45+
raise ValueError("Error: OPENAI_API_KEY is not set. Please provide a valid API key.")
46+
2547
return ChatOpenAI(model='gpt-4o', temperature=0.0)
2648

2749
else:

examples/ui/gradio_demo.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import asyncio
21
import os
2+
import asyncio
33
from dataclasses import dataclass
44
from typing import List, Optional
55

6+
# Third-party imports
67
import gradio as gr
78
from dotenv import load_dotenv
89
from langchain_openai import ChatOpenAI
910
from rich.console import Console
1011
from rich.panel import Panel
1112
from rich.text import Text
1213

14+
# Local module imports
1315
from browser_use import Agent
1416

1517
load_dotenv()

0 commit comments

Comments
 (0)