From b097e6016df23c235977c241ecc97f2cca06ee01 Mon Sep 17 00:00:00 2001 From: Kye Date: Thu, 25 Jan 2024 17:13:16 -0500 Subject: [PATCH] [README] --- README.md | 50 +++++++++++++++++++++++++++++ playground/agents/worker_example.py | 8 +++++ playground/demos/fof/langchain.py | 5 +++ swarms/agents/__init__.py | 2 ++ swarms/agents/agent_wrapper.py | 25 +++++++++++++++ swarms/agents/worker_agent.py | 2 ++ swarms/models/anthropic.py | 2 -- swarms/structs/task.py | 1 + 8 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 playground/demos/fof/langchain.py create mode 100644 swarms/agents/agent_wrapper.py diff --git a/README.md b/README.md index 43407822..64db98ae 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,56 @@ generated_data = agent.run(task) print(generated_data) +``` + + +### `Worker` +The `Worker` is a simple all-in-one agent equipped with an LLM, tools, and RAG. Get started below: + +✅ Plug in and Play LLM. Utilize any LLM from anywhere and any framework + +✅ Reliable RAG: Utilizes FAISS for efficient RAG but it's modular so you can use any DB. + +✅ Multi-Step Parallel Function Calling: Use any tool + +```python +# Importing necessary modules +import os +from dotenv import load_dotenv +from swarms import Worker, OpenAIChat, tool + +# Loading environment variables from .env file +load_dotenv() + +# Retrieving the OpenAI API key from environment variables +api_key = os.getenv("OPENAI_API_KEY") + + +# Create a tool +@tool +def search_api(query: str): + pass + + +# Creating a Worker instance +worker = Worker( + name="My Worker", + role="Worker", + human_in_the_loop=False, + tools=[search_api], + temperature=0.5, + llm=OpenAIChat(openai_api_key=api_key), +) + +# Running the worker with a prompt +out = worker.run( + "Hello, how are you? Create an image of how your are doing!" +) + +# Printing the output +print(out) + + ``` ------ diff --git a/playground/agents/worker_example.py b/playground/agents/worker_example.py index ceead3a9..bd8d47c0 100644 --- a/playground/agents/worker_example.py +++ b/playground/agents/worker_example.py @@ -1,12 +1,16 @@ +# Importing necessary modules import os from dotenv import load_dotenv from swarms.agents.worker_agent import Worker from swarms import OpenAIChat +# Loading environment variables from .env file load_dotenv() +# Retrieving the OpenAI API key from environment variables api_key = os.getenv("OPENAI_API_KEY") +# Creating a Worker instance worker = Worker( name="My Worker", role="Worker", @@ -14,9 +18,13 @@ worker = Worker( tools=[], temperature=0.5, llm=OpenAIChat(openai_api_key=api_key), + verbose = True, ) +# Running the worker with a prompt out = worker.run( "Hello, how are you? Create an image of how your are doing!" ) + +# Printing the output print(out) diff --git a/playground/demos/fof/langchain.py b/playground/demos/fof/langchain.py new file mode 100644 index 00000000..ef338520 --- /dev/null +++ b/playground/demos/fof/langchain.py @@ -0,0 +1,5 @@ +""" +This tutorial shows you how to integrate swarms with Langchain + +""" + diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index 4b786bf4..461baa16 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -15,6 +15,7 @@ from swarms.agents.stopping_conditions import ( ) from swarms.agents.tool_agent import ToolAgent from swarms.agents.worker_agent import Worker +from swarms.agents.agent_wrapper import agent_wrapper __all__ = [ "AbstractAgent", @@ -32,4 +33,5 @@ __all__ = [ "check_exit", "check_end", "Worker", + "agent_wrapper", ] diff --git a/swarms/agents/agent_wrapper.py b/swarms/agents/agent_wrapper.py new file mode 100644 index 00000000..109048e9 --- /dev/null +++ b/swarms/agents/agent_wrapper.py @@ -0,0 +1,25 @@ +from swarms.structs.agent import Agent + +def agent_wrapper(ClassToWrap): + """ + This function takes a class 'ClassToWrap' and returns a new class that + inherits from both 'ClassToWrap' and 'Agent'. The new class overrides + the '__init__' method of 'Agent' to call the '__init__' method of 'ClassToWrap'. + + Args: + ClassToWrap (type): The class to be wrapped and made to inherit from 'Agent'. + + Returns: + type: The new class that inherits from both 'ClassToWrap' and 'Agent'. + """ + + class WrappedClass(ClassToWrap, Agent): + def __init__(self, *args, **kwargs): + try: + Agent.__init__(self, *args, **kwargs) + ClassToWrap.__init__(self, *args, **kwargs) + except Exception as e: + print(f"Error initializing WrappedClass: {e}") + raise e + + return WrappedClass \ No newline at end of file diff --git a/swarms/agents/worker_agent.py b/swarms/agents/worker_agent.py index 8ed7d0d3..292466a2 100644 --- a/swarms/agents/worker_agent.py +++ b/swarms/agents/worker_agent.py @@ -51,6 +51,7 @@ class Worker: tools: List[Any] = None, embedding_size: int = 1536, search_kwargs: dict = {"k": 8}, + verbose: bool = False, *args, **kwargs, ): @@ -64,6 +65,7 @@ class Worker: self.tools = tools self.embedding_size = embedding_size self.search_kwargs = search_kwargs + self.verbose = verbose self.setup_tools(external_tools) self.setup_memory() diff --git a/swarms/models/anthropic.py b/swarms/models/anthropic.py index adffe49d..0e4690f9 100644 --- a/swarms/models/anthropic.py +++ b/swarms/models/anthropic.py @@ -29,9 +29,7 @@ from langchain.schema.language_model import BaseLanguageModel from langchain.schema.output import GenerationChunk from langchain.schema.prompt import PromptValue from langchain.utils import ( - check_package_version, get_from_dict_or_env, - get_pydantic_field_names, ) from packaging.version import parse from requests import HTTPError, Response diff --git a/swarms/structs/task.py b/swarms/structs/task.py index a794506f..fd6a1ab8 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -233,6 +233,7 @@ class Task: if task.description is not None else "" ) + result = ( task.result if task.result is not None else "" )