pull/421/head
Kye 10 months ago
parent 581d6558d6
commit fe4966c849

@ -29,17 +29,17 @@ class vLLMLM(AbstractLLM):
model_name: str = "acebook/opt-13b",
tensor_parallel_size: int = 4,
*args,
**kwargs
**kwargs,
):
super().__init__(*args, **kwargs)
self.model_name = model_name
self.tensor_parallel_size = tensor_parallel_size
self.llm = LLM(
model_name=self.model_name,
tensor_parallel_size=self.tensor_parallel_size,
)
def run(self, task: str, *args, **kwargs):
"""
Runs the LLM model to generate output for the given task.
@ -54,8 +54,8 @@ class vLLMLM(AbstractLLM):
"""
return self.llm.generate(task)
# Initializing the agent with the vLLMLM instance and other parameters
model = vLLMLM(
"facebook/opt-13b",
@ -86,4 +86,4 @@ agent = Agent(
docs_folder="docs",
),
stopping_condition="finish",
)
)

@ -56,10 +56,7 @@ chromadb = "*"
termcolor = "2.2.0"
torchvision = "0.16.1"
rich = "13.5.2"
sqlalchemy = "*"
bitsandbytes = "*"
pgvector = "*"
cohere = "*"
sentence-transformers = "*"
peft = "*"
psutil = "*"

@ -1,4 +1,5 @@
from typing import Dict, List
from abc import abstractmethod
from typing import Dict, List, Union, Optional
class AbstractAgent:
@ -36,7 +37,8 @@ class AbstractAgent:
def reset(self):
"""(Abstract method) Reset the agent."""
def run(self, task: str):
@abstractmethod
def run(self, task: str, *args, **kwargs):
"""Run the agent once"""
def _arun(self, taks: str):
@ -53,3 +55,65 @@ class AbstractAgent:
def _astep(self, message: str):
"""Asynchronous step"""
def send(
self,
message: Union[Dict, str],
recipient, # add AbstractWorker
request_reply: Optional[bool] = None,
):
"""(Abstract method) Send a message to another worker."""
async def a_send(
self,
message: Union[Dict, str],
recipient, # add AbstractWorker
request_reply: Optional[bool] = None,
):
"""(Aabstract async method) Send a message to another worker."""
def receive(
self,
message: Union[Dict, str],
sender, # add AbstractWorker
request_reply: Optional[bool] = None,
):
"""(Abstract method) Receive a message from another worker."""
async def a_receive(
self,
message: Union[Dict, str],
sender, # add AbstractWorker
request_reply: Optional[bool] = None,
):
"""(Abstract async method) Receive a message from another worker."""
def generate_reply(
self,
messages: Optional[List[Dict]] = None,
sender=None, # Optional["AbstractWorker"] = None,
**kwargs,
) -> Union[str, Dict, None]:
"""(Abstract method) Generate a reply based on the received messages.
Args:
messages (list[dict]): a list of messages received.
sender: sender of an Agent instance.
Returns:
str or dict or None: the generated reply. If None, no reply is generated.
"""
async def a_generate_reply(
self,
messages: Optional[List[Dict]] = None,
sender=None, # Optional["AbstractWorker"] = None,
**kwargs,
) -> Union[str, Dict, None]:
"""(Abstract async method) Generate a reply based on the received messages.
Args:
messages (list[dict]): a list of messages received.
sender: sender of an Agent instance.
Returns:
str or dict or None: the generated reply. If None, no reply is generated.
"""

@ -1,70 +0,0 @@
import os
import multion
from dotenv import load_dotenv
from swarms.models.base_llm import AbstractLLM
# Load environment variables
load_dotenv()
# Muliton key
MULTION_API_KEY = os.getenv("MULTION_API_KEY")
class MultiOnAgent(AbstractLLM):
"""
Represents a multi-on agent that performs browsing tasks.
Args:
max_steps (int): The maximum number of steps to perform during browsing.
starting_url (str): The starting URL for browsing.
Attributes:
max_steps (int): The maximum number of steps to perform during browsing.
starting_url (str): The starting URL for browsing.
"""
def __init__(
self,
multion_api_key: str = MULTION_API_KEY,
max_steps: int = 4,
starting_url: str = "https://www.google.com",
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.multion_api_key = multion_api_key
self.max_steps = max_steps
self.starting_url = starting_url
def run(self, task: str, *args, **kwargs):
"""
Runs a browsing task.
Args:
task (str): The task to perform during browsing.
*args: Additional positional arguments.
**kwargs: Additional keyword arguments.
Returns:
dict: The response from the browsing task.
"""
multion.login(
use_api=True,
multion_api_key=str(self.multion_api_key),
*args,
**kwargs,
)
response = multion.browse(
{
"cmd": task,
"url": self.starting_url,
"maxSteps": self.max_steps,
},
*args,
**kwargs,
)
return response.result, response.status, response.lastUrl

@ -4,22 +4,18 @@ from swarms.memory.base_vectordb import AbstractVectorDatabase
from swarms.memory.chroma_db import ChromaDB
from swarms.memory.dict_internal_memory import DictInternalMemory
from swarms.memory.dict_shared_memory import DictSharedMemory
from swarms.memory.lanchain_chroma import LangchainChromaVectorMemory
from swarms.memory.short_term_memory import ShortTermMemory
from swarms.memory.sqlite import SQLiteDB
from swarms.memory.visual_memory import VisualShortTermMemory
from swarms.memory.weaviate_db import WeaviateDB
__all__ = [
"AbstractVectorDatabase",
"AbstractDatabase",
"ShortTermMemory",
"SQLiteDB",
"WeaviateDB",
"VisualShortTermMemory",
"ActionSubtaskEntry",
"ChromaDB",
"DictInternalMemory",
"DictSharedMemory",
"LangchainChromaVectorMemory",
]

@ -2,17 +2,8 @@ from swarms.models.anthropic import Anthropic # noqa: E402
from swarms.models.base_embedding_model import BaseEmbeddingModel
from swarms.models.base_llm import AbstractLLM # noqa: E402
from swarms.models.base_multimodal_model import BaseMultiModalModel
# noqa: E402
from swarms.models.biogpt import BioGPT # noqa: E402
from swarms.models.clipq import CLIPQ # noqa: E402
# from swarms.models.dalle3 import Dalle3
# from swarms.models.distilled_whisperx import DistilWhisperModel # noqa: E402
# from swarms.models.whisperx_model import WhisperX # noqa: E402
# from swarms.models.kosmos_two import Kosmos # noqa: E402
# from swarms.models.cog_agent import CogAgent # noqa: E402
## Function calling models
from swarms.models.fire_function import FireFunctionCaller
from swarms.models.fuyu import Fuyu # noqa: E402
from swarms.models.gemini import Gemini # noqa: E402
@ -22,20 +13,11 @@ from swarms.models.huggingface import HuggingfaceLLM # noqa: E402
from swarms.models.idefics import Idefics # noqa: E402
from swarms.models.kosmos_two import Kosmos # noqa: E402
from swarms.models.layoutlm_document_qa import LayoutLMDocumentQA
# noqa: E402
from swarms.models.llava import LavaMultiModal # noqa: E402
from swarms.models.mistral import Mistral # noqa: E402
from swarms.models.mixtral import Mixtral # noqa: E402
from swarms.models.mpt import MPT7B # noqa: E402
from swarms.models.nougat import Nougat # noqa: E402
from swarms.models.openai_models import (
AzureOpenAI,
OpenAI,
OpenAIChat,
)
# noqa: E402
from swarms.models.openai_tts import OpenAITTS # noqa: E402
from swarms.models.petals import Petals # noqa: E402
from swarms.models.qwen import QwenVLMultiModal # noqa: E402
@ -43,14 +25,7 @@ from swarms.models.roboflow_model import RoboflowMultiModal
from swarms.models.sam_supervision import SegmentAnythingMarkGenerator
from swarms.models.sampling_params import SamplingParams, SamplingType
from swarms.models.timm import TimmModel # noqa: E402
# from swarms.models.modelscope_pipeline import ModelScopePipeline
# from swarms.models.modelscope_llm import (
# ModelScopeAutoModel,
# ) # noqa: E402
from swarms.models.together import TogetherLLM # noqa: E402
############## Types
from swarms.models.types import ( # noqa: E402
AudioModality,
ImageModality,
@ -59,60 +34,49 @@ from swarms.models.types import ( # noqa: E402
VideoModality,
)
from swarms.models.ultralytics_model import UltralyticsModel
# noqa: E402
from swarms.models.vilt import Vilt # noqa: E402
from swarms.models.wizard_storytelling import WizardLLMStoryTeller
# noqa: E402
# from swarms.models.vllm import vLLM # noqa: E402
from swarms.models.zephyr import Zephyr # noqa: E402
from swarms.models.zeroscope import ZeroscopeTTV # noqa: E402
__all__ = [
"AbstractLLM",
"Anthropic",
"Petals",
"Mistral",
"OpenAI",
"AzureOpenAI",
"OpenAIChat",
"Zephyr",
"AbstractLLM",
"BaseEmbeddingModel",
"BaseMultiModalModel",
"Idefics",
"Vilt",
"Nougat",
"LayoutLMDocumentQA",
"BioGPT",
"CLIPQ",
"FireFunctionCaller",
"Fuyu",
"Gigabind",
"GPT4VisionAPI",
"HuggingfaceLLM",
"Idefics",
"Kosmos",
"LavaMultiModal",
"LayoutLMDocumentQA",
"Mistral",
"Mixtral",
"MPT7B",
"WizardLLMStoryTeller",
# "Dalle3",
# "DistilWhisperModel",
"GPT4VisionAPI",
# "vLLM",
"OpenAITTS",
"Nougat",
"Gemini",
"Gigabind",
"Mixtral",
"ZeroscopeTTV",
"TextModality",
"ImageModality",
"AudioModality",
"VideoModality",
"MultimodalData",
"TogetherLLM",
"TimmModel",
"UltralyticsModel",
"LavaMultiModal",
"OpenAITTS",
"Petals",
"QwenVLMultiModal",
"CLIPQ",
"Kosmos",
"Fuyu",
"BaseEmbeddingModel",
"RoboflowMultiModal",
"SegmentAnythingMarkGenerator",
"SamplingType",
"SamplingParams",
"FireFunctionCaller",
"SamplingType",
"TimmModel",
"TogetherLLM",
"UltralyticsModel",
"Vilt",
"WizardLLMStoryTeller",
"Zephyr",
"ZeroscopeTTV",
"AudioModality",
"ImageModality",
"MultimodalData",
"TextModality",
"VideoModality",
]

@ -1,575 +0,0 @@
import contextlib
import datetime
import functools
import importlib
import re
import warnings
from importlib.metadata import version
from typing import (
Any,
AsyncIterator,
Callable,
Dict,
Iterator,
List,
Mapping,
Optional,
Set,
Tuple,
Union,
)
from langchain.callbacks.manager import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
)
from langchain.llms.base import LLM
from langchain.schema.language_model import BaseLanguageModel
from langchain.schema.output import GenerationChunk
from langchain.schema.prompt import PromptValue
from langchain.utils import get_from_dict_or_env
from packaging.version import parse
from pydantic import Field, SecretStr, root_validator
from requests import HTTPError, Response
def xor_args(*arg_groups: Tuple[str, ...]) -> Callable:
"""Validate specified keyword args are mutually exclusive."""
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
"""Validate exactly one arg in each group is not None."""
counts = [
sum(
1
for arg in arg_group
if kwargs.get(arg) is not None
)
for arg_group in arg_groups
]
invalid_groups = [
i for i, count in enumerate(counts) if count != 1
]
if invalid_groups:
invalid_group_names = [
", ".join(arg_groups[i]) for i in invalid_groups
]
raise ValueError(
"Exactly one argument in each of the following"
" groups must be defined:"
f" {', '.join(invalid_group_names)}"
)
return func(*args, **kwargs)
return wrapper
return decorator
def raise_for_status_with_text(response: Response) -> None:
"""Raise an error with the response text."""
try:
response.raise_for_status()
except HTTPError as e:
raise ValueError(response.text) from e
@contextlib.contextmanager
def mock_now(dt_value): # type: ignore
"""Context manager for mocking out datetime.now() in unit tests.
Example:
with mock_now(datetime.datetime(2011, 2, 3, 10, 11)):
assert datetime.datetime.now() == datetime.datetime(2011, 2, 3, 10, 11)
"""
class MockDateTime(datetime.datetime):
"""Mock datetime.datetime.now() with a fixed datetime."""
@classmethod
def now(cls): # type: ignore
# Create a copy of dt_value.
return datetime.datetime(
dt_value.year,
dt_value.month,
dt_value.day,
dt_value.hour,
dt_value.minute,
dt_value.second,
dt_value.microsecond,
dt_value.tzinfo,
)
real_datetime = datetime.datetime
datetime.datetime = MockDateTime
try:
yield datetime.datetime
finally:
datetime.datetime = real_datetime
def guard_import(
module_name: str,
*,
pip_name: Optional[str] = None,
package: Optional[str] = None,
) -> Any:
"""Dynamically imports a module and raises a helpful exception if the module is not
installed."""
try:
module = importlib.import_module(module_name, package)
except ImportError:
raise ImportError(
f"Could not import {module_name} python package. Please"
" install it with `pip install"
f" {pip_name or module_name}`."
)
return module
def check_package_version(
package: str,
lt_version: Optional[str] = None,
lte_version: Optional[str] = None,
gt_version: Optional[str] = None,
gte_version: Optional[str] = None,
) -> None:
"""Check the version of a package."""
imported_version = parse(version(package))
if lt_version is not None and imported_version >= parse(
lt_version
):
raise ValueError(
f"Expected {package} version to be < {lt_version}."
f" Received {imported_version}."
)
if lte_version is not None and imported_version > parse(
lte_version
):
raise ValueError(
f"Expected {package} version to be <= {lte_version}."
f" Received {imported_version}."
)
if gt_version is not None and imported_version <= parse(
gt_version
):
raise ValueError(
f"Expected {package} version to be > {gt_version}."
f" Received {imported_version}."
)
if gte_version is not None and imported_version < parse(
gte_version
):
raise ValueError(
f"Expected {package} version to be >= {gte_version}."
f" Received {imported_version}."
)
def get_pydantic_field_names(pydantic_cls: Any) -> Set[str]:
"""Get field names, including aliases, for a pydantic class.
Args:
pydantic_cls: Pydantic class."""
all_required_field_names = set()
for field in pydantic_cls.__fields__.values():
all_required_field_names.add(field.name)
if field.has_alias:
all_required_field_names.add(field.alias)
return all_required_field_names
def build_extra_kwargs(
extra_kwargs: Dict[str, Any],
values: Dict[str, Any],
all_required_field_names: Set[str],
) -> Dict[str, Any]:
"""Build extra kwargs from values and extra_kwargs.
Args:
extra_kwargs: Extra kwargs passed in by user.
values: Values passed in by user.
all_required_field_names: All required field names for the pydantic class.
"""
for field_name in list(values):
if field_name in extra_kwargs:
raise ValueError(f"Found {field_name} supplied twice.")
if field_name not in all_required_field_names:
warnings.warn(
f"""WARNING! {field_name} is not default parameter.
{field_name} was transferred to model_kwargs.
Please confirm that {field_name} is what you intended."""
)
extra_kwargs[field_name] = values.pop(field_name)
invalid_model_kwargs = all_required_field_names.intersection(
extra_kwargs.keys()
)
if invalid_model_kwargs:
raise ValueError(
f"Parameters {invalid_model_kwargs} should be specified"
" explicitly. Instead they were passed in as part of"
" `model_kwargs` parameter."
)
return extra_kwargs
def convert_to_secret_str(value: Union[SecretStr, str]) -> SecretStr:
"""Convert a string to a SecretStr if needed."""
if isinstance(value, SecretStr):
return value
return SecretStr(value)
class _AnthropicCommon(BaseLanguageModel):
client: Any = None #: :meta private:
async_client: Any = None #: :meta private:
model: str = Field(default="claude-2", alias="model_name")
"""Model name to use."""
max_tokens_to_sample: int = Field(default=256, alias="max_tokens")
"""Denotes the number of tokens to predict per generation."""
temperature: Optional[float] = None
"""A non-negative float that tunes the degree of randomness in generation."""
top_k: Optional[int] = None
"""Number of most likely tokens to consider at each step."""
top_p: Optional[float] = None
"""Total probability mass of tokens to consider at each step."""
streaming: bool = False
"""Whether to stream the results."""
default_request_timeout: Optional[float] = None
"""Timeout for requests to Anthropic Completion API. Default is 600 seconds."""
anthropic_api_url: Optional[str] = None
anthropic_api_key: Optional[SecretStr] = None
HUMAN_PROMPT: Optional[str] = None
AI_PROMPT: Optional[str] = None
count_tokens: Optional[Callable[[str], int]] = None
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
@root_validator(pre=True)
def build_extra(cls, values: Dict) -> Dict:
extra = values.get("model_kwargs", {})
all_required_field_names = get_pydantic_field_names(cls)
values["model_kwargs"] = build_extra_kwargs(
extra, values, all_required_field_names
)
return values
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
values["anthropic_api_key"] = convert_to_secret_str(
get_from_dict_or_env(
values, "anthropic_api_key", "ANTHROPIC_API_KEY"
)
)
# Get custom api url from environment.
values["anthropic_api_url"] = get_from_dict_or_env(
values,
"anthropic_api_url",
"ANTHROPIC_API_URL",
default="https://api.anthropic.com",
)
try:
import anthropic
check_package_version("anthropic", gte_version="0.3")
values["client"] = anthropic.Anthropic(
base_url=values["anthropic_api_url"],
api_key=values[
"anthropic_api_key"
].get_secret_value(),
timeout=values["default_request_timeout"],
)
values["async_client"] = anthropic.AsyncAnthropic(
base_url=values["anthropic_api_url"],
api_key=values[
"anthropic_api_key"
].get_secret_value(),
timeout=values["default_request_timeout"],
)
values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT
values["AI_PROMPT"] = anthropic.AI_PROMPT
values["count_tokens"] = values["client"].count_tokens
except ImportError:
raise ImportError(
"Could not import anthropic python package. "
"Please it install it with `pip install anthropic`."
)
return values
@property
def _default_params(self) -> Mapping[str, Any]:
"""Get the default parameters for calling Anthropic API."""
d = {
"max_tokens_to_sample": self.max_tokens_to_sample,
"model": self.model,
}
if self.temperature is not None:
d["temperature"] = self.temperature
if self.top_k is not None:
d["top_k"] = self.top_k
if self.top_p is not None:
d["top_p"] = self.top_p
return {**d, **self.model_kwargs}
@property
def _identifying_params(self) -> Mapping[str, Any]:
"""Get the identifying parameters."""
return {**{}, **self._default_params}
def _get_anthropic_stop(
self, stop: Optional[List[str]] = None
) -> List[str]:
if not self.HUMAN_PROMPT or not self.AI_PROMPT:
raise NameError(
"Please ensure the anthropic package is loaded"
)
if stop is None:
stop = []
# Never want model to invent new turns of Human / Assistant dialog.
stop.extend([self.HUMAN_PROMPT])
return stop
class Anthropic(LLM, _AnthropicCommon):
"""Anthropic large language models.
To use, you should have the ``anthropic`` python package installed, and the
environment variable ``ANTHROPIC_API_KEY`` set with your API key, or pass
it as a named parameter to the constructor.
Example:
.. code-block:: python
import anthropic
from langchain.llms import Anthropic
model = Anthropic(model="<model_name>", anthropic_api_key="my-api-key")
# Simplest invocation, automatically wrapped with HUMAN_PROMPT
# and AI_PROMPT.
response = model("What are the biggest risks facing humanity?")
# Or if you want to use the chat mode, build a few-shot-prompt, or
# put words in the Assistant's mouth, use HUMAN_PROMPT and AI_PROMPT:
raw_prompt = "What are the biggest risks facing humanity?"
prompt = f"{anthropic.HUMAN_PROMPT} {prompt}{anthropic.AI_PROMPT}"
response = model(prompt)
"""
class Config:
"""Configuration for this pydantic object."""
allow_population_by_field_name = True
arbitrary_types_allowed = True
@root_validator()
def raise_warning(cls, values: Dict) -> Dict:
"""Raise warning that this class is deprecated."""
warnings.warn(
"There may be an updated version of"
f" {cls.__name__} available."
)
return values
@property
def _llm_type(self) -> str:
"""Return type of llm."""
return "anthropic-llm"
def _wrap_prompt(self, prompt: str) -> str:
if not self.HUMAN_PROMPT or not self.AI_PROMPT:
raise NameError(
"Please ensure the anthropic package is loaded"
)
if prompt.startswith(self.HUMAN_PROMPT):
return prompt # Already wrapped.
# Guard against common errors in specifying wrong number of newlines.
corrected_prompt, n_subs = re.subn(
r"^\n*Human:", self.HUMAN_PROMPT, prompt
)
if n_subs == 1:
return corrected_prompt
# As a last resort, wrap the prompt ourselves to emulate instruct-style.
return (
f"{self.HUMAN_PROMPT} {prompt}{self.AI_PROMPT} Sure, here"
" you go:\n"
)
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
r"""Call out to Anthropic's completion endpoint.
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
Returns:
The string generated by the model.
Example:
.. code-block:: python
prompt = "What are the biggest risks facing humanity?"
prompt = f"\n\nHuman: {prompt}\n\nAssistant:"
response = model(prompt)
"""
if self.streaming:
completion = ""
for chunk in self._stream(
prompt=prompt,
stop=stop,
run_manager=run_manager,
**kwargs,
):
completion += chunk.text
return completion
stop = self._get_anthropic_stop(stop)
params = {**self._default_params, **kwargs}
response = self.client.completions.create(
prompt=self._wrap_prompt(prompt),
stop_sequences=stop,
**params,
)
return response.completion
def convert_prompt(self, prompt: PromptValue) -> str:
return self._wrap_prompt(prompt.to_string())
async def _acall(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Call out to Anthropic's completion endpoint asynchronously."""
if self.streaming:
completion = ""
async for chunk in self._astream(
prompt=prompt,
stop=stop,
run_manager=run_manager,
**kwargs,
):
completion += chunk.text
return completion
stop = self._get_anthropic_stop(stop)
params = {**self._default_params, **kwargs}
response = await self.async_client.completions.create(
prompt=self._wrap_prompt(prompt),
stop_sequences=stop,
**params,
)
return response.completion
def _stream(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> Iterator[GenerationChunk]:
r"""Call Anthropic completion_stream and return the resulting generator.
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
Returns:
A generator representing the stream of tokens from Anthropic.
Example:
.. code-block:: python
prompt = "Write a poem about a stream."
prompt = f"\n\nHuman: {prompt}\n\nAssistant:"
generator = anthropic.stream(prompt)
for token in generator:
yield token
"""
stop = self._get_anthropic_stop(stop)
params = {**self._default_params, **kwargs}
for token in self.client.completions.create(
prompt=self._wrap_prompt(prompt),
stop_sequences=stop,
stream=True,
**params,
):
chunk = GenerationChunk(text=token.completion)
yield chunk
if run_manager:
run_manager.on_llm_new_token(chunk.text, chunk=chunk)
async def _astream(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> AsyncIterator[GenerationChunk]:
r"""Call Anthropic completion_stream and return the resulting generator.
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
Returns:
A generator representing the stream of tokens from Anthropic.
Example:
.. code-block:: python
prompt = "Write a poem about a stream."
prompt = f"\n\nHuman: {prompt}\n\nAssistant:"
generator = anthropic.stream(prompt)
for token in generator:
yield token
"""
stop = self._get_anthropic_stop(stop)
params = {**self._default_params, **kwargs}
async for token in await self.async_client.completions.create(
prompt=self._wrap_prompt(prompt),
stop_sequences=stop,
stream=True,
**params,
):
chunk = GenerationChunk(text=token.completion)
yield chunk
if run_manager:
await run_manager.on_llm_new_token(
chunk.text, chunk=chunk
)
def get_num_tokens(self, text: str) -> int:
"""Calculate number of tokens."""
if not self.count_tokens:
raise NameError(
"Please ensure the anthropic package is loaded"
)
return self.count_tokens(text)

@ -1,223 +0,0 @@
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,
}

@ -1,258 +0,0 @@
import logging
from typing import Any, Callable, Dict, List, Optional
from langchain.callbacks.manager import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
)
from langchain.llms.base import LLM
from langchain.llms.utils import enforce_stop_tokens
from langchain.load.serializable import Serializable
from langchain.utils import get_from_dict_or_env
from pydantic import Extra, Field, root_validator
from tenacity import (
before_sleep_log,
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
logger = logging.getLogger(__name__)
def _create_retry_decorator(llm) -> Callable[[Any], Any]:
import cohere
min_seconds = 4
max_seconds = 10
# Wait 2^x * 1 second between each retry starting with
# 4 seconds, then up to 10 seconds, then 10 seconds afterwards
return retry(
reraise=True,
stop=stop_after_attempt(llm.max_retries),
wait=wait_exponential(
multiplier=1, min=min_seconds, max=max_seconds
),
retry=retry_if_exception_type(cohere.error.CohereError),
before_sleep=before_sleep_log(logger, logging.WARNING),
)
def completion_with_retry(llm, **kwargs: Any) -> Any:
"""Use tenacity to retry the completion call."""
retry_decorator = _create_retry_decorator(llm)
@retry_decorator
def _completion_with_retry(**kwargs: Any) -> Any:
return llm.client.generate(**kwargs)
return _completion_with_retry(**kwargs)
def acompletion_with_retry(llm, **kwargs: Any) -> Any:
"""Use tenacity to retry the completion call."""
retry_decorator = _create_retry_decorator(llm)
@retry_decorator
async def _completion_with_retry(**kwargs: Any) -> Any:
return await llm.async_client.generate(**kwargs)
return _completion_with_retry(**kwargs)
class BaseCohere(Serializable):
"""Base class for Cohere models."""
client: Any #: :meta private:
async_client: Any #: :meta private:
model: Optional[str] = Field(
default=None, description="Model name to use."
)
"""Model name to use."""
temperature: float = 0.75
"""A non-negative float that tunes the degree of randomness in generation."""
cohere_api_key: Optional[str] = None
stop: Optional[List[str]] = None
streaming: bool = Field(default=False)
"""Whether to stream the results."""
user_agent: str = "langchain"
"""Identifier for the application making the request."""
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
try:
import cohere
except ImportError:
raise ImportError(
"Could not import cohere python package. "
"Please install it with `pip install cohere`."
)
else:
cohere_api_key = get_from_dict_or_env(
values, "cohere_api_key", "COHERE_API_KEY"
)
client_name = values["user_agent"]
values["client"] = cohere.Client(
cohere_api_key, client_name=client_name
)
values["async_client"] = cohere.AsyncClient(
cohere_api_key, client_name=client_name
)
return values
class Cohere(LLM, BaseCohere):
"""Cohere large language models.
To use, you should have the ``cohere`` python package installed, and the
environment variable ``COHERE_API_KEY`` set with your API key, or pass
it as a named parameter to the constructor.
Example:
.. code-block:: python
from langchain.llms import Cohere
cohere = Cohere(model="gptd-instruct-tft", cohere_api_key="my-api-key")
"""
max_tokens: int = 256
"""Denotes the number of tokens to predict per generation."""
k: int = 0
"""Number of most likely tokens to consider at each step."""
p: int = 1
"""Total probability mass of tokens to consider at each step."""
frequency_penalty: float = 0.0
"""Penalizes repeated tokens according to frequency. Between 0 and 1."""
presence_penalty: float = 0.0
"""Penalizes repeated tokens. Between 0 and 1."""
truncate: Optional[str] = None
"""Specify how the client handles inputs longer than the maximum token
length: Truncate from START, END or NONE"""
max_retries: int = 10
"""Maximum number of retries to make when generating."""
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
@property
def _default_params(self) -> Dict[str, Any]:
"""Get the default parameters for calling Cohere API."""
return {
"max_tokens": self.max_tokens,
"temperature": self.temperature,
"k": self.k,
"p": self.p,
"frequency_penalty": self.frequency_penalty,
"presence_penalty": self.presence_penalty,
"truncate": self.truncate,
}
@property
def lc_secrets(self) -> Dict[str, str]:
return {"cohere_api_key": "COHERE_API_KEY"}
@property
def _identifying_params(self) -> Dict[str, Any]:
"""Get the identifying parameters."""
return {**{"model": self.model}, **self._default_params}
@property
def _llm_type(self) -> str:
"""Return type of llm."""
return "cohere"
def _invocation_params(
self, stop: Optional[List[str]], **kwargs: Any
) -> dict:
params = self._default_params
if self.stop is not None and stop is not None:
raise ValueError(
"`stop` found in both the input and default params."
)
elif self.stop is not None:
params["stop_sequences"] = self.stop
else:
params["stop_sequences"] = stop
return {**params, **kwargs}
def _process_response(
self, response: Any, stop: Optional[List[str]]
) -> str:
text = response.generations[0].text
# If stop tokens are provided, Cohere's endpoint returns them.
# In order to make this consistent with other endpoints, we strip them.
if stop:
text = enforce_stop_tokens(text, stop)
return text
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Call out to Cohere's generate endpoint.
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
Returns:
The string generated by the model.
Example:
.. code-block:: python
response = cohere("Tell me a joke.")
"""
params = self._invocation_params(stop, **kwargs)
response = completion_with_retry(
self, model=self.model, prompt=prompt, **params
)
_stop = params.get("stop_sequences")
return self._process_response(response, _stop)
async def _acall(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> str:
"""Async call out to Cohere's generate endpoint.
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
Returns:
The string generated by the model.
Example:
.. code-block:: python
response = await cohere("Tell me a joke.")
"""
params = self._invocation_params(stop, **kwargs)
response = await acompletion_with_retry(
self, model=self.model, prompt=prompt, **params
)
_stop = params.get("stop_sequences")
return self._process_response(response, _stop)

@ -1 +0,0 @@
# Base implementation for the diffusers library

@ -1,114 +0,0 @@
import tempfile
from enum import Enum
from typing import Any, Dict, Union
from langchain.utils import get_from_dict_or_env
from pydantic import model_validator
from swarms.tools.tool import BaseTool
def _import_elevenlabs() -> Any:
try:
import elevenlabs
except ImportError as e:
raise ImportError(
"Cannot import elevenlabs, please install `pip install"
" elevenlabs`."
) from e
return elevenlabs
class ElevenLabsModel(str, Enum):
"""Models available for Eleven Labs Text2Speech."""
MULTI_LINGUAL = "eleven_multilingual_v1"
MONO_LINGUAL = "eleven_monolingual_v1"
class ElevenLabsText2SpeechTool(BaseTool):
"""Tool that queries the Eleven Labs Text2Speech API.
In order to set this up, follow instructions at:
https://docs.elevenlabs.io/welcome/introduction
Attributes:
model (ElevenLabsModel): The model to use for text to speech.
Defaults to ElevenLabsModel.MULTI_LINGUAL.
name (str): The name of the tool. Defaults to "eleven_labs_text2speech".
description (str): The description of the tool.
Defaults to "A wrapper around Eleven Labs Text2Speech. Useful for when you need to convert text to speech. It supports multiple languages, including English, German, Polish, Spanish, Italian, French, Portuguese, and Hindi."
Usage:
>>> from swarms.models import ElevenLabsText2SpeechTool
>>> stt = ElevenLabsText2SpeechTool()
>>> speech_file = stt.run("Hello world!")
>>> stt.play(speech_file)
>>> stt.stream_speech("Hello world!")
"""
model: Union[ElevenLabsModel, str] = ElevenLabsModel.MULTI_LINGUAL
name: str = "eleven_labs_text2speech"
description: str = (
"A wrapper around Eleven Labs Text2Speech. Useful for when"
" you need to convert text to speech. It supports multiple"
" languages, including English, German, Polish, Spanish,"
" Italian, French, Portuguese, and Hindi. "
)
@model_validator(mode="before")
@classmethod
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key exists in environment."""
_ = get_from_dict_or_env(
values, "eleven_api_key", "ELEVEN_API_KEY"
)
return values
def _run(
self,
task: str,
) -> str:
"""Use the tool."""
elevenlabs = _import_elevenlabs()
try:
speech = elevenlabs.generate(text=task, model=self.model)
with tempfile.NamedTemporaryFile(
mode="bx", suffix=".wav", delete=False
) as f:
f.write(speech)
return f.name
except Exception as e:
raise RuntimeError(
f"Error while running ElevenLabsText2SpeechTool: {e}"
)
def play(self, speech_file: str) -> None:
"""Play the text as speech."""
elevenlabs = _import_elevenlabs()
with open(speech_file, mode="rb") as f:
speech = f.read()
elevenlabs.play(speech)
def stream_speech(self, query: str) -> None:
"""Stream the text as speech as it is generated.
Play the text in your speakers."""
elevenlabs = _import_elevenlabs()
speech_stream = elevenlabs.generate(
text=query, model=self.model, stream=True
)
elevenlabs.stream(speech_stream)
def save(self, speech_file: str, path: str) -> None:
"""Save the speech file to a path."""
raise NotImplementedError(
"Saving not implemented for this tool."
)
def __str__(self):
return "ElevenLabsText2SpeechTool"

@ -1,82 +0,0 @@
import inspect
import pkgutil
class ModelRegistry:
"""
A registry for storing and querying models.
Attributes:
models (dict): A dictionary of model names and corresponding model classes.
Methods:
__init__(): Initializes the ModelRegistry object and retrieves all available models.
_get_all_models(): Retrieves all available models from the models package.
query(text): Queries the models based on the given text and returns a dictionary of matching models.
"""
def __init__(self):
self.models = self._get_all_models()
def _get_all_models(self):
"""
Retrieves all available models from the models package.
Returns:
dict: A dictionary of model names and corresponding model classes.
"""
models = {}
for importer, modname, ispkg in pkgutil.iter_modules(
models.__path__
):
module = importer.find_module(modname).load_module(
modname
)
for name, obj in inspect.getmembers(module):
if inspect.isclass(obj):
models[name] = obj
return models
def query(self, text):
"""
Queries the models based on the given text and returns a dictionary of matching models.
Args:
text (str): The text to search for in the model names.
Returns:
dict: A dictionary of matching model names and corresponding model classes.
"""
return {
name: model
for name, model in self.models.items()
if text in name
}
def run_model(
self, model_name: str, task: str, img: str, *args, **kwargs
):
"""
Runs the specified model for the given task and image.
Args:
model_name (str): The name of the model to run.
task (str): The task to perform using the model.
img (str): The image to process.
*args: Additional positional arguments to pass to the model's run method.
**kwargs: Additional keyword arguments to pass to the model's run method.
Returns:
The result of running the model.
Raises:
ValueError: If the specified model is not found in the model registry.
"""
if model_name not in self.models:
raise ValueError(f"Model {model_name} not found")
# Get the model
model = self.models[model_name]
# Run the model
return model.run(task, img, *args, **kwargs)

@ -1,83 +0,0 @@
from typing import Optional
from modelscope import AutoModelForCausalLM, AutoTokenizer
from swarms.models.base_llm import AbstractLLM
class ModelScopeAutoModel(AbstractLLM):
"""
ModelScopeAutoModel is a class that represents a model for generating text using the ModelScope framework.
Args:
model_name (str): The name or path of the pre-trained model.
tokenizer_name (str, optional): The name or path of the tokenizer to use. Defaults to None.
device (str, optional): The device to use for model inference. Defaults to "cuda".
device_map (str, optional): The device mapping for multi-GPU setups. Defaults to "auto".
max_new_tokens (int, optional): The maximum number of new tokens to generate. Defaults to 500.
skip_special_tokens (bool, optional): Whether to skip special tokens during decoding. Defaults to True.
*args: Additional positional arguments.
**kwargs: Additional keyword arguments.
Attributes:
tokenizer (AutoTokenizer): The tokenizer used for tokenizing input text.
model (AutoModelForCausalLM): The pre-trained model for generating text.
Methods:
run(task, *args, **kwargs): Generates text based on the given task.
Examples:
>>> from swarms.models import ModelScopeAutoModel
>>> mp = ModelScopeAutoModel(
... model_name="gpt2",
... )
>>> mp.run("Generate a 10,000 word blog on health and wellness.")
"""
def __init__(
self,
model_name: str,
tokenizer_name: Optional[str] = None,
device: str = "cuda",
device_map: str = "auto",
max_new_tokens: int = 500,
skip_special_tokens: bool = True,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.model_name = model_name
self.tokenizer_name = tokenizer_name
self.device = device
self.device_map = device_map
self.max_new_tokens = max_new_tokens
self.skip_special_tokens = skip_special_tokens
self.tokenizer = AutoTokenizer.from_pretrained(
self.tokenizer_name
)
self.model = AutoModelForCausalLM.from_pretrained(
self.model_name, device_map=device_map * args, **kwargs
)
def run(self, task: str, *args, **kwargs):
"""
Run the model on the given task.
Parameters:
task (str): The input task to be processed.
*args: Additional positional arguments.
**kwargs: Additional keyword arguments.
Returns:
str: The generated output from the model.
"""
text = self.tokenizer(task, return_tensors="pt")
outputs = self.model.generate(
**text, max_new_tokens=self.max_new_tokens, **kwargs
)
return self.tokenizer.decode(
outputs[0], skip_special_tokens=self.skip_special_tokens
)

@ -1,58 +0,0 @@
from modelscope.pipelines import pipeline
from swarms.models.base_llm import AbstractLLM
class ModelScopePipeline(AbstractLLM):
"""
A class representing a ModelScope pipeline.
Args:
type_task (str): The type of task for the pipeline.
model_name (str): The name of the model for the pipeline.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Attributes:
type_task (str): The type of task for the pipeline.
model_name (str): The name of the model for the pipeline.
model: The pipeline model.
Methods:
run: Runs the pipeline for a given task.
Examples:
>>> from swarms.models import ModelScopePipeline
>>> mp = ModelScopePipeline(
... type_task="text-generation",
... model_name="gpt2",
... )
>>> mp.run("Generate a 10,000 word blog on health and wellness.")
"""
def __init__(
self, type_task: str, model_name: str, *args, **kwargs
):
super().__init__(*args, **kwargs)
self.type_task = type_task
self.model_name = model_name
self.model = pipeline(
self.type_task, model=self.model_name, *args, **kwargs
)
def run(self, task: str, *args, **kwargs):
"""
Runs the pipeline for a given task.
Args:
task (str): The task to be performed by the pipeline.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
The result of running the pipeline on the given task.
"""
return self.model(task, *args, **kwargs)

@ -1,262 +0,0 @@
from typing import Any, Dict, List, Optional, Union
import openai
import requests
from pydantic import BaseModel, validator
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
)
from termcolor import colored
class FunctionSpecification(BaseModel):
"""
Defines the specification for a function including its parameters and metadata.
Attributes:
-----------
name: str
The name of the function.
description: str
A brief description of what the function does.
parameters: Dict[str, Any]
The parameters required by the function, with their details.
required: Optional[List[str]]
List of required parameter names.
Methods:
--------
validate_params(params: Dict[str, Any]) -> None:
Validates the parameters against the function's specification.
Example:
# Example Usage
def get_current_weather(location: str, format: str) -> str:
``'
Example function to get current weather.
Args:
location (str): The city and state, e.g. San Francisco, CA.
format (str): The temperature unit, e.g. celsius or fahrenheit.
Returns:
str: Weather information.
'''
# Implementation goes here
return "Sunny, 23°C"
weather_function_spec = FunctionSpecification(
name="get_current_weather",
description="Get the current weather",
parameters={
"location": {"type": "string", "description": "The city and state"},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit",
},
},
required=["location", "format"],
)
# Validating parameters for the function
params = {"location": "San Francisco, CA", "format": "celsius"}
weather_function_spec.validate_params(params)
# Calling the function
print(get_current_weather(**params))
"""
name: str
description: str
parameters: Dict[str, Any]
required: Optional[List[str]] = None
@validator("parameters")
def check_parameters(cls, params):
if not isinstance(params, dict):
raise ValueError("Parameters must be a dictionary.")
return params
def validate_params(self, params: Dict[str, Any]) -> None:
"""
Validates the parameters against the function's specification.
Args:
params (Dict[str, Any]): The parameters to validate.
Raises:
ValueError: If any required parameter is missing or if any parameter is invalid.
"""
for key, value in params.items():
if key in self.parameters:
self.parameters[key]
# Perform specific validation based on param_spec
# This can include type checking, range validation, etc.
else:
raise ValueError(f"Unexpected parameter: {key}")
for req_param in self.required or []:
if req_param not in params:
raise ValueError(
f"Missing required parameter: {req_param}"
)
class OpenAIFunctionCaller:
def __init__(
self,
openai_api_key: str,
model: str = "text-davinci-003",
max_tokens: int = 3000,
temperature: float = 0.5,
top_p: float = 1.0,
n: int = 1,
stream: bool = False,
stop: Optional[str] = None,
echo: bool = False,
frequency_penalty: float = 0.0,
presence_penalty: float = 0.0,
logprobs: Optional[int] = None,
best_of: int = 1,
logit_bias: Dict[str, float] = None,
user: str = None,
messages: List[Dict] = None,
timeout_sec: Union[float, None] = None,
):
self.openai_api_key = openai_api_key
self.model = model
self.max_tokens = max_tokens
self.temperature = temperature
self.top_p = top_p
self.n = n
self.stream = stream
self.stop = stop
self.echo = echo
self.frequency_penalty = frequency_penalty
self.presence_penalty = presence_penalty
self.logprobs = logprobs
self.best_of = best_of
self.logit_bias = logit_bias
self.user = user
self.messages = messages if messages is not None else []
self.timeout_sec = timeout_sec
def add_message(self, role: str, content: str):
self.messages.append({"role": role, "content": content})
@retry(
wait=wait_random_exponential(multiplier=1, max=40),
stop=stop_after_attempt(3),
)
def chat_completion_request(
self,
messages,
tools=None,
tool_choice=None,
):
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + openai.api_key,
}
json_data = {"model": self.model, "messages": messages}
if tools is not None:
json_data.update({"tools": tools})
if tool_choice is not None:
json_data.update({"tool_choice": tool_choice})
try:
response = requests.post(
"https://api.openai.com/v1/chat/completions",
headers=headers,
json=json_data,
)
return response
except Exception as e:
print("Unable to generate ChatCompletion response")
print(f"Exception: {e}")
return e
def pretty_print_conversation(self, messages):
role_to_color = {
"system": "red",
"user": "green",
"assistant": "blue",
"tool": "magenta",
}
for message in messages:
if message["role"] == "system":
print(
colored(
f"system: {message['content']}\n",
role_to_color[message["role"]],
)
)
elif message["role"] == "user":
print(
colored(
f"user: {message['content']}\n",
role_to_color[message["role"]],
)
)
elif message["role"] == "assistant" and message.get(
"function_call"
):
print(
colored(
f"assistant: {message['function_call']}\n",
role_to_color[message["role"]],
)
)
elif message["role"] == "assistant" and not message.get(
"function_call"
):
print(
colored(
f"assistant: {message['content']}\n",
role_to_color[message["role"]],
)
)
elif message["role"] == "tool":
print(
colored(
(
f"function ({message['name']}):"
f" {message['content']}\n"
),
role_to_color[message["role"]],
)
)
def call(self, task: str, *args, **kwargs) -> Dict:
return openai.Completion.create(
engine=self.model,
prompt=task,
max_tokens=self.max_tokens,
temperature=self.temperature,
top_p=self.top_p,
n=self.n,
stream=self.stream,
stop=self.stop,
echo=self.echo,
frequency_penalty=self.frequency_penalty,
presence_penalty=self.presence_penalty,
logprobs=self.logprobs,
best_of=self.best_of,
logit_bias=self.logit_bias,
user=self.user,
messages=self.messages,
timeout_sec=self.timeout_sec,
*args,
**kwargs,
)
def run(self, task: str, *args, **kwargs) -> str:
response = self.call(task, *args, **kwargs)
return response["choices"][0]["text"].strip()

File diff suppressed because it is too large Load Diff

@ -1 +0,0 @@
"""Phi by Microsoft written by Kye"""

@ -1,44 +0,0 @@
from unittest.mock import MagicMock
from swarms.models.fire_function import FireFunctionCaller
def test_fire_function_caller_run(mocker):
# Create mock model and tokenizer
model = MagicMock()
tokenizer = MagicMock()
mocker.patch.object(FireFunctionCaller, "model", model)
mocker.patch.object(FireFunctionCaller, "tokenizer", tokenizer)
# Create mock task and arguments
task = "Add 2 and 3"
args = (2, 3)
kwargs = {}
# Create mock generated_ids and decoded output
generated_ids = [1, 2, 3]
decoded_output = "5"
model.generate.return_value = generated_ids
tokenizer.batch_decode.return_value = [decoded_output]
# Create FireFunctionCaller instance
fire_function_caller = FireFunctionCaller()
# Run the function
fire_function_caller.run(task, *args, **kwargs)
# Assert model.generate was called with the correct inputs
model.generate.assert_called_once_with(
tokenizer.apply_chat_template.return_value,
max_new_tokens=fire_function_caller.max_tokens,
*args,
**kwargs,
)
# Assert tokenizer.batch_decode was called with the correct inputs
tokenizer.batch_decode.assert_called_once_with(generated_ids)
# Assert the decoded output is printed
assert decoded_output in mocker.patch.object(
print, "call_args_list"
)

@ -1,97 +0,0 @@
import torch
from swarms.models.base_llm import AbstractLLM
if torch.cuda.is_available() or torch.cuda.device_count() > 0:
# Download vllm with pip
try:
from vllm import LLM, SamplingParams
except ImportError as error:
print(f"[ERROR] [vLLM] {error}")
raise error
else:
from swarms.models.huggingface import HuggingfaceLLM as LLM
SamplingParams = None
class vLLM(AbstractLLM):
"""vLLM model
Args:
model_name (str, optional): _description_. Defaults to "facebook/opt-13b".
tensor_parallel_size (int, optional): _description_. Defaults to 4.
trust_remote_code (bool, optional): _description_. Defaults to False.
revision (str, optional): _description_. Defaults to None.
temperature (float, optional): _description_. Defaults to 0.5.
top_p (float, optional): _description_. Defaults to 0.95.
*args: _description_.
**kwargs: _description_.
Methods:
run: run the vLLM model
Raises:
error: _description_
Examples:
>>> from swarms.models.vllm import vLLM
>>> vllm = vLLM()
>>> vllm.run("Hello world!")
"""
def __init__(
self,
model_name: str = "facebook/opt-13b",
tensor_parallel_size: int = 4,
trust_remote_code: bool = False,
revision: str = None,
temperature: float = 0.5,
top_p: float = 0.95,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.model_name = model_name
self.tensor_parallel_size = tensor_parallel_size
self.trust_remote_code = trust_remote_code
self.revision = revision
self.top_p = top_p
# LLM model
self.llm = LLM(
model_name=self.model_name,
tensor_parallel_size=self.tensor_parallel_size,
trust_remote_code=self.trust_remote_code,
revision=self.revision,
*args,
**kwargs,
)
# Sampling parameters
self.sampling_params = SamplingParams(
temperature=temperature, top_p=top_p, *args, **kwargs
)
def run(self, task: str = None, *args, **kwargs):
"""Run the vLLM model
Args:
task (str, optional): _description_. Defaults to None.
Raises:
error: _description_
Returns:
_type_: _description_
"""
try:
return self.llm.generate(
task, self.sampling_params, *args, **kwargs
)
except Exception as error:
print(f"[ERROR] [vLLM] [run] {error}")
raise error

@ -3,7 +3,6 @@ from swarms.tokenizers.anthropic_tokenizer import (
import_optional_dependency,
)
from swarms.tokenizers.base_tokenizer import BaseTokenizer
from swarms.tokenizers.cohere_tokenizer import CohereTokenizer
from swarms.tokenizers.openai_tokenizers import OpenAITokenizer
from swarms.tokenizers.r_tokenizers import (
HuggingFaceTokenizer,
@ -19,5 +18,4 @@ __all__ = [
"OpenAITokenizer",
"import_optional_dependency",
"AnthropicTokenizer",
"CohereTokenizer",
]

@ -1,36 +0,0 @@
from __future__ import annotations
from dataclasses import dataclass
from cohere import Client
@dataclass
class CohereTokenizer:
"""
A tokenizer class for Cohere models.
"""
model: str
client: Client
DEFAULT_MODEL: str = "command"
DEFAULT_MAX_TOKENS: int = 2048
max_tokens: int = DEFAULT_MAX_TOKENS
def count_tokens(self, text: str | list) -> int:
"""
Count the number of tokens in the given text.
Args:
text (str | list): The input text to tokenize.
Returns:
int: The number of tokens in the text.
Raises:
ValueError: If the input text is not a string.
"""
if isinstance(text, str):
return len(self.client.tokenize(text=text).tokens)
else:
raise ValueError("Text must be a string.")

@ -1,93 +0,0 @@
from typing import Dict, List, Optional, Union
class AbstractWorker:
"""(In preview) An abstract class for AI worker.
An worker can communicate with other workers and perform actions.
Different workers can differ in what actions they perform in the `receive` method.
"""
def __init__(
self,
name: str,
):
"""
Args:
name (str): name of the worker.
"""
# a dictionary of conversations, default value is list
self._name = name
@property
def name(self):
"""Get the name of the worker."""
return self._name
def run(self, task: str):
"""Run the worker agent once"""
def send(
self,
message: Union[Dict, str],
recipient, # add AbstractWorker
request_reply: Optional[bool] = None,
):
"""(Abstract method) Send a message to another worker."""
async def a_send(
self,
message: Union[Dict, str],
recipient, # add AbstractWorker
request_reply: Optional[bool] = None,
):
"""(Aabstract async method) Send a message to another worker."""
def receive(
self,
message: Union[Dict, str],
sender, # add AbstractWorker
request_reply: Optional[bool] = None,
):
"""(Abstract method) Receive a message from another worker."""
async def a_receive(
self,
message: Union[Dict, str],
sender, # add AbstractWorker
request_reply: Optional[bool] = None,
):
"""(Abstract async method) Receive a message from another worker."""
def reset(self):
"""(Abstract method) Reset the worker."""
def generate_reply(
self,
messages: Optional[List[Dict]] = None,
sender=None, # Optional["AbstractWorker"] = None,
**kwargs,
) -> Union[str, Dict, None]:
"""(Abstract method) Generate a reply based on the received messages.
Args:
messages (list[dict]): a list of messages received.
sender: sender of an Agent instance.
Returns:
str or dict or None: the generated reply. If None, no reply is generated.
"""
async def a_generate_reply(
self,
messages: Optional[List[Dict]] = None,
sender=None, # Optional["AbstractWorker"] = None,
**kwargs,
) -> Union[str, Dict, None]:
"""(Abstract async method) Generate a reply based on the received messages.
Args:
messages (list[dict]): a list of messages received.
sender: sender of an Agent instance.
Returns:
str or dict or None: the generated reply. If None, no reply is generated.
"""
Loading…
Cancel
Save