Create an instance of SDKContext with the path to your configuration file.
The SDKContext allows you to manage configurations, resources, and utilities across your Agents more efficiently.
from swarmzero.sdk_context import SDKContextsdk_context =SDKContext(config_path="./swarmzero_config.toml")
Specify the Configuration Path:
When creating an Agent instance, provide the relative or absolute path to your configuration file.
Agent will use the configuration from the SDK Context. If you have one agent you can directly pass the config_path it will create the sdk_context for you.
from swarmzero import Agentsimple_agent =Agent( name="Simple Agent", functions=[], instruction="your instructions for this agent's goal",# sdk_context=sdk_context config_path="./swarmzero_config.toml")
Usage
First import the Agent class:
from swarmzero import Agent
Load your environment variables:
from dotenv import load_dotenvload_dotenv()
Then create a Agent instance:
my_agent =Agent( name="my_agent", functions=[], instruction="your instructions for this agent's goal",)
Then, run your agent:
my_agent.run()
Finally, call the API endpoint, /api/v1/chat, to see the result:
You configure your Agent by adding tools, you can create a swarm of agents to handle a complex task, and you can add a retriever to get semantic information. Below are some examples of how you can do it:
Adding tools
You can create tools that help your agent handle more complex tasks. Here's an example:
import osfrom typing import Optional, Dictfrom web3 import Web3from swarmzero import Agentfrom dotenv import load_dotenvload_dotenv()rpc_url = os.getenv("RPC_URL")# add an ETH Mainnet HTTP RPC URL to your `.env` filedefget_transaction_receipt(transaction_hash:str) -> Optional[Dict]:""" Fetches the receipt of a specified transaction on the Ethereum blockchain and returns it as a dictionary. :param transaction_hash: The hash of the transaction to fetch the receipt for. :return: A dictionary containing the transaction receipt details, or None if the transaction cannot be found. """ web3 =Web3(Web3.HTTPProvider(rpc_url))ifnot web3.is_connected():print("unable to connect to Ethereum")returnNonetry: transaction_receipt = web3.eth.get_transaction_receipt(transaction_hash)returndict(transaction_receipt)exceptExceptionas e:print(f"an error occurred: {e}")returnNoneif__name__=="__main__": my_agent =Agent( name="my_agent", functions=[get_transaction_receipt] ) my_agent.run()""" [1] send a request: ``` curl --request POST \ --url http://localhost:8000/api/v1/chat \ --header 'Content-Type: multipart/form-data' \ --form 'user_id="test"' \ --form 'session_id="test"' \ --form 'chat_data={ "messages": [ { "role": "user", "content": "Who is the sender of this transaction - 0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060" } ] }'
``` [2] result: The address that initiated the transaction with hash 0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060 is 0xA1E4380A3B1f749673E270229993eE55F35663b4.
"""
Creating a Swarm
You can create a swarm of agents to collaborate on complex tasks. Here's an example of how to set up and use a swarm:
from swarmzero.swarm import Swarmfrom swarmzero.agent import Agentfrom swarmzero.sdk_context import SDKContextimport asyncio# Create SDK Contextsdk_context =SDKContext(config_path="./swarmzero_config_example.toml")defsave_report():return"save_item_to_csv"defsearch_on_web():return"search_on_web"# Create individual agentsagent1 =Agent(name="Research Agent", instruction="Conduct research on given topics", sdk_context=sdk_context, functions=[search_on_web])agent2 =Agent(name="Analysis Agent", instruction="Analyze data and provide insights", sdk_context=sdk_context, functions=[save_report])agent3 = Agent(name="Report Agent", instruction="Compile findings into a report", sdk_context=sdk_context, functions=[])
# Create swarmswarm =Swarm(name="Research Team", description="A swarm of agents that collaborate on research tasks", instruction="Be helpful and collaborative", functions=[], agents=[agent1, agent2, agent3])asyncdefchat_with_swarm():returnawait swarm.chat("Can you analyze the following data: [1, 2, 3, 4, 5]")if__name__=="__main__": asyncio.run(chat_with_swarm())
Adding Retriever
You can add retriever tools to create vector embeddings and retrieve semantic information. It will create vector index for every pdf documents under 'swarmzero-data/files/user' folder and can filter files with required_exts parameter.
Users of your agent/swarm may not always be familiar with its abilities. Providing sample prompts allows them to explore what you have built. Here's how to add sample prompts which they can use before committing to use your agent/swarm.
Default
In your swarmzero_config.toml file, create a top level entry called [sample_prompts] and add a new array to the key prompts like this:
[sample_prompts]prompts = ["What can you help me do?","Which tools do you have access to?","What are your capabilities?"]
Specific agents in a swarm
[target_agent_id]model ="gpt-3.5-turbo"timeout =15environment ="dev"enable_multi_modal = trueollama_server_url ='http://123.456.78.90:11434'sample_prompts = ["What can you help me do?","Which tools do you have access to?","What are your capabilities?"]