From 8ae77944832750302929ebca71c25d2dd244e322 Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 7 Jul 2023 19:58:25 -0400 Subject: [PATCH] clean up for swarms --- swarms/swarms.py | 144 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 131 insertions(+), 13 deletions(-) diff --git a/swarms/swarms.py b/swarms/swarms.py index aab4ca13..409908c5 100644 --- a/swarms/swarms.py +++ b/swarms/swarms.py @@ -7,6 +7,13 @@ from swarms.tools.main import RequestsGet logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') + + + + + + + class Swarms: def __init__(self, openai_api_key): self.openai_api_key = openai_api_key @@ -26,10 +33,6 @@ class Swarms: process_csv, WebpageQATool(qa_chain=load_qa_with_sources_chain(llm)), - - # RequestsGet() - Tool(name="RequestsGet", func=RequestsGet.get, description="A portal to the internet, Use this when you need to get specific content from a website. Input should be a url (i.e. https://www.google.com). The output will be the text response of the GET request."), - # CodeEditor, # Terminal, @@ -37,7 +40,6 @@ class Swarms: # ExitConversation #code editor + terminal editor + visual agent - # Give the worker node itself as a tool ] assert tools is not None, "tools is not initialized" @@ -77,7 +79,7 @@ class Swarms: return BossNode(llm, vectorstore, agent_executor, max_iterations=5) - def run_swarms(self, objective, run_as=None): + def run_swarms(self, objective): try: # Run the swarm with the given objective worker_tools = self.initialize_tools(OpenAI) @@ -86,13 +88,10 @@ class Swarms: vectorstore = self.initialize_vectorstore() worker_node = self.initialize_worker_node(worker_tools, vectorstore) - if run_as.lower() == 'worker': - tool_input = {'prompt': objective} - return worker_node.run(tool_input) - else: - boss_node = self.initialize_boss_node(vectorstore, worker_node) - task = boss_node.create_task(objective) - return boss_node.execute_task(task) + boss_node = self.initialize_boss_node(vectorstore, worker_node) + + task = boss_node.create_task(objective) + return boss_node.execute_task(task) except Exception as e: logging.error(f"An error occurred in run_swarms: {e}") raise @@ -116,6 +115,125 @@ class Swarms: + + + + + + + + + + +# class Swarms: +# def __init__(self, openai_api_key): +# self.openai_api_key = openai_api_key + +# def initialize_llm(self, llm_class, temperature=0.5): +# # Initialize language model +# return llm_class(openai_api_key=self.openai_api_key, temperature=temperature) + +# def initialize_tools(self, llm_class): +# llm = self.initialize_llm(llm_class) +# # Initialize tools +# web_search = DuckDuckGoSearchRun() +# tools = [ +# web_search, +# WriteFileTool(root_dir=ROOT_DIR), +# ReadFileTool(root_dir=ROOT_DIR), + +# process_csv, +# WebpageQATool(qa_chain=load_qa_with_sources_chain(llm)), + +# # RequestsGet() +# Tool(name="RequestsGet", func=RequestsGet.get, description="A portal to the internet, Use this when you need to get specific content from a website. Input should be a url (i.e. https://www.google.com). The output will be the text response of the GET request."), + + +# # CodeEditor, +# # Terminal, +# # RequestsGet, +# # ExitConversation + +# #code editor + terminal editor + visual agent +# # Give the worker node itself as a tool + +# ] +# assert tools is not None, "tools is not initialized" +# return tools + +# def initialize_vectorstore(self): +# # Initialize vector store +# embeddings_model = OpenAIEmbeddings(openai_api_key=self.openai_api_key) +# embedding_size = 1536 +# index = faiss.IndexFlatL2(embedding_size) +# return FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) + +# def initialize_worker_node(self, worker_tools, vectorstore): +# # Initialize worker node +# llm = self.initialize_llm(ChatOpenAI) +# worker_node = WorkerNode(llm=llm, tools=worker_tools, vectorstore=vectorstore) +# worker_node.create_agent(ai_name="Swarm Worker AI Assistant", ai_role="Assistant", human_in_the_loop=False, search_kwargs={}) +# worker_node_tool = Tool(name="WorkerNode AI Agent", func=worker_node.run, description="Input: an objective with a todo list for that objective. Output: your task completed: Please be very clear what the objective and task instructions are. The Swarm worker agent is Useful for when you need to spawn an autonomous agent instance as a worker to accomplish any complex tasks, it can search the internet or write code or spawn child multi-modality models to process and generate images and text or audio and so on") +# return worker_node_tool + +# def initialize_boss_node(self, vectorstore, worker_node): +# # Initialize boss node +# llm = self.initialize_llm(OpenAI) +# todo_prompt = PromptTemplate.from_template("You are a boss planer in a swarm who is an expert at coming up with a todo list for a given objective and then creating an worker to help you accomplish your task. Come up with a todo list for this objective: {objective} and then spawn a worker agent to complete the task for you. Always spawn an worker agent after creating a plan and pass the objective and plan to the worker agent.") +# todo_chain = LLMChain(llm=llm, prompt=todo_prompt) +# tools = [ +# Tool(name="TODO", func=todo_chain.run, description="useful for when you need to come up with todo lists. Input: an objective to create a todo list for. Output: a todo list for that objective. Please be very clear what the objective is!"), +# worker_node +# ] +# suffix = """Question: {task}\n{agent_scratchpad}""" +# prefix = """You are an Boss in a swarm who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}.\n """ +# prompt = ZeroShotAgent.create_prompt(tools, prefix=prefix, suffix=suffix, input_variables=["objective", "task", "context", "agent_scratchpad"],) +# llm_chain = LLMChain(llm=llm, prompt=prompt) +# agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=[tool.name for tool in tools]) +# agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) +# # return BossNode(return BossNode(llm, vectorstore, agent_executor, max_iterations=5) +# return BossNode(llm, vectorstore, agent_executor, max_iterations=5) + + +# def run_swarms(self, objective, run_as=None): +# try: +# # Run the swarm with the given objective +# worker_tools = self.initialize_tools(OpenAI) +# assert worker_tools is not None, "worker_tools is not initialized" + +# vectorstore = self.initialize_vectorstore() +# worker_node = self.initialize_worker_node(worker_tools, vectorstore) + +# if run_as.lower() == 'worker': +# tool_input = {'prompt': objective} +# return worker_node.run(tool_input) +# else: +# boss_node = self.initialize_boss_node(vectorstore, worker_node) +# task = boss_node.create_task(objective) +# return boss_node.execute_task(task) +# except Exception as e: +# logging.error(f"An error occurred in run_swarms: {e}") +# raise + + + + + + + + + + + + + + + + + + + +