Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Search API #3049

Open
wants to merge 11 commits into
base: 0.2
Choose a base branch
from
10 changes: 3 additions & 7 deletions autogen/agentchat/contrib/web_surfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
from typing_extensions import Annotated

from ... import Agent, AssistantAgent, ConversableAgent, GroupChat, GroupChatManager, OpenAIWrapper, UserProxyAgent
from ...browser_utils.bing_browser import BingTextBrowser
from ...browser_utils.google_broswer import GoogleTextBrowser
from ...browser_utils.browser_creator import TextBrowserCreator
from ...code_utils import content_str
from ...oai.openai_utils import filter_config
from ...token_count_utils import count_token, get_max_token_limit

logger = logging.getLogger(__name__)

BROWSERS = {"google": GoogleTextBrowser, "bing": BingTextBrowser}


class WebSurferAgent(ConversableAgent):
"""(In preview) An agent that acts as a basic web surfer that can search the web and visit web pages."""
Expand All @@ -43,7 +40,7 @@ def __init__(
llm_config: Optional[Union[Dict, Literal[False]]] = None,
summarizer_llm_config: Optional[Union[Dict, Literal[False]]] = None,
default_auto_reply: Optional[Union[str, Dict, None]] = "",
browser_name: str = "bing",
browser_name: Literal['bing', 'google'] = "bing",
browser_config: Optional[Union[Dict, None]] = None,
):
super().__init__(
Expand All @@ -63,8 +60,7 @@ def __init__(

# Create the browser
self.browser_name = browser_name
chosen_browser = BROWSERS[self.browser_name]
self.browser = chosen_browser(**(browser_config if browser_config else {}))
self.browser = TextBrowserCreator.create_browser(self.browser_name)

inner_llm_config = copy.deepcopy(llm_config)

Expand Down
2 changes: 1 addition & 1 deletion autogen/browser_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
pass


class SimpleTextBrowser:
class TextBrowserBase:
"""(In preview) An extremely simple text-based web browser comparable to Lynx. Suitable for Agentic use."""

def __init__(
Expand Down
2 changes: 1 addition & 1 deletion autogen/browser_utils/base_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
pass


class SimpleTextBrowser:
class TextBrowserBase:
"""(In preview) An extremely simple text-based web browser comparable to Lynx. Suitable for Agentic use."""

def __init__(
Expand Down
6 changes: 4 additions & 2 deletions autogen/browser_utils/bing_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import requests

from .base_browser import SimpleTextBrowser
from .base_browser import TextBrowserBase


class BingTextBrowser(SimpleTextBrowser):
class BingTextBrowser(TextBrowserBase):
"""(In preview) An extremely simple text-based web browser comparable to Lynx. Suitable for Agentic use."""

def __init__(
Expand All @@ -19,6 +19,8 @@ def __init__(
request_kwargs: Optional[Union[Dict[str, Any], None]] = None,
):
super().__init__(start_page, viewport_size, downloads_folder, base_url, api_key, request_kwargs)
self.name = 'bing'


def set_address(self, uri_or_path: str) -> None:
self.history.append(uri_or_path)
Expand Down
19 changes: 19 additions & 0 deletions autogen/browser_utils/browser_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from bing_browser import BingTextBrowser
from google_browser import GoogleTextBrowser


class TextBrowserCreator:
"""Creator class for creating different text browsers at runtime. Make sure to add newly registered browsers here"""

browser_classes = {
'BingTextBrowser': BingTextBrowser,
'GoogleTextBrowser': GoogleTextBrowser
}

@classmethod
def create_browser(cls, name: str):
MohammedNagdy marked this conversation as resolved.
Show resolved Hide resolved
"""Factory method to create a text browser instance based on the name."""
if name in cls.browser_classes:
return cls.browser_classes[name]()
MohammedNagdy marked this conversation as resolved.
Show resolved Hide resolved
else:
raise ValueError(f"Unknown browser name: {name}")
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import requests

from .base_browser import SimpleTextBrowser
from .base_browser import TextBrowserBase


class GoogleTextBrowser(SimpleTextBrowser):
class GoogleTextBrowser(TextBrowserBase):
"""(In preview) An extremely simple text-based web browser comparable to Lynx. Suitable for Agentic use."""

def __init__(
Expand All @@ -22,6 +22,7 @@ def __init__(
):
super().__init__(start_page, viewport_size, downloads_folder, base_url, api_key, request_kwargs)
self.cx = cx
self.name = 'google'

def set_address(self, uri_or_path: str) -> None:
self.history.append(uri_or_path)
Expand Down
4 changes: 2 additions & 2 deletions test/test_browser_utils_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import requests
from agentchat.test_assistant_agent import KEY_LOC # noqa: E402

from autogen.browser_utils.google_broswer import GoogleTextBrowser
from autogen.browser_utils.google_browser import GoogleTextBrowser

BLOG_POST_URL = "https://microsoft.github.io/autogen/blog/2023/04/21/LLM-tuning-math"
rysweet marked this conversation as resolved.
Show resolved Hide resolved
BLOG_POST_TITLE = "Does Model and Inference Parameter Matter in LLM Applications? - A Case Study for MATH | AutoGen"
Expand All @@ -36,7 +36,7 @@


try:
from autogen.browser_utils.google_broswer import GoogleTextBrowser
from autogen.browser_utils.google_browser import GoogleTextBrowser
except ImportError:
skip_all = True
else:
Expand Down