diff --git a/.gitignore b/.gitignore index 50ad6dc1..2e45b091 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ static/generated runs Financial-Analysis-Agent_state.json artifacts_five +errors chroma Accounting Assistant_state.json Unit Testing Agent_state.json diff --git a/docs/swarms/tools/build_tool.md b/docs/swarms/tools/build_tool.md index 0bb0c9cd..fb680de6 100644 --- a/docs/swarms/tools/build_tool.md +++ b/docs/swarms/tools/build_tool.md @@ -1,8 +1,6 @@ -### Swarms Tool Documentation Page +### Swarms Tool Documentation -Welcome to the Swarms Tool Documentation! - -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. +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 @@ -31,9 +29,6 @@ To create a tool in the Swarms environment, follow these rules: ### Example Tools - - - ### Examples and Anti-Examples #### 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="", + 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) + + +``` diff --git a/playground/agents/tools/agent_with_tools_example.py b/playground/agents/tools/agent_with_tools_example.py deleted file mode 100644 index 068b3465..00000000 --- a/playground/agents/tools/agent_with_tools_example.py +++ /dev/null @@ -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) diff --git a/playground/agents/first_agent_example.py b/playground/agents/use_cases/finance/first_agent_example.py similarity index 100% rename from playground/agents/first_agent_example.py rename to playground/agents/use_cases/finance/first_agent_example.py diff --git a/main.py b/playground/agents/use_cases/finance/main.py similarity index 83% rename from main.py rename to playground/agents/use_cases/finance/main.py index aef0de2f..9961c4b5 100644 --- a/main.py +++ b/playground/agents/use_cases/finance/main.py @@ -3,9 +3,6 @@ import os import requests 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 api_key = os.getenv("OPENAI_API_KEY") @@ -90,7 +87,7 @@ agent = Agent( # interactive=True, # Set to False to disable interactive mode dynamic_temperature_enabled=True, saved_state_path="finance_agent.json", - # tools=[fetch_financial_news], + tools=[fetch_financial_news], # stopping_token="Stop!", # interactive=True, # docs_folder="docs", # Enter your folder name @@ -111,23 +108,6 @@ agent = Agent( ) -def run_finance_agent(query: str) -> str: - """ - 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}") +# Run the agent +response = agent("What are the latest financial news on Nvidia?") print(response) diff --git a/openai_model.py b/playground/agents/use_cases/finance/openai_model.py similarity index 100% rename from openai_model.py rename to playground/agents/use_cases/finance/openai_model.py diff --git a/playground/agents/basic_agent_with_azure_openai.py b/playground/agents/various_models/basic_agent_with_azure_openai.py similarity index 100% rename from playground/agents/basic_agent_with_azure_openai.py rename to playground/agents/various_models/basic_agent_with_azure_openai.py diff --git a/playground/agents/custom_model_with_agent.py b/playground/agents/various_models/custom_model_with_agent.py similarity index 100% rename from playground/agents/custom_model_with_agent.py rename to playground/agents/various_models/custom_model_with_agent.py diff --git a/playground/agents/example_agent.py b/playground/agents/various_models/example_agent.py similarity index 100% rename from playground/agents/example_agent.py rename to playground/agents/various_models/example_agent.py diff --git a/playground/agents/llama3_agent.py b/playground/agents/various_models/llama3_agent.py similarity index 100% rename from playground/agents/llama3_agent.py rename to playground/agents/various_models/llama3_agent.py diff --git a/playground/agents/example_task.py b/playground/tasks/example_task.py similarity index 100% rename from playground/agents/example_task.py rename to playground/tasks/example_task.py diff --git a/pyproject.toml b/pyproject.toml index 9113eb6e..6591da4b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,12 +5,12 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "5.4.4" +version = "5.4.5" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] homepage = "https://github.com/kyegomez/swarms" -documentation = "https://swarms.world" +documentation = "https://docs.swarms.world" readme = "README.md" repository = "https://github.com/kyegomez/swarms" keywords = [ @@ -30,7 +30,11 @@ keywords = [ "Multi-Grade-Agents", "Swarms", "Transformers", - + "LLMs", + "Prompt Engineering", + "Agents", + "Generative Agents", + "Generative AI", ] classifiers = [ "Development Status :: 4 - Beta", @@ -69,7 +73,8 @@ networkx = "*" swarms-memory = "*" - +# [tool.poetry.scripts] +# swarms = "swarms.cli:main" [tool.poetry.group.lint.dependencies] black = ">=23.1,<25.0" diff --git a/swarms/cli/main.py b/swarms/cli/main.py index f80f07f8..5e4ae9f6 100644 --- a/swarms/cli/main.py +++ b/swarms/cli/main.py @@ -17,7 +17,7 @@ SWARMS_LOGO = """ RED_COLOR_CODE = "\033[91m" 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(): @@ -87,5 +87,5 @@ def main(): parser.print_help() -if __name__ == "__main__": - main() +# if __name__ == "__main__": +# main()