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/docs/examples/worker.md

3.8 KiB

The Ultimate Guide to Mastering the Worker Class from Swarms


Table of Contents

  1. Introduction: Welcome to the World of the Worker
  2. The Basics: What Does the Worker Do?
  3. Installation: Setting the Stage
  4. Dive Deep: Understanding the Architecture
  5. Practical Usage: Let's Get Rolling!
  6. Advanced Tips and Tricks
  7. Handling Errors: Because We All Slip Up Sometimes
  8. Beyond the Basics: Advanced Features and Customization
  9. Conclusion: Taking Your Knowledge Forward

1. Introduction: Welcome to the World of the Worker

Greetings, future master of the Worker! Step into a universe where you can command an AI worker to perform intricate tasks, be it searching the vast expanse of the internet or crafting multi-modality masterpieces. Ready to embark on this thrilling journey? Lets go!


2. The Basics: What Does the Worker Do?

The Worker is your personal AI assistant. Think of it as a diligent bee in a swarm, ready to handle complex tasks across various modalities, from text and images to audio and beyond.


3. Installation: Setting the Stage

Before we can call upon our Worker, we need to set the stage:

pip install swarms

Voila! Youre now ready to summon your Worker.


4. Dive Deep: Understanding the Architecture

  • Language Model (LLM): The brain of our Worker. It understands and crafts intricate language-based responses.
  • Tools: Think of these as the Worker's toolkit. They range from file tools, website querying, to even complex tasks like image captioning.
  • Memory: No, our Worker doesnt forget. It employs a sophisticated memory mechanism to remember past interactions and learn from them.

5. Practical Usage: Let's Get Rolling!

Heres a simple way to invoke the Worker and give it a task:

from swarms.models import OpenAIChat
from swarms import Worker

llm = OpenAIChat(
    #enter your api key
    openai_api_key="",
    temperature=0.5,
)

node = Worker(
    llm=llm,
    ai_name="Optimus Prime",
    openai_api_key="",
    ai_role="Worker in a swarm",
    external_tools=None,
    human_in_the_loop=False,
    temperature=0.5,
)

task = "What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times."
response = node.run(task)
print(response)


The result? An agent with elegantly integrated tools and long term memories


6. Advanced Tips and Tricks

  • Streaming Responses: Want your Worker to respond in a more dynamic fashion? Use the _stream_response method to get results token by token.
  • Human-in-the-Loop: By setting human_in_the_loop to True, you can involve a human in the decision-making process, ensuring the best results.

7. Handling Errors: Because We All Slip Up Sometimes

Your Worker is designed to be robust. But if it ever encounters a hiccup, it's equipped to let you know. Error messages are crafted to be informative, guiding you on the next steps.


8. Beyond the Basics: Advanced Features and Customization

  • Custom Tools: Want to expand the Worker's toolkit? Use the external_tools parameter to integrate your custom tools.
  • Memory Customization: You can tweak the Worker's memory settings, ensuring it remembers what's crucial for your tasks.

9. Conclusion: Taking Your Knowledge Forward

Congratulations! Youre now well-equipped to harness the power of the Worker from Swarms. As you venture further, remember: the possibilities are endless, and with the Worker by your side, theres no task too big!

Happy Coding and Exploring! 🚀🎉


Note: This guide provides a stepping stone to the vast capabilities of the Worker. Dive into the official documentation for a deeper understanding and stay updated with the latest features.