You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

multimodal_browser_chat.py 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import os
  2. from autogen import Agent, ConversableAgent, OpenAIWrapper, config_list_from_json
  3. from autogen.agentchat.contrib.multimodal_web_surfer import MultimodalWebSurferAgent
  4. from autogen.code_utils import content_str
  5. from typing import Any, Dict, List, Optional, Union, Callable, Literal, Tuple
  6. def main():
  7. # Load LLM inference endpoints from an env variable or a file
  8. # See https://microsoft.github.io/autogen/docs/FAQ#set-your-api-endpoints
  9. # and OAI_CONFIG_LIST_sample.
  10. # For example, if you have created a OAI_CONFIG_LIST file in the current working directory, that file will be used.
  11. # NOTE: In this case, the LLM needs to be vision-capable
  12. llm_config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST", filter_dict={"tags": ["mlm"]})
  13. web_surfer = MultimodalWebSurferAgent(
  14. "web_surfer",
  15. llm_config={"config_list": llm_config_list},
  16. is_termination_msg=lambda x: x.get("content", "").rstrip().find("TERMINATE") >= 0,
  17. headless=True,
  18. chromium_channel="chromium",
  19. chromium_data_dir=None,
  20. start_page="https://www.bing.com/",
  21. debug_dir=os.path.join(os.getcwd(), "debug"),
  22. )
  23. mmagent = MultimodalAgent(
  24. "assistant",
  25. system_message="You are a general-purpose AI assistant and can handle many questions -- but you don't have access to a we boweser. However, the user you are talking to does have a browser, and you can see the screen. Provide short direct instructions to them to take you where you need to go to answer the initial question posed to you.",
  26. llm_config={"config_list": llm_config_list},
  27. human_input_mode="ALWAYS",
  28. is_termination_msg=lambda x: str(x.get("content", "")).find("TERMINATE") >= 0,
  29. )
  30. web_surfer.initiate_chat(mmagent, message="How can I help you today?")
  31. class MultimodalAgent(ConversableAgent):
  32. def __init__(
  33. self,
  34. name: str,
  35. **kwargs,
  36. ):
  37. super().__init__(
  38. name=name,
  39. **kwargs,
  40. )
  41. self._reply_func_list = []
  42. self.register_reply([Agent, None], MultimodalAgent.generate_mlm_reply)
  43. self.register_reply([Agent, None], ConversableAgent.generate_code_execution_reply)
  44. self.register_reply([Agent, None], ConversableAgent.generate_function_call_reply)
  45. self.register_reply([Agent, None], ConversableAgent.check_termination_and_human_reply)
  46. def generate_mlm_reply(
  47. self,
  48. messages: Optional[List[Dict[str, str]]] = None,
  49. sender: Optional[Agent] = None,
  50. config: Optional[OpenAIWrapper] = None,
  51. ) -> Tuple[bool, Optional[Union[str, Dict[str, str]]]]:
  52. """Generate a reply using autogen.oai."""
  53. if messages is None:
  54. messages = self._oai_messages[sender]
  55. # Clone the messages to give context, but remove old screenshots
  56. history = []
  57. for i in range(0, len(messages) - 1):
  58. message = {}
  59. message.update(messages[i])
  60. message["content"] = content_str(message["content"])
  61. history.append(message)
  62. history.append(messages[-1])
  63. response = self.client.create(messages=self._oai_system_message + history)
  64. completion = self.client.extract_text_or_completion_object(response)[0]
  65. return True, completion
  66. if __name__ == "__main__":
  67. main()