|
| 1 | +from dotenv import load_dotenv |
| 2 | + |
| 3 | +from camel.models import ModelFactory |
| 4 | +from camel.toolkits import ( |
| 5 | + ExcelToolkit, |
| 6 | + SearchToolkit, |
| 7 | + FileWriteToolkit, |
| 8 | + CodeExecutionToolkit, |
| 9 | + BrowserToolkit, |
| 10 | + VideoAnalysisToolkit, |
| 11 | + ImageAnalysisToolkit, |
| 12 | +) |
| 13 | +from camel.types import ModelPlatformType, ModelType |
| 14 | +from camel.societies import RolePlaying |
| 15 | +from camel.logger import set_log_level |
| 16 | + |
| 17 | +from owl.utils import run_society, DocumentProcessingToolkit |
| 18 | + |
| 19 | +import pathlib |
| 20 | + |
| 21 | +# Set the log level to DEBUG for detailed debugging information |
| 22 | +set_log_level(level="DEBUG") |
| 23 | + |
| 24 | +# Get the parent directory of the current file and construct the path to the .env file |
| 25 | +base_dir = pathlib.Path(__file__).parent.parent |
| 26 | +env_path = base_dir / "owl" / ".env" |
| 27 | +load_dotenv(dotenv_path=str(env_path)) |
| 28 | + |
| 29 | + |
| 30 | +def get_user_input(prompt): |
| 31 | + # Get user input and strip leading/trailing whitespace |
| 32 | + return input(prompt).strip() |
| 33 | + |
| 34 | + |
| 35 | +def get_construct_params() -> dict[str, any]: |
| 36 | + # Welcome message |
| 37 | + print("Welcome to owl! Have fun!") |
| 38 | + |
| 39 | + # Select model platform type |
| 40 | + model_platforms = ModelPlatformType |
| 41 | + print("Please select the model platform type:") |
| 42 | + for i, platform in enumerate(model_platforms, 1): |
| 43 | + print(f"{i}. {platform}") |
| 44 | + model_platform_choice = int( |
| 45 | + get_user_input("Please enter the model platform number:") |
| 46 | + ) |
| 47 | + selected_model_platform = list(model_platforms)[model_platform_choice - 1] |
| 48 | + print(f"The model platform you selected is: {selected_model_platform}") |
| 49 | + |
| 50 | + # Select model type |
| 51 | + models = ModelType |
| 52 | + print("Please select the model type:") |
| 53 | + for i, model in enumerate(models, 1): |
| 54 | + print(f"{i}. {model}") |
| 55 | + model_choice = int(get_user_input("Please enter the model number:")) |
| 56 | + selected_model = list(models)[model_choice - 1] |
| 57 | + print(f"The model you selected is: {selected_model}") |
| 58 | + |
| 59 | + # Select language |
| 60 | + languages = ["English", "Chinese"] |
| 61 | + print("Please select the language:") |
| 62 | + for i, lang in enumerate(languages, 1): |
| 63 | + print(f"{i}. {lang}") |
| 64 | + language_choice = int(get_user_input("Please enter the language number:")) |
| 65 | + selected_language = languages[language_choice - 1] |
| 66 | + print(f"The language you selected is: {selected_language}") |
| 67 | + |
| 68 | + # Enter the question |
| 69 | + question = get_user_input("Please enter your question:") |
| 70 | + print(f"Your question is: {question}") |
| 71 | + |
| 72 | + return { |
| 73 | + "language": selected_language, |
| 74 | + "model_type": selected_model, |
| 75 | + "model_platform": selected_model_platform, |
| 76 | + "question": question, |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | +def construct_society() -> RolePlaying: |
| 81 | + # Get user input parameters |
| 82 | + params = get_construct_params() |
| 83 | + question = params["question"] |
| 84 | + selected_model_type = params["model_type"] |
| 85 | + selected_model_platform = params["model_platform"] |
| 86 | + selected_language = params["language"] |
| 87 | + |
| 88 | + # Create model instances for different roles |
| 89 | + models = { |
| 90 | + "user": ModelFactory.create( |
| 91 | + model_platform=selected_model_platform, |
| 92 | + model_type=selected_model_type, |
| 93 | + model_config_dict={"temperature": 0}, |
| 94 | + ), |
| 95 | + "assistant": ModelFactory.create( |
| 96 | + model_platform=selected_model_platform, |
| 97 | + model_type=selected_model_type, |
| 98 | + model_config_dict={"temperature": 0}, |
| 99 | + ), |
| 100 | + "web": ModelFactory.create( |
| 101 | + model_platform=selected_model_platform, |
| 102 | + model_type=selected_model_type, |
| 103 | + model_config_dict={"temperature": 0}, |
| 104 | + ), |
| 105 | + "planning": ModelFactory.create( |
| 106 | + model_platform=selected_model_platform, |
| 107 | + model_type=selected_model_type, |
| 108 | + model_config_dict={"temperature": 0}, |
| 109 | + ), |
| 110 | + "video": ModelFactory.create( |
| 111 | + model_platform=selected_model_platform, |
| 112 | + model_type=selected_model_type, |
| 113 | + model_config_dict={"temperature": 0}, |
| 114 | + ), |
| 115 | + "image": ModelFactory.create( |
| 116 | + model_platform=selected_model_platform, |
| 117 | + model_type=selected_model_type, |
| 118 | + model_config_dict={"temperature": 0}, |
| 119 | + ), |
| 120 | + "document": ModelFactory.create( |
| 121 | + model_platform=selected_model_platform, |
| 122 | + model_type=selected_model_type, |
| 123 | + model_config_dict={"temperature": 0}, |
| 124 | + ), |
| 125 | + } |
| 126 | + |
| 127 | + # Configure toolkits |
| 128 | + tools = [ |
| 129 | + *BrowserToolkit( |
| 130 | + headless=False, |
| 131 | + web_agent_model=models["web"], |
| 132 | + planning_agent_model=models["planning"], |
| 133 | + output_language="Chinese", |
| 134 | + ).get_tools(), |
| 135 | + *VideoAnalysisToolkit(model=models["video"]).get_tools(), |
| 136 | + *CodeExecutionToolkit(sandbox="subprocess", verbose=True).get_tools(), |
| 137 | + *ImageAnalysisToolkit(model=models["image"]).get_tools(), |
| 138 | + SearchToolkit().search_duckduckgo, |
| 139 | + SearchToolkit().search_google, |
| 140 | + SearchToolkit().search_wiki, |
| 141 | + SearchToolkit().search_baidu, |
| 142 | + SearchToolkit().search_bing, |
| 143 | + *ExcelToolkit().get_tools(), |
| 144 | + *DocumentProcessingToolkit(model=models["document"]).get_tools(), |
| 145 | + *FileWriteToolkit(output_dir="./").get_tools(), |
| 146 | + ] |
| 147 | + |
| 148 | + # Configure agent roles and parameters |
| 149 | + user_agent_kwargs = {"model": models["user"]} |
| 150 | + assistant_agent_kwargs = {"model": models["assistant"], "tools": tools} |
| 151 | + |
| 152 | + # Configure task parameters |
| 153 | + task_kwargs = { |
| 154 | + "task_prompt": question, |
| 155 | + "with_task_specify": False, |
| 156 | + } |
| 157 | + |
| 158 | + # Create and return the society |
| 159 | + society = RolePlaying( |
| 160 | + **task_kwargs, |
| 161 | + user_role_name="user", |
| 162 | + user_agent_kwargs=user_agent_kwargs, |
| 163 | + assistant_role_name="assistant", |
| 164 | + assistant_agent_kwargs=assistant_agent_kwargs, |
| 165 | + output_language=selected_language, |
| 166 | + ) |
| 167 | + |
| 168 | + return society |
| 169 | + |
| 170 | + |
| 171 | +def main(): |
| 172 | + # Construct the society |
| 173 | + society = construct_society() |
| 174 | + # Run the society and get the answer, chat history, and token count |
| 175 | + answer, chat_history, token_count = run_society(society) |
| 176 | + # Print the answer |
| 177 | + print(f"\033[94mAnswer: {answer}\033[0m") |
| 178 | + |
| 179 | + |
| 180 | +if __name__ == "__main__": |
| 181 | + main() |
0 commit comments