Skip to content

Commit

Permalink
[REFACTOR][Agent]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Mar 24, 2024
1 parent ab90a40 commit b3dc64c
Show file tree
Hide file tree
Showing 14 changed files with 555 additions and 334 deletions.
17 changes: 17 additions & 0 deletions Transcript Generator_state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"agent_id": "<function agent_id at 0x16368c400>",
"agent_name": "Transcript Generator",
"agent_description": "Generate a transcript for a youtube video on what swarms are!",
"system_prompt": "\n You are a fully autonomous agent serving the user in automating tasks, workflows, and activities. \n Agent's use custom instructions, capabilities, and data to optimize LLMs for a more narrow set of tasks.\n \n You will have internal dialogues with yourself and or interact with the user to aid in these tasks. \n Your responses should be coherent, contextually relevant, and tailored to the task at hand.\n",
"sop": null,
"short_memory": "system: \n You are a fully autonomous agent serving the user in automating tasks, workflows, and activities. \n Agent's use custom instructions, capabilities, and data to optimize LLMs for a more narrow set of tasks.\n \n You will have internal dialogues with yourself and or interact with the user to aid in these tasks. \n Your responses should be coherent, contextually relevant, and tailored to the task at hand.\n\n\n\nHuman:: Generate a transcript for a youtube video on what swarms are!\n\n\nTranscript Generator: \nSwarms are composed of large numbers of independent individuals that collectively carry out complex behaviors. For example, an ant colony functions as a swarm - each ant follows simple rules but together the colony can build intricate nests and find food.\n\nIn artificial swarms, we try to emulate these naturally-occurring phenomena. By programming basic behaviors into agents and allowing them to interact, we can observe emergent group behaviors without centralized control. For example, groups of robots may be designed with attraction and repulsion forces to self-assemble or explore environments.\n\nSimilarly, swarms may allow optimization algorithms to explore solutions in parallel. Each program follows their own trajectory while sharing information to converge on the best result. High-level commands give a rough direction, but the specific behaviors emerge from the interactions at the local level. \n\nPotential applications of artificial swarms include self-configuring robot teams for search & rescue, intelligent routing of network packets, and distributed processing for enhanced efficiency. The decentralized nature of swarms provides robustness, scalability and adaptability surpassing individual agents. \n\nBy harnessing simple local rules and interactions, swarm systems transcend the capabilities of any single member. They provide distributed solutions to coordinate large numbers independent agents to achieve a collective purpose.\n\n\nTranscript Generator: \nSwarms are composed of large numbers of independent individuals that collectively carry out complex behaviors. For example, an ant colony functions as a swarm - each ant follows simple rules but together the colony can build intricate nests and find food.\n\nIn artificial swarms, we try to emulate these naturally-occurring phenomena. By programming basic behaviors into agents and allowing them to interact, we can observe emergent group behaviors without centralized control. For example, groups of robots may be designed with attraction and repulsion forces to self-assemble or explore environments.\n\nSimilarly, swarms may allow optimization algorithms to explore solutions in parallel. Each program follows their own trajectory while sharing information to converge on the best result. High-level commands give a rough direction, but the specific behaviors emerge from the interactions at the local level. \n\nPotential applications of artificial swarms include self-configuring robot teams for search & rescue, intelligent routing of network packets, and distributed processing for enhanced efficiency. The decentralized nature of swarms provides robustness, scalability and adaptability surpassing individual agents. \n\nBy harnessing simple local rules and interactions, swarm systems transcend the capabilities of any single member. They provide distributed solutions to coordinate large numbers independent agents to achieve a collective purpose.\n\n\nHuman:: what is your purpose\n\n",
"loop_interval": 1,
"retry_attempts": 3,
"retry_interval": 1,
"interactive": true,
"dashboard": false,
"dynamic_temperature": false,
"autosave": true,
"saved_state_path": "Transcript Generator_state.json",
"max_loops": 1
}
7 changes: 4 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
" are!"
),
llm=Anthropic(),
max_loops="auto",
max_loops=3,
autosave=True,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
interactive=True,
)

# Run the workflow on a task
agent(
out = agent(
"Generate a transcript for a youtube video on what swarms are!"
" Output a <DONE> token when done."
)
print(out)
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "swarms"
version = "4.3.7"
version = "4.5.8"
description = "Swarms - Pytorch"
license = "MIT"
authors = ["Kye Gomez <[email protected]>"]
Expand Down Expand Up @@ -33,16 +33,15 @@ google-generativeai = "0.3.1"
langchain = "0.1.13"
langchain-core = "0.1.33"
langchain-community = "0.0.29"
langsmith = "0.1.17"
langchain-openai = "0.0.5"
langchain-experimental = "0.0.55"
faiss-cpu = "1.7.4"
backoff = "2.2.1"
datasets = "*"
optimum = "1.15.0"
supervision = "0.19.0"
opencv-python = "4.9.0.80"
diffusers = "*"
anthropic = "0.2.5"
anthropic = "0.21.3"
toml = "*"
pypdf = "4.1.0"
accelerate = "*"
Expand All @@ -64,7 +63,6 @@ sentence-transformers = "*"
peft = "*"
psutil = "*"
timm = "*"
supervision = "*"
sentry-sdk = "*"

[tool.poetry.dev-dependencies]
Expand Down
72 changes: 46 additions & 26 deletions swarms/agents/tool_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from swarms.structs.agent import Agent
from swarms.tools.format_tools import Jsonformer
from swarms.utils.loguru_logger import logger


class ToolAgent(Agent):
Expand Down Expand Up @@ -68,13 +69,14 @@ def __init__(
json_schema: Any = None,
max_number_tokens: int = 500,
parsing_function: Optional[Callable] = None,
llm: Any = None,
*args,
**kwargs,
):
super().__init__(
agent_name=name,
agent_description=description,
sop=f"{name} {description} {str(json_schema)}" * args,
llm=llm,
**kwargs,
)
self.name = name
Expand All @@ -101,33 +103,51 @@ def run(self, task: str, *args, **kwargs):
Exception: If an error occurs during the execution of the tool agent.
"""
try:
self.toolagent = Jsonformer(
model=self.model,
tokenizer=self.tokenizer,
json_schema=self.json_schema,
prompt=task,
max_number_tokens=self.max_number_tokens,
*args,
**kwargs,
)
if self.model:
logger.info(f"Running {self.name} for task: {task}")
self.toolagent = Jsonformer(
model=self.model,
tokenizer=self.tokenizer,
json_schema=self.json_schema,
llm=self.llm,
prompt=task,
max_number_tokens=self.max_number_tokens,
*args,
**kwargs,
)

if self.parsing_function:
out = self.parsing_function(self.toolagent())
else:
out = self.toolagent()

return out
elif self.llm:
logger.info(f"Running {self.name} for task: {task}")
self.toolagent = Jsonformer(
json_schema=self.json_schema,
llm=self.llm,
prompt=task,
max_number_tokens=self.max_number_tokens,
*args,
**kwargs,
)

if self.parsing_function:
out = self.parsing_function(self.toolagent())
else:
out = self.toolagent()

return out

if self.parsing_function:
out = self.parsing_function(self.toolagent())
else:
out = self.toolagent()
raise Exception(
"Either model or llm should be provided to the"
" ToolAgent"
)

return out
except Exception as error:
print(f"[Error] [ToolAgent] {error}")
logger.error(
f"Error running {self.name} for task: {task}"
)
raise error

def __call__(self, task: str, *args, **kwargs):
"""Call self as a function.
Args:
task (str): _description_
Returns:
_type_: _description_
"""
return self.run(task, *args, **kwargs)
11 changes: 1 addition & 10 deletions swarms/memory/base_vectordb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from abc import ABC, abstractmethod
from abc import ABC


class AbstractVectorDatabase(ABC):
Expand All @@ -12,7 +12,6 @@ class AbstractVectorDatabase(ABC):
"""

@abstractmethod
def connect(self):
"""
Connect to the database.
Expand All @@ -21,7 +20,6 @@ def connect(self):
"""

@abstractmethod
def close(self):
"""
Close the database connection.
Expand All @@ -30,7 +28,6 @@ def close(self):
"""

@abstractmethod
def query(self, query: str):
"""
Execute a database query.
Expand All @@ -42,7 +39,6 @@ def query(self, query: str):
"""

@abstractmethod
def fetch_all(self):
"""
Fetch all rows from the result set.
Expand All @@ -54,7 +50,6 @@ def fetch_all(self):
"""

@abstractmethod
def fetch_one(self):
"""
Fetch one row from the result set.
Expand All @@ -66,7 +61,6 @@ def fetch_one(self):
"""

@abstractmethod
def add(self, doc: str):
"""
Add a new record to the database.
Expand All @@ -79,7 +73,6 @@ def add(self, doc: str):
"""

@abstractmethod
def get(self, query: str):
"""
Get a record from the database.
Expand All @@ -95,7 +88,6 @@ def get(self, query: str):
"""

@abstractmethod
def update(self, doc):
"""
Update a record in the database.
Expand All @@ -109,7 +101,6 @@ def update(self, doc):
"""

@abstractmethod
def delete(self, message):
"""
Delete a record from the database.
Expand Down
10 changes: 10 additions & 0 deletions swarms/models/mistral_model_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from swarms.models.popular_llms import OpenAIChat


class MistralAPILLM(OpenAIChat):
def __init__(self, url):
super().__init__()
self.openai_proxy_url = url

def __call__(self, task: str):
super().__call__(task)
21 changes: 9 additions & 12 deletions swarms/models/together.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,15 @@ def run(self, task: str = None, *args, **kwargs):
)

out = response.json()
if "choices" in out and out["choices"]:
content = (
out["choices"][0]
.get("message", {})
.get("content", None)
)
if self.streaming_enabled:
content = self.stream_response(content)
return content
else:
print("No valid response in 'choices'")
return None
content = (
out["choices"][0]
.get("message", {})
.get("content", None)
)
if self.streaming_enabled:
content = self.stream_response(content)

return content

except Exception as error:
print(
Expand Down
Loading

0 comments on commit b3dc64c

Please sign in to comment.