Skip to content

Commit

Permalink
[BUFG][CLEANUP]]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Mar 27, 2024
1 parent 32a42b4 commit 41c5295
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 79 deletions.
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@


# Creative Commons Attribution 4.0 International Public License
Creative Commons Attribution 4.0 International Public License

By exercising the Licensed Rights (defined below), You accept and agree
to be bound by the terms and conditions of this Creative Commons
Expand Down
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,52 @@ print(out)
```


# `Agent` with Long Term Memory ++ Tools!
An LLM equipped with long term memory and tools, a full stack agent capable of automating all and any digital tasks given a good prompt.

```python
from swarms import Agent, ChromaDB, OpenAIChat, tool

# Making an instance of the ChromaDB class
memory = ChromaDB(
metric="cosine",
n_results=3,
output_dir="results",
docs_folder="docs",
)

# Initialize a tool
@tool
def search_api(query: str):
# Add your logic here
return query

# Initializing the agent with the Gemini instance and other parameters
agent = Agent(
agent_name="Covid-19-Chat",
agent_description=(
"This agent provides information about COVID-19 symptoms."
),
llm=OpenAIChat(),
max_loops="auto",
autosave=True,
verbose=True,
long_term_memory=memory,
stopping_condition="finish",
tools=[search_api],
)

# Defining the task and image path
task = ("What are the symptoms of COVID-19?",)

# Running the agent with the specified task and image
out = agent.run(task)
print(out)

```






Expand Down Expand Up @@ -873,6 +919,10 @@ agent = Agent(
# Run the workflow on a task
agent.run(task=task, img=img)
```
----


## Build your own LLMs, Agents, and Swarms!

### Swarms Compliant Model Interface
```python
Expand Down Expand Up @@ -935,6 +985,16 @@ agent = MyCustomAgent()
out = agent("Analyze and summarize these financial documents: ")
print(out)

```


### Compliant Interface for Multi-Agent Collaboration

```python
from swarms import Agent, AbstractSwarm



```

---
Expand Down
Empty file added playground/agents/sales_team.py
Empty file.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "swarms"
version = "4.6.1"
version = "4.6.7"
description = "Swarms - Pytorch"
license = "MIT"
authors = ["Kye Gomez <[email protected]>"]
Expand Down
4 changes: 1 addition & 3 deletions swarms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@

from swarms.agents import * # noqa: E402, F403
from swarms.artifacts import * # noqa: E402, F403
from swarms.chunkers import * # noqa: E402, F403
from swarms.memory 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
from swarms.telemetry import * # noqa: E402, F403
from swarms.tools import * # noqa: E402, F403
from swarms.utils import * # noqa: E402, F403
from swarms.schedulers import * # noqa: E402, F403
from swarms.utils import * # noqa: E402, F403
8 changes: 4 additions & 4 deletions swarms/artifacts/text_artifact.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

from dataclasses import dataclass, field

from typing import Callable
from swarms.artifacts.base_artifact import BaseArtifact
from swarms.tokenizers.base_tokenizer import BaseTokenizer


@dataclass
Expand Down Expand Up @@ -31,6 +30,7 @@ class TextArtifact(BaseArtifact):
value: str
encoding: str = "utf-8"
encoding_error_handler: str = "strict"
tokenizer: Callable = None
_embedding: list[float] = field(default_factory=list)

@property
Expand All @@ -49,8 +49,8 @@ def generate_embedding(self, model) -> list[float] | None:

return self.embedding

def token_count(self, tokenizer: BaseTokenizer) -> int:
return tokenizer.count_tokens(str(self.value))
def token_count(self) -> int:
return self.tokenizer.count_tokens(str(self.value))

def to_bytes(self) -> bytes:
return self.value.encode(
Expand Down
3 changes: 1 addition & 2 deletions swarms/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
ReplicateLLM as Replicate,
)
from swarms.models.qwen import QwenVLMultiModal # noqa: E402
from swarms.models.sam_supervision import SegmentAnythingMarkGenerator
# from swarms.models.sam_supervision import SegmentAnythingMarkGenerator
from swarms.models.sampling_params import SamplingParams, SamplingType
from swarms.models.together import TogetherLLM # noqa: E402
from swarms.models.types import ( # noqa: E402
Expand Down Expand Up @@ -79,7 +79,6 @@
"Replicate",
"SamplingParams",
"SamplingType",
"SegmentAnythingMarkGenerator",
"TextModality",
"TogetherLLM",
"Vilt",
Expand Down
13 changes: 3 additions & 10 deletions swarms/models/base_embedding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
from dataclasses import dataclass, field

import numpy as np

from typing import Callable
from swarms.artifacts.text_artifact import TextArtifact
from swarms.chunkers.base_chunker import BaseChunker
from swarms.chunkers.text_chunker import TextChunker
from swarms.tokenizers.base_tokenizer import BaseTokenizer
from swarms.utils.exponential_backoff import ExponentialBackoffMixin


Expand All @@ -25,12 +22,8 @@ class BaseEmbeddingModel(
"""

model: str = None
tokenizer: BaseTokenizer | None = None
chunker: BaseChunker = field(init=False)

def __post_init__(self) -> None:
if self.tokenizer:
self.chunker = TextChunker(tokenizer=self.tokenizer)
tokenizer: Callable = None
chunker: Callable = None

def embed_text_artifact(
self, artifact: TextArtifact
Expand Down
10 changes: 5 additions & 5 deletions swarms/models/sam_supervision.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Optional

from typing import Optional, Callable
import cv2
import numpy as np
import supervision as sv
Expand All @@ -12,7 +11,6 @@
)

from swarms.models.base_multimodal_model import BaseMultiModalModel
from swarms.utils.supervision_masking import masks_to_marks


class SegmentAnythingMarkGenerator(BaseMultiModalModel):
Expand All @@ -30,13 +28,15 @@ def __init__(
device: str = "cpu",
model_name: str = "facebook/sam-vit-huge",
visualize_marks: bool = False,
masks_to_marks: Callable = sv.masks_to_marks,
*args,
**kwargs,
):
super(SegmentAnythingMarkGenerator).__init__(*args, **kwargs)
self.device = device
self.model_name = model_name
self.visualize_marks = visualize_marks
self.masks_to_marks = masks_to_marks

self.model = SamModel.from_pretrained(
model_name, *args, **kwargs
Expand Down Expand Up @@ -74,7 +74,7 @@ def __call__(
if mask is None:
outputs = self.pipeline(image, points_per_batch=64)
masks = np.array(outputs["masks"])
return masks_to_marks(masks=masks)
return self.masks_to_marks(masks=masks)
else:
inputs = self.processor(image, return_tensors="pt").to(
self.device
Expand Down Expand Up @@ -112,6 +112,6 @@ def __call__(
)
masks.append(mask)
masks = np.array(masks)
return masks_to_marks(masks=masks)
return self.masks_to_marks(masks=masks)

# def visualize_img(self):
86 changes: 43 additions & 43 deletions swarms/structs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
TaskQueueBase,
synchronized_queue,
)
from swarms.structs.tool_json_schema import JSON
from swarms.structs.utils import (
detect_markdown,
distribute_tasks,
Expand All @@ -80,39 +79,47 @@
parse_tasks,
)



__all__ = [
"Agent",
"SequentialWorkflow",
"AgentJob",
"AgentProcess",
"AgentProcessQueue",
"AutoSwarm",
"AutoSwarmRouter",
"AutoScaler",
"Conversation",
"TaskInput",
"Artifact",
"ArtifactUpload",
"StepInput",
"SwarmNetwork",
"ModelParallelizer",
"MultiAgentCollaboration",
"BaseStructure",
"AbstractSwarm",
"GroupChat",
"GroupChatManager",
"parse_tasks",
"find_agent_by_id",
"distribute_tasks",
"find_token_in_text",
"extract_key_from_json",
"extract_tokens_from_text",
"ConcurrentWorkflow",
"RecursiveWorkflow",
"NonlinearWorkflow",
"BaseWorkflow",
"BaseStructure",
"detect_markdown",
"Task",
"block",
"ConcurrentWorkflow",
"Conversation",
"GraphWorkflow",
"Step",
"Plan",
"GroupChat",
"GroupChatManager",
"MajorityVoting",
"majority_voting",
"most_frequent",
"parse_code_completion",
"Message",
"ModelParallelizer",
"MultiAgentCollaboration",
"MultiProcessWorkflow",
"MultiThreadedWorkflow",
"NonlinearWorkflow",
"Plan",
"RecursiveWorkflow",
"Artifact",
"ArtifactUpload",
"StepInput",
"StepOutput",
"StepRequestBody",
"TaskInput",
"TaskRequestBody",
"SequentialWorkflow",
"Step",
"SwarmNetwork",
"broadcast",
"circular_swarm",
"exponential_swarm",
Expand All @@ -131,21 +138,14 @@
"sigmoid_swarm",
"staircase_swarm",
"star_swarm",
"StepOutput",
"StepRequestBody",
"TaskRequestBody",
"JSON",
"most_frequent",
"parse_code_completion",
"majority_voting",
"MajorityVoting",
"synchronized_queue",
"Task",
"TaskQueueBase",
"MultiProcessWorkflow",
"MultiThreadedWorkflow",
"AgentJob",
"AutoSwarm",
"AutoSwarmRouter",
"AgentProcess",
"AgentProcessQueue",
]
"synchronized_queue",
"detect_markdown",
"distribute_tasks",
"extract_key_from_json",
"extract_tokens_from_text",
"find_agent_by_id",
"find_token_in_text",
"parse_tasks",
]
8 changes: 4 additions & 4 deletions swarms/structs/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
)
from swarms.prompts.worker_prompt import worker_tools_sop_promp
from swarms.structs.conversation import Conversation
from swarms.tokenizers.base_tokenizer import BaseTokenizer
from swarms.tools.tool import BaseTool
from swarms.utils.code_interpreter import SubprocessCodeInterpreter
from swarms.utils.data_to_text import data_to_text
from swarms.utils.parse_code import extract_code_from_markdown
from swarms.utils.pdf_to_text import pdf_to_text
from swarms.utils.token_count_tiktoken import limit_tokens_from_string


# Utils
Expand Down Expand Up @@ -184,7 +182,7 @@ def __init__(
multi_modal: Optional[bool] = None,
pdf_path: Optional[str] = None,
list_of_pdf: Optional[str] = None,
tokenizer: Optional[BaseTokenizer] = None,
tokenizer: Optional[Any] = None,
long_term_memory: Optional[AbstractVectorDatabase] = None,
preset_stopping_token: Optional[bool] = False,
traceback: Any = None,
Expand All @@ -208,6 +206,7 @@ def __init__(
sentiment_threshold: Optional[float] = None,
custom_exit_command: Optional[str] = "exit",
sentiment_analyzer: Optional[Callable] = None,
limit_tokens_from_string: Optional[Callable] = None,
*args,
**kwargs,
):
Expand Down Expand Up @@ -267,6 +266,7 @@ def __init__(
self.sentiment_threshold = sentiment_threshold
self.custom_exit_command = custom_exit_command
self.sentiment_analyzer = sentiment_analyzer
self.limit_tokens_from_string = limit_tokens_from_string

# The max_loops will be set dynamically if the dynamic_loop
if self.dynamic_loops:
Expand Down Expand Up @@ -1262,7 +1262,7 @@ def pdf_chunker(self, text: str = None, num_limits: int = 1000):
_type_: _description_
"""
text = text or self.pdf_connector()
text = limit_tokens_from_string(text, num_limits)
text = self.limit_tokens_from_string(text, num_limits)
return text

def ingest_docs(self, docs: List[str], *args, **kwargs):
Expand Down
Loading

0 comments on commit 41c5295

Please sign in to comment.