commit
97b394d7cd
@ -1,131 +1,16 @@
|
||||
|
||||
# Swarms
|
||||
|
||||
Orchestrate enterprise-grade agents for multi-agent collaboration and orchestration to automate real-world problems.
|
||||
|
||||
<div style="display:flex; margin:0 auto; justify-content: center;">
|
||||
<div style="width:25%">
|
||||
<h2>Core Concepts</h2>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="swarms/structs/agent.md">
|
||||
Agents
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="swarms/memory/diy_memory.md">
|
||||
Memory
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="swarms/tools/main.md">
|
||||
Tools
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="swarms/structs/task.md">
|
||||
Tasks
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="swarms/structs/agent_rearrange.md">
|
||||
Multi-Agent Orchestration
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div style="width:25%">
|
||||
<h2>How-To Guides</h2>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="swarms/install/install.md">
|
||||
Installing Swarms
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="swarms/install/docker_setup.md">
|
||||
Docker Setup
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="./how-to/Create-Custom-Tools">
|
||||
Create Custom Tools
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="swarms/structs/agent_rearrange.md">
|
||||
Multi-Agent Flows
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="swarms/structs/sequential_workflow.md">
|
||||
Sequential Workflows
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="./how-to/LLM-Connections">
|
||||
Connecting to LLMs
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="./how-to/Customizing-Agents">
|
||||
Customizing Agents
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="./how-to/Human-Input-on-Execution">
|
||||
Human Input on Execution
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="./how-to/AgentOps-Observability">
|
||||
Agent Monitoring with AgentOps
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div style="width:25%">
|
||||
<h2>Examples</h2>
|
||||
<ul>
|
||||
<li>
|
||||
<a target='_blank' href="applications/business-analyst-agent.md">
|
||||
Swarm of Buisness Analysts for Business Reports
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a target='_blank' href="https://medium.com/@kyeg/building-compliance-agents-with-chroma-db-llama3-sop-prompting-0ed3e73559d2">
|
||||
Compliance Swarm for Customer Privacy
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a target='_blank' href="https://medium.com/@kyeg/announcing-neosapiens-self-replicating-swarms-0a47410aafa7">
|
||||
Self-Replicating Hiearchical Swarms
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div style="width:25%">
|
||||
<h2>Community</h2>
|
||||
<ul>
|
||||
<li>
|
||||
<a target='_blank' href="https://discord.gg/3Zck7nX6">
|
||||
Join the Swarms Community!
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a target='_blank' href="https://github.com/kyegomez/swarm-ecosystem">
|
||||
Swarms Ecosystem
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a target='_blank' href="https://cal.com/swarms/swarms-onboarding-session">
|
||||
Support Team
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a target='_blank' href="https://cal.com/swarms/swarms-onboarding-session">
|
||||
Book a 1 on 1 Call With Founder: Kye
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
| Core Concepts | How-To Guides | Examples | Community |
|
||||
|--------------------------------------------------|----------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
|
||||
| [Agents](swarms/structs/agent) | [Installing Swarms](swarms/install/install) | [Swarm of Business Analysts for Business Reports](applications/business-analyst-agent) | [Join the Swarms Community!](https://discord.gg/3Zck7nX6) |
|
||||
| [Memory](swarms/memory/diy_memory) | [Docker Setup](swarms/install/docker_setup) | [Compliance Swarm for Customer Privacy](https://medium.com/@kyeg/building-compliance-agents-with-chroma-db-llama3-sop-prompting-0ed3e73559d2) | [Swarms Ecosystem](https://github.com/kyegomez/swarm-ecosystem) |
|
||||
| [Tools](swarms/tools/main) | [Create Custom Tools](./how-to/Create-Custom-Tools) | [Self-Replicating Hierarchical Swarms](https://medium.com/@kyeg/announcing-neosapiens-self-replicating-swarms-0a47410aafa7) | [Support Team](https://cal.com/swarms/swarms-onboarding-session) |
|
||||
| [Tasks](swarms/structs/task) | [Multi-Agent Flows](swarms/structs/agent_rearrange) | | [Book a 1 on 1 Call With Founder: Kye](https://cal.com/swarms/swarms-onboarding-session) |
|
||||
| [Multi-Agent Orchestration](swarms/structs/agent_rearrange) | [Sequential Workflows](swarms/structs/sequential_workflow) | | |
|
||||
| | [Connecting to LLMs](./how-to/LLM-Connections) | | |
|
||||
| | [Customizing Agents](./how-to/Customizing-Agents) | | |
|
||||
| | [Human Input on Execution](./how-to/Human-Input-on-Execution) | | |
|
||||
| | [Agent Monitoring with AgentOps](./how-to/AgentOps-Observability) | | |
|
@ -0,0 +1,25 @@
|
||||
from swarms import Agent, OpenAIChat
|
||||
|
||||
|
||||
# Initialize the agent
|
||||
agent = Agent(
|
||||
agent_name="Transcript Generator",
|
||||
system_prompt="Generate a transcript for a youtube video on what swarms are!",
|
||||
agent_description=(
|
||||
"Generate a transcript for a youtube video on what swarms" " are!"
|
||||
),
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
dashboard=False,
|
||||
streaming_on=True,
|
||||
verbose=True,
|
||||
stopping_token="<DONE>",
|
||||
interactive=False,
|
||||
state_save_file_type="json",
|
||||
saved_state_path="transcript_generator.json",
|
||||
agent_ops_on=True,
|
||||
)
|
||||
|
||||
# Run the Agent on a task
|
||||
agent.run("Generate a transcript for a youtube video on what swarms are!")
|
@ -0,0 +1,82 @@
|
||||
from swarms import Agent, OpenAIChat
|
||||
|
||||
|
||||
def calculate_profit(revenue: float, expenses: float):
|
||||
"""
|
||||
Calculates the profit by subtracting expenses from revenue.
|
||||
|
||||
Args:
|
||||
revenue (float): The total revenue.
|
||||
expenses (float): The total expenses.
|
||||
|
||||
Returns:
|
||||
float: The calculated profit.
|
||||
"""
|
||||
return revenue - expenses
|
||||
|
||||
|
||||
def generate_report(company_name: str, profit: float):
|
||||
"""
|
||||
Generates a report for a company's profit.
|
||||
|
||||
Args:
|
||||
company_name (str): The name of the company.
|
||||
profit (float): The calculated profit.
|
||||
|
||||
Returns:
|
||||
str: The report for the company's profit.
|
||||
"""
|
||||
return f"The profit for {company_name} is ${profit}."
|
||||
|
||||
|
||||
def account_agent(task: str = None):
|
||||
"""
|
||||
delegate a task to an agent!
|
||||
|
||||
Task: str (What task to give to an agent)
|
||||
|
||||
"""
|
||||
agent = Agent(
|
||||
agent_name="Finance Agent",
|
||||
system_prompt="You're the Finance agent, your purpose is to generate a profit report for a company!",
|
||||
agent_description="Generate a profit report for a company!",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
dynamic_temperature_enabled=True,
|
||||
dashboard=False,
|
||||
verbose=True,
|
||||
# interactive=True, # Set to False to disable interactive mode
|
||||
# stopping_token="<DONE>",
|
||||
# saved_state_path="accounting_agent.json",
|
||||
# tools=[calculate_profit, generate_report],
|
||||
# docs_folder="docs",
|
||||
# pdf_path="docs/accounting_agent.pdf",
|
||||
)
|
||||
|
||||
out = agent.run(task)
|
||||
return out
|
||||
|
||||
|
||||
# Initialize the agent
|
||||
agent = Agent(
|
||||
agent_name="Accounting Assistant",
|
||||
system_prompt="You're the accounting agent, your purpose is to generate a profit report for a company!",
|
||||
agent_description="Generate a profit report for a company!",
|
||||
llm=OpenAIChat(),
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
dynamic_temperature_enabled=True,
|
||||
dashboard=False,
|
||||
verbose=True,
|
||||
# interactive=True, # Set to False to disable interactive mode
|
||||
# stopping_token="<DONE>",
|
||||
# saved_state_path="accounting_agent.json",
|
||||
tools=[account_agent],
|
||||
# docs_folder="docs",
|
||||
# pdf_path="docs/accounting_agent.pdf",
|
||||
)
|
||||
|
||||
agent.run(
|
||||
"Delegate a task to the accounting agent: what are the best ways to read cashflow statements"
|
||||
)
|
@ -1,182 +0,0 @@
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from functools import wraps
|
||||
from typing import Union
|
||||
|
||||
import torch
|
||||
from termcolor import colored
|
||||
from transformers import (
|
||||
AutoModelForSpeechSeq2Seq,
|
||||
AutoProcessor,
|
||||
pipeline,
|
||||
)
|
||||
|
||||
|
||||
def async_retry(max_retries=3, exceptions=(Exception,), delay=1):
|
||||
"""
|
||||
A decorator for adding retry logic to async functions.
|
||||
:param max_retries: Maximum number of retries before giving up.
|
||||
:param exceptions: A tuple of exceptions to catch and retry on.
|
||||
:param delay: Delay between retries.
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
@wraps(func)
|
||||
async def wrapper(*args, **kwargs):
|
||||
retries = max_retries
|
||||
while retries:
|
||||
try:
|
||||
return await func(*args, **kwargs)
|
||||
except exceptions as e:
|
||||
retries -= 1
|
||||
if retries <= 0:
|
||||
raise
|
||||
print(
|
||||
f"Retry after exception: {e}, Attempts"
|
||||
f" remaining: {retries}"
|
||||
)
|
||||
await asyncio.sleep(delay)
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
class DistilWhisperModel:
|
||||
"""
|
||||
This class encapsulates the Distil-Whisper model for English speech recognition.
|
||||
It allows for both synchronous and asynchronous transcription of short and long-form audio.
|
||||
|
||||
Args:
|
||||
model_id: The model ID to use. Defaults to "distil-whisper/distil-large-v2".
|
||||
|
||||
|
||||
Attributes:
|
||||
device: The device to use for inference.
|
||||
torch_dtype: The torch data type to use for inference.
|
||||
model_id: The model ID to use.
|
||||
model: The model instance.
|
||||
processor: The processor instance.
|
||||
|
||||
Usage:
|
||||
model_wrapper = DistilWhisperModel()
|
||||
transcription = model_wrapper('path/to/audio.mp3')
|
||||
|
||||
# For async usage
|
||||
transcription = asyncio.run(model_wrapper.async_transcribe('path/to/audio.mp3'))
|
||||
"""
|
||||
|
||||
def __init__(self, model_id="distil-whisper/distil-large-v2"):
|
||||
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
||||
self.torch_dtype = (
|
||||
torch.float16 if torch.cuda.is_available() else torch.float32
|
||||
)
|
||||
self.model_id = model_id
|
||||
self.model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
||||
model_id,
|
||||
torch_dtype=self.torch_dtype,
|
||||
low_cpu_mem_usage=True,
|
||||
use_safetensors=True,
|
||||
).to(self.device)
|
||||
self.processor = AutoProcessor.from_pretrained(model_id)
|
||||
|
||||
def __call__(self, inputs: Union[str, dict]):
|
||||
return self.transcribe(inputs)
|
||||
|
||||
def transcribe(self, inputs: Union[str, dict]):
|
||||
"""
|
||||
Synchronously transcribe the given audio input using the Distil-Whisper model.
|
||||
:param inputs: A string representing the file path or a dict with audio data.
|
||||
:return: The transcribed text.
|
||||
"""
|
||||
pipe = pipeline(
|
||||
"automatic-speech-recognition",
|
||||
model=self.model,
|
||||
tokenizer=self.processor.tokenizer,
|
||||
feature_extractor=self.processor.feature_extractor,
|
||||
max_new_tokens=128,
|
||||
torch_dtype=self.torch_dtype,
|
||||
device=self.device,
|
||||
)
|
||||
|
||||
return pipe(inputs)["text"]
|
||||
|
||||
@async_retry()
|
||||
async def async_transcribe(self, inputs: Union[str, dict]):
|
||||
"""
|
||||
Asynchronously transcribe the given audio input using the Distil-Whisper model.
|
||||
:param inputs: A string representing the file path or a dict with audio data.
|
||||
:return: The transcribed text.
|
||||
"""
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.run_in_executor(None, self.transcribe, inputs)
|
||||
|
||||
def real_time_transcribe(self, audio_file_path, chunk_duration=5):
|
||||
"""
|
||||
Simulates real-time transcription of an audio file, processing and printing results
|
||||
in chunks with colored output for readability.
|
||||
|
||||
:param audio_file_path: Path to the audio file to be transcribed.
|
||||
:param chunk_duration: Duration in seconds of each audio chunk to be processed.
|
||||
"""
|
||||
if not os.path.isfile(audio_file_path):
|
||||
print(colored("The audio file was not found.", "red"))
|
||||
return
|
||||
|
||||
# Assuming `chunk_duration` is in seconds and `processor` can handle chunk-wise processing
|
||||
try:
|
||||
with torch.no_grad():
|
||||
# Load the whole audio file, but process and transcribe it in chunks
|
||||
audio_input = self.processor.audio_file_to_array(
|
||||
audio_file_path
|
||||
)
|
||||
sample_rate = audio_input.sampling_rate
|
||||
len(audio_input.array) / sample_rate
|
||||
chunks = [
|
||||
audio_input.array[i : i + sample_rate * chunk_duration]
|
||||
for i in range(
|
||||
0,
|
||||
len(audio_input.array),
|
||||
sample_rate * chunk_duration,
|
||||
)
|
||||
]
|
||||
|
||||
print(
|
||||
colored("Starting real-time transcription...", "green")
|
||||
)
|
||||
|
||||
for i, chunk in enumerate(chunks):
|
||||
# Process the current chunk
|
||||
processed_inputs = self.processor(
|
||||
chunk,
|
||||
sampling_rate=sample_rate,
|
||||
return_tensors="pt",
|
||||
padding=True,
|
||||
)
|
||||
processed_inputs = processed_inputs.input_values.to(
|
||||
self.device
|
||||
)
|
||||
|
||||
# Generate transcription for the chunk
|
||||
logits = self.model.generate(processed_inputs)
|
||||
transcription = self.processor.batch_decode(
|
||||
logits, skip_special_tokens=True
|
||||
)[0]
|
||||
|
||||
# Print the chunk's transcription
|
||||
print(
|
||||
colored(f"Chunk {i+1}/{len(chunks)}: ", "yellow")
|
||||
+ transcription
|
||||
)
|
||||
|
||||
# Wait for the chunk's duration to simulate real-time processing
|
||||
time.sleep(chunk_duration)
|
||||
|
||||
except Exception as e:
|
||||
print(
|
||||
colored(
|
||||
f"An error occurred during transcription: {e}",
|
||||
"red",
|
||||
)
|
||||
)
|
@ -0,0 +1,48 @@
|
||||
from typing import List
|
||||
import json
|
||||
import loguru
|
||||
from swarms.utils.parse_code import extract_code_from_markdown
|
||||
|
||||
|
||||
def parse_and_execute_json(
|
||||
functions: List[callable] = None,
|
||||
json_string: str = None,
|
||||
parse_md: bool = False,
|
||||
):
|
||||
"""
|
||||
Parses and executes a JSON string containing function name and parameters.
|
||||
|
||||
Args:
|
||||
functions (List[callable]): A list of callable functions.
|
||||
json_string (str): The JSON string to parse and execute.
|
||||
parse_md (bool): Flag indicating whether to extract code from Markdown.
|
||||
|
||||
Returns:
|
||||
The result of executing the function with the parsed parameters, or None if an error occurs.
|
||||
|
||||
"""
|
||||
if parse_md:
|
||||
json_string = extract_code_from_markdown(json_string)
|
||||
|
||||
try:
|
||||
# Create a dictionary that maps function names to functions
|
||||
function_dict = {func.__name__: func for func in functions}
|
||||
|
||||
loguru.logger.info(f"Extracted code: {json_string}")
|
||||
data = json.loads(json_string)
|
||||
function_name = data.get("function", {}).get("name")
|
||||
parameters = data.get("function", {}).get("parameters")
|
||||
|
||||
# Check if the function name is in the function dictionary
|
||||
if function_name in function_dict:
|
||||
# Call the function with the parsed parameters
|
||||
result = function_dict[function_name](**parameters)
|
||||
return result
|
||||
else:
|
||||
loguru.logger.warning(
|
||||
f"No function named '{function_name}' found."
|
||||
)
|
||||
return None
|
||||
except Exception as e:
|
||||
loguru.logger.error(f"Error: {e}")
|
||||
return None
|
@ -0,0 +1,28 @@
|
||||
from swarms.utils.loguru_logger import logger
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
||||
def try_import_agentops(*args, **kwargs):
|
||||
try:
|
||||
load_dotenv()
|
||||
logger.info("Trying to import agentops")
|
||||
import agentops
|
||||
|
||||
agentops.init(os.getenv("AGENTOPS_API_KEY"), *args, **kwargs)
|
||||
|
||||
return "agentops imported successfully."
|
||||
except ImportError:
|
||||
logger.error("Could not import agentops")
|
||||
|
||||
|
||||
def end_session_agentops():
|
||||
try:
|
||||
logger.info("Trying to end session")
|
||||
import agentops
|
||||
|
||||
agentops.end_session("Success")
|
||||
return "Session ended successfully."
|
||||
except ImportError:
|
||||
logger.error("Could not import agentops")
|
||||
return "Could not end session."
|
Loading…
Reference in new issue