pull/552/head
Kye Gomez 5 months ago
parent 52d2e0e031
commit 51802e7f30

1
.gitignore vendored

@ -13,6 +13,7 @@ static/generated
runs runs
Financial-Analysis-Agent_state.json Financial-Analysis-Agent_state.json
artifacts_five artifacts_five
errors
chroma chroma
Accounting Assistant_state.json Accounting Assistant_state.json
Unit Testing Agent_state.json Unit Testing Agent_state.json

@ -1,8 +1,6 @@
### Swarms Tool Documentation Page ### Swarms Tool Documentation
Welcome to the Swarms Tool Documentation! A tool is a Python function designed to perform specific tasks, with clear type annotations and comprehensive docstrings. Below are examples of tools to help you get started.
Here you will find examples and guidelines on how to build and use tools in the Swarms environment. A tool is a Python function designed to perform specific tasks, with clear type annotations and comprehensive docstrings. Below are examples of tools to help you get started.
# Rules # Rules
@ -31,9 +29,6 @@ To create a tool in the Swarms environment, follow these rules:
### Example Tools ### Example Tools
### Examples and Anti-Examples ### Examples and Anti-Examples
#### Example 1: Fetch Financial News #### Example 1: Fetch Financial News
@ -351,3 +346,239 @@ By following the examples provided, you can create your own tools to perform var
## Integrate tools into Agent
To integrate tools into an agent, you'd simply just pass in a callable function with types and documentation into the agent class.
```python
from swarms import Agent, OpenAIChat # ChromaDB
import subprocess
# Model
llm = OpenAIChat(
temperature=0.1,
)
# Tools
def terminal(
code: str,
):
"""
Run code in the terminal.
Args:
code (str): The code to run in the terminal.
Returns:
str: The output of the code.
"""
out = subprocess.run(
code, shell=True, capture_output=True, text=True
).stdout
return str(out)
def browser(query: str):
"""
Search the query in the browser with the `browser` tool.
Args:
query (str): The query to search in the browser.
Returns:
str: The search results.
"""
import webbrowser
url = f"https://www.google.com/search?q={query}"
webbrowser.open(url)
return f"Searching for {query} in the browser."
def create_file(file_path: str, content: str):
"""
Create a file using the file editor tool.
Args:
file_path (str): The path to the file.
content (str): The content to write to the file.
Returns:
str: The result of the file creation operation.
"""
with open(file_path, "w") as file:
file.write(content)
return f"File {file_path} created successfully."
def file_editor(file_path: str, mode: str, content: str):
"""
Edit a file using the file editor tool.
Args:
file_path (str): The path to the file.
mode (str): The mode to open the file in.
content (str): The content to write to the file.
Returns:
str: The result of the file editing operation.
"""
with open(file_path, mode) as file:
file.write(content)
return f"File {file_path} edited successfully."
# Agent
agent = Agent(
agent_name="Devin",
system_prompt=(
"Autonomous agent that can interact with humans and other"
" agents. Be Helpful and Kind. Use the tools provided to"
" assist the user. Return all code in markdown format."
),
llm=llm,
max_loops="auto",
autosave=True,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
interactive=True,
tools=[terminal, browser, file_editor, create_file],
# long_term_memory=chromadb,
metadata_output_type="json",
# List of schemas that the agent can handle
# list_base_models=[tool_schema],
function_calling_format_type="OpenAI",
function_calling_type="json", # or soon yaml
)
# Run the agent
agent.run("Create a new file for a plan to take over the world.")
```
## Example 2
```python
import os
import requests
from swarms import Agent, OpenAIChat
# Get the OpenAI API key from the environment variable
api_key = os.getenv("OPENAI_API_KEY")
# Create an instance of the OpenAIChat class
model = OpenAIChat(
api_key=api_key, model_name="gpt-4o-mini", temperature=0.1
)
def fetch_financial_news(
query: str = "Nvidia news", num_articles: int = 5
) -> str:
"""
Fetches financial news from the Google News API and returns a formatted string of the top news.
Args:
api_key (str): Your Google News API key.
query (str): The query term to search for news. Default is "financial".
num_articles (int): The number of top articles to fetch. Default is 5.
Returns:
str: A formatted string of the top financial news articles.
Raises:
ValueError: If the API response is invalid or there are no articles found.
requests.exceptions.RequestException: If there is an error with the request.
"""
url = "https://newsapi.org/v2/everything"
params = {
"q": query,
"apiKey": os.getenv("NEWSAPI_KEY"),
"pageSize": num_articles,
"sortBy": "relevancy",
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
if "articles" not in data or len(data["articles"]) == 0:
raise ValueError("No articles found or invalid API response.")
articles = data["articles"]
formatted_articles = []
for i, article in enumerate(articles, start=1):
title = article.get("title", "No Title")
description = article.get("description", "No Description")
url = article.get("url", "No URL")
formatted_articles.append(
f"{i}. {title}\nDescription: {description}\nRead more: {url}\n"
)
return "\n".join(formatted_articles)
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")
raise
except ValueError as e:
print(f"Value Error: {e}")
raise
# # Example usage:
# api_key = "ceabc81a7d8f45febfedadb27177f3a3"
# print(fetch_financial_news(api_key))
# Initialize the agent
agent = Agent(
agent_name="Financial-Analysis-Agent",
# system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
llm=model,
max_loops=2,
autosave=True,
# dynamic_temperature_enabled=True,
dashboard=False,
verbose=True,
streaming_on=True,
# interactive=True, # Set to False to disable interactive mode
dynamic_temperature_enabled=True,
saved_state_path="finance_agent.json",
tools=[fetch_financial_news],
# stopping_token="Stop!",
# interactive=True,
# docs_folder="docs", # Enter your folder name
# pdf_path="docs/finance_agent.pdf",
# sop="Calculate the profit for a company.",
# sop_list=["Calculate the profit for a company."],
user_name="swarms_corp",
# # docs=
# # docs_folder="docs",
retry_attempts=3,
# context_length=1000,
# tool_schema = dict
context_length=200000,
# tool_schema=
# tools
# agent_ops_on=True,
# long_term_memory=ChromaDB(docs_folder="artifacts"),
)
# Run the agent
response = agent("What are the latest financial news on Nvidia?")
print(response)
```

@ -1,40 +0,0 @@
import os
from dotenv import load_dotenv
from swarms import Agent, OpenAIChat
from swarms import tool
load_dotenv()
api_key = os.environ.get("OPENAI_API_KEY")
llm = OpenAIChat(api_key=api_key)
@tool
def search_api(query: str) -> str:
"""Search API
Args:
query (str): _description_
Returns:
str: _description_
"""
print(f"Searching API for {query}")
## Initialize the workflow
agent = Agent(
llm=llm,
max_loops=5,
tools=[search_api],
dashboard=True,
)
out = agent.run(
"Use the search api to find the best restaurants in New York" " City."
)
print(out)

@ -3,9 +3,6 @@ import os
import requests import requests
from swarms import Agent, OpenAIChat from swarms import Agent, OpenAIChat
from swarms.prompts.finance_agent_sys_prompt import (
FINANCIAL_AGENT_SYS_PROMPT,
)
# Get the OpenAI API key from the environment variable # Get the OpenAI API key from the environment variable
api_key = os.getenv("OPENAI_API_KEY") api_key = os.getenv("OPENAI_API_KEY")
@ -90,7 +87,7 @@ agent = Agent(
# interactive=True, # Set to False to disable interactive mode # interactive=True, # Set to False to disable interactive mode
dynamic_temperature_enabled=True, dynamic_temperature_enabled=True,
saved_state_path="finance_agent.json", saved_state_path="finance_agent.json",
# tools=[fetch_financial_news], tools=[fetch_financial_news],
# stopping_token="Stop!", # stopping_token="Stop!",
# interactive=True, # interactive=True,
# docs_folder="docs", # Enter your folder name # docs_folder="docs", # Enter your folder name
@ -111,23 +108,6 @@ agent = Agent(
) )
def run_finance_agent(query: str) -> str: # Run the agent
""" response = agent("What are the latest financial news on Nvidia?")
Runs the financial analysis agent with the given query.
Args:
query (str): The user query to run the agent with.
Returns:
str: The response from the financial analysis agent.
"""
query = fetch_financial_news(query)
print(query)
response = agent(query)
return response
# Example usage:
query = "Nvidia news"
response = run_finance_agent(f"Summarize the latest Nvidia financial news {query}")
print(response) print(response)

@ -5,12 +5,12 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry] [tool.poetry]
name = "swarms" name = "swarms"
version = "5.4.4" version = "5.4.5"
description = "Swarms - Pytorch" description = "Swarms - Pytorch"
license = "MIT" license = "MIT"
authors = ["Kye Gomez <kye@apac.ai>"] authors = ["Kye Gomez <kye@apac.ai>"]
homepage = "https://github.com/kyegomez/swarms" homepage = "https://github.com/kyegomez/swarms"
documentation = "https://swarms.world" documentation = "https://docs.swarms.world"
readme = "README.md" readme = "README.md"
repository = "https://github.com/kyegomez/swarms" repository = "https://github.com/kyegomez/swarms"
keywords = [ keywords = [
@ -30,7 +30,11 @@ keywords = [
"Multi-Grade-Agents", "Multi-Grade-Agents",
"Swarms", "Swarms",
"Transformers", "Transformers",
"LLMs",
"Prompt Engineering",
"Agents",
"Generative Agents",
"Generative AI",
] ]
classifiers = [ classifiers = [
"Development Status :: 4 - Beta", "Development Status :: 4 - Beta",
@ -69,7 +73,8 @@ networkx = "*"
swarms-memory = "*" swarms-memory = "*"
# [tool.poetry.scripts]
# swarms = "swarms.cli:main"
[tool.poetry.group.lint.dependencies] [tool.poetry.group.lint.dependencies]
black = ">=23.1,<25.0" black = ">=23.1,<25.0"

@ -17,7 +17,7 @@ SWARMS_LOGO = """
RED_COLOR_CODE = "\033[91m" RED_COLOR_CODE = "\033[91m"
RESET_COLOR_CODE = "\033[0m" RESET_COLOR_CODE = "\033[0m"
# print(RED_COLOR_CODE + SWARMS_LOGO + RESET_COLOR_CODE) print(RED_COLOR_CODE + SWARMS_LOGO + RESET_COLOR_CODE)
def main(): def main():
@ -87,5 +87,5 @@ def main():
parser.print_help() parser.print_help()
if __name__ == "__main__": # if __name__ == "__main__":
main() # main()

Loading…
Cancel
Save