boss node fix cleanup

pull/48/head
Kye 1 year ago
parent 3562fd6a9f
commit 1009493e3c

@ -1,32 +0,0 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
# Required
version: 2
# Set the OS, Python version and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.11"
# You can also specify other tool versions:
# nodejs: "19"
# rust: "1.64"
# golang: "1.19"
# Build documentation in the "docs/" directory with Sphinx
sphinx:
configuration: docs/conf.py
# Optionally build your docs in additional formats such as PDF and ePub
# formats:
# - pdf
# - epub
# Optional but recommended, declare the Python requirements required
# to build your documentation
# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
# python:
# install:
# - requirements: docs/requirements.txt

@ -7,20 +7,18 @@ from pydantic import ValidationError
from swarms.agents.memory.base import VectorStoreRetriever
from swarms.agents.memory.base_memory import BaseChatMessageHistory, ChatMessageHistory
from swarms.agents.memory.document import Document
from swarms.agents.models.base import AbstractModel
from swarms.agents.models.prompts.base import (
AIMessage,
HumanMessage,
SystemMessage,
)
from swarms.agents.models.prompts.agent_prompt_auto import (
MessageFormatter,
PromptConstructor,
)
from swarms.agents.models.prompts.agent_prompt_generator import FINISH_NAME
from swarms.agents.models.prompts.base import (
AIMessage,
HumanMessage,
SystemMessage,
)
from swarms.agents.tools.base import BaseTool
from swarms.agents.utils.Agent import AgentOutputParser
from swarms.agents.utils.human_input import HumanInputRun

@ -104,18 +104,19 @@ class BossNodeInitializer:
class BossNode:
def __init__(self,
# vectorstore,
llm=None,
vectorstore=None,
agent_executor=None,
max_iterations=5,
human_in_the_loop=None,
objective: Optional[str] = None,
boss_system_prompt: Optional[str] = "You are a boss planner in a swarm...",
api_key=None,
worker_node=None,
llm_class=OpenAI,
max_iterations=5,
verbose=False,
):
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
# self.vectorstore = vectorstore
self.worker_node = worker_node
self.boss_system_prompt = boss_system_prompt
self.llm_class = llm_class
@ -123,46 +124,54 @@ class BossNode:
self.verbose = verbose
if not self.api_key:
raise ValueError("[BossNode][ValueError][API KEY must be provided either as an argument or as an environment variable API_KEY]")
raise ValueError("[MasterBossNode][ValueError][API KEY must be provided either as an argument or as an environment variable API_KEY]")
self.llm = self.initialize_llm(self.llm_class)
# Initialize components if not provided
self.llm = llm if llm else self._initialize_llm(self.llm_class)
self.vectorstore = vectorstore if vectorstore else self._initialize_vectorstore()
# Setting up todo_chain and agent_executor
todo_prompt = PromptTemplate.from_template(boss_system_prompt)
todo_chain = LLMChain(llm=self.llm, prompt=todo_prompt)
tools = [
Tool(name="TODO", func=todo_chain.run, description="useful for when you need to come up with todo lists..."),
self.worker_node
]
suffix = """Question: {task}\n{agent_scratchpad}"""
prefix = """You are a 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=self.llm, prompt=prompt)
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=[tool.name for tool in tools])
self.agent_executor = agent_executor if agent_executor else AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=self.verbose)
self.agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=self.verbose)
vectorstore = self.initialize_vectorstore()
self.boss_initializer = BossNodeInitializer(
# Setup BabyAGI
try:
self.baby_agi = BabyAGI.from_llm(
llm=self.llm,
vectorstore=vectorstore,
agent_executor=self.agent_executor,
vectorstore=self.vectorstore,
task_execution_chain=self.agent_executor,
max_iterations=self.max_iterations,
human_in_the_loop=human_in_the_loop
)
self.task = self.boss_initializer.create_task(objective)
except ValidationError as e:
logging.error(f"Validation Error while initializing BabyAGI: {e}")
raise
except Exception as e:
logging.error(f"Unexpected Error while initializing BabyAGI: {e}")
raise
def initialize_llm(self, llm_class, temperature=0.5):
self.task = self._create_task(objective)
def _initialize_llm(self, llm_class, temperature=0.5):
try:
return llm_class(openai_api_key=self.api_key, temperature=temperature)
except Exception as e:
logging.error(f"Failed to initialize language model: {e}")
raise e
def initialize_vectorstore(self):
def _initialize_vectorstore(self):
try:
embeddings_model = OpenAIEmbeddings(openai_api_key=self.openai_api_key)
embeddings_model = OpenAIEmbeddings(openai_api_key=self.api_key)
embedding_size = 8192
index = faiss.IndexFlatL2(embedding_size)
return FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
@ -170,7 +179,18 @@ class BossNode:
logging.error(f"Failed to initialize vector store: {e}")
return None
def _create_task(self, objective):
if not objective:
logging.error("Objective cannot be empty.")
raise ValueError("Objective cannot be empty.")
return {"objective": objective}
def run(self):
self.boss_initializer.run(self.task)
if not self.task:
logging.error("Task cannot be empty.")
raise ValueError("Task cannot be empty.")
try:
self.baby_agi(self.task)
except Exception as e:
logging.error(f"Error while executing task: {e}")
raise

@ -58,7 +58,7 @@ class VortexWorkerAgent:
self.chat_history_file = chat_history_file
self.llm = llm or self.init_llm(ChatOpenAI)
self.tools = self.init_tools()
self.tools = tools or self.init_tools()
self.vectorstore = self.init_vectorstore()
self.agent = self.create_agent()

Loading…
Cancel
Save