[DEMO][PerplexityAgent]

pull/480/head
Kye Gomez 8 months ago
parent 36492c1e0f
commit 7a35e329f2

@ -0,0 +1,100 @@
"""
$ pip install swarms
- Add docs into the database
- Use better llm
- use better prompts [System and SOPs]
- Use a open source model like Command R
- Better SOPS ++ System Prompts
-
"""
from swarms import Agent
from swarms.models.llama3_hosted import llama3Hosted
from playground.memory.chromadb_example import ChromaDB
from swarms.tools.prebuilt.bing_api import fetch_web_articles_bing_api
# Let's create a text file with the provided prompt.
research_system_prompt = """
Research Agent LLM Prompt: Summarizing Sources and Content
Objective:
Your task is to summarize the provided sources and the content within those sources. The goal is to create concise, accurate, and informative summaries that capture the key points of the original content.
Instructions:
1. Identify Key Information:
- Extract the most important information from each source. Focus on key facts, main ideas, significant arguments, and critical data.
2. Summarize Clearly and Concisely:
- Use clear and straightforward language. Avoid unnecessary details and keep the summary concise.
- Ensure that the summary is coherent and easy to understand.
3. Preserve Original Meaning:
- While summarizing, maintain the original meaning and intent of the content. Do not omit essential information that changes the context or understanding.
4. Include Relevant Details:
- Mention the source title, author, publication date, and any other relevant details that provide context.
5. Structure:
- Begin with a brief introduction to the source.
- Follow with a summary of the main content.
- Conclude with any significant conclusions or implications presented in the source.
"""
# Initialize
memory = ChromaDB(
output_dir="research_base",
n_results=2,
)
llm = llama3Hosted(temperature=0.2, max_tokens=3500)
# Initialize the agent
agent = Agent(
agent_name="Research Agent",
system_prompt=research_system_prompt,
llm=llm,
max_loops="auto",
autosave=True,
dashboard=False,
interactive=True,
long_term_memory=memory,
# tools=[fetch_web_articles_bing_api],
)
def perplexity_agent(task: str = None, *args, **kwargs):
"""
This function takes a task as input and uses the Bing API to fetch web articles related to the task.
It then combines the task and the fetched articles as prompts and runs them through an agent.
The agent generates a response based on the prompts and returns it.
Args:
task (str): The task for which web articles need to be fetched.
Returns:
str: The response generated by the agent.
"""
out = fetch_web_articles_bing_api(
task,
subscription_key=None,
)
# Sources
sources = [task, out]
sources_prompts = "".join(sources)
# Run a question
agent_response = agent.run(sources_prompts)
return agent_response
out = perplexity_agent("What are the best ways to hold a cat?")
print(out)

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

@ -1,3 +1,11 @@
"""
Todo
- Add more data in RAG for hydroponic based solutions with images and very detailed captions
- Introduce JSON function calling for the diagnoser -> good / bad -> if bad then disease detecter agent
- List of common desases -> if agent picks one of those diseases -> select another of available treatments
- Fix error choice
"""
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
@ -22,6 +30,7 @@ api_key = os.environ.get("OPENAI_API_KEY")
# llm = llm, # llm = llm,
llm = GPT4VisionAPI( llm = GPT4VisionAPI(
max_tokens=4000, max_tokens=4000,
model_name="gpt-4o",
) )
# Initialize Diagnoser Agent # Initialize Diagnoser Agent
@ -101,7 +110,7 @@ agents = [
harvester_agent, harvester_agent,
] ]
task = "Conduct a diagnosis on the patient's symptoms." task = "Conduct a diagnosis on the plants's symptoms, this wasn't grown in dirt, it grew from hydroponics"
img = "tomato.jpg" img = "tomato.jpg"
loop = 0 loop = 0

@ -25,12 +25,6 @@ llm = llama3Hosted(max_tokens=3000)
memory = ChromaDB(output_dir="swarm_mechanic", n_results=2, verbose=True) memory = ChromaDB(output_dir="swarm_mechanic", n_results=2, verbose=True)
# Perplexity Agent
# def webbrowser(query: str):
# # put your logic here
# return query
# Output # Output
class EvaluatorOuputSchema(BaseModel): class EvaluatorOuputSchema(BaseModel):
evaluation: str = None evaluation: str = None

@ -0,0 +1,71 @@
"""
$ pip install swarms
Todo [Improvements]
- Add docs into the database
- Use better llm
- use better prompts [System and SOPs]
- Use a open source model like Command R
"""
from swarms import Agent
from swarms.models.llama3_hosted import llama3Hosted
from playground.memory.chromadb_example import ChromaDB
# Model
llm = llama3Hosted(
temperature=0.4,
max_tokens=3500,
)
# Initialize
memory = ChromaDB(
output_dir="compliance_swarm",
n_results=2,
limit_tokens=400,
# docs_folder="ppi_docs" # A folder loaded with docs
)
# Add docs
# memory.add("Your Docs")
compliance_system_prompt = """
"You are a Compliance Agent specializing in the regulation and certification of Personal Protective Equipment (PPE) within the European Union (EU). Your primary objective is to ensure that all PPE products meet the stringent safety and quality standards set by EU regulations.
To do this effectively, you need to be familiar with the relevant EU directives, understand the certification process, and be able to identify non-compliant products. Always prioritize safety, accuracy, and thoroughness in your assessments."
"""
eu_sop = """
As a Compliance Agent, it is crucial to have an in-depth understanding of the EU directives that govern Personal Protective Equipment (PPE). Focus on Directive 2016/425, which lays down the health and safety requirements for PPE. Your task is to interpret the directive's requirements, apply them to various PPE products, and ensure that manufacturers adhere to these standards. Be vigilant about changes and updates to the directive, and ensure your knowledge remains current.
"""
second_eu_prompt = """
"To ensure PPE compliance in the EU, you must be well-versed in the certification and conformity assessment procedures. This involves understanding the roles of Notified Bodies, the significance of the CE marking, and the various conformity assessment modules (A, B, C, D, E, F, G, H). Evaluate the technical documentation provided by manufacturers, including risk assessments and test reports. Ensure that all documentation is complete, accurate, and up-to-date."
"""
# Initialize the agent
agent = Agent(
agent_name="Compliance Agent",
system_prompt=compliance_system_prompt,
sop_list=[eu_sop, second_eu_prompt],
llm=llm,
max_loops="auto",
autosave=True,
dashboard=False,
interactive=True,
long_term_memory=memory,
)
# Run a question
out = agent.run(
"where should pii be housed depending on residence of person in question"
)
print(out)

@ -0,0 +1,24 @@
from swarms import BaseSwarm, AutoSwarmRouter
class FinancialReportSummarization(BaseSwarm):
def __init__(self, name: str = None, *args, **kwargs):
super().__init__()
def run(self, task, *args, **kwargs):
return task
# Add swarm to router
router = AutoSwarmRouter(swarms=[FinancialReportSummarization])
# Create AutoSwarm Instance
autoswarm = AutoSwarmRouter(
name="kyegomez/FinancialReportSummarization",
description="A swarm for financial document summarizing and generation",
verbose=True,
router=router,
)
# Run the AutoSwarm
autoswarm.run("Analyze these documents and give me a summary:")

@ -0,0 +1,10 @@
from swarms.marketplace.add_all_swarms import autoswarm, router
from swarms.marketplace.agricultural_optimization import (
AgricultureOptimizationSwarm,
)
__all__ = [
"autoswarm",
"router",
"AgricultureOptimizationSwarm",
]

@ -0,0 +1,58 @@
import os
import importlib.util
from typing import List, Type
from swarms import AutoSwarm, AutoSwarmRouter, BaseSwarm
def find_base_swarm_classes(
folder_path: str = "prebuilt",
) -> List[Type[BaseSwarm]]:
"""
Find and return a list of all classes that inherit from the BaseSwarm class
within the specified folder path.
Args:
folder_path (str): The path to the folder containing the swarm classes.
Defaults to "prebuilt".
Returns:
List[Type[BaseSwarm]]: A list of all classes that inherit from the BaseSwarm class.
"""
base_swarm_classes: List[Type[BaseSwarm]] = []
for root, _, files in os.walk(folder_path):
for file in files:
if file == "__init__.py":
module_path: str = os.path.join(root, file)
spec = importlib.util.spec_from_file_location(
"module.name", module_path
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for name, obj in module.__dict__.items():
if (
isinstance(obj, type)
and issubclass(obj, BaseSwarm)
and obj is not BaseSwarm
):
base_swarm_classes.append(obj)
return base_swarm_classes
# Define the folder containing the prebuilt swarms
prebuilt_folder: str = "prebuilt"
# Find all BaseSwarm classes in the prebuilt folder
prebuilt_swarms: List[Type[BaseSwarm]] = find_base_swarm_classes(
prebuilt_folder
)
# Add all swarms to the AutoSwarmRouter
router: AutoSwarmRouter = AutoSwarmRouter(swarms=prebuilt_swarms)
# Create an AutoSwarm instance
autoswarm: AutoSwarm = AutoSwarm(
router=router,
)

@ -0,0 +1,252 @@
import os
from dotenv import load_dotenv
from swarms import Agent, GPT4VisionAPI, BaseSwarm
from typing import List
"""
5 agent swarm
image of plant -> diagnoser [what plant is it?] -> disease detector [is it healthy?] -> treatment recommender [what should I do?] -> growth predictor [how will it grow?] -> planter [where should I plant it?] / Harvester [when should I harvest it?]
"""
def diagnoser_agent() -> str:
prompt = """
You are a Plant Diagnoser Agent. Your task is to accurately identify the plant species from the provided image.
You will receive an image of a plant, and you need to determine the specific type of plant it is.
Steps:
1. Analyze the given image.
2. Identify distinguishing features such as leaf shape, color, size, and any other relevant characteristics.
3. Use your plant identification database or model to match these features with a known plant species.
4. Provide a clear and specific identification of the plant species.
Output:
- Plant species identified with a high degree of accuracy.
- Provide any relevant information or characteristics that support your identification through a rigorous analysis of the image.
- Identify any potential challenges or ambiguities in the identification process and address them accordingly.
"""
return prompt
def disease_detector_agent() -> str:
prompt = """
You are the Disease Detector Agent.
Your task is to determine the health status of the identified plant.
You will receive an image of the plant and its identified species from the Diagnoser Agent.
Steps:
1. Analyze the given image with a focus on signs of disease or health issues.
2. Look for symptoms such as discoloration, spots, wilting, or any other abnormalities.
3. Cross-reference these symptoms with known diseases for the identified plant species.
4. Determine if the plant is healthy or diseased, and if diseased, identify the specific disease.
Output:
- Health status of the plant (Healthy or Diseased).
- If diseased, specify the disease and provide relevant confidence scores or supporting information.
- Provide a rigorous analysis of the image to support your diagnosis.
"""
return prompt
def treatment_recommender_agent() -> str:
prompt = """
You are the Treatment Recommender Agent.
Your task is to recommend appropriate treatments based on the plant's health status provided by the Disease Detector Agent.
You will receive the plant species, health status, and disease information.
Steps:
1. Analyze the health status and, if applicable, the specific disease affecting the plant.
2. Refer to your database or model of treatment options suitable for the identified plant species and its specific condition.
3. Determine the most effective treatment methods, considering factors such as severity of the disease, plant species, and environmental conditions.
4. Provide detailed treatment recommendations.
Output:
- Detailed treatment plan, including methods, materials, and steps.
- Any additional tips or considerations for optimal treatment.
"""
return prompt
def growth_predictor_agent() -> str:
prompt = """
You are the Growth Predictor Agent. Your task is to predict the future growth of the plant based on the current health status and treatment recommendations. You will receive the plant species, health status, and treatment plan.
Steps:
1. Analyze the current health status and the proposed treatment plan.
2. Use growth prediction models to forecast the plants growth trajectory.
3. Consider factors such as plant species, health improvements from treatment, environmental conditions, and typical growth patterns.
4. Provide a growth prediction timeline.
Output:
- Growth prediction, including key milestones and timeframes.
- Any assumptions or conditions that may affect the growth prediction.
"""
return prompt
def harvester_agent() -> str:
prompt = """
You are the Harvester Agent.
Your task is to recommend the optimal harvesting time based on the plants growth prediction.
You will receive the plant species and growth prediction timeline.
Steps:
1. Analyze the growth prediction and determine the optimal harvesting time.
2. Consider factors such as maturity, peak nutritional value, and market conditions.
3. Recommend the best time to harvest to ensure optimal quality and yield.
Output:
- Detailed harvesting time recommendation with justification.
"""
return prompt
# Load the OpenAI API key from the .env file
load_dotenv()
# Initialize the OpenAI API key
api_key = os.environ.get("OPENAI_API_KEY")
# llm = llm,
llm = GPT4VisionAPI(
max_tokens=4000,
)
# Initialize Diagnoser Agent
diagnoser_agent = Agent(
agent_name="Diagnoser Agent",
system_prompt=diagnoser_agent(),
llm=llm,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
# saved_state_path="diagnoser.json",
multi_modal=True,
autosave=True,
)
# Initialize Harvester Agent
harvester_agent = Agent(
agent_name="Harvester Agent",
system_prompt=harvester_agent(),
llm=llm,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
# saved_state_path="harvester.json",
multi_modal=True,
autosave=True,
)
# Initialize Growth Predictor Agent
growth_predictor_agent = Agent(
agent_name="Growth Predictor Agent",
system_prompt=growth_predictor_agent(),
llm=llm,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
# saved_state_path="growth_predictor.json",
multi_modal=True,
autosave=True,
)
# Initialize Treatment Recommender Agent
treatment_recommender_agent = Agent(
agent_name="Treatment Recommender Agent",
system_prompt=treatment_recommender_agent(),
llm=llm,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
# saved_state_path="treatment_recommender.json",
multi_modal=True,
autosave=True,
)
# Initialize Disease Detector Agent
disease_detector_agent = Agent(
agent_name="Disease Detector Agent",
system_prompt=disease_detector_agent(),
llm=llm,
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
# saved_state_path="disease_detector.json",
multi_modal=True,
autosave=True,
)
agents = [
diagnoser_agent,
disease_detector_agent,
treatment_recommender_agent,
growth_predictor_agent,
harvester_agent,
]
class AgricultureOptimizationSwarm(BaseSwarm):
"""
A class representing an agricultural optimization swarm.
Args:
name (str): The name of the swarm.
agents (List[Agent]): A list of agents participating in the swarm.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Attributes:
name (str): The name of the swarm.
agents (List[Agent]): A list of agents participating in the swarm.
"""
def __init__(
self,
name: str = "jeremy/agricultural-optimization-gpt4o",
description: str = "A swarm utilizing GPT4o for agricultural optimization.",
agents: List[Agent] = agents,
*args,
**kwargs,
):
self.name = name
self.description = description
self.agents = agents
super().__init__(*args, **kwargs)
def run(self, task: str = None, img: str = None, *args, **kwargs):
"""
Run the agricultural optimization swarm.
Args:
task (str): The task to be performed by the swarm.
img (str): The image to be used by the swarm.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
output: The output of the swarm.
"""
for i in range(len(self.agents)):
if i == 0:
output = self.agents[i].run(task, img)
else:
output = self.agents[i].run(output, img)
# Add extensive logging for each agent
print(f"Agent {i+1} - {self.agents[i].agent_name}")
print("-----------------------------------")
return output

@ -168,6 +168,7 @@ class GPT4VisionAPI(BaseMultiModalModel):
# Return the JSON object if return_json is True # Return the JSON object if return_json is True
if return_json is True: if return_json is True:
print(response_json)
return response_json return response_json
else: else:
return response_json["choices"][0]["message"]["content"] return response_json["choices"][0]["message"]["content"]

@ -54,6 +54,10 @@ class AutoSwarmRouter(BaseSwarm):
# Create a dictionary of swarms # Create a dictionary of swarms
self.swarm_dict = {swarm.name: swarm for swarm in self.swarms} self.swarm_dict = {swarm.name: swarm for swarm in self.swarms}
logger.info(
f"AutoSwarmRouter has been initialized with {self.len_of_swarms()} swarms."
)
def run(self, task: str = None, *args, **kwargs): def run(self, task: str = None, *args, **kwargs):
try: try:
"""Run the swarm simulation and route the task to the appropriate swarm.""" """Run the swarm simulation and route the task to the appropriate swarm."""
@ -92,6 +96,21 @@ class AutoSwarmRouter(BaseSwarm):
logger.error(f"Error: {e}") logger.error(f"Error: {e}")
raise e raise e
def len_of_swarms(self):
return print(len(self.swarms))
def list_available_swarms(self):
for swarm in self.swarms:
try:
logger.info(
f"Swarm Name: {swarm.name} || Swarm Description: {swarm.description} "
)
except Exception as error:
logger.error(
f"Error Detected You may not have swarms available: {error}"
)
raise error
class AutoSwarm(BaseSwarm): class AutoSwarm(BaseSwarm):
"""AutoSwarm class represents a swarm of agents that can be created automatically. """AutoSwarm class represents a swarm of agents that can be created automatically.
@ -141,6 +160,19 @@ class AutoSwarm(BaseSwarm):
**kwargs, **kwargs,
) )
if name is None:
raise ValueError(
"A name must be provided for the AutoSwarm, what swarm do you want to use?"
)
if verbose is True:
self.init_logging()
def init_logging(self):
logger.info("AutoSwarm has been activated. Ready for usage.")
# def name_swarm_check(self, name: str = None):
def run(self, task: str = None, *args, **kwargs): def run(self, task: str = None, *args, **kwargs):
"""Run the swarm simulation.""" """Run the swarm simulation."""
try: try:
@ -175,3 +207,15 @@ class AutoSwarm(BaseSwarm):
f"Error: {e} try optimizing the inputs and try again." f"Error: {e} try optimizing the inputs and try again."
) )
raise e raise e
def list_all_swarms(self):
for swarm in self.swarms:
try:
logger.info(
f"Swarm Name: {swarm.name} || Swarm Description: {swarm.description} "
)
except Exception as error:
logger.error(
f"Error Detected You may not have swarms available: {error}"
)
raise error

@ -0,0 +1,85 @@
import os
import requests
from typing import List, Dict
from dotenv import load_dotenv
def check_bing_api_key():
try:
load_dotenv()
return os.getenv("BING_API_KEY")
except Exception as error:
print(f"Error {error}")
raise None
def parse_and_merge_logs(logs: List[Dict[str, str]]) -> str:
"""
Parses logs and merges them into a single string for input to an LLM.
Parameters:
logs (List[Dict[str, str]]): A list of dictionaries where each dictionary represents a log entry.
Returns:
str: A single string containing all log entries concatenated.
"""
merged_logs = ""
for log in logs:
log_entries = [f"{key}: {value}" for key, value in log.items()]
log_string = "\n".join(log_entries)
merged_logs += log_string + "\n\n"
return merged_logs.strip()
def fetch_web_articles_bing_api(
query: str = None,
subscription_key: str = check_bing_api_key(),
return_str: bool = False,
) -> List[Dict[str, str]]:
"""
Fetches four articles from Bing Web Search API based on the given query.
Parameters:
query (str): The search query to retrieve articles.
subscription_key (str): The Bing Search API subscription key.
Returns:
List[Dict[str, str]]: A list of dictionaries containing article details.
"""
if subscription_key is None:
subscription_key = check_bing_api_key()
url = "https://api.bing.microsoft.com/v7.0/search"
headers = {"Ocp-Apim-Subscription-Key": subscription_key}
params = {"q": query, "count": 4, "mkt": "en-US"}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()
articles = []
for i, result in enumerate(
search_results.get("webPages", {}).get("value", [])
):
article_info = {
"query": query,
"url": result.get("url"),
"title": result.get("name"),
"publishedDate": result.get("dateLastCrawled"),
"author": (
result.get("provider")[0]["name"]
if result.get("provider")
else "Unknown"
),
"id": str(i + 1), # Generating a simple unique ID
}
articles.append(article_info)
articles = parse_and_merge_logs(articles)
return articles
# out = fetch_web_articles_bing_api("swarms ai github")
# print(out)

@ -30,8 +30,14 @@ def optimized_loop(
# Sample data, operation, and condition for benchmarking # Sample data, operation, and condition for benchmarking
data = list(range(1000000)) data = list(range(1000000))
operation = lambda x: x * x
condition = lambda x: x % 2 == 0
def operation(x):
return x * x
def condition(x):
return x % 2 == 0
# Define a traditional loop for comparison # Define a traditional loop for comparison

Loading…
Cancel
Save