[PYDANTIC UPDATE]

pull/421/head
Kye 10 months ago
parent 8fe317b909
commit 5a60eb4f2d

@ -12,6 +12,7 @@ Orchestrate swarms of agents for production-grade applications.
</div>
Individual agents are barely being deployd into production because of 5 suffocating challanges: short memory, single task threading, hallucinations, high cost, and lack of collaboration. With Multi-agent collaboration, you can effectively eliminate all of these issues. Swarms provides you with simple, reliable, and agile primitives to build your own Swarm for your specific use case. Now, Swarms is being used in production by RBC, John Deere, and many AI startups. To learn more about the unparalled benefits about multi-agent collaboration check out this github repository for research papers or book a call with me!
----
@ -21,7 +22,7 @@ Orchestrate swarms of agents for production-grade applications.
---
## Usage
With Swarms, you can create structures, such as Agents, Swarms, and Workflows, that are composed of different types of tasks. Let's build a simple creative agent that will dynamically create a 10,000 word blog on health and wellness.
Run example in Collab: <a target="_blank" href="https://colab.research.google.com/github/kyegomez/swarms/blob/master/playground/swarms_example.ipynb">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>

@ -0,0 +1,70 @@
# Swarm Ecosystem
Welcome to the Swarm Ecosystem, a comprehensive suite of tools and frameworks designed to empower developers to orhestrate swarms of autonomous agents for a variety of applications. Dive into our ecosystem below:
| Project | Description | Link |
| ------- | ----------- | ---- |
| **Swarms Framework** | A Python-based framework that enables the creation, deployment, and scaling of reliable swarms of autonomous agents aimed at automating complex workflows. | [Swarms Framework](https://github.com/kyegomez/swarms) |
| **Swarms Cloud** | A cloud-based service offering Swarms-as-a-Service with guaranteed 100% uptime, cutting-edge performance, and enterprise-grade reliability for seamless scaling and management of swarms. | [Swarms Cloud](https://github.com/kyegomez/swarms-core) |
| **Swarms Core** | Provides backend utilities focusing on concurrency, multi-threading, and advanced execution strategies, developed in Rust for maximum efficiency and performance. | [Swarms Core](https://github.com/kyegomez/swarms-core) |
| **Swarm Foundation Models** | A dedicated repository for the creation, optimization, and training of groundbreaking swarming models. Features innovative models like PSO with transformers, ant colony optimizations, and more, aiming to surpass traditional architectures like Transformers and SSMs. Open for community contributions and ideas. | [Swarm Foundation Models](https://github.com/kyegomez/swarms-pytorch) |
| **Swarm Platform** | The Swarms dashboard Platform | [Swarm Platform](https://swarms.world/) |
| **Swarms JS** | Swarms Framework in JS. Orchestrate any agents and enable multi-agent collaboration between various agents! | [Swarm JS](https://github.com/kyegomez/swarms-js) |
----
## 🫶 Contributions:
The easiest way to contribute is to pick any issue with the `good first issue` tag 💪. Read the Contributing guidelines [here](/CONTRIBUTING.md). Bug Report? [File here](https://github.com/swarms/gateway/issues) | Feature Request? [File here](https://github.com/swarms/gateway/issues)
Swarms is an open-source project, and contributions are VERY welcome. If you want to contribute, you can create new features, fix bugs, or improve the infrastructure. Please refer to the [CONTRIBUTING.md](https://github.com/kyegomez/swarms/blob/master/CONTRIBUTING.md) and our [contributing board](https://github.com/users/kyegomez/projects/1) to participate in Roadmap discussions!
<a href="https://github.com/kyegomez/swarms/graphs/contributors">
<img src="https://contrib.rocks/image?repo=kyegomez/swarms" />
</a>
<a href="https://github.com/kyegomez/swarms/graphs/contributors">
<img src="https://contrib.rocks/image?repo=kyegomez/swarms-cloud" />
</a>
<a href="https://github.com/kyegomez/swarms/graphs/contributors">
<img src="https://contrib.rocks/image?repo=kyegomez/swarms-platform" />
</a>
<a href="https://github.com/kyegomez/swarms/graphs/contributors">
<img src="https://contrib.rocks/image?repo=kyegomez/swarms-js" />
</a>
----
## Community
Join our growing community around the world, for real-time support, ideas, and discussions on Swarms 😊
- View our official [Blog](https://swarms.apac.ai)
- Chat live with us on [Discord](https://discord.gg/kS3rwKs3ZC)
- Follow us on [Twitter](https://twitter.com/kyegomez)
- Connect with us on [LinkedIn](https://www.linkedin.com/company/the-swarm-corporation)
- Visit us on [YouTube](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ)
- [Join the Swarms community on Discord!](https://discord.gg/AJazBmhKnr)
- Join our Swarms Community Gathering every Thursday at 1pm NYC Time to unlock the potential of autonomous agents in automating your daily tasks [Sign up here](https://lu.ma/5p2jnc2v)
---
## Discovery Call
Book a discovery call to learn how Swarms can lower your operating costs by 40% with swarms of autonomous agents in lightspeed. [Click here to book a time that works for you!](https://calendly.com/swarm-corp/30min?month=2023-11)
## Accelerate Backlog
Help us accelerate our backlog by supporting us financially! Note, we're an open source corporation and so all the revenue we generate is through donations at the moment ;)
<a href="https://polar.sh/kyegomez"><img src="https://polar.sh/embed/fund-our-backlog.svg?org=kyegomez" /></a>
---

@ -1,15 +1,26 @@
from swarms import Agent, OpenAIChat
from swarms import Agent, AnthropicChat
from langchain.tools import tool
# Tool
@tool
def search_api(query: str, max_results: int = 10):
"""
Search the web for the query and return the top `max_results` results.
"""
return f"Search API: {query} -> {max_results} results"
## Initialize the workflow
agent = Agent(
llm=OpenAIChat(),
llm=AnthropicChat(),
max_loops="auto",
autosave=True,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
interactive=True,
tools=[search_api],
)
# Run the workflow on a task

@ -0,0 +1,34 @@
from swarms import Agent, AnthropicChat, tool
# Tool
@tool # Wrap the function with the tool decorator
def search_api(query: str, max_results: int = 10):
"""
Search the web for the query and return the top `max_results` results.
"""
return f"Search API: {query} -> {max_results} results"
## Initialize the workflow
agent = Agent(
agent_name="Youtube Transcript Generator",
agent_description=(
"Generate a transcript for a youtube video on what swarms"
" are!"
),
llm=AnthropicChat(),
max_loops="auto",
autosave=True,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
tools=[search_api],
)
# Run the workflow on a task
agent(
"Generate a transcript for a youtube video on what swarms are!"
" Output a <DONE> token when done."
)

@ -37,7 +37,7 @@ backoff = "2.2.1"
datasets = "*"
optimum = "1.15.0"
diffusers = "*"
langchain = "*"
langchain = "0.1.7"
toml = "*"
pypdf = "4.0.1"
accelerate = "*"
@ -46,7 +46,6 @@ httpx = "0.24.1"
tiktoken = "0.4.0"
ratelimit = "2.2.1"
loguru = "0.7.2"
pydantic-settings = "*"
huggingface-hub = "*"
pydantic = "*"
tenacity = "8.2.2"
@ -59,10 +58,7 @@ bitsandbytes = "*"
sentence-transformers = "*"
peft = "*"
psutil = "*"
ultralytics = "*"
timm = "*"
supervision = "*"
roboflow = "*"
[tool.poetry.dev-dependencies]
black = "23.3.0"

@ -1,24 +1,19 @@
torch==2.1.1
transformers
pandas==2.2.1
langchain==0.0.333
langchain-experimental==0.0.10
pandas
langchain==0.1.7
langchain-experimental
httpx==0.24.1
Pillow==9.4.0
faiss-cpu==1.7.4
openai==0.28.0
datasets==2.14.5
pydantic==1.10.12
bitsandbytes
pydantic
huggingface-hub
google-generativeai==0.3.1
sentencepiece==0.1.98
requests_mock
pypdf==4.0.1
accelerate==0.22.0
loguru==0.7.2
chromadb
optimum
diffusers
toml
tiktoken==0.4.0
colored
@ -26,26 +21,13 @@ addict
backoff==2.2.1
ratelimit==2.2.1
termcolor==2.2.0
diffusers
einops==0.7.0
opencv-python-headless==4.8.1.78
numpy
openai==0.28.0
langchain-community
opencv-python==4.9.0.80
timm
cohere==4.53
torchvision==0.16.1
rich==13.5.2
mkdocs
mkdocs-material
mkdocs-glightbox
pre-commit==3.6.2
peft
psutil
ultralytics
supervision
anthropic
pinecone-client
roboflow
black

@ -10,7 +10,8 @@ bootup()
from swarms.agents import * # noqa: E402, F403
from swarms.artifacts import * # noqa: E402, F403
from swarms.chunkers import * # noqa: E402, F403
from swarms.loaders import * # noqa: E402, F403
# from swarms.loaders import * # noqa: E402, F403
from swarms.models import * # noqa: E402, F403
from swarms.prompts import * # noqa: E402, F403
from swarms.structs import * # noqa: E402, F403

@ -47,8 +47,6 @@ class ChromaDB:
limit_tokens: Optional[int] = 1000,
n_results: int = 2,
embedding_function: Callable = None,
data_loader: Callable = None,
multimodal: bool = False,
docs_folder: str = None,
verbose: bool = False,
*args,
@ -75,22 +73,12 @@ class ChromaDB:
**kwargs,
)
# Data loader
if data_loader:
self.data_loader = data_loader
else:
self.data_loader = None
# Embedding model
if embedding_function:
self.embedding_function = embedding_function
else:
self.embedding_function = None
# If multimodal set the embedding model to OpenCLIP
if multimodal:
self.embedding_function = None
# Create ChromaDB client
self.client = chromadb.Client()

@ -1,11 +1,11 @@
from langchain_community.llms import (
Anthropic,
AzureOpenAI,
Cohere,
MosaicML,
OpenAI,
OpenAIChat,
Replicate,
from swarms.models.popular_llms import (
AnthropicChat,
CohereChat,
MosaicMLChat,
OpenAILLM,
ReplicateLLM,
AzureOpenAILLM,
OpenAIChatLLM,
)
from swarms.models.base_embedding_model import BaseEmbeddingModel
from swarms.models.base_llm import AbstractLLM # noqa: E402
@ -47,6 +47,7 @@ from swarms.models.wizard_storytelling import WizardLLMStoryTeller
from swarms.models.zephyr import Zephyr # noqa: E402
from swarms.models.zeroscope import ZeroscopeTTV # noqa: E402
__all__ = [
"AbstractLLM",
"BaseEmbeddingModel",
@ -55,7 +56,6 @@ __all__ = [
"CLIPQ",
"FireFunctionCaller",
"Fuyu",
"Gemini",
"Gigabind",
"GPT4VisionAPI",
"HuggingfaceLLM",
@ -71,27 +71,27 @@ __all__ = [
"Petals",
"QwenVLMultiModal",
"RoboflowMultiModal",
"SegmentAnythingMarkGenerator",
"SamplingParams",
"SamplingType",
"SegmentAnythingMarkGenerator",
"TimmModel",
"TogetherLLM",
"AudioModality",
"ImageModality",
"MultimodalData",
"TextModality",
"VideoModality",
"UltralyticsModel",
"Vilt",
"WizardLLMStoryTeller",
"Zephyr",
"ZeroscopeTTV",
"AzureChatOpenAI",
"OpenAIChat",
"Anthropic",
"AzureOpenAI",
"Cohere",
"MosaicML",
"OpenAI",
"Replicate",
"AnthropicChat",
"CohereChat",
"MosaicMLChat",
"OpenAILLM",
"ReplicateLLM",
"AzureOpenAILLM",
"OpenAIChatLLM",
"AudioModality",
"ImageModality",
"MultimodalData",
"TextModality",
"Gemini",
"VideoModality",
]

@ -5,7 +5,7 @@ import warnings
from typing import Any, Callable, Literal, Sequence
import numpy as np
from pydantic import BaseModel, Extra, Field, root_validator
from pydantic import model_validator, ConfigDict, BaseModel, Field
from tenacity import (
AsyncRetrying,
before_sleep_log,
@ -179,7 +179,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
"""
client: Any #: :meta private:
client: Any = None #: :meta private:
model: str = "text-embedding-ada-002"
deployment: str = model # to support Azure OpenAI Service custom deployment names
openai_api_version: str | None = None
@ -218,13 +218,10 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
"""Whether to show a progress bar when embedding."""
model_kwargs: dict[str, Any] = Field(default_factory=dict)
"""Holds any model parameters valid for `create` call not explicitly specified."""
model_config = ConfigDict(extra="forbid")
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
@root_validator(pre=True)
@model_validator(mode="before")
@classmethod
def build_extra(cls, values: dict[str, Any]) -> dict[str, Any]:
"""Build extra kwargs from additional params that were passed in."""
all_required_field_names = get_pydantic_field_names(cls)
@ -255,7 +252,8 @@ class OpenAIEmbeddings(BaseModel, Embeddings):
values["model_kwargs"] = extra
return values
@root_validator()
@model_validator()
@classmethod
def validate_environment(cls, values: dict) -> dict:
"""Validate that api key and python package exists in environment."""
values["openai_api_key"] = get_from_dict_or_env(

@ -5,7 +5,7 @@ from typing import Any, Callable
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms import BaseLLM
from langchain.pydantic_v1 import BaseModel, root_validator
from langchain.pydantic_v1 import BaseModel
from langchain.schema import Generation, LLMResult
from langchain.utils import get_from_dict_or_env
from tenacity import (
@ -15,6 +15,7 @@ from tenacity import (
stop_after_attempt,
wait_exponential,
)
from pydantic import model_validator
logger = logging.getLogger(__name__)
@ -104,7 +105,8 @@ class GooglePalm(BaseLLM, BaseModel):
"""Number of chat completions to generate for each prompt. Note that the API may
not return the full n completions if duplicates are generated."""
@root_validator()
@model_validator()
@classmethod
def validate_environment(cls, values: dict) -> dict:
"""Validate api key, python package exists."""
google_api_key = get_from_dict_or_env(

@ -0,0 +1,48 @@
from langchain_community.chat_models.azure_openai import (
AzureChatOpenAI,
)
from langchain_community.chat_models.openai import (
ChatOpenAI as OpenAIChat,
)
from langchain_community.llms import (
Anthropic,
Cohere,
MosaicML,
OpenAI,
Replicate,
)
class AnthropicChat(Anthropic):
def __call__(self, *args, **kwargs):
return self.invoke(*args, **kwargs)
class CohereChat(Cohere):
def __call__(self, *args, **kwargs):
return self.invoke(*args, **kwargs)
class MosaicMLChat(MosaicML):
def __call__(self, *args, **kwargs):
return self.invoke(*args, **kwargs)
class OpenAILLM(OpenAI):
def __call__(self, *args, **kwargs):
return self.invoke(*args, **kwargs)
class ReplicateLLM(Replicate):
def __call__(self, *args, **kwargs):
return self.invoke(*args, **kwargs)
class AzureOpenAILLM(AzureChatOpenAI):
def __call__(self, *args, **kwargs):
return self.invoke(*args, **kwargs)
class OpenAIChatLLM(OpenAIChat):
def __call__(self, *args, **kwargs):
return self.invoke(*args, **kwargs)

@ -10,7 +10,7 @@ import torch
from cachetools import TTLCache
from diffusers import StableDiffusionXLPipeline
from PIL import Image
from pydantic import validator
from pydantic import field_validator
from termcolor import colored
@ -72,7 +72,8 @@ class SSD1B:
arbitrary_types_allowed = True
@validator("max_retries", "time_seconds")
@field_validator("max_retries", "time_seconds")
@classmethod
def must_be_positive(cls, value):
if value <= 0:
raise ValueError("Must be positive")

@ -1,11 +1,31 @@
import datetime
from pydantic import BaseModel, Field
time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
class Thoughts(BaseModel):
text: str = Field(..., title="Thoughts")
reasoning: str = Field(..., title="Reasoning")
plan: str = Field(..., title="Plan")
class Command(BaseModel):
name: str = Field(..., title="Command Name")
args: dict = Field({}, title="Command Arguments")
class ResponseFormat(BaseModel):
thoughts: Thoughts = Field(..., title="Thoughts")
command: Command = Field(..., title="Command")
response_json = ResponseFormat.model_json_schema()
def worker_tools_sop_promp(name: str, memory: str, time=time):
out = """
You are {name},
out = f"""
You are {name},
Your decisions must always be made independently without seeking user assistance.
Play to your strengths as an LLM and pursue simple strategies with no legal complications.
If you have completed all your tasks, make sure to use the 'finish' command.
@ -29,7 +49,7 @@ def worker_tools_sop_promp(name: str, memory: str, time=time):
1. Internet access for searches and information gathering.
2. Long Term memory management.
3. GPT-3.5 powered Agents for delegation of simple tasks.
3. Agents for delegation of simple tasks.
4. File output.
Performance Evaluation:
@ -39,29 +59,18 @@ def worker_tools_sop_promp(name: str, memory: str, time=time):
3. Reflect on past decisions and strategies to refine your approach.
4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.
You should only respond in JSON format as described below
Response Format:
{
'thoughts': {
'text': 'thoughts',
'reasoning': 'reasoning',
'plan': '- short bulleted - list that conveys - long-term plan',
'criticism': 'constructive self-criticism',
'speak': 'thoughts summary to say to user'
},
'command': {
'name': 'command name',
'args': {
'arg name': 'value'
}
}
}
You should only respond in JSON format as described below Response Format, you will respond only in markdown format within 6 backticks. The JSON will be in markdown format.
```
{response_json}
```
Ensure the response can be parsed by Python json.loads
System: The current time and date is {time}
System: This reminds you of these events from your past:
[{memory}]
Human: Determine which next command to use, and respond using the format specified above:
""".format(name=name, time=time, memory=memory)
"""
return str(out)

@ -6,7 +6,7 @@ import random
import sys
import time
import uuid
from typing import Any, Callable, Dict, List, Optional, Tuple
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
import yaml
from loguru import logger
@ -174,7 +174,7 @@ class Agent:
agent_name: str = "swarm-worker-01",
agent_description: str = None,
system_prompt: str = AGENT_SYSTEM_PROMPT_3,
tools: List[BaseTool] = None,
tools: Union[List[BaseTool]] = None,
dynamic_temperature_enabled: Optional[bool] = False,
sop: Optional[str] = None,
sop_list: Optional[List[str]] = None,

@ -7,7 +7,7 @@ from pydantic import BaseModel, Field
class TaskInput(BaseModel):
__root__: Any = Field(
task: Any = Field(
...,
description=(
"The input parameters for the task. Any value is allowed."
@ -57,7 +57,7 @@ class ArtifactUpload(BaseModel):
class StepInput(BaseModel):
__root__: Any = Field(
step: Any = Field(
...,
description=(
"Input parameters for the task step. Any value is"
@ -68,7 +68,7 @@ class StepInput(BaseModel):
class StepOutput(BaseModel):
__root__: Any = Field(
step: Any = Field(
...,
description=(
"Output that the task step has produced. Any value is"

@ -1,3 +1,4 @@
from swarms.tools.tool import BaseTool, Tool, StructuredTool, tool
from swarms.tools.code_executor import CodeExecutor
from swarms.tools.exec_tool import (
AgentAction,
@ -6,13 +7,12 @@ from swarms.tools.exec_tool import (
execute_tool_by_name,
preprocess_json_input,
)
from swarms.tools.tool import BaseTool, StructuredTool, Tool, tool
from swarms.tools.tool_utils import (
execute_tools,
extract_tool_commands,
parse_and_execute_tools,
tool_find_by_name,
scrape_tool_func_docs,
tool_find_by_name,
)
__all__ = [

@ -1,953 +0,0 @@
"""Base implementation for tools or skills."""
from __future__ import annotations
import asyncio
import inspect
import warnings
from abc import abstractmethod
from functools import partial
from inspect import signature
from typing import Any, Awaitable, Callable, Dict, Union
from langchain.callbacks.base import BaseCallbackManager
from langchain.callbacks.manager import (
AsyncCallbackManager,
AsyncCallbackManagerForToolRun,
CallbackManager,
CallbackManagerForToolRun,
Callbacks,
)
from langchain.load.serializable import Serializable
from langchain.schema.runnable import (
Runnable,
RunnableConfig,
RunnableSerializable,
)
from pydantic import (
BaseModel,
Extra,
Field,
create_model,
root_validator,
validate_arguments,
)
class SchemaAnnotationError(TypeError):
"""Raised when 'args_schema' is missing or has an incorrect type annotation."""
def _create_subset_model(
name: str, model: BaseModel, field_names: list
) -> type[BaseModel]:
"""Create a pydantic model with only a subset of model's fields."""
fields = {}
for field_name in field_names:
field = model.__fields__[field_name]
fields[field_name] = (field.outer_type_, field.field_info)
return create_model(name, **fields) # type: ignore
def _get_filtered_args(
inferred_model: type[BaseModel],
func: Callable,
) -> dict:
"""Get the arguments from a function's signature."""
schema = inferred_model.schema()["properties"]
valid_keys = signature(func).parameters
return {
k: schema[k]
for k in valid_keys
if k not in ("run_manager", "callbacks")
}
class _SchemaConfig:
"""Configuration for the pydantic model."""
extra: Any = Extra.forbid
arbitrary_types_allowed: bool = True
def create_schema_from_function(
model_name: str,
func: Callable,
) -> type[BaseModel]:
"""Create a pydantic schema from a function's signature.
Args:
model_name: Name to assign to the generated pydandic schema
func: Function to generate the schema from
Returns:
A pydantic model with the same arguments as the function
"""
# https://docs.pydantic.dev/latest/usage/validation_decorator/
validated = validate_arguments(func, config=_SchemaConfig) # type: ignore
inferred_model = validated.model # type: ignore
if "run_manager" in inferred_model.__fields__:
del inferred_model.__fields__["run_manager"]
if "callbacks" in inferred_model.__fields__:
del inferred_model.__fields__["callbacks"]
# Pydantic adds placeholder virtual fields we need to strip
valid_properties = _get_filtered_args(inferred_model, func)
return _create_subset_model(
f"{model_name}Schema", inferred_model, list(valid_properties)
)
class ToolException(Exception):
"""An optional exception that tool throws when execution error occurs.
When this exception is thrown, the agent will not stop working,
but will handle the exception according to the handle_tool_error
variable of the tool, and the processing result will be returned
to the agent as observation, and printed in red on the console.
"""
class BaseTool(RunnableSerializable[Union[str, Dict], Any]):
"""Interface swarms tools must implement."""
def __init_subclass__(cls, **kwargs: Any) -> None:
"""Create the definition of the new tool class."""
super().__init_subclass__(**kwargs)
args_schema_type = cls.__annotations__.get(
"args_schema", None
)
if args_schema_type is not None:
if (
args_schema_type is None
or args_schema_type == BaseModel
):
# Throw errors for common mis-annotations.
# TODO: Use get_args / get_origin and fully
# specify valid annotations.
typehint_mandate = """
class ChildTool(BaseTool):
...
args_schema: Type[BaseModel] = SchemaClass
..."""
name = cls.__name__
raise SchemaAnnotationError(
f"Tool definition for {name} must include valid"
" type annotations for argument 'args_schema' to"
" behave as expected.\nExpected annotation of"
" 'Type[BaseModel]' but got"
f" '{args_schema_type}'.\nExpected class looks"
f" like:\n{typehint_mandate}"
)
name: str
"""The unique name of the tool that clearly communicates its purpose."""
description: str
"""Used to tell the model how/when/why to use the tool.
You can provide few-shot examples as a part of the description.
"""
args_schema: type[BaseModel] | None = None
"""Pydantic model class to validate and parse the tool's input arguments."""
return_direct: bool = False
"""Whether to return the tool's output directly. Setting this to True means
that after the tool is called, the AgentExecutor will stop looping.
"""
verbose: bool = False
"""Whether to log the tool's progress."""
callbacks: Callbacks = Field(default=None, exclude=True)
"""Callbacks to be called during tool execution."""
callback_manager: BaseCallbackManager | None = Field(
default=None, exclude=True
)
"""Deprecated. Please use callbacks instead."""
tags: list[str] | None = None
"""Optional list of tags associated with the tool. Defaults to None
These tags will be associated with each call to this tool,
and passed as arguments to the handlers defined in `callbacks`.
You can use these to eg identify a specific instance of a tool with its use case.
"""
metadata: dict[str, Any] | None = None
"""Optional metadata associated with the tool. Defaults to None
This metadata will be associated with each call to this tool,
and passed as arguments to the handlers defined in `callbacks`.
You can use these to eg identify a specific instance of a tool with its use case.
"""
handle_tool_error: (
bool | str | Callable[[ToolException], str] | None
) = False
"""Handle the content of the ToolException thrown."""
class Config(Serializable.Config):
"""Configuration for this pydantic object."""
arbitrary_types_allowed = True
@property
def is_single_input(self) -> bool:
"""Whether the tool only accepts a single input."""
keys = {k for k in self.args if k != "kwargs"}
return len(keys) == 1
@property
def args(self) -> dict:
if self.args_schema is not None:
return self.args_schema.schema()["properties"]
else:
schema = create_schema_from_function(self.name, self._run)
return schema.schema()["properties"]
# --- Runnable ---
@property
def input_schema(self) -> type[BaseModel]:
"""The tool's input schema."""
if self.args_schema is not None:
return self.args_schema
else:
return create_schema_from_function(self.name, self._run)
def invoke(
self,
input: str | dict,
config: RunnableConfig | None = None,
**kwargs: Any,
) -> Any:
config = config or {}
return self.run(
input,
callbacks=config.get("callbacks"),
tags=config.get("tags"),
metadata=config.get("metadata"),
run_name=config.get("run_name"),
**kwargs,
)
async def ainvoke(
self,
input: str | dict,
config: RunnableConfig | None = None,
**kwargs: Any,
) -> Any:
config = config or {}
return await self.arun(
input,
callbacks=config.get("callbacks"),
tags=config.get("tags"),
metadata=config.get("metadata"),
run_name=config.get("run_name"),
**kwargs,
)
# --- Tool ---
def _parse_input(
self,
tool_input: str | dict,
) -> str | dict[str, Any]:
"""Convert tool input to pydantic model."""
input_args = self.args_schema
if isinstance(tool_input, str):
if input_args is not None:
key_ = next(iter(input_args.__fields__.keys()))
input_args.validate({key_: tool_input})
return tool_input
else:
if input_args is not None:
result = input_args.parse_obj(tool_input)
return {
k: v
for k, v in result.dict().items()
if k in tool_input
}
return tool_input
@root_validator(skip_on_failure=True)
def raise_deprecation(cls, values: dict) -> dict:
"""Raise deprecation warning if callback_manager is used."""
if values.get("callback_manager") is not None:
warnings.warn(
(
"callback_manager is deprecated. Please use"
" callbacks instead."
),
DeprecationWarning,
)
values["callbacks"] = values.pop("callback_manager", None)
return values
@abstractmethod
def _run(
self,
*args: Any,
**kwargs: Any,
) -> Any:
"""Use the tool.
Add run_manager: Optional[CallbackManagerForToolRun] = None
to child implementations to enable tracing,
"""
async def _arun(
self,
*args: Any,
**kwargs: Any,
) -> Any:
"""Use the tool asynchronously.
Add run_manager: Optional[AsyncCallbackManagerForToolRun] = None
to child implementations to enable tracing,
"""
return await asyncio.get_running_loop().run_in_executor(
None,
partial(self._run, **kwargs),
*args,
)
def _to_args_and_kwargs(
self, tool_input: str | dict
) -> tuple[tuple, dict]:
# For backwards compatibility, if run_input is a string,
# pass as a positional argument.
if isinstance(tool_input, str):
return (tool_input,), {}
else:
return (), tool_input
def run(
self,
tool_input: str | dict,
verbose: bool | None = None,
start_color: str | None = "green",
color: str | None = "green",
callbacks: Callbacks = None,
*,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
run_name: str | None = None,
**kwargs: Any,
) -> Any:
"""Run the tool."""
parsed_input = self._parse_input(tool_input)
if not self.verbose and verbose is not None:
verbose_ = verbose
else:
verbose_ = self.verbose
callback_manager = CallbackManager.configure(
callbacks,
self.callbacks,
verbose_,
tags,
self.tags,
metadata,
self.metadata,
)
# TODO: maybe also pass through run_manager is _run supports kwargs
new_arg_supported = signature(self._run).parameters.get(
"run_manager"
)
run_manager = callback_manager.on_tool_start(
{"name": self.name, "description": self.description},
(
tool_input
if isinstance(tool_input, str)
else str(tool_input)
),
color=start_color,
name=run_name,
**kwargs,
)
try:
tool_args, tool_kwargs = self._to_args_and_kwargs(
parsed_input
)
observation = (
self._run(
*tool_args, run_manager=run_manager, **tool_kwargs
)
if new_arg_supported
else self._run(*tool_args, **tool_kwargs)
)
except ToolException as e:
if not self.handle_tool_error:
run_manager.on_tool_error(e)
raise e
elif isinstance(self.handle_tool_error, bool):
if e.args:
observation = e.args[0]
else:
observation = "Tool execution error"
elif isinstance(self.handle_tool_error, str):
observation = self.handle_tool_error
elif callable(self.handle_tool_error):
observation = self.handle_tool_error(e)
else:
raise ValueError(
"Got unexpected type of `handle_tool_error`."
" Expected bool, str or callable. Received:"
f" {self.handle_tool_error}"
)
run_manager.on_tool_end(
str(observation),
color="red",
name=self.name,
**kwargs,
)
return observation
except (Exception, KeyboardInterrupt) as e:
run_manager.on_tool_error(e)
raise e
else:
run_manager.on_tool_end(
str(observation),
color=color,
name=self.name,
**kwargs,
)
return observation
async def arun(
self,
tool_input: str | dict,
verbose: bool | None = None,
start_color: str | None = "green",
color: str | None = "green",
callbacks: Callbacks = None,
*,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
run_name: str | None = None,
**kwargs: Any,
) -> Any:
"""Run the tool asynchronously."""
parsed_input = self._parse_input(tool_input)
if not self.verbose and verbose is not None:
verbose_ = verbose
else:
verbose_ = self.verbose
callback_manager = AsyncCallbackManager.configure(
callbacks,
self.callbacks,
verbose_,
tags,
self.tags,
metadata,
self.metadata,
)
new_arg_supported = signature(self._arun).parameters.get(
"run_manager"
)
run_manager = await callback_manager.on_tool_start(
{"name": self.name, "description": self.description},
(
tool_input
if isinstance(tool_input, str)
else str(tool_input)
),
color=start_color,
name=run_name,
**kwargs,
)
try:
# We then call the tool on the tool input to get an observation
tool_args, tool_kwargs = self._to_args_and_kwargs(
parsed_input
)
observation = (
await self._arun(
*tool_args, run_manager=run_manager, **tool_kwargs
)
if new_arg_supported
else await self._arun(*tool_args, **tool_kwargs)
)
except ToolException as e:
if not self.handle_tool_error:
await run_manager.on_tool_error(e)
raise e
elif isinstance(self.handle_tool_error, bool):
if e.args:
observation = e.args[0]
else:
observation = "Tool execution error"
elif isinstance(self.handle_tool_error, str):
observation = self.handle_tool_error
elif callable(self.handle_tool_error):
observation = self.handle_tool_error(e)
else:
raise ValueError(
"Got unexpected type of `handle_tool_error`."
" Expected bool, str or callable. Received:"
f" {self.handle_tool_error}"
)
await run_manager.on_tool_end(
str(observation),
color="red",
name=self.name,
**kwargs,
)
return observation
except (Exception, KeyboardInterrupt) as e:
await run_manager.on_tool_error(e)
raise e
else:
await run_manager.on_tool_end(
str(observation),
color=color,
name=self.name,
**kwargs,
)
return observation
def __call__(
self, tool_input: str, callbacks: Callbacks = None
) -> str:
"""Make tool callable."""
return self.run(tool_input, callbacks=callbacks)
class Tool(BaseTool):
"""Tool that takes in function or coroutine directly."""
description: str = ""
func: Callable[..., str] | None
"""The function to run when the tool is called."""
coroutine: Callable[..., Awaitable[str]] | None = None
"""The asynchronous version of the function."""
# --- Runnable ---
async def ainvoke(
self,
input: str | dict,
config: RunnableConfig | None = None,
**kwargs: Any,
) -> Any:
if not self.coroutine:
# If the tool does not implement async, fall back to default implementation
return await asyncio.get_running_loop().run_in_executor(
None, partial(self.invoke, input, config, **kwargs)
)
return await super().ainvoke(input, config, **kwargs)
# --- Tool ---
@property
def args(self) -> dict:
"""The tool's input arguments."""
if self.args_schema is not None:
return self.args_schema.schema()["properties"]
# For backwards compatibility, if the function signature is ambiguous,
# assume it takes a single string input.
return {"tool_input": {"type": "string"}}
def _to_args_and_kwargs(
self, tool_input: str | dict
) -> tuple[tuple, dict]:
"""Convert tool input to pydantic model."""
args, kwargs = super()._to_args_and_kwargs(tool_input)
# For backwards compatibility. The tool must be run with a single input
all_args = list(args) + list(kwargs.values())
if len(all_args) != 1:
raise ToolException(
"Too many arguments to single-input tool"
f" {self.name}. Args: {all_args}"
)
return tuple(all_args), {}
def _run(
self,
*args: Any,
run_manager: CallbackManagerForToolRun | None = None,
**kwargs: Any,
) -> Any:
"""Use the tool."""
if self.func:
new_argument_supported = signature(
self.func
).parameters.get("callbacks")
return (
self.func(
*args,
callbacks=(
run_manager.get_child()
if run_manager
else None
),
**kwargs,
)
if new_argument_supported
else self.func(*args, **kwargs)
)
raise NotImplementedError("Tool does not support sync")
async def _arun(
self,
*args: Any,
run_manager: AsyncCallbackManagerForToolRun | None = None,
**kwargs: Any,
) -> Any:
"""Use the tool asynchronously."""
if self.coroutine:
new_argument_supported = signature(
self.coroutine
).parameters.get("callbacks")
return (
await self.coroutine(
*args,
callbacks=(
run_manager.get_child()
if run_manager
else None
),
**kwargs,
)
if new_argument_supported
else await self.coroutine(*args, **kwargs)
)
else:
return await asyncio.get_running_loop().run_in_executor(
None,
partial(self._run, run_manager=run_manager, **kwargs),
*args,
)
# TODO: this is for backwards compatibility, remove in future
def __init__(
self,
name: str,
func: Callable | None,
description: str,
**kwargs: Any,
) -> None:
"""Initialize tool."""
super().__init__(
name=name, func=func, description=description, **kwargs
)
@classmethod
def from_function(
cls,
func: Callable | None,
name: str, # We keep these required to support backwards compatibility
description: str,
return_direct: bool = False,
args_schema: type[BaseModel] | None = None,
coroutine: (Callable[..., Awaitable[Any]])
| None = None, # This is last for compatibility, but should be after func
**kwargs: Any,
) -> Tool:
"""Initialize tool from a function."""
if func is None and coroutine is None:
raise ValueError(
"Function and/or coroutine must be provided"
)
return cls(
name=name,
func=func,
coroutine=coroutine,
description=description,
return_direct=return_direct,
args_schema=args_schema,
**kwargs,
)
class StructuredTool(BaseTool):
"""Tool that can operate on any number of inputs."""
description: str = ""
args_schema: type[BaseModel] = Field(
..., description="The tool schema."
)
"""The input arguments' schema."""
func: Callable[..., Any] | None
"""The function to run when the tool is called."""
coroutine: Callable[..., Awaitable[Any]] | None = None
"""The asynchronous version of the function."""
# --- Runnable ---
async def ainvoke(
self,
input: str | dict,
config: RunnableConfig | None = None,
**kwargs: Any,
) -> Any:
if not self.coroutine:
# If the tool does not implement async, fall back to default implementation
return await asyncio.get_running_loop().run_in_executor(
None, partial(self.invoke, input, config, **kwargs)
)
return await super().ainvoke(input, config, **kwargs)
# --- Tool ---
@property
def args(self) -> dict:
"""The tool's input arguments."""
return self.args_schema.schema()["properties"]
def _run(
self,
*args: Any,
run_manager: CallbackManagerForToolRun | None = None,
**kwargs: Any,
) -> Any:
"""Use the tool."""
if self.func:
new_argument_supported = signature(
self.func
).parameters.get("callbacks")
return (
self.func(
*args,
callbacks=(
run_manager.get_child()
if run_manager
else None
),
**kwargs,
)
if new_argument_supported
else self.func(*args, **kwargs)
)
raise NotImplementedError("Tool does not support sync")
async def _arun(
self,
*args: Any,
run_manager: AsyncCallbackManagerForToolRun | None = None,
**kwargs: Any,
) -> str:
"""Use the tool asynchronously."""
if self.coroutine:
new_argument_supported = signature(
self.coroutine
).parameters.get("callbacks")
return (
await self.coroutine(
*args,
callbacks=(
run_manager.get_child()
if run_manager
else None
),
**kwargs,
)
if new_argument_supported
else await self.coroutine(*args, **kwargs)
)
return await asyncio.get_running_loop().run_in_executor(
None,
partial(self._run, run_manager=run_manager, **kwargs),
*args,
)
@classmethod
def from_function(
cls,
func: Callable | None = None,
coroutine: Callable[..., Awaitable[Any]] | None = None,
name: str | None = None,
description: str | None = None,
return_direct: bool = False,
args_schema: type[BaseModel] | None = None,
infer_schema: bool = True,
**kwargs: Any,
) -> StructuredTool:
"""Create tool from a given function.
A classmethod that helps to create a tool from a function.
Args:
func: The function from which to create a tool
coroutine: The async function from which to create a tool
name: The name of the tool. Defaults to the function name
description: The description of the tool. Defaults to the function docstring
return_direct: Whether to return the result directly or as a callback
args_schema: The schema of the tool's input arguments
infer_schema: Whether to infer the schema from the function's signature
**kwargs: Additional arguments to pass to the tool
Returns:
The tool
Examples:
.. code-block:: python
def add(a: int, b: int) -> int:
\"\"\"Add two numbers\"\"\"
return a + b
tool = StructuredTool.from_function(add)
tool.run(1, 2) # 3
"""
if func is not None:
source_function = func
elif coroutine is not None:
source_function = coroutine
else:
raise ValueError(
"Function and/or coroutine must be provided"
)
name = name or source_function.__name__
description = description or source_function.__doc__
if description is None:
raise ValueError(
"Function must have a docstring if description not"
" provided."
)
# Description example:
# search_api(query: str) - Searches the API for the query.
sig = signature(source_function)
description = f"{name}{sig} - {description.strip()}"
_args_schema = args_schema
if _args_schema is None and infer_schema:
_args_schema = create_schema_from_function(
f"{name}Schema", source_function
)
return cls(
name=name,
func=func,
coroutine=coroutine,
args_schema=_args_schema,
description=description,
return_direct=return_direct,
**kwargs,
)
def tool(
*args: str | Callable | Runnable,
return_direct: bool = False,
args_schema: type[BaseModel] | None = None,
infer_schema: bool = True,
) -> Callable:
"""Make tools out of functions, can be used with or without arguments.
Args:
*args: The arguments to the tool.
return_direct: Whether to return directly from the tool rather
than continuing the agent loop.
args_schema: optional argument schema for user to specify
infer_schema: Whether to infer the schema of the arguments from
the function's signature. This also makes the resultant tool
accept a dictionary input to its `run()` function.
Requires:
- Function must be of type (str) -> str
- Function must have a docstring
Examples:
.. code-block:: python
@tool
def search_api(query: str) -> str:
# Searches the API for the query.
return
@tool("search", return_direct=True)
def search_api(query: str) -> str:
# Searches the API for the query.
return
"""
def _make_with_name(tool_name: str) -> Callable:
def _make_tool(dec_func: Callable | Runnable) -> BaseTool:
if isinstance(dec_func, Runnable):
runnable = dec_func
if (
runnable.input_schema.schema().get("type")
!= "object"
):
raise ValueError(
"Runnable must have an object schema."
)
async def ainvoke_wrapper(
callbacks: Callbacks | None = None,
**kwargs: Any,
) -> Any:
return await runnable.ainvoke(
kwargs, {"callbacks": callbacks}
)
def invoke_wrapper(
callbacks: Callbacks | None = None,
**kwargs: Any,
) -> Any:
return runnable.invoke(
kwargs, {"callbacks": callbacks}
)
coroutine = ainvoke_wrapper
func = invoke_wrapper
schema: type[BaseModel] | None = runnable.input_schema
description = repr(runnable)
elif inspect.iscoroutinefunction(dec_func):
coroutine = dec_func
func = None
schema = args_schema
description = None
else:
coroutine = None
func = dec_func
schema = args_schema
description = None
if infer_schema or args_schema is not None:
return StructuredTool.from_function(
func,
coroutine,
name=tool_name,
description=description,
return_direct=return_direct,
args_schema=schema,
infer_schema=infer_schema,
)
# If someone doesn't want a schema applied, we must treat it as
# a simple string->string function
if func.__doc__ is None:
raise ValueError(
"Function must have a docstring if description"
" not provided and infer_schema is False."
)
return Tool(
name=tool_name,
func=func,
description=f"{tool_name} tool",
return_direct=return_direct,
coroutine=coroutine,
)
return _make_tool
if (
len(args) == 2
and isinstance(args[0], str)
and isinstance(args[1], Runnable)
):
return _make_with_name(args[0])(args[1])
elif len(args) == 1 and isinstance(args[0], str):
# if the argument is a string, then we use the string as the tool name
# Example usage: @tool("search", return_direct=True)
return _make_with_name(args[0])
elif len(args) == 1 and callable(args[0]):
# if the argument is a function, then we use the function name as the tool name
# Example usage: @tool
return _make_with_name(args[0].__name__)(args[0])
elif len(args) == 0:
# if there are no arguments, then we use the function name as the tool name
# Example usage: @tool(return_direct=True)
def _partial(func: Callable[[str], str]) -> BaseTool:
return _make_with_name(func.__name__)(func)
return _partial
else:
raise ValueError("Too many arguments for tool decorator")

@ -0,0 +1,61 @@
from typing import Any, List, Union
from pydantic import BaseModel
from swarms.tools.tool import BaseTool
from swarms.utils.loguru_logger import logger
class OmniTool(BaseModel):
"""
A class representing an OmniTool.
Attributes:
tools (Union[List[BaseTool], List[BaseModel], List[Any]]): A list of tools.
verbose (bool): A flag indicating whether to enable verbose mode.
Methods:
transform_models_to_tools(): Transforms models to tools.
__call__(*args, **kwargs): Calls the tools.
"""
tools: Union[List[BaseTool], List[BaseModel], List[Any]]
verbose: bool = False
def transform_models_to_tools(self):
"""
Transforms models to tools.
"""
for i, tool in enumerate(self.tools):
if isinstance(tool, BaseModel):
tool_json = tool.model_dump_json()
# Assuming BaseTool has a method to load from json
self.tools[i] = BaseTool.load_from_json(tool_json)
def __call__(self, *args, **kwargs):
"""
Calls the tools.
Args:
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
Tuple: A tuple containing the arguments and keyword arguments.
"""
try:
self.transform_models_to_tools()
logger.info(f"Number of tools: {len(self.tools)}")
try:
for tool in self.tools:
logger.info(f"Running tool: {tool}")
tool(*args, **kwargs)
except Exception as e:
logger.error(f"Error occurred while running tools: {e}")
return args, kwargs
except Exception as error:
logger.error(f"Error occurred while running tools: {error}")
return args, kwargs

@ -1,7 +1,7 @@
from abc import ABC
from typing import Any, Dict, List, Literal, TypedDict, Union, cast
from pydantic import BaseModel, PrivateAttr
from pydantic import ConfigDict, BaseModel, PrivateAttr
class BaseSerialized(TypedDict):
@ -65,8 +65,7 @@ class Serializable(BaseModel, ABC):
"""
return {}
class Config:
extra = "ignore"
model_config = ConfigDict(extra="ignore")
_lc_kwargs = PrivateAttr(default_factory=dict)

Loading…
Cancel
Save