You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/playground/structs/agent_with_tools_example.py

81 lines
1.5 KiB

1 year ago
"""
11 months ago
tool decorated func [search_api] -> agent which parses the docs of the tool func
1 year ago
-> injected into prompt -> agent will output json containing tool usage -> agent output will be parsed -> tool executed
-> terminal response can be returned to agent for self-healing
11 months ago
1 year ago
"""
import os
from dotenv import load_dotenv
# Import the OpenAIChat model and the Agent struct
11 months ago
from swarms import Agent, OpenAIChat
1 year ago
from swarms.tools.tool import tool
# Load the environment variables
load_dotenv()
1 year ago
# Define a tool
@tool
def search_api(query: str, description: str):
1 year ago
"""Search the web for the query
Args:
query (str): _description_
Returns:
_type_: _description_
"""
return f"Search results for {query}"
@tool
1 year ago
def weather_api(
query: str,
):
"""_summary_
Args:
query (str): _description_
"""
print(f"Getting the weather for {query}")
1 year ago
@tool
def rapid_api(query: str):
"""_summary_
Args:
query (str): _description_
"""
print(f"Getting the weather for {query}")
1 year ago
1 year ago
# Get the API key from the environment
api_key = os.environ.get("OPENAI_API_KEY")
# Initialize the language model
llm = OpenAIChat(
temperature=0.5,
openai_api_key=api_key,
)
# Initialize the workflow
1 year ago
agent = Agent(
agent_name="Research Agent",
llm=llm,
max_loops=1,
dashboard=True,
tools=[search_api, weather_api, rapid_api],
1 year ago
)
# Run the workflow on a task
out = agent.run("Generate a 10,000 word blog on health and wellness.")
print(out)