main
Kye 2 years ago
parent 9ced5d277f
commit ab24ecb54a

@ -69,24 +69,25 @@ tools = [
Tool(name='code_editor', func=CodeEditor, description='Edits code'), Tool(name='code_editor', func=CodeEditor, description='Edits code'),
# Add any additional tools here... # Add any additional tools here...
] ]
############## Vectorstore
embeddings_model = OpenAIEmbeddings()
embedding_size = 1536
index = faiss.IndexFlatL2(embedding_size)
vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
####################################################################### => Worker Node
# ############## Vectorstore
# embeddings_model = OpenAIEmbeddings()
# embedding_size = 1536
# index = faiss.IndexFlatL2(embedding_size)
# vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
# ####################################################################### => Worker Node
# worker_agent = AutoGPT.from_llm_and_tools(
# ai_name="WorkerX",
# ai_role="Assistant",
# tools=tools,
# llm=llm,
# memory=vectorstore.as_retriever(search_kwargs={"k": 8}),
# human_in_the_loop=True, # Set to True if you want to add feedback at each step.
# )
worker_agent = AutoGPT.from_llm_and_tools( # worker_agent.chain.verbose = True
ai_name="WorkerX",
ai_role="Assistant",
tools=tools,
llm=llm,
memory=vectorstore.as_retriever(search_kwargs={"k": 8}),
human_in_the_loop=True, # Set to True if you want to add feedback at each step.
)
worker_agent.chain.verbose = True
@ -119,8 +120,8 @@ class WorkerNode:
#inti worker node with llm # #inti worker node with llm
worker_node = WorkerNode(llm=llm, tools=tools, vectorstore=vectorstore) # worker_node = WorkerNode(llm=llm, tools=tools, vectorstore=vectorstore)
# #create an agent within the worker node # #create an agent within the worker node
# worker_node.create_agent(ai_name="AI Assistant", ai_role="Assistant", human_in_the_loop=True, search_kwargs={}) # worker_node.create_agent(ai_name="AI Assistant", ai_role="Assistant", human_in_the_loop=True, search_kwargs={})
@ -128,77 +129,77 @@ worker_node = WorkerNode(llm=llm, tools=tools, vectorstore=vectorstore)
# #use the agent to perform a task # #use the agent to perform a task
# worker_node.run_agent("Find 20 potential customers for a Swarms based AI Agent automation infrastructure") # worker_node.run_agent("Find 20 potential customers for a Swarms based AI Agent automation infrastructure")
class BossNode: # class BossNode:
def __init__(self, openai_api_key, llm, vectorstore, task_execution_chain, verbose, max_iterations): # def __init__(self, openai_api_key, llm, vectorstore, task_execution_chain, verbose, max_iterations):
self.llm = llm # self.llm = llm
self.openai_api_key = openai_api_key # self.openai_api_key = openai_api_key
self.vectorstore = vectorstore # self.vectorstore = vectorstore
self.task_execution_chain = task_execution_chain # self.task_execution_chain = task_execution_chain
self.verbose = verbose # self.verbose = verbose
self.max_iterations = max_iterations # self.max_iterations = max_iterations
self.baby_agi = BabyAGI.from_llm( # self.baby_agi = BabyAGI.from_llm(
llm=self.llm, # llm=self.llm,
vectorstore=self.vectorstore, # vectorstore=self.vectorstore,
task_execution_chain=self.task_execution_chain # task_execution_chain=self.task_execution_chain
) # )
def create_task(self, objective): # def create_task(self, objective):
return {"objective": objective} # return {"objective": objective}
def execute_task(self, task): # def execute_task(self, task):
self.baby_agi(task) # self.baby_agi(task)
########### ===============> inputs to boss None # ########### ===============> inputs to boss None
todo_prompt = PromptTemplate.from_template( # todo_prompt = PromptTemplate.from_template(
"You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}""" # "You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}"""
) # )
todo_chain = LLMChain(llm=OpenAI(temperature=0), prompt=todo_prompt) # todo_chain = LLMChain(llm=OpenAI(temperature=0), prompt=todo_prompt)
# search = SerpAPIWrapper() # # search = SerpAPIWrapper()
tools = [ # tools = [
# # Tool(
# # name="Search",
# # func=search.run,
# # description="useful for when you need to answer questions about current events",
# # ),
# Tool( # Tool(
# name="Search", # name="TODO",
# func=search.run, # func=todo_chain.run,
# description="useful for when you need to answer questions about current events", # 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!",
# ), # ),
Tool( # Tool(
name="TODO", # name="AUTONOMOUS Worker AGENT",
func=todo_chain.run, # func=worker_agent.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!", # description="Useful for when you need to spawn an autonomous agent instance as a worker to accomplish complex tasks, it can search the internet or spawn child multi-modality models to process and generate images and text or audio and so on"
), # )
Tool( # ]
name="AUTONOMOUS Worker AGENT",
func=worker_agent.run,
description="Useful for when you need to spawn an autonomous agent instance as a worker to accomplish complex tasks, it can search the internet or spawn child multi-modality models to process and generate images and text or audio and so on"
)
]
suffix = """Question: {task} # suffix = """Question: {task}
{agent_scratchpad}""" # {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}. # 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}.
""" # """
prompt = ZeroShotAgent.create_prompt( # prompt = ZeroShotAgent.create_prompt(
tools, # tools,
prefix=prefix, # prefix=prefix,
suffix=suffix, # suffix=suffix,
input_variables=["objective", "task", "context", "agent_scratchpad"], # input_variables=["objective", "task", "context", "agent_scratchpad"],
) # )
llm = OpenAI(temperature=0) # llm = OpenAI(temperature=0)
llm_chain = LLMChain(llm=llm, prompt=prompt) # llm_chain = LLMChain(llm=llm, prompt=prompt)
tool_names = [tool.name for tool in tools] # tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names) # agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)
agent_executor = AgentExecutor.from_agent_and_tools( # agent_executor = AgentExecutor.from_agent_and_tools(
agent=agent, tools=tools, verbose=True # agent=agent, tools=tools, verbose=True
) # )
boss_node = BossNode(llm=llm, vectorstore=vectorstore, task_execution_chain=agent_executor, verbose=True, max_iterations=5) # boss_node = BossNode(llm=llm, vectorstore=vectorstore, task_execution_chain=agent_executor, verbose=True, max_iterations=5)
# #create a task # #create a task
# task = boss_node.create_task(objective="Write a research paper on the impact of climate change on global agriculture") # task = boss_node.create_task(objective="Write a research paper on the impact of climate change on global agriculture")

Loading…
Cancel
Save