From 0599853994f9df55ce78ac0782ca6acda13eb663 Mon Sep 17 00:00:00 2001 From: qcampbel Date: Wed, 3 Jan 2024 14:53:32 -0500 Subject: [PATCH 1/7] added back tool retrieval --- mdagent/mainagent/agent.py | 45 ++++++++++++++++++++++++++++---------- mdagent/tools/maketools.py | 15 +++++++------ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/mdagent/mainagent/agent.py b/mdagent/mainagent/agent.py index 0e20df8c..df66bab2 100644 --- a/mdagent/mainagent/agent.py +++ b/mdagent/mainagent/agent.py @@ -7,7 +7,7 @@ from mdagent.subagents import SubAgentSettings from mdagent.utils import PathRegistry, _make_llm -from ..tools import make_all_tools +from ..tools import get_tools, make_all_tools from .prompt import openaifxn_prompt, structured_prompt load_dotenv() @@ -45,14 +45,16 @@ def __init__( subagents_model="gpt-4-1106-preview", ckpt_dir="ckpt", resume=False, - top_k_tools=10, + top_k_tools=20, # set "all" if you want to use all tools (& skills if resume) use_human_tool=False, ): if path_registry is None: path_registry = PathRegistry.get_instance() - if tools is None: - tools_llm = _make_llm(tools_model, temp, verbose) - tools = make_all_tools(tools_llm, human=use_human_tool) + self.agent_type = agent_type + self.tools = tools + self.tools_llm = _make_llm(tools_model, temp, verbose) + self.use_human_tool = use_human_tool + self.top_k_tools = top_k_tools self.llm = ChatOpenAI( temperature=temp, @@ -61,11 +63,7 @@ def __init__( streaming=True, callbacks=[StreamingStdOutCallbackHandler()], ) - self.agent = AgentExecutor.from_agent_and_tools( - tools=tools, - agent=AgentType.get_agent(agent_type).from_llm_and_tools(self.llm, tools), - handle_parsing_errors=True, - ) + # assign prompt if agent_type == "Structured": self.prompt = structured_prompt @@ -80,9 +78,34 @@ def __init__( verbose=verbose, ckpt_dir=ckpt_dir, resume=resume, - retrieval_top_k=top_k_tools, + ) + + def _initialize_tools_and_agent(self, user_input=None): + if self.tools is not None: + tools = self.tools + elif self.top_k_tools != "all" and user_input is not None: + tools = get_tools( + query=user_input, + llm=self.tools_llm, + subagent_settings=self.subagents_settings, + human=self.use_human_tool, + ) + else: + tools = make_all_tools( + self.tools_llm, + subagent_settings=self.subagents_settings, + human=self.use_human_tool, + ) + return AgentExecutor.from_agent_and_tools( + tools=tools, + agent=AgentType.get_agent(self.agent_type).from_llm_and_tools( + self.llm, tools + ), + handle_parsing_errors=True, ) def run(self, user_input, callbacks=None): # todo: check this for both agent types + + self.agent = self._initialize_tools_and_agent(user_input) return self.agent.run(self.prompt.format(input=user_input), callbacks=callbacks) diff --git a/mdagent/tools/maketools.py b/mdagent/tools/maketools.py index c9f2891d..cb29f355 100644 --- a/mdagent/tools/maketools.py +++ b/mdagent/tools/maketools.py @@ -78,7 +78,6 @@ def make_all_tools( base_tools = [ CleaningToolFunction(path_registry=path_instance), CheckDirectoryFiles(), - # InstructionSummary(path_registry=path_instance), ListRegistryPaths(path_registry=path_instance), MapPath2Name(path_registry=path_instance), Name2PDBTool(path_registry=path_instance), @@ -125,22 +124,25 @@ def get_tools( query, llm: BaseLanguageModel, subagent_settings: Optional[SubAgentSettings] = None, - ckpt_dir="ckpt", - retrieval_top_k=10, + top_k_tools=15, subagents_required=True, human=False, ): + if subagent_settings: + ckpt_dir = subagent_settings.ckpt_dir + else: + ckpt_dir = "ckpt" + retrieved_tools = [] if subagents_required: # add subagents-related tools by default - PathRegistry.get_instance() retrieved_tools = [ CreateNewTool(subagent_settings=subagent_settings), ExecuteSkill(subagent_settings=subagent_settings), SkillRetrieval(subagent_settings=subagent_settings), WorkflowPlan(subagent_settings=subagent_settings), ] - retrieval_top_k -= len(retrieved_tools) + top_k_tools -= len(retrieved_tools) all_tools = make_all_tools( llm, subagent_settings, skip_subagents=True, human=human ) @@ -165,7 +167,7 @@ def get_tools( vectordb.persist() # retrieve 'k' tools - k = min(retrieval_top_k, vectordb._collection.count()) + k = min(top_k_tools, vectordb._collection.count()) if k == 0: return None docs = vectordb.similarity_search(query, k=k) @@ -188,7 +190,6 @@ class CreateNewToolInputSchema(BaseModel): ) -# move this here to avoid circular import error (since it gets a list of all tools) class CreateNewTool(BaseTool): name: str = "CreateNewTool" description: str = """ From 2fc61e986a7f9e498656e091742d055553317f73 Mon Sep 17 00:00:00 2001 From: qcampbel Date: Wed, 3 Jan 2024 15:05:37 -0500 Subject: [PATCH 2/7] fixed and tested --- mdagent/mainagent/agent.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/mdagent/mainagent/agent.py b/mdagent/mainagent/agent.py index df66bab2..e600d001 100644 --- a/mdagent/mainagent/agent.py +++ b/mdagent/mainagent/agent.py @@ -81,25 +81,24 @@ def __init__( ) def _initialize_tools_and_agent(self, user_input=None): - if self.tools is not None: - tools = self.tools - elif self.top_k_tools != "all" and user_input is not None: - tools = get_tools( - query=user_input, - llm=self.tools_llm, - subagent_settings=self.subagents_settings, - human=self.use_human_tool, - ) - else: - tools = make_all_tools( - self.tools_llm, - subagent_settings=self.subagents_settings, - human=self.use_human_tool, - ) + if self.tools is None: + if self.top_k_tools != "all" and user_input is not None: + self.tools = get_tools( + query=user_input, + llm=self.tools_llm, + subagent_settings=self.subagents_settings, + human=self.use_human_tool, + ) + else: + self.tools = make_all_tools( + self.tools_llm, + subagent_settings=self.subagents_settings, + human=self.use_human_tool, + ) return AgentExecutor.from_agent_and_tools( - tools=tools, + tools=self.tools, agent=AgentType.get_agent(self.agent_type).from_llm_and_tools( - self.llm, tools + self.llm, self.tools ), handle_parsing_errors=True, ) From cc87581ffed165eed531e2b3a5a81ff4f624f6fd Mon Sep 17 00:00:00 2001 From: qcampbel Date: Wed, 10 Jan 2024 16:47:37 -0500 Subject: [PATCH 3/7] updated initialize fxn --- mdagent/mainagent/agent.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/mdagent/mainagent/agent.py b/mdagent/mainagent/agent.py index e600d001..a9061ea3 100644 --- a/mdagent/mainagent/agent.py +++ b/mdagent/mainagent/agent.py @@ -35,7 +35,7 @@ class MDAgent: def __init__( self, tools=None, - agent_type="OpenAIFunctionsAgent", # this can also be strucured_chat + agent_type="OpenAIFunctionsAgent", # this can also be structured_chat model="gpt-4-1106-preview", # current name for gpt-4 turbo tools_model="gpt-4-1106-preview", temp=0.1, @@ -50,11 +50,13 @@ def __init__( ): if path_registry is None: path_registry = PathRegistry.get_instance() + + # save parameters to initialize the agent in the first run self.agent_type = agent_type self.tools = tools self.tools_llm = _make_llm(tools_model, temp, verbose) - self.use_human_tool = use_human_tool self.top_k_tools = top_k_tools + self.use_human_tool = use_human_tool self.llm = ChatOpenAI( temperature=temp, @@ -81,30 +83,34 @@ def __init__( ) def _initialize_tools_and_agent(self, user_input=None): - if self.tools is None: + """Retrieve tools and initialize the agent.""" + if self.tools is not None: + tools = self.tools + else: if self.top_k_tools != "all" and user_input is not None: - self.tools = get_tools( + # retrieve only tools relevant to user input + tools = get_tools( query=user_input, llm=self.tools_llm, subagent_settings=self.subagents_settings, human=self.use_human_tool, ) else: - self.tools = make_all_tools( + # retrieve all tools, including new tools if any + tools = make_all_tools( self.tools_llm, subagent_settings=self.subagents_settings, human=self.use_human_tool, ) return AgentExecutor.from_agent_and_tools( - tools=self.tools, + tools=tools, agent=AgentType.get_agent(self.agent_type).from_llm_and_tools( - self.llm, self.tools + self.llm, + tools, ), handle_parsing_errors=True, ) def run(self, user_input, callbacks=None): - # todo: check this for both agent types - self.agent = self._initialize_tools_and_agent(user_input) return self.agent.run(self.prompt.format(input=user_input), callbacks=callbacks) From 141673dea4e6884d93806769e859ab6aab7b28d0 Mon Sep 17 00:00:00 2001 From: qcampbel Date: Wed, 10 Jan 2024 16:51:04 -0500 Subject: [PATCH 4/7] removed old comment --- mdagent/mainagent/agent.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mdagent/mainagent/agent.py b/mdagent/mainagent/agent.py index a9061ea3..08ce7b1f 100644 --- a/mdagent/mainagent/agent.py +++ b/mdagent/mainagent/agent.py @@ -50,8 +50,6 @@ def __init__( ): if path_registry is None: path_registry = PathRegistry.get_instance() - - # save parameters to initialize the agent in the first run self.agent_type = agent_type self.tools = tools self.tools_llm = _make_llm(tools_model, temp, verbose) From a28ab577cea9adc8cc9d8f351ac990f9fc28aeae Mon Sep 17 00:00:00 2001 From: qcampbel Date: Wed, 10 Jan 2024 17:30:44 -0500 Subject: [PATCH 5/7] fixed bug --- mdagent/tools/maketools.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mdagent/tools/maketools.py b/mdagent/tools/maketools.py index f7fb3a10..17daab31 100644 --- a/mdagent/tools/maketools.py +++ b/mdagent/tools/maketools.py @@ -176,7 +176,8 @@ def get_tools( retrieved_tools.append(all_tools[index]) else: print(f"Invalid index {index}.") - print(f"Try deleting vectordb at {ckpt_dir}/all_tools_vectordb.") + print("Some tools may be duplicated.") + print(f"Try to delete vector DB at {ckpt_dir}/all_tools_vectordb.") return retrieved_tools @@ -220,7 +221,7 @@ def get_all_tools_string(self): all_tools_string += f"{tool.name}: {tool.description}\n" return all_tools_string - def _run(self, task, orig_prompt, curr_tools, execute, args=None): + def _run(self, task, orig_prompt, curr_tools, execute=True, args=None): # run iterator try: all_tools_string = self.get_all_tools_string() From a8e40e185bde0ae56d3fa83ed891f3894a5ba413 Mon Sep 17 00:00:00 2001 From: qcampbel Date: Wed, 10 Jan 2024 17:32:27 -0500 Subject: [PATCH 6/7] separate param for user-provided tools --- mdagent/mainagent/agent.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mdagent/mainagent/agent.py b/mdagent/mainagent/agent.py index 08ce7b1f..6f8d7f3b 100644 --- a/mdagent/mainagent/agent.py +++ b/mdagent/mainagent/agent.py @@ -51,7 +51,7 @@ def __init__( if path_registry is None: path_registry = PathRegistry.get_instance() self.agent_type = agent_type - self.tools = tools + self.user_tools = tools self.tools_llm = _make_llm(tools_model, temp, verbose) self.top_k_tools = top_k_tools self.use_human_tool = use_human_tool @@ -82,12 +82,12 @@ def __init__( def _initialize_tools_and_agent(self, user_input=None): """Retrieve tools and initialize the agent.""" - if self.tools is not None: - tools = self.tools + if self.user_tools is not None: + self.tools = self.user_tools else: if self.top_k_tools != "all" and user_input is not None: # retrieve only tools relevant to user input - tools = get_tools( + self.tools = get_tools( query=user_input, llm=self.tools_llm, subagent_settings=self.subagents_settings, @@ -95,16 +95,16 @@ def _initialize_tools_and_agent(self, user_input=None): ) else: # retrieve all tools, including new tools if any - tools = make_all_tools( + self.tools = make_all_tools( self.tools_llm, subagent_settings=self.subagents_settings, human=self.use_human_tool, ) return AgentExecutor.from_agent_and_tools( - tools=tools, + tools=self.tools, agent=AgentType.get_agent(self.agent_type).from_llm_and_tools( self.llm, - tools, + self.tools, ), handle_parsing_errors=True, ) From 5d4e3af261b6098c1459aab4c9b0d3eac313d4a0 Mon Sep 17 00:00:00 2001 From: qcampbel Date: Wed, 10 Jan 2024 17:36:00 -0500 Subject: [PATCH 7/7] removed old output file --- notebooks/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 notebooks/.DS_Store diff --git a/notebooks/.DS_Store b/notebooks/.DS_Store deleted file mode 100644 index 78ff399b1eb286aa95575e56cb286133c1d96ec2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T>S5dJn+sCcPA2R#URf`V@lNDEnPk6RGFbqjH!seC4uCdQFxg}CgUGySLlU#>5`*Tr#{grzV2Hbhw;Q&R z0ol7Fj8JmlhFIRencBLz#SE{U$uG<4C@-fuBjmWeQ@fuk>;cD^Fvl~!3*<}ZU`?KI zR?Bn9vomC#n+;AJJQ9z!`7`77WPuA*BjthE+uUbg`h!3?;-+ zVhjy-#RxH+_SF0`!zyA7N3hFBuqOw*p#*(8^QY>LkVW*?8E^(x8Q6)#j@18)-|zpc zK|XQ@oPmGEKxp-^`aLemYHMS0QfpJ{HC06Xs)&macC-}JS4!~-RfYCcI>gMdibxB^ Oe*}~UZ=8WYW#9|ZU}new