|
| 1 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 14 | +import sys |
| 15 | +import pathlib |
| 16 | +from dotenv import load_dotenv |
| 17 | +from camel.models import ModelFactory |
| 18 | +from camel.toolkits import ( |
| 19 | + AudioAnalysisToolkit, |
| 20 | + CodeExecutionToolkit, |
| 21 | + ExcelToolkit, |
| 22 | + ImageAnalysisToolkit, |
| 23 | + SearchToolkit, |
| 24 | + VideoAnalysisToolkit, |
| 25 | + BrowserToolkit, |
| 26 | + FileWriteToolkit, |
| 27 | +) |
| 28 | +from camel.types import ModelPlatformType, ModelType |
| 29 | +from camel.logger import set_log_level |
| 30 | +from camel.societies import RolePlaying |
| 31 | + |
| 32 | +from owl.utils import run_society, DocumentProcessingToolkit |
| 33 | + |
| 34 | +base_dir = pathlib.Path(__file__).parent.parent |
| 35 | +env_path = base_dir / "owl" / ".env" |
| 36 | +load_dotenv(dotenv_path=str(env_path)) |
| 37 | + |
| 38 | +set_log_level(level="DEBUG") |
| 39 | + |
| 40 | + |
| 41 | +def construct_society(question: str) -> RolePlaying: |
| 42 | + r"""Construct a society of agents based on the given question. |
| 43 | +
|
| 44 | + Args: |
| 45 | + question (str): The task or question to be addressed by the society. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + RolePlaying: A configured society of agents ready to address the question. |
| 49 | + """ |
| 50 | + |
| 51 | + # Create models for different components |
| 52 | + models = { |
| 53 | + "user": ModelFactory.create( |
| 54 | + model_platform=ModelPlatformType.GEMINI, |
| 55 | + model_type=ModelType.GEMINI_2_5_PRO_EXP, |
| 56 | + model_config_dict={"temperature": 0}, |
| 57 | + ), |
| 58 | + "assistant": ModelFactory.create( |
| 59 | + model_platform=ModelPlatformType.GEMINI, |
| 60 | + model_type=ModelType.GEMINI_2_5_PRO_EXP, |
| 61 | + model_config_dict={"temperature": 0}, |
| 62 | + ), |
| 63 | + "browsing": ModelFactory.create( |
| 64 | + model_platform=ModelPlatformType.GEMINI, |
| 65 | + model_type=ModelType.GEMINI_2_5_PRO_EXP, |
| 66 | + model_config_dict={"temperature": 0}, |
| 67 | + ), |
| 68 | + "planning": ModelFactory.create( |
| 69 | + model_platform=ModelPlatformType.GEMINI, |
| 70 | + model_type=ModelType.GEMINI_2_5_PRO_EXP, |
| 71 | + model_config_dict={"temperature": 0}, |
| 72 | + ), |
| 73 | + "video": ModelFactory.create( |
| 74 | + model_platform=ModelPlatformType.GEMINI, |
| 75 | + model_type=ModelType.GEMINI_2_5_PRO_EXP, |
| 76 | + model_config_dict={"temperature": 0}, |
| 77 | + ), |
| 78 | + "image": ModelFactory.create( |
| 79 | + model_platform=ModelPlatformType.GEMINI, |
| 80 | + model_type=ModelType.GEMINI_2_5_PRO_EXP, |
| 81 | + model_config_dict={"temperature": 0}, |
| 82 | + ), |
| 83 | + "document": ModelFactory.create( |
| 84 | + model_platform=ModelPlatformType.GEMINI, |
| 85 | + model_type=ModelType.GEMINI_2_5_PRO_EXP, |
| 86 | + model_config_dict={"temperature": 0}, |
| 87 | + ), |
| 88 | + } |
| 89 | + |
| 90 | + # Configure toolkits |
| 91 | + tools = [ |
| 92 | + *BrowserToolkit( |
| 93 | + headless=False, # Set to True for headless mode (e.g., on remote servers) |
| 94 | + web_agent_model=models["browsing"], |
| 95 | + planning_agent_model=models["planning"], |
| 96 | + ).get_tools(), |
| 97 | + *CodeExecutionToolkit(sandbox="subprocess", verbose=True).get_tools(), |
| 98 | + *ImageAnalysisToolkit(model=models["image"]).get_tools(), |
| 99 | + SearchToolkit().search_duckduckgo, |
| 100 | + SearchToolkit().search_google, # Comment this out if you don't have google search |
| 101 | + SearchToolkit().search_wiki, |
| 102 | + *ExcelToolkit().get_tools(), |
| 103 | + *DocumentProcessingToolkit(model=models["document"]).get_tools(), |
| 104 | + *FileWriteToolkit(output_dir="./").get_tools(), |
| 105 | + ] |
| 106 | + |
| 107 | + # Configure agent roles and parameters |
| 108 | + user_agent_kwargs = {"model": models["user"]} |
| 109 | + assistant_agent_kwargs = {"model": models["assistant"], "tools": tools} |
| 110 | + |
| 111 | + # Configure task parameters |
| 112 | + task_kwargs = { |
| 113 | + "task_prompt": question, |
| 114 | + "with_task_specify": False, |
| 115 | + } |
| 116 | + |
| 117 | + # Create and return the society |
| 118 | + society = RolePlaying( |
| 119 | + **task_kwargs, |
| 120 | + user_role_name="user", |
| 121 | + user_agent_kwargs=user_agent_kwargs, |
| 122 | + assistant_role_name="assistant", |
| 123 | + assistant_agent_kwargs=assistant_agent_kwargs, |
| 124 | + ) |
| 125 | + |
| 126 | + return society |
| 127 | + |
| 128 | + |
| 129 | +def main(): |
| 130 | + r"""Main function to run the OWL system with an example question.""" |
| 131 | + # Default research question |
| 132 | + default_task = "Navigate to Amazon.com and identify one product that is attractive to coders. Please provide me with the product name and price. No need to verify your answer." |
| 133 | + |
| 134 | + # Override default task if command line argument is provided |
| 135 | + task = sys.argv[1] if len(sys.argv) > 1 else default_task |
| 136 | + |
| 137 | + # Construct and run the society |
| 138 | + society = construct_society(task) |
| 139 | + answer, chat_history, token_count = run_society(society) |
| 140 | + |
| 141 | + # Output the result |
| 142 | + print(f"\033[94mAnswer: {answer}\033[0m") |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments