main swarming classes

main
Kye 2 years ago
parent df966c7a1c
commit ca473e3a3d

@ -1,4 +1,4 @@
from swarms.utils.helpers import BossNode from swarms.agents.swarms import BossNode
# Initialize boss node with given parameters # Initialize boss node with given parameters
boss_node = BossNode boss_node = BossNode

@ -37,34 +37,179 @@ vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {
# General ------------------------- WORKER NODE
import pandas as pd
from langchain.experimental.autonomous_agents.autogpt.agent import AutoGPT
from langchain.chat_models import ChatOpenAI
from langchain.agents.agent_toolkits.pandas.base import create_pandas_dataframe_agent
from langchain.docstore.document import Document
import asyncio
import nest_asyncio
llm = ChatOpenAI(model_name="gpt-4", temperature=1.0, openai_api_key="")
# Tools
import os
from contextlib import contextmanager
from typing import Optional
from langchain.tools.file_management.read import ReadFileTool
from langchain.tools.file_management.write import WriteFileTool
ROOT_DIR = "./data/"
from langchain.tools import BaseTool, DuckDuckGoSearchRun
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.qa_with_sources.loading import load_qa_with_sources_chain, BaseCombineDocumentsChain
from langchain.tools.human.tool import HumanInputRun
from swarms.agents.workers.auto_agent import MultiModalVisualAgent
from swarms.tools.main import Terminal, CodeWriter, CodeEditor, process_csv, WebpageQATool
class MultiModalVisualAgentTool(BaseTool):
name = "multi_visual_agent"
description = "Multi-Modal Visual agent tool"
def __init__(self, agent: MultiModalVisualAgent):
self.agent = agent
def _run(self, text: str) -> str:
#run the multi-modal visual agent with the give task
return self.agent.run_text(text)
####################################################################### => Worker Node
####################################################################### => Worker Node
####################################################################### => Worker Node
class WorkerNode: class WorkerNode:
def __init__(self, llm: AutoGPT, vectorstore: FAISS): def __init__(self, llm, tools, vectorstore):
self.llm = llm self.llm = llm
self.tools = tools
self.vectorstore = vectorstore self.vectorstore = vectorstore
self.task_queue = deque()
self.completed_tasks = deque() def create_agent(self, ai_name, ai_role, human_in_the_loop, search_kwargs):
self.task_status: Dict[Any, str] = {}
def receive_task(self, task): embeddings_model = OpenAIEmbeddings(openai_api_key="")
self.task_queue.append(task) embedding_size = 1536
self.task_status[task] = 'pending' index = faiss.IndexFlatL2(embedding_size)
vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
def complete_task(self):
task = self.task_queue.popleft()
result = self.llm.run(task)
self.completed_tasks.append(result)
self.task_status[task] = 'completed' query_website_tool = WebpageQATool(qa_chain=load_qa_with_sources_chain(llm))
# Insert task result into the vectorstore
self.vectorstore.insert(task, result) # !pip install duckduckgo_search
return result web_search = DuckDuckGoSearchRun()
def communicate(self): #
# Share task results and status through vectorstore multimodal_agent_tool = MultiModalVisualAgentTool(MultiModalVisualAgent)
completed_tasks = [(task, self.task_status[task]) for task in self.task_queue if self.task_status[task] == 'completed']
for task, status in completed_tasks: tools = [
self.vectorstore.insert(task, status)
web_search,
WriteFileTool(root_dir="./data"),
ReadFileTool(root_dir="./data"),
process_csv,
multimodal_agent_tool
query_website_tool,
Terminal,
CodeWriter,
CodeEditor
# HumanInputRun(), # Activate if you want the permit asking for help from the human
]
# Instantiate the agent
self.agent = AutoGPT.from_llm_and_tools(
ai_name=ai_name,
ai_role=ai_role,
tools=self.tools,
llm=self.llm,
memory=self.vectorstore.as_retriever(search_kwargs=search_kwargs),
human_in_the_loop=human_in_the_loop,
)
self.agent.chain.verbose = True
def run_agent(self, prompt):
# Run the agent with the given prompt
self.agent.run([prompt])
#worker node example
worker_node = WorkerNode(llm, tools, vectorstore)
worker_node.create_agent(
ai_name="Worker",
ai_role="Assistant",
human_in_the_loop=True,
search_kwargs={"k": 8}
)
tree_of_thoughts_prompt = """
Imagine three different experts are answering this question. All experts will write down each chain of thought of each step of their thinking, then share it with the group. Then all experts will go on to the next step, etc. If any expert realises they're wrong at any point then they leave. The question is...
"""
#Input problem
input_problem = """
Input: 2 8 8 14
Possible next steps:
2 + 8 = 10 (left: 8 10 14)
8 / 2 = 4 (left: 4 8 14)
14 + 2 = 16 (left: 8 8 16)
2 * 8 = 16 (left: 8 14 16)
8 - 2 = 6 (left: 6 8 14)
14 - 8 = 6 (left: 2 6 8)
14 / 2 = 7 (left: 7 8 8)
14 - 2 = 12 (left: 8 8 12)
Input: use 4 numbers and basic arithmetic operations (+-*/) to obtain 24 in 1 equation
Possible next steps:
"""
worker_node.run_agent([f"{tree_of_thoughts_prompt} {input_problem}"])
####################################################################### => Boss Node
####################################################################### => Boss Node
####################################################################### => Boss Node
class BossNode: class BossNode:
def __init__(self, llm, vectorstore, task_execution_chain, verbose, max_iterations): def __init__(self, llm, vectorstore, task_execution_chain, verbose, max_iterations):
Loading…
Cancel
Save