Skip to content

Commit

Permalink
Using enums for browser for extendabilty
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammedNagdy committed Jul 30, 2024
1 parent bd45a68 commit 65b096c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
4 changes: 2 additions & 2 deletions autogen/agentchat/contrib/web_surfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing_extensions import Annotated

from ... import Agent, AssistantAgent, ConversableAgent, GroupChat, GroupChatManager, OpenAIWrapper, UserProxyAgent
from ...browser_utils.browser_creator import TextBrowserCreator
from ...browser_utils.browser_creator import TextBrowserEnum
from ...code_utils import content_str
from ...oai.openai_utils import filter_config
from ...token_count_utils import count_token, get_max_token_limit
Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(

# Create the browser
self.browser_name = browser_name
self.browser = TextBrowserCreator.create_browser(self.browser_name)
self.browser = TextBrowserEnum.get_browser(browser_name)

inner_llm_config = copy.deepcopy(llm_config)

Expand Down
23 changes: 9 additions & 14 deletions autogen/browser_utils/browser_creator.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
from bing_browser import BingTextBrowser
from google_browser import GoogleTextBrowser
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"""
class TextBrowserEnum:
"""Enum class for creating different text browsers. Make sure to add newly registered browsers here"""

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

@classmethod
def create_browser(cls, name: str):
"""Factory method to create a text browser instance based on the name."""
if name in cls.browser_classes:
return cls.browser_classes[name]()
else:
raise ValueError(f"Unknown browser name: {name}")
def get_browser(cls, browser_str):
return getattr(cls, browser_str)

0 comments on commit 65b096c

Please sign in to comment.