finished conclusion for stacked worker

pull/64/head
Kye 1 year ago
parent 883505253f
commit a6f1f09602

@ -1,49 +0,0 @@
```python
import hashlib
import json
from urllib.request import urlopen
from decouple import config
from swarms.drivers import PineconeVectorStoreDriver
def load_data(driver: PineconeVectorStoreDriver) -> None:
response = urlopen(
"https://raw.githubusercontent.com/wedeploy-examples/"
"supermarket-web-example/master/products.json"
)
for product in json.loads(response.read()):
driver.upsert_text(
product["description"],
vector_id=hashlib.md5(product["title"].encode()).hexdigest(),
meta={
"title": product["title"],
"description": product["description"],
"type": product["type"],
"price": product["price"],
"rating": product["rating"]
},
namespace="supermarket-products"
)
vector_driver = PineconeVectorStoreDriver(
api_key=config("PINECONE_API_KEY"),
environment=config("PINECONE_ENVIRONMENT"),
index_name=config("PINECONE_INDEX_NAME")
)
load_data(vector_driver)
result = vector_driver.query(
"fruit",
count=3,
filter={
"price": {"$lte": 15},
"rating": {"$gte": 4}
},
namespace="supermarket-products"
)
print(result)
```

@ -1,51 +0,0 @@
```python
from swarms import utils
from swarms.drivers import MarqoVectorStoreDriver
from swarms.engines import VectorQueryEngine
from swarms.loaders import WebLoader
from swarms.structures import Agent
from swarms.tools import KnowledgeBaseClient
import openai
from marqo import Client
# Set the OpenAI API key
openai.api_key_path = "../openai_api_key.txt"
# Define the namespace
namespace = "kyegomez"
# Initialize the vector store driver
vector_store = MarqoVectorStoreDriver(
api_key=openai.api_key_path,
url="http://localhost:8882",
index="chat2",
mq=Client(api_key="foobar", url="http://localhost:8882")
)
# Get a list of all indexes
#indexes = vector_store.get_indexes()
#print(indexes)
# Initialize the query engine
query_engine = VectorQueryEngine(vector_store_driver=vector_store)
# Initialize the knowledge base tool
kb_tool = KnowledgeBaseClient(
description="Contains information about the Swarms Framework from www.swarms.ai",
query_engine=query_engine,
namespace=namespace
)
# Load artifacts from the web
artifacts = WebLoader(max_tokens=200).load("https://www.swarms.ai")
# Upsert the artifacts into the vector store
vector_store.upsert_text_artifacts({namespace: artifacts,})
# Initialize the agent
agent = Agent(tools=[kb_tool])
# Start the chat
utils.Chat(agent).start()
```

@ -141,45 +141,76 @@ print(response)
## 8. Conclusion
In this extensive tutorial, we embarked on a journey to understand and utilize the swarm examples provided. We broke down the code step by step, examined its components, and explored how to leverage its modularity for various AI tasks. As we wrap up our exploration, let's recap what we've learned, revisit the concept of the worker node, and envision the exciting future possibilities of this system.
In this extensive tutorial, we've embarked on a journey to explore a sophisticated system designed to harness the power of AI models and tools for a myriad of tasks. We've peeled back the layers of code, dissected its various components, and gained a profound understanding of how these elements come together to create a versatile, modular, and powerful swarm-based AI system.
Recap of What We've Learned
Throughout this tutorial, we've gained a deep understanding of the following key concepts and components:
## What We've Learned
Code Structure: We dissected the provided code, starting with import statements and progressing through the initialization of API keys, language models, and the definition of swarm tools.
Throughout this tutorial, we've covered the following key aspects:
Swarm Tools: We introduced three essential tools—hf_agent, omni_agent, and compile. Each tool serves a specific purpose, from generating text to interacting with Huggingface models and running code locally.
### Code Structure and Components
We dissected the code into its fundamental building blocks:
- **Import Statements:** We imported necessary modules and libraries, setting the stage for our system's functionality.
- **Initializing API Key and Language Model:** We learned how to set up the essential API key and initialize the language model, a core component for text generation and understanding.
- **Creating Swarm Tools:** We explored how to define tools, encapsulating specific functionalities that our system can leverage.
- **Appending Tools to a List:** We aggregated our tools into a list, making them readily available for use.
- **Initializing a Worker Node:** We created a worker node equipped with tools, a name, and configuration settings.
Worker Node: We explored the worker node, a pivotal component in this system. The worker node combines language models, API keys, and tools to perform tasks autonomously, without the need for human intervention.
### Tools and Their Functions
We dove deep into the purpose and functionality of three crucial tools:
- **`hf_agent`:** We understood how this tool employs an OpenAI model for multi-modal tasks, and its use cases beyond simple summarization.
- **`omni_agent`:** We explored the versatility of this tool, guiding Huggingface models to perform a wide range of multi-modal tasks.
- **`compile`:** We saw how this tool allows the execution of code in multiple languages, providing a natural language interface for various computational tasks.
Interactive Examples: We conducted interactive examples to demonstrate the practical application of each block of code. These examples showed us how to initialize the system, generate text, answer questions, and execute code.
### Interactive Examples
We brought the code to life through interactive examples, showcasing how to initialize the language model, generate text, perform document-question-answering, and execute code—all with practical, real-world scenarios.
Recap of the Worker Node
The worker node is at the heart of this system. It acts as a digital assistant, capable of utilizing various tools and models to accomplish tasks. Let's recap the core features and benefits of the worker node:
## A Recap: The Worker Node's Role
Modularity: The worker node is highly modular. It can seamlessly integrate new tools, models, and APIs, making it adaptable to a wide range of tasks and applications.
At the heart of this system lies the "Worker Node," a versatile entity capable of wielding the power of AI models and tools to accomplish tasks. The Worker Node's role is pivotal in the following ways:
Automation: Once set up, the worker node can perform tasks autonomously, reducing the need for manual intervention. This automation can significantly boost productivity and efficiency.
1. **Task Execution:** It is responsible for executing tasks, harnessing the capabilities of the defined tools to generate responses or perform actions.
Versatility: With the ability to switch between tools and models, the worker node can tackle diverse tasks, from generating creative content to executing code and answering questions.
2. **Modularity:** The Worker Node benefits from the modularity of the system. It can easily access and utilize a variety of tools, allowing it to adapt to diverse tasks and requirements.
Scalability: This system can be scaled by adding multiple worker nodes, enabling concurrent task processing and handling larger workloads.
3. **Human in the Loop:** While the example here is configured to operate without human intervention, the Worker Node can be customized to incorporate human input or approval when needed.
Consistency: The worker node consistently applies the defined rules and procedures for each tool, ensuring reliable and reproducible results.
4. **Integration:** It can be extended to integrate with other AI models, APIs, or services, expanding its functionality and versatility.
The Future of the Worker Node
As we conclude this tutorial, it's essential to consider the potential future developments and features that could enhance the capabilities of the worker node and the broader system. Here are some exciting possibilities:
## The Road Ahead: Future Features and Enhancements
Enhanced Natural Language Understanding
Future iterations of the worker node could incorporate advanced natural language understanding capabilities. This would enable it to comprehend and respond to more complex and context-aware queries, making it even more proficient in various tasks.
As we conclude this tutorial, let's peek into the future of this system. While the current implementation is already powerful, there is always room for growth and improvement. Here are some potential future features and enhancements to consider:
Improved Multi-Modal Integration
Enhancements in multi-modal capabilities could enable the worker node to seamlessly combine text, images, audio, and video to generate richer and more comprehensive responses. This would be particularly valuable for tasks like content generation, summarization, and content transformation.
### 1. Enhanced Natural Language Understanding
- **Semantic Understanding:** Improve the system's ability to understand context and nuances in natural language, enabling more accurate responses.
Expanded Tool Ecosystem
The worker node's tool ecosystem could be expanded to include a broader range of specialized tools. These might encompass tools for data analysis, machine learning model training, and data visualization, allowing users to perform more sophisticated tasks.
### 2. Multimodal Capabilities
- **Extended Multimodal Support:** Expand the `omni_agent` tool to support additional types of multimodal tasks, such as video generation or audio processing.
Learning and Adaptation
Future versions of the worker node could incorporate machine learning and adaptive capabilities. This would enable it to learn from user interactions and improve its performance over time. It could also adapt to specific user preferences and work more intelligently.
### 3. Customization and Integration
- **User-defined Tools:** Allow users to define their own custom tools, opening up endless possibilities for tailoring the system to specific needs.
### 4. Collaborative Swarms
- **Swarm Collaboration:** Enable multiple Worker Nodes to collaborate on complex tasks, creating a distributed, intelligent swarm system.
### 5. User-Friendly Interfaces
- **Graphical User Interface (GUI):** Develop a user-friendly GUI for easier interaction and task management, appealing to a wider audience.
### 6. Continuous Learning
- **Active Learning:** Implement mechanisms for the system to learn and adapt over time, improving its performance with each task.
### 7. Security and Privacy
- **Enhanced Security:** Implement robust security measures to safeguard sensitive data and interactions within the system.
### 8. Community and Collaboration
- **Open Source Community:** Foster an open-source community around the system, encouraging contributions and innovation from developers worldwide.
### 9. Integration with Emerging Technologies
- **Integration with Emerging AI Models:** Keep the system up-to-date by seamlessly integrating with new and powerful AI models as they emerge in the industry.
## In Conclusion
In this tutorial, we've journeyed through a complex AI system, unraveling its inner workings, and understanding its potential. We've witnessed how code can transform into a powerful tool, capable of handling a vast array of tasks, from generating creative stories to executing code snippets.
As we conclude, we stand at the threshold of an exciting future for AI and technology. This system, with its modular design and the potential for continuous improvement, embodies the spirit of innovation and adaptability. Whether you're a developer, a researcher, or an enthusiast, the possibilities are boundless, and the journey is just beginning.
Embrace this knowledge, explore the system, and embark on your own quest to shape the future of AI. With each line of code, you have the power to transform ideas into reality and unlock new horizons of innovation. The future is yours to create, and the tools are at your fingertips.

@ -1,47 +0,0 @@
To store your conversation on DynamoDB you can use DynamoDbConversationMemoryDriver.
```python
from swarms.memory.structure import ConversationMemory
from swarms.memory.structure import ConversationMemoryElement, Turn, Message
from swarms.drivers import DynamoDbConversationMemoryDriver
# Instantiate DynamoDbConversationMemoryDriver
dynamo_driver = DynamoDbConversationMemoryDriver(
aws_region="us-east-1",
table_name="conversations",
partition_key="convo_id",
value_attribute_key="convo_data",
partition_key_value="convo1"
)
# Create a ConversationMemory structure
conv_mem = ConversationMemory(
turns=[
Turn(
turn_index=0,
system=Message("Hello"),
user=Message("Hi")
),
Turn(
turn_index=1,
system=Message("How can I assist you today?"),
user=Message("I need some information")
)
],
latest_turn=Turn(
turn_index=2,
system=Message("Sure, what information do you need?"),
user=None # user has not yet responded
),
driver=dynamo_driver # set the driver
)
# Store the conversation in DynamoDB
dynamo_driver.store(conv_mem)
# Load the conversation from DynamoDB
loaded_conv_mem = dynamo_driver.load()
# Display the loaded conversation
print(loaded_conv_mem.to_json())
```

@ -1,37 +0,0 @@
This example demonstrates how to vectorize a PDF of the [Attention Is All You Need](https://arxiv.org/pdf/1706.03762.pdf) paper and setup a Swarms agent with rules and the `KnowledgeBase` tool to use it during conversations.
```python
import io
import requests
from swarms.engines import VectorQueryEngine
from swarms.loaders import PdfLoader
from swarms.structures import Agent
from swarms.tools import KnowledgeBaseClient
from swarms.utils import Chat
namespace = "attention"
response = requests.get("https://arxiv.org/pdf/1706.03762.pdf")
engine = VectorQueryEngine()
engine.vector_store_driver.upsert_text_artifacts(
{
namespace: PdfLoader().load(
io.BytesIO(response.content)
)
}
)
kb_client = KnowledgeBaseClient(
description="Contains information about the Attention Is All You Need paper. "
"Use it to answer any related questions.",
query_engine=engine,
namespace=namespace
)
agent = Agent(
tools=[kb_client]
)
Chat(agent).start()
```

@ -1,50 +0,0 @@
This example demonstrates how to vectorize a webpage and setup a Swarms agent with rules and the `KnowledgeBase` tool to use it during conversations.
```python
from swarms.engines import VectorQueryEngine
from swarms.loaders import WebLoader
from swarms.rules import Ruleset, Rule
from swarms.structures import Agent
from swarms.tools import KnowledgeBaseClient
from swarms.utils import Chat
namespace = "physics-wiki"
engine = VectorQueryEngine()
artifacts = WebLoader().load(
"https://en.wikipedia.org/wiki/Physics"
)
engine.vector_store_driver.upsert_text_artifacts(
{namespace: artifacts}
)
kb_client = KnowledgeBaseClient(
description="Contains information about physics. "
"Use it to answer any physics-related questions.",
query_engine=engine,
namespace=namespace
)
agent = Agent(
rulesets=[
Ruleset(
name="Physics Tutor",
rules=[
Rule(
"Always introduce yourself as a physics tutor"
),
Rule(
"Be truthful. Only discuss physics."
)
]
)
],
tools=[kb_client]
)
Chat(agent).start()
```

@ -1,46 +0,0 @@
This example demonstrates how to build an agent that can dynamically query Amazon Redshift Serverless tables and store its contents on the local hard drive.
Let's build a support agent that uses GPT-4:
```python
import boto3
from swarms.drivers import AmazonRedshiftSqlDriver, OpenAiPromptDriver
from swarms.loaders import SqlLoader
from swarms.rules import Ruleset, Rule
from swarms.structures import Agent
from swarms.tools import SqlClient, FileManager
from swarms.utils import Chat
session = boto3.Session(region_name="REGION_NAME")
sql_loader = SqlLoader(
sql_driver=AmazonRedshiftSqlDriver(
database="DATABASE",
session=session,
workgroup_name="WORKGROUP_NAME"
)
)
sql_tool = SqlClient(
sql_loader=sql_loader,
table_name="people",
table_description="contains information about tech industry professionals",
engine_name="redshift"
)
agent = Agent(
tools=[sql_tool, FileManager())],
rulesets=[
Ruleset(
name="HumansOrg Agent",
rules=[
Rule("Act and introduce yourself as a HumansOrg, Inc. support agent"),
Rule("Your main objective is to help with finding information about people"),
Rule("Only use information about people from the sources available to you")
]
)
]
)
Chat(agent).start()
```

@ -1,97 +0,0 @@
This example demonstrates how to build an agent that can integrate with [Text Generation Web UI](https://github.com/oobabooga/text-generation-webui).
To be able to perform successful connection, run text gen with '--api' and if you running text gen not on the same host, add '--listen'. see more option [here](https://github.com/oobabooga/text-generation-webui)
Check out the bare API usage [example](https://github.com/oobabooga/text-generation-webui/blob/main/api-examples/api-example.py).
## Tokenizer
To match the tokenizer used in the text gen, one can use [PreTrainedTokenizerFast](https://huggingface.co/docs/transformers/fast_tokenizers#loading-from-a-json-file) to load tokenizer from saved json setting file.
Example:
Let's say you using [TheBloke/WizardLM-13B-V1-1-SuperHOT-8K-GPTQ](https://huggingface.co/TheBloke/WizardLM-13B-V1-1-SuperHOT-8K-GPTQ/tree/main) in text gen, you can get hold of 'tokenizer.json' file that can be used to setup a corresponding tokenizer.
## Code Snippets
Code snippet using a pre defined 'preset'.
'max_tokens' argument here need to be set with the same value as in the preset in text gen.
```shell
from swarms.structures import Agent
from swarms.drivers import TextGenPromptDriver
from swarms.tokenizers import TextGenTokenizer
from transformers import PreTrainedTokenizerFast
fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json")
prompt_driver = TextGenPromptDriver(
preset="swarms",
tokenizer=TextGenTokenizer(max_tokens=300, tokenizer=fast_tokenizer)
)
agent = Agent(
prompt_driver=prompt_driver
)
agent.run(
"tell me what Swarms is"
)
```
Code snippet example using params, if params and preset is defined, preset will be used.
this params are overriding the current preset set in text gen, not all of them must be used.
```shell
from swarms.structures import Agent
from swarms.drivers import TextGenPromptDriver
from swarms.tokenizers import TextGenTokenizer
from transformers import PreTrainedTokenizerFast
params = {
'max_new_tokens': 250,
'do_sample': True,
'temperature': 0.7,
'top_p': 0.1,
'typical_p': 1,
'epsilon_cutoff': 0, # In units of 1e-4
'eta_cutoff': 0, # In units of 1e-4
'tfs': 1,
'top_a': 0,
'repetition_penalty': 1.18,
'repetition_penalty_range': 0,
'top_k': 40,
'min_length': 0,
'no_repeat_ngram_size': 0,
'num_beams': 1,
'penalty_alpha': 0,
'length_penalty': 1,
'early_stopping': False,
'mirostat_mode': 0,
'mirostat_tau': 5,
'mirostat_eta': 0.1,
'seed': 235245345,
'add_bos_token': True,
'truncation_length': 2048,
'ban_eos_token': False,
'skip_special_tokens': True,
'stopping_strings': []
}
fast_tokenizer = PreTrainedTokenizerFast(tokenizer_file="tokenizer.json")
prompt_driver = TextGenPromptDriver(
params=params,
tokenizer=TextGenTokenizer(max_tokens=params['max_new_tokens'], tokenizer=fast_tokenizer)
)
agent = Agent(
prompt_driver=prompt_driver
)
agent.run(
"tell me what Swarms is"
)
```
Loading…
Cancel
Save