Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
kyegomez committed Jul 3, 2023
1 parent bdf5d87 commit 39ffd6a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 89 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = 'swarms',
packages = find_packages(exclude=[]),
version = '0.1.0',
version = '0.1.1',
license='MIT',
description = 'Swarms - Pytorch',
author = 'Kye Gomez',
Expand Down
176 changes: 90 additions & 86 deletions swarms/agents/swarms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@
from langchain import OpenAI, SerpAPIWrapper, LLMChain


from swarms.agents.workers.auto_agent import worker_agent
worker_agent = worker_agent

# Define your embedding model
embeddings_model = OpenAIEmbeddings()
# embeddings_model = OpenAIEmbeddings()
# Initialize the vectorstore as empty
import faiss

embedding_size = 1536
index = faiss.IndexFlatL2(embedding_size)
vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})

# from swarms.agents.workers.auto_agent import AutoGPT


Expand Down Expand Up @@ -123,11 +115,23 @@ def _run(self, text: str) -> str:
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.chain.verbose = True




####################################################################### => Worker Node

class WorkerNode:
def __init__(self, llm, tools, vectorstore):
Expand Down Expand Up @@ -170,101 +174,101 @@ def run_agent(self, prompt):
#======================================> WorkerNode


class MetaWorkerNode:
def __init__(self, llm, tools, vectorstore):
self.llm = llm
self.tools = tools
self.vectorstore = vectorstore
# class MetaWorkerNode:
# def __init__(self, llm, tools, vectorstore):
# self.llm = llm
# self.tools = tools
# self.vectorstore = vectorstore

self.agent = None
self.meta_chain = None
# self.agent = None
# self.meta_chain = None

def init_chain(self, instructions):
self.agent = WorkerNode(self.llm, self.tools, self.vectorstore)
self.agent.create_agent("Assistant", "Assistant Role", False, {})
# def init_chain(self, instructions):
# self.agent = WorkerNode(self.llm, self.tools, self.vectorstore)
# self.agent.create_agent("Assistant", "Assistant Role", False, {})

def initialize_meta_chain():
meta_template = """
Assistant has just had the below interactions with a User. Assistant followed their "Instructions" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future.
# def initialize_meta_chain():
# meta_template = """
# Assistant has just had the below interactions with a User. Assistant followed their "Instructions" closely. Your job is to critique the Assistant's performance and then revise the Instructions so that Assistant would quickly and correctly respond in the future.

####
# ####

{chat_history}
# {chat_history}

####
# ####

Please reflect on these interactions.
# Please reflect on these interactions.

You should first critique Assistant's performance. What could Assistant have done better? What should the Assistant remember about this user? Are there things this user always wants? Indicate this with "Critique: ...".
# You should first critique Assistant's performance. What could Assistant have done better? What should the Assistant remember about this user? Are there things this user always wants? Indicate this with "Critique: ...".

You should next revise the Instructions so that Assistant would quickly and correctly respond in the future. Assistant's goal is to satisfy the user in as few interactions as possible. Assistant will only see the new Instructions, not the interaction history, so anything important must be summarized in the Instructions. Don't forget any important details in the current Instructions! Indicate the new Instructions by "Instructions: ...".
"""
# You should next revise the Instructions so that Assistant would quickly and correctly respond in the future. Assistant's goal is to satisfy the user in as few interactions as possible. Assistant will only see the new Instructions, not the interaction history, so anything important must be summarized in the Instructions. Don't forget any important details in the current Instructions! Indicate the new Instructions by "Instructions: ...".
# """

meta_prompt = PromptTemplate(
input_variables=["chat_history"], template=meta_template
)
# meta_prompt = PromptTemplate(
# input_variables=["chat_history"], template=meta_template
# )

meta_chain = LLMChain(
llm=OpenAI(temperature=0),
prompt=meta_prompt,
verbose=True,
)
return meta_chain
# meta_chain = LLMChain(
# llm=OpenAI(temperature=0),
# prompt=meta_prompt,
# verbose=True,
# )
# return meta_chain

def meta_chain(self):
#define meta template and meta prompting as per your needs
self.meta_chain = initialize_meta_chain()
# def meta_chain(self):
# #define meta template and meta prompting as per your needs
# self.meta_chain = initialize_meta_chain()


def get_chat_history(chain_memory):
memory_key = chain_memory.memory_key
chat_history = chain_memory.load_memory_variables(memory_key)[memory_key]
return chat_history
# def get_chat_history(chain_memory):
# memory_key = chain_memory.memory_key
# chat_history = chain_memory.load_memory_variables(memory_key)[memory_key]
# return chat_history


def get_new_instructions(meta_output):
delimiter = "Instructions: "
new_instructions = meta_output[meta_output.find(delimiter) + len(delimiter) :]
return new_instructions
# def get_new_instructions(meta_output):
# delimiter = "Instructions: "
# new_instructions = meta_output[meta_output.find(delimiter) + len(delimiter) :]
# return new_instructions


def main(self, task, max_iters=3, max_meta_iters=5):
failed_phrase = "task failed"
success_phrase = "task succeeded"
key_phrases = [success_phrase, failed_phrase]
# def main(self, task, max_iters=3, max_meta_iters=5):
# failed_phrase = "task failed"
# success_phrase = "task succeeded"
# key_phrases = [success_phrase, failed_phrase]

instructions = "None"
for i in range(max_meta_iters):
print(f"[Episode {i+1}/{max_meta_iters}]")
self.initialize_chain(instructions)
output = self.agent.perform('Assistant', {'request': task})
for j in range(max_iters):
print(f"(Step {j+1}/{max_iters})")
print(f"Assistant: {output}")
print(f"Human: ")
human_input = input()
if any(phrase in human_input.lower() for phrase in key_phrases):
break
output = self.agent.perform('Assistant', {'request': human_input})
if success_phrase in human_input.lower():
print(f"You succeeded! Thanks for playing!")
return
self.initialize_meta_chain()
meta_output = self.meta_chain.predict(chat_history=self.get_chat_history())
print(f"Feedback: {meta_output}")
instructions = self.get_new_instructions(meta_output)
print(f"New Instructions: {instructions}")
print("\n" + "#" * 80 + "\n")
print(f"You failed! Thanks for playing!")
# instructions = "None"
# for i in range(max_meta_iters):
# print(f"[Episode {i+1}/{max_meta_iters}]")
# self.initialize_chain(instructions)
# output = self.agent.perform('Assistant', {'request': task})
# for j in range(max_iters):
# print(f"(Step {j+1}/{max_iters})")
# print(f"Assistant: {output}")
# print(f"Human: ")
# human_input = input()
# if any(phrase in human_input.lower() for phrase in key_phrases):
# break
# output = self.agent.perform('Assistant', {'request': human_input})
# if success_phrase in human_input.lower():
# print(f"You succeeded! Thanks for playing!")
# return
# self.initialize_meta_chain()
# meta_output = self.meta_chain.predict(chat_history=self.get_chat_history())
# print(f"Feedback: {meta_output}")
# instructions = self.get_new_instructions(meta_output)
# print(f"New Instructions: {instructions}")
# print("\n" + "#" * 80 + "\n")
# print(f"You failed! Thanks for playing!")


#init instance of MetaWorkerNode
meta_worker_node = MetaWorkerNode(llm=OpenAI, tools=tools, vectorstore=vectorstore)
# #init instance of MetaWorkerNode
# meta_worker_node = MetaWorkerNode(llm=OpenAI, tools=tools, vectorstore=vectorstore)


#specify a task and interact with the agent
task = "Provide a sysmatic argument for why we should always eat past with olives"
meta_worker_node.main(task)
# #specify a task and interact with the agent
# task = "Provide a sysmatic argument for why we should always eat past with olives"
# meta_worker_node.main(task)


####################################################################### => Boss Node
Expand Down Expand Up @@ -356,11 +360,11 @@ def execute_task(self, task):

boss_node = BossNode(llm=llm, vectorstore=vectorstore, task_execution_chain=agent_executor, verbose=True, max_iterations=5)

#create a task
task = boss_node.create_task(objective="Write a research paper on the impact of climate change on global agriculture")
# #create a task
# task = boss_node.create_task(objective="Write a research paper on the impact of climate change on global agriculture")

#execute the task
boss_node.execute_task(task)
# #execute the task
# boss_node.execute_task(task)


class Swarms:
Expand Down
5 changes: 3 additions & 2 deletions swarms/agents/workers/auto_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def _run(self, text: str) -> str:
ReadFileTool(root_dir="./data"),
process_csv,

# multimodal_agent_tool,


query_website_tool,
Terminal,
Expand All @@ -101,8 +103,7 @@ def _run(self, text: str) -> str:

agent_worker.chain.verbose = True

worker_agent = agent_worker

# worker_agent = agent_worker
# 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...
Expand Down

0 comments on commit 39ffd6a

Please sign in to comment.