commit
3e7046546b
After Width: | Height: | Size: 40 KiB |
@ -1,7 +1,110 @@
|
||||
This page summarizes questions we were asked on [Discord](https://discord.gg/gnWRz88eym), Hacker News, and Reddit. Feel free to post a question to [Discord](https://discord.gg/gnWRz88eym) or open a discussion on our [Github Page](https://github.com/kyegomez) or hit us up directly: [kye@apac.ai](mailto:hello@swarms.ai).
|
||||
### FAQ on Swarm Intelligence and Multi-Agent Systems
|
||||
|
||||
## 1. How is Swarms different from LangChain?
|
||||
#### What is an agent in the context of AI and swarm intelligence?
|
||||
|
||||
Swarms is an open source alternative to LangChain and differs in its approach to creating LLM pipelines and DAGs. In addition to agents, it uses more general-purpose DAGs and pipelines. A close proxy might be *Airflow for LLMs*. Swarms still implements chain of thought logic for prompt tasks that use "tools" but it also supports any type of input / output (images, audio, etc.).
|
||||
In artificial intelligence (AI), an agent refers to an LLM with some objective to accomplish.
|
||||
|
||||
In swarm intelligence, each agent interacts with other agents and possibly the environment to achieve complex collective behaviors or solve problems more efficiently than individual agents could on their own.
|
||||
|
||||
|
||||
#### What do you need Swarms at all?
|
||||
Individual agents are limited by a vast array of issues such as context window loss, single task execution, hallucination, and no collaboration.
|
||||
|
||||
|
||||
#### How does a swarm work?
|
||||
|
||||
A swarm works through the principles of decentralized control, local interactions, and simple rules followed by each agent. Unlike centralized systems, where a single entity dictates the behavior of all components, in a swarm, each agent makes its own decisions based on local information and interactions with nearby agents. These local interactions lead to the emergence of complex, organized behaviors or solutions at the collective level, enabling the swarm to tackle tasks efficiently.
|
||||
|
||||
#### Why do you need more agents in a swarm?
|
||||
|
||||
More agents in a swarm can enhance its problem-solving capabilities, resilience, and efficiency. With more agents:
|
||||
|
||||
- **Diversity and Specialization**: The swarm can leverage a wider range of skills, knowledge, and perspectives, allowing for more creative and effective solutions to complex problems.
|
||||
- **Scalability**: Adding more agents can increase the swarm's capacity to handle larger tasks or multiple tasks simultaneously.
|
||||
- **Robustness**: A larger number of agents enhances the system's redundancy and fault tolerance, as the failure of a few agents has a minimal impact on the overall performance of the swarm.
|
||||
|
||||
#### Isn't it more expensive to use more agents?
|
||||
|
||||
While deploying more agents can initially increase costs, especially in terms of computational resources, hosting, and potentially API usage, there are several factors and strategies that can mitigate these expenses:
|
||||
|
||||
- **Efficiency at Scale**: Larger swarms can often solve problems more quickly or effectively, reducing the overall computational time and resources required.
|
||||
- **Optimization and Caching**: Implementing optimizations and caching strategies can reduce redundant computations, lowering the workload on individual agents and the overall system.
|
||||
- **Dynamic Scaling**: Utilizing cloud services that offer dynamic scaling can ensure you only pay for the resources you need when you need them, optimizing cost-efficiency.
|
||||
|
||||
#### Can swarms make decisions better than individual agents?
|
||||
|
||||
Yes, swarms can make better decisions than individual agents for several reasons:
|
||||
|
||||
- **Collective Intelligence**: Swarms combine the knowledge and insights of multiple agents, leading to more informed and well-rounded decision-making processes.
|
||||
- **Error Correction**: The collaborative nature of swarms allows for error checking and correction among agents, reducing the likelihood of mistakes.
|
||||
- **Adaptability**: Swarms are highly adaptable to changing environments or requirements, as the collective can quickly reorganize or shift strategies based on new information.
|
||||
|
||||
#### How do agents in a swarm communicate?
|
||||
|
||||
Communication in a swarm can vary based on the design and purpose of the system but generally involves either direct or indirect interactions:
|
||||
|
||||
- **Direct Communication**: Agents exchange information directly through messaging, signals, or other communication protocols designed for the system.
|
||||
- **Indirect Communication**: Agents influence each other through the environment, a method known as stigmergy. Actions by one agent alter the environment, which in turn influences the behavior of other agents.
|
||||
|
||||
#### Are swarms only useful in computational tasks?
|
||||
|
||||
While swarms are often associated with computational tasks, their applications extend far beyond. Swarms can be utilized in:
|
||||
|
||||
- **Robotics**: Coordinating multiple robots for tasks like search and rescue, exploration, or surveillance.
|
||||
- **Environmental Monitoring**: Using sensor networks to monitor pollution, wildlife, or climate conditions.
|
||||
- **Social Sciences**: Modeling social behaviors or economic systems to understand complex societal dynamics.
|
||||
- **Healthcare**: Coordinating care strategies in hospital settings or managing pandemic responses through distributed data analysis.
|
||||
|
||||
#### How do you ensure the security of a swarm system?
|
||||
|
||||
Security in swarm systems involves:
|
||||
|
||||
- **Encryption**: Ensuring all communications between agents are encrypted to prevent unauthorized access or manipulation.
|
||||
- **Authentication**: Implementing strict authentication mechanisms to verify the identity of each agent in the swarm.
|
||||
- **Resilience to Attacks**: Designing the swarm to continue functioning effectively even if some agents are compromised or attacked, utilizing redundancy and fault tolerance strategies.
|
||||
|
||||
#### How do individual agents within a swarm share insights without direct learning mechanisms like reinforcement learning?
|
||||
|
||||
In the context of pre-trained Large Language Models (LLMs) that operate within a swarm, sharing insights typically involves explicit communication and data exchange protocols rather than direct learning mechanisms like reinforcement learning. Here's how it can work:
|
||||
|
||||
- **Shared Databases and Knowledge Bases**: Agents can write to and read from a shared database or knowledge base where insights, generated content, and relevant data are stored. This allows agents to benefit from the collective experience of the swarm by accessing information that other agents have contributed.
|
||||
|
||||
- **APIs for Information Exchange**: Custom APIs can facilitate the exchange of information between agents. Through these APIs, agents can request specific information or insights from others within the swarm, effectively sharing knowledge without direct learning.
|
||||
|
||||
#### How do you balance the autonomy of individual LLMs with the need for coherent collective behavior in a swarm?
|
||||
|
||||
Balancing autonomy with collective coherence in a swarm of LLMs involves:
|
||||
|
||||
- **Central Coordination Mechanism**: Implementing a lightweight central coordination mechanism that can assign tasks, distribute information, and collect outputs from individual LLMs. This ensures that while each LLM operates autonomously, their actions are aligned with the swarm's overall objectives.
|
||||
|
||||
- **Standardized Communication Protocols**: Developing standardized protocols for how LLMs communicate and share information ensures that even though each agent works autonomously, the information exchange remains coherent and aligned with the collective goals.
|
||||
|
||||
#### How do LLM swarms adapt to changing environments or tasks without machine learning techniques?
|
||||
|
||||
Adaptation in LLM swarms, without relying on machine learning techniques for dynamic learning, can be achieved through:
|
||||
|
||||
- **Dynamic Task Allocation**: A central system or distributed algorithm can dynamically allocate tasks to different LLMs based on the changing environment or requirements. This ensures that the most suitable LLMs are addressing tasks for which they are best suited as conditions change.
|
||||
|
||||
- **Pre-trained Versatility**: Utilizing a diverse set of pre-trained LLMs with different specialties or training data allows the swarm to select the most appropriate agent for a task as the requirements evolve.
|
||||
|
||||
- **In Context Learning**: In context learning is another mechanism that can be employed within LLM swarms to adapt to changing environments or tasks. This approach involves leveraging the collective knowledge and experiences of the swarm to facilitate learning and improve performance. Here's how it can work:
|
||||
|
||||
|
||||
#### Can LLM swarms operate in physical environments, or are they limited to digital spaces?
|
||||
|
||||
LLM swarms primarily operate in digital spaces, given their nature as software entities. However, they can interact with physical environments indirectly through interfaces with sensors, actuaries, or other devices connected to the Internet of Things (IoT). For example, LLMs can process data from physical sensors and control devices based on their outputs, enabling applications like smart home management or autonomous vehicle navigation.
|
||||
|
||||
#### Without direct learning from each other, how do agents in a swarm improve over time?
|
||||
|
||||
Improvement over time in a swarm of pre-trained LLMs, without direct learning from each other, can be achieved through:
|
||||
|
||||
- **Human Feedback**: Incorporating feedback from human operators or users can guide adjustments to the usage patterns or selection criteria of LLMs within the swarm, optimizing performance based on observed outcomes.
|
||||
|
||||
- **Periodic Re-training and Updating**: The individual LLMs can be periodically re-trained or updated by their developers based on collective insights and feedback from their deployment within swarms. While this does not involve direct learning from each encounter, it allows the LLMs to improve over time based on aggregated experiences.
|
||||
|
||||
These adjustments to the FAQ reflect the specific context of pre-trained LLMs operating within a swarm, focusing on communication, coordination, and adaptation mechanisms that align with their capabilities and constraints.
|
||||
|
||||
|
||||
#### Conclusion
|
||||
|
||||
Swarms represent a powerful paradigm in AI, offering innovative solutions to complex, dynamic problems through collective intelligence and decentralized control. While challenges exist, particularly regarding cost and security, strategic design and management can leverage the strengths of swarm intelligence to achieve remarkable efficiency, adaptability, and robustness in a wide range of applications.
|
@ -0,0 +1,53 @@
|
||||
# Why Swarms?
|
||||
|
||||
The need for multiple agents to work together in artificial intelligence (AI) and particularly in the context of Large Language Models (LLMs) stems from several inherent limitations and challenges in handling complex, dynamic, and multifaceted tasks with single-agent systems. Collaborating with multiple agents offers a pathway to enhance computational efficiency, cognitive diversity, and problem-solving capabilities. This section delves into the rationale behind employing multi-agent systems and strategizes on overcoming the associated expenses, such as API bills and hosting costs.
|
||||
|
||||
### Why Multiple Agents Are Necessary
|
||||
|
||||
#### 1. **Cognitive Diversity**
|
||||
|
||||
Different agents can bring varied perspectives, knowledge bases, and problem-solving approaches to a task. This diversity is crucial in complex problem-solving scenarios where a single approach might not be sufficient. Cognitive diversity enhances creativity, leading to innovative solutions and the ability to tackle a broader range of problems.
|
||||
|
||||
#### 2. **Specialization and Expertise**
|
||||
|
||||
In many cases, tasks are too complex for a single agent to handle efficiently. By dividing the task among multiple specialized agents, each can focus on a segment where it excels, thereby increasing the overall efficiency and effectiveness of the solution. This approach leverages the expertise of individual agents to achieve superior performance in tasks that require multifaceted knowledge and skills.
|
||||
|
||||
#### 3. **Scalability and Flexibility**
|
||||
|
||||
Multi-agent systems can more easily scale to handle large-scale or evolving tasks. Adding more agents to the system can increase its capacity or capabilities, allowing it to adapt to larger workloads or new types of tasks. This scalability is essential in dynamic environments where the demand and nature of tasks can change rapidly.
|
||||
|
||||
#### 4. **Robustness and Redundancy**
|
||||
|
||||
Collaboration among multiple agents enhances the system's robustness by introducing redundancy. If one agent fails or encounters an error, others can compensate, ensuring the system remains operational. This redundancy is critical in mission-critical applications where failure is not an option.
|
||||
|
||||
### Overcoming Expenses with API Bills and Hosting
|
||||
|
||||
Deploying multiple agents, especially when relying on cloud-based services or APIs, can incur significant costs. Here are strategies to manage and reduce these expenses:
|
||||
|
||||
#### 1. **Optimize Agent Efficiency**
|
||||
|
||||
Before scaling up the number of agents, ensure each agent operates as efficiently as possible. This can involve refining algorithms, reducing unnecessary API calls, and optimizing data processing to minimize computational requirements and, consequently, the associated costs.
|
||||
|
||||
#### 2. **Use Open Source and Self-Hosted Solutions**
|
||||
|
||||
Where possible, leverage open-source models and technologies that can be self-hosted. While there is an initial investment in setting up the infrastructure, over time, self-hosting can significantly reduce costs related to API calls and reliance on third-party services.
|
||||
|
||||
#### 3. **Implement Intelligent Caching**
|
||||
|
||||
Caching results for frequently asked questions or common tasks can drastically reduce the need for repeated computations or API calls. Intelligent caching systems can determine what information to store and for how long, optimizing the balance between fresh data and computational savings.
|
||||
|
||||
#### 4. **Dynamic Scaling and Load Balancing**
|
||||
|
||||
Use cloud services that offer dynamic scaling and load balancing to adjust the resources allocated based on the current demand. This ensures you're not paying for idle resources during low-usage periods while still being able to handle high demand when necessary.
|
||||
|
||||
#### 5. **Collaborative Cost-Sharing Models**
|
||||
|
||||
In scenarios where multiple stakeholders benefit from the multi-agent system, consider implementing a cost-sharing model. This approach distributes the financial burden among the users or beneficiaries, making it more sustainable.
|
||||
|
||||
#### 6. **Monitor and Analyze Costs**
|
||||
|
||||
Regularly monitor and analyze your usage and associated costs to identify potential savings. Many cloud providers offer tools to track and forecast expenses, helping you to adjust your usage patterns and configurations to minimize costs without sacrificing performance.
|
||||
|
||||
### Conclusion
|
||||
|
||||
The collaboration of multiple agents in AI systems presents a robust solution to the complexity, specialization, scalability, and robustness challenges inherent in single-agent approaches. While the associated costs can be significant, strategic optimization, leveraging open-source technologies, intelligent caching, dynamic resource management, collaborative cost-sharing, and diligent monitoring can mitigate these expenses. By adopting these strategies, organizations can harness the power of multi-agent systems to tackle complex problems more effectively and efficiently, ensuring the sustainable deployment of these advanced technologies.
|
@ -0,0 +1,10 @@
|
||||
from swarms.models.azure_openai_llm import AzureOpenAI
|
||||
|
||||
# Initialize Azure OpenAI
|
||||
model = AzureOpenAI()
|
||||
|
||||
# Run the model
|
||||
model(
|
||||
"Create a youtube script for a video on how to use the swarms"
|
||||
" framework"
|
||||
)
|
@ -0,0 +1,36 @@
|
||||
from swarms import Agent, OpenAIChat, SequentialWorkflow
|
||||
|
||||
# Example usage
|
||||
llm = OpenAIChat(
|
||||
temperature=0.5,
|
||||
max_tokens=3000,
|
||||
)
|
||||
|
||||
# Initialize the Agent with the language agent
|
||||
agent1 = Agent(
|
||||
agent_name="John the writer",
|
||||
llm=llm,
|
||||
max_loops=1,
|
||||
dashboard=False,
|
||||
)
|
||||
|
||||
|
||||
# Create another Agent for a different task
|
||||
agent2 = Agent("Summarizer", llm=llm, max_loops=1, dashboard=False)
|
||||
|
||||
|
||||
# Create the workflow
|
||||
workflow = SequentialWorkflow(
|
||||
name="Blog Generation Workflow",
|
||||
description=(
|
||||
"Generate a youtube transcript on how to deploy agents into"
|
||||
" production"
|
||||
),
|
||||
max_loops=1,
|
||||
autosave=True,
|
||||
dashboard=False,
|
||||
agents=[agent1, agent2],
|
||||
)
|
||||
|
||||
# Run the workflow
|
||||
workflow.run()
|
@ -0,0 +1,223 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Callable, Mapping
|
||||
|
||||
import openai
|
||||
from langchain_core.pydantic_v1 import (
|
||||
Field,
|
||||
SecretStr,
|
||||
root_validator,
|
||||
)
|
||||
from langchain_core.utils import (
|
||||
convert_to_secret_str,
|
||||
get_from_dict_or_env,
|
||||
)
|
||||
from langchain_openai.llms.base import BaseOpenAI
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AzureOpenAI(BaseOpenAI):
|
||||
"""Azure-specific OpenAI large language models.
|
||||
|
||||
To use, you should have the ``openai`` python package installed, and the
|
||||
environment variable ``OPENAI_API_KEY`` set with your API key.
|
||||
|
||||
Any parameters that are valid to be passed to the openai.create call can be passed
|
||||
in, even if not explicitly saved on this class.
|
||||
|
||||
Example:
|
||||
.. code-block:: python
|
||||
|
||||
from swarms import AzureOpenAI
|
||||
|
||||
openai = AzureOpenAI(model_name="gpt-3.5-turbo-instruct")
|
||||
"""
|
||||
|
||||
azure_endpoint: str | None = None
|
||||
"""Your Azure endpoint, including the resource.
|
||||
|
||||
Automatically inferred from env var `AZURE_OPENAI_ENDPOINT` if not provided.
|
||||
|
||||
Example: `https://example-resource.azure.openai.com/`
|
||||
"""
|
||||
deployment_name: str | None = Field(
|
||||
default=None, alias="azure_deployment"
|
||||
)
|
||||
"""A model deployment.
|
||||
|
||||
If given sets the base client URL to include `/deployments/{azure_deployment}`.
|
||||
Note: this means you won't be able to use non-deployment endpoints.
|
||||
"""
|
||||
openai_api_version: str = Field(default="", alias="api_version")
|
||||
"""Automatically inferred from env var `OPENAI_API_VERSION` if not provided."""
|
||||
openai_api_key: SecretStr | None = Field(
|
||||
default=None, alias="api_key"
|
||||
)
|
||||
"""Automatically inferred from env var `AZURE_OPENAI_API_KEY` if not provided."""
|
||||
azure_ad_token: SecretStr | None = None
|
||||
"""Your Azure Active Directory token.
|
||||
|
||||
Automatically inferred from env var `AZURE_OPENAI_AD_TOKEN` if not provided.
|
||||
|
||||
For more:
|
||||
https://www.microsoft.com/en-us/security/business/identity-access/microsoft-entra-id.
|
||||
""" # noqa: E501
|
||||
azure_ad_token_provider: Callable[[], str] | None = None
|
||||
"""A function that returns an Azure Active Directory token.
|
||||
|
||||
Will be invoked on every request.
|
||||
"""
|
||||
openai_api_type: str = ""
|
||||
"""Legacy, for openai<1.0.0 support."""
|
||||
validate_base_url: bool = True
|
||||
"""For backwards compatibility. If legacy val openai_api_base is passed in, try to
|
||||
infer if it is a base_url or azure_endpoint and update accordingly.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def get_lc_namespace(cls) -> list[str]:
|
||||
"""Get the namespace of the langchain object."""
|
||||
return ["langchain", "llms", "openai"]
|
||||
|
||||
@root_validator()
|
||||
def validate_environment(cls, values: dict) -> dict:
|
||||
"""Validate that api key and python package exists in environment."""
|
||||
if values["n"] < 1:
|
||||
raise ValueError("n must be at least 1.")
|
||||
if values["streaming"] and values["n"] > 1:
|
||||
raise ValueError("Cannot stream results when n > 1.")
|
||||
if values["streaming"] and values["best_of"] > 1:
|
||||
raise ValueError(
|
||||
"Cannot stream results when best_of > 1."
|
||||
)
|
||||
|
||||
# Check OPENAI_KEY for backwards compatibility.
|
||||
# TODO: Remove OPENAI_API_KEY support to avoid possible conflict when using
|
||||
# other forms of azure credentials.
|
||||
openai_api_key = (
|
||||
values["openai_api_key"]
|
||||
or os.getenv("AZURE_OPENAI_API_KEY")
|
||||
or os.getenv("OPENAI_API_KEY")
|
||||
)
|
||||
values["openai_api_key"] = (
|
||||
convert_to_secret_str(openai_api_key)
|
||||
if openai_api_key
|
||||
else None
|
||||
)
|
||||
|
||||
values["azure_endpoint"] = values[
|
||||
"azure_endpoint"
|
||||
] or os.getenv("AZURE_OPENAI_ENDPOINT")
|
||||
azure_ad_token = values["azure_ad_token"] or os.getenv(
|
||||
"AZURE_OPENAI_AD_TOKEN"
|
||||
)
|
||||
values["azure_ad_token"] = (
|
||||
convert_to_secret_str(azure_ad_token)
|
||||
if azure_ad_token
|
||||
else None
|
||||
)
|
||||
values["openai_api_base"] = values[
|
||||
"openai_api_base"
|
||||
] or os.getenv("OPENAI_API_BASE")
|
||||
values["openai_proxy"] = get_from_dict_or_env(
|
||||
values,
|
||||
"openai_proxy",
|
||||
"OPENAI_PROXY",
|
||||
default="",
|
||||
)
|
||||
values["openai_organization"] = (
|
||||
values["openai_organization"]
|
||||
or os.getenv("OPENAI_ORG_ID")
|
||||
or os.getenv("OPENAI_ORGANIZATION")
|
||||
)
|
||||
values["openai_api_version"] = values[
|
||||
"openai_api_version"
|
||||
] or os.getenv("OPENAI_API_VERSION")
|
||||
values["openai_api_type"] = get_from_dict_or_env(
|
||||
values,
|
||||
"openai_api_type",
|
||||
"OPENAI_API_TYPE",
|
||||
default="azure",
|
||||
)
|
||||
# For backwards compatibility. Before openai v1, no distinction was made
|
||||
# between azure_endpoint and base_url (openai_api_base).
|
||||
openai_api_base = values["openai_api_base"]
|
||||
if openai_api_base and values["validate_base_url"]:
|
||||
if "/openai" not in openai_api_base:
|
||||
values["openai_api_base"] = (
|
||||
values["openai_api_base"].rstrip("/") + "/openai"
|
||||
)
|
||||
raise ValueError(
|
||||
"As of openai>=1.0.0, Azure endpoints should be"
|
||||
" specified via the `azure_endpoint` param not"
|
||||
" `openai_api_base` (or alias `base_url`)."
|
||||
)
|
||||
if values["deployment_name"]:
|
||||
raise ValueError(
|
||||
"As of openai>=1.0.0, if `deployment_name` (or"
|
||||
" alias `azure_deployment`) is specified then"
|
||||
" `openai_api_base` (or alias `base_url`) should"
|
||||
" not be. Instead use `deployment_name` (or alias"
|
||||
" `azure_deployment`) and `azure_endpoint`."
|
||||
)
|
||||
values["deployment_name"] = None
|
||||
client_params = {
|
||||
"api_version": values["openai_api_version"],
|
||||
"azure_endpoint": values["azure_endpoint"],
|
||||
"azure_deployment": values["deployment_name"],
|
||||
"api_key": (
|
||||
values["openai_api_key"].get_secret_value()
|
||||
if values["openai_api_key"]
|
||||
else None
|
||||
),
|
||||
"azure_ad_token": (
|
||||
values["azure_ad_token"].get_secret_value()
|
||||
if values["azure_ad_token"]
|
||||
else None
|
||||
),
|
||||
"azure_ad_token_provider": values[
|
||||
"azure_ad_token_provider"
|
||||
],
|
||||
"organization": values["openai_organization"],
|
||||
"base_url": values["openai_api_base"],
|
||||
"timeout": values["request_timeout"],
|
||||
"max_retries": values["max_retries"],
|
||||
"default_headers": values["default_headers"],
|
||||
"default_query": values["default_query"],
|
||||
"http_client": values["http_client"],
|
||||
}
|
||||
values["client"] = openai.AzureOpenAI(
|
||||
**client_params
|
||||
).completions
|
||||
values["async_client"] = openai.AsyncAzureOpenAI(
|
||||
**client_params
|
||||
).completions
|
||||
|
||||
return values
|
||||
|
||||
@property
|
||||
def _identifying_params(self) -> Mapping[str, Any]:
|
||||
return {
|
||||
**{"deployment_name": self.deployment_name},
|
||||
**super()._identifying_params,
|
||||
}
|
||||
|
||||
@property
|
||||
def _invocation_params(self) -> dict[str, Any]:
|
||||
openai_params = {"model": self.deployment_name}
|
||||
return {**openai_params, **super()._invocation_params}
|
||||
|
||||
@property
|
||||
def _llm_type(self) -> str:
|
||||
"""Return type of llm."""
|
||||
return "azure"
|
||||
|
||||
@property
|
||||
def lc_attributes(self) -> dict[str, Any]:
|
||||
return {
|
||||
"openai_api_type": self.openai_api_type,
|
||||
"openai_api_version": self.openai_api_version,
|
||||
}
|
Loading…
Reference in new issue