Worker autonomous agent example

pull/53/head
Kye 1 year ago
parent ffea0d9810
commit 23ee3bb77d

@ -67,14 +67,14 @@
##########
from swarms import AutoBot
from swarms import Worker
worker = AutoBot(
node = Worker(
openai_api_key="",
ai_name="Optimus Prime",
)
task = "What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times."
response = worker.run(task)
response = node.run(task)
print(response)

@ -2,7 +2,8 @@
from swarms.orchestrator.autoscaler import AutoScaler
# worker
from swarms.workers.worker_node import WorkerNode
# from swarms.workers.worker_node import WorkerNode
from swarms.workers.workers import Workers
from swarms.workers.autobot import AutoBot
#boss

@ -0,0 +1,104 @@
import faiss
from langchain.chat_models import ChatOpenAI
from langchain.docstore import InMemoryDocstore
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain_experimental.autonomous_agents import AutoGPT
from swarms.agents.tools.autogpt import (
ReadFileTool,
WriteFileTool,
process_csv,
# web_search,
query_website_tool,
)
from swarms.utils.decorators import error_decorator, log_decorator, timing_decorator
ROOT_DIR = "./data/"
class Workers:
@log_decorator
@error_decorator
@timing_decorator
def __init__(self,
model_name="gpt-4",
openai_api_key=None,
ai_name="Autobot Swarm Worker",
ai_role="Worker in a swarm",
# embedding_size=None,
# k=None,
temperature=0.5):
self.openai_api_key = openai_api_key
self.temperature = temperature
try:
self.llm = ChatOpenAI(model_name=model_name,
openai_api_key=self.openai_api_key,
temperature=self.temperature)
except Exception as error:
raise RuntimeError(f"Error Initializing ChatOpenAI: {error}")
self.ai_name = ai_name
self.ai_role = ai_role
# self.embedding_size = embedding_size
# # self.k = k
self.setup_tools()
self.setup_memory()
self.setup_agent()
@log_decorator
@error_decorator
@timing_decorator
def setup_tools(self):
self.tools = [
WriteFileTool(root_dir=ROOT_DIR),
ReadFileTool(root_dir=ROOT_DIR),
process_csv,
query_website_tool,
]
def setup_memory(self):
try:
embeddings_model = OpenAIEmbeddings(openai_api_key=self.openai_api_key)
embedding_size = 1536
index = faiss.IndexFlatL2(embedding_size)
self.vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})
except Exception as error:
raise RuntimeError(f"Error setting up memory perhaps try try tuning the embedding size: {error}")
def setup_agent(self):
try:
self.agent = AutoGPT.from_llm_and_tools(
ai_name=self.ai_name,
ai_role=self.ai_role,
tools=self.tools,
llm=self.llm,
memory=self.vectorstore.as_retriever(search_kwargs={"k": 8}),
)
except Exception as error:
raise RuntimeError(f"Error setting up agent: {error}")
@log_decorator
@error_decorator
@timing_decorator
def run(self, task):
try:
result = self.agent.run([task])
return result
except Exception as error:
raise RuntimeError(f"Error while running agent: {error}")
@log_decorator
@error_decorator
@timing_decorator
def __call__(self, task):
try:
results = self.agent.run([task])
return results
except Exception as error:
raise RuntimeError(f"Error while running agent: {error}")
Loading…
Cancel
Save