diff --git a/errors.txt b/errors.txt index 9115a5ae..880d5cef 100644 --- a/errors.txt +++ b/errors.txt @@ -1,2 +1,12 @@ -Created a temporary directory at /tmp/tmpy1_589mf -Writing /tmp/tmpy1_589mf/_remote_module_non_scriptable.py +Traceback (most recent call last): + File "/home/zack/code/swarms/app.py", line 15, in + from swarms.modelui.modules.block_requests import OpenMonkeyPatch, RequestBlocker + File "/home/zack/code/swarms/swarms/__init__.py", line 6, in + from swarms.swarms import * # noqa: E402, F403 + File "/home/zack/code/swarms/swarms/swarms/__init__.py", line 1, in + from swarms.structs.autoscaler import AutoScaler + File "/home/zack/code/swarms/swarms/structs/__init__.py", line 1, in + from swarms.structs.agent import Agent + File "/home/zack/code/swarms/swarms/structs/agent.py", line 25, in + from swarms.tools.tool import BaseTool +ImportError: cannot import name 'BaseTool' from 'swarms.tools.tool' (/home/zack/code/swarms/swarms/tools/tool.py) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 1ad6f8ca..52ced644 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -19,19 +19,26 @@ from swarms.prompts.agent_system_prompts import ( from swarms.prompts.multi_modal_autonomous_instruction_prompt import ( MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1, ) -from swarms.prompts.tools import ( - SCENARIOS, -) -from swarms.tools.tool import BaseTool -from swarms.tools.tool_func_doc_scraper import scrape_tool_func_docs -from swarms.utils.code_interpreter import SubprocessCodeInterpreter -from swarms.utils.parse_code import ( - extract_code_in_backticks_in_string, -) -from swarms.utils.pdf_to_text import pdf_to_text +from swarms.prompts.tools import SCENARIOS + +#TODO temp removed to test new tool system revert fro old system and rename tool.old to tools +# from swarms.prompts.tools import ( +# SCENARIOS, +# ) +# from swarms.tools.tool import BaseTool +# from swarms.tools.tool_func_doc_scraper import scrape_tool_func_docs +# from swarms.utils.code_interpreter import SubprocessCodeInterpreter +# from swarms.utils.parse_code import ( +# extract_code_in_backticks_in_string, +# ) +# from swarms.utils.pdf_to_text import pdf_to_text +# from swarms.utils.token_count_tiktoken import limit_tokens_from_string + +from swarms.tools.tools_controller import load_valid_tools, tools_mappings +from swarms.utils import pdf_to_text +from swarms.utils.parse_code import extract_code_in_backticks_in_string from swarms.utils.token_count_tiktoken import limit_tokens_from_string - # Utils # Custom stopping condition def stop_when_repeats(response: str) -> bool: @@ -176,7 +183,7 @@ class Agent: agent_name: str = "Autonomous-Agent-XYZ1B", agent_description: str = None, system_prompt: str = AGENT_SYSTEM_PROMPT_3, - tools: List[BaseTool] = None, + # tools: List[BaseTool] = None, dynamic_temperature_enabled: Optional[bool] = False, sop: Optional[str] = None, sop_list: Optional[List[str]] = None, @@ -344,72 +351,72 @@ class Agent: return "\n".join(params_str_list) - def get_tool_description(self): - """Get the tool description""" - if self.tools: - try: - tool_descriptions = [] - for tool in self.tools: - description = f"{tool.name}: {tool.description}" - tool_descriptions.append(description) - return "\n".join(tool_descriptions) - except Exception as error: - print( - f"Error getting tool description: {error} try" - " adding a description to the tool or removing" - " the tool" - ) - else: - return "No tools available" - - def find_tool_by_name(self, name: str): - """Find a tool by name""" - for tool in self.tools: - if tool.name == name: - return tool - - def extract_tool_commands(self, text: str): - """ - Extract the tool commands from the text - - Example: - ```json - { - "tool": "tool_name", - "params": { - "tool1": "inputs", - "param2": "value2" - } - } - ``` - - """ - # Regex to find JSON like strings - pattern = r"```json(.+?)```" - matches = re.findall(pattern, text, re.DOTALL) - json_commands = [] - for match in matches: - try: - json_commands = json.loads(match) - json_commands.append(json_commands) - except Exception as error: - print(f"Error parsing JSON command: {error}") - - def execute_tools(self, tool_name, params): - """Execute the tool with the provided params""" - tool = self.tool_find_by_name(tool_name) - if tool: - # Execute the tool with the provided parameters - tool_result = tool.run(**params) - print(tool_result) - - def parse_and_execute_tools(self, response: str): - """Parse and execute the tools""" - json_commands = self.extract_tool_commands(response) - for command in json_commands: - tool_name = command.get("tool") - params = command.get("parmas", {}) - self.execute_tools(tool_name, params) + #TODO deprecated moving to new system delete if not used + # def get_tool_description(self): + # """Get the tool description""" + # if self.tools: + # try: + # tool_descriptions = [] + # for tool in self.tools: + # description = f"{tool.name}: {tool.description}" + # tool_descriptions.append(description) + # return "\n".join(tool_descriptions) + # except Exception as error: + # print( + # f"Error getting tool description: {error} try" + # " adding a description to the tool or removing" + # " the tool" + # ) + # else: + # return "No tools available" +# def find_tool_by_name(self, name: str): +# """Find a tool by name""" +# for tool in self.tools: +# if tool.name == name: +# return tool + + # def extract_tool_commands(self, text: str): + # """ + # Extract the tool commands from the text + + # Example: + # ```json + # { + # "tool": "tool_name", + # "params": { + # "tool1": "inputs", + # "param2": "value2" + # } + # } + # ``` + + # """ + # # Regex to find JSON like strings + # pattern = r"```json(.+?)```" + # matches = re.findall(pattern, text, re.DOTALL) + # json_commands = [] + # for match in matches: + # try: + # json_commands = json.loads(match) + # json_commands.append(json_commands) + # except Exception as error: + # print(f"Error parsing JSON command: {error}") + + # def execute_tools(self, tool_name, params): + # """Execute the tool with the provided params""" + # tool = self.tool_find_by_name(tool_name) + # if tool: + # # Execute the tool with the provided parameters + # tool_result = tool.run(**params) + # print(tool_result) + + # def parse_and_execute_tools(self, response: str): + # """Parse and execute the tools""" + # json_commands = self.extract_tool_commands(response) + # for command in json_commands: + # tool_name = command.get("tool") + # params = command.get("parmas", {}) + # self.execute_tools(tool_name, params) def truncate_history(self): """ @@ -454,11 +461,11 @@ class Agent: self.short_memory[-1].append(message) self.truncate_history() - def parse_tool_docs(self): - """Parse the tool docs""" - for tool in self.tools: - docs = self.tool_docs.append(scrape_tool_func_docs(tool)) - return str(docs) + # def parse_tool_docs(self): + # """Parse the tool docs""" + # for tool in self.tools: + # docs = self.tool_docs.append(scrape_tool_func_docs(tool)) + # return str(docs) def print_dashboard(self, task: str): """Print dashboard""" @@ -1213,16 +1220,17 @@ class Agent: def generate_reply(self, history: str, **kwargs) -> str: """ - Generate a response based on initial or task + Generate a reply using the LLM and the tools. """ - prompt = f""" + # Generate a response using the LLM + response = self.llm.generate_reply(history, **kwargs) - SYSTEM_PROMPT: {self.system_prompt} + # Execute the tools + for tool in self.tools: + if tool.command in response: + response = tool.execute(response) - History: {history} - """ - response = self.llm(prompt, **kwargs) - return {"role": self.agent_name, "content": response} + return response def update_system_prompt(self, system_prompt: str): """Upddate the system message""" @@ -1350,3 +1358,4 @@ class Agent: ‘‘‘ """ return PROMPT + diff --git a/swarms/tools/tools_controller.py b/swarms/tools/tools_controller.py index 45bf775a..a84f103d 100644 --- a/swarms/tools/tools_controller.py +++ b/swarms/tools/tools_controller.py @@ -19,17 +19,6 @@ logger = get_logger(__name__) tools_mappings = {} -# data = json.load(open('openai.json')) -# items = data['items'] - -# for plugin in items: -# url = plugin['manifest']['api']['url'] -# tool_name = plugin['namespace'] -# tools_mappings[tool_name] = url[:-len('/.well-known/openai.yaml')] - -# print(tools_mappings) -# all_tools_list = [] - def load_valid_tools(tools_mappings): tools_to_config = {} for key in tools_mappings: @@ -46,14 +35,10 @@ def load_valid_tools(tools_mappings): return tools_to_config - -# Read the model/ directory and get the list of models model_dir = Path("./models/") available_models = ["ChatGPT", "GPT-3.5"] + [f.name for f in model_dir.iterdir() if f.is_dir()] class MTQuestionAnswerer: - """Use multiple tools to answer a question. Basically pass a natural question to""" - def __init__(self, openai_api_key, all_tools, stream_output=False, llm="ChatGPT", model_path=None): if len(openai_api_key) < 3: # not valid key (TODO: more rigorous checking) openai_api_key = os.environ.get("OPENAI_API_KEY") @@ -106,12 +91,9 @@ class MTQuestionAnswerer: .replace("}", "}}"), func=subagent, ) - tool.tool_logo_md = tool_logo_md self.tools_pool.append(tool) - def build_runner( - self, - ): + def build_runner(self): from langchain.vectorstores import FAISS from langchain.docstore import InMemoryDocstore from langchain.embeddings import OpenAIEmbeddings @@ -125,7 +107,6 @@ class MTQuestionAnswerer: embeddings_model.embed_query, index, InMemoryDocstore({}), {} ) - # TODO refactor to use the flow from swarms.tools.agent.autogptmulti.agent import AutoGPT from langchain.chat_models import ChatOpenAI @@ -136,6 +117,8 @@ class MTQuestionAnswerer: llm=ChatOpenAI(temperature=0), memory=vectorstore.as_retriever(), ) + + ''' # You can modify the prompt to improve the model's performance, or modify the tool's doc prefix = """Answer the following questions as best you can. In this level, you are calling the tools in natural language format, since the tools are actually an intelligent agent like you, but they expert only in one area. Several things to remember. (1) Remember to follow the format of passing natural language as the Action Input. (2) DO NOT use your imagination, only use concrete information given by the tools. (3) If the observation contains images or urls which has useful information, YOU MUST INCLUDE ALL USEFUL IMAGES and links in your Answer and Final Answers using format ![img](url). BUT DO NOT provide any imaginary links. (4) The information in your Final Answer should include ALL the informations returned by the tools. (5) If a user's query is a language other than English, please translate it to English without tools, and translate it back to the source language in Final Answer. You have access to the following tools (Only use these tools we provide you):""" diff --git a/swarms/utils/disable_logging.py b/swarms/utils/disable_logging.py index 3b6884d2..54f478c9 100644 --- a/swarms/utils/disable_logging.py +++ b/swarms/utils/disable_logging.py @@ -5,8 +5,8 @@ import sys def disable_logging(): - log_file = open("errors.txt", "w") - sys.stderr = log_file + # log_file = open("errors.txt", "w") + # sys.stderr = log_file warnings.filterwarnings("ignore", category=UserWarning)