Skip to main content

Pluggable LLM/AI Agent library

Project description

Sweet Agent

This is not another Agent Framework. It is a library that gives you only the basic you need to build or transform your current codebase in agents.

Why we build it? Most agent frameworks around there don't integrate all the actors of a real world case usage. These actors are the developer/admin, the LLMs, the system/product and the user.

These frameworks let you start an agent which will do a task and return you a result. But what most people really wants is the ability to chat with an llm which can understand their need at some point and call the appropriate tool.

MCP came in and fixed this. But the LLM has to support it.

Sweet Agent gives you the following features:

  • Support for LLM trained without tools use.
  • Transparent tools use for different LLM
  • Native support of human in the loop
  • Easy class to agent transformation
  • Tools as method
  • Agent collaboration
  • Multiple api keys support
  • Short term memory
  • Long term memory
  • Easy prompt customization
  • Integrations: django-channels

Sweet Agent gives you those primitives—nothing more, nothing less—so you can integrate them into your architecture.


Why choose Sweet Agent?

Feature Sweet Agent Typical frameworks
Any LLM (native tools or none) Often limited
Human‑in‑the‑loop prompts at every step Optional / hard
Class‑to‑agent in one line
Tools = plain Python functions or other agents Partial
API‑key rotation built in
Plug‑and‑play I/O layers (console, Django Channels, …) Rare
Memory backends swappable Often single choice

Installation

# From PyPI
pip install sweetagent

# Or from source
pip install git+https://github.com/jefcolbi/sweetagent.git

Sweet Agent requires Python ⩾ 3.8.


Quick start

Below is the smallest useful agent: it exposes one tool (get_weather) and answers a user in the console.

from sweetagent.io.base import ConsoleStaIO
from sweetagent.llm_client import LLMClient
from sweetagent.short_term_memory.session import SessionMemory
from sweetagent.llm_agent import LLMAgent


class WeatherAgent(LLMAgent):
    """Return current weather for a city (demo)."""

    def get_weather(self, city: str = None):
        # <-- Real code would query a weather API
        return "cloudy"

    def configure_tools(self):
        # Turn the method into an LLM‑callable tool
        self.register_function_as_tool(self.get_weather)


if __name__ == "__main__":
    stdio = ConsoleStaIO("weather")
    client = LLMClient(
        provider="openai",
        model="gpt-4o",
        api_keys_rotator=["sk‑...", "sk‑backup‑..."],
        stdio=stdio,
    )

    agent = WeatherAgent(
        name="Weather Agent",
        role="Return the weather of cities",
        llm_client=client,
        short_term_memory=SessionMemory(),
        stdio=stdio,
    )

    answer = agent.run("What is the weather in Douala?")
    print(answer)

Run it:

python examples/weather_agent.py

Agent collaboration

Agents can register other agents as tools. The Assistant below delegates weather questions to the WeatherAgent above while chatting freely with the user.

from sweetagent.core import WorkMode
from examples.weather_agent import WeatherAgent  # see previous snippet

class Assistant(LLMAgent):
    """General‑purpose helper that can invoke WeatherAgent."""
    pass

# wire‑up
assistant = Assistant(
    "Assistant",
    "chat with user and help as much as you can",
    client,
    short_term_memory=SessionMemory(),
    stdio=stdio,
    work_mode=WorkMode.CHAT,
)
assistant.register_agent_as_tool(weather_agent)

assistant.run("Hi!")

See test_agent.py for a full example. citeturn1file1


Prompt Engine

Sweet Agent ships a strict response‑format contract so your UI and downstream code never guess what the LLM meant.

  • Sections are marked with +++ section +++ fences.
  • YAML blocks can carry structured data (choices, tables, …).
  • A built‑in parser turns the raw reply into a FormatResponseModel.
+++ kind +++        # message | tool_call | final_answer
+++ message +++     # free text (optional)
+++ data +++        # YAML (optional)
+++ tool_name +++   # when kind == tool_call
+++ tool_arguments +++
~~~ field ~~~       # arguments in their own fences
value

+++ end +++         # terminator (mandatory)

PromptEngine handles:

  • building the initial system prompt with examples and tool JSON
  • decoding every LLM response (see test_prompting.py). citeturn1file0

You can subclass it or plug your own templating engine—just return a markdown string.


Memory

Short‑term

Implement BaseShortTermMemory to implement a short term memory strategy.

SessionMemory keeps the last messages in RAM for the current execution.

Long‑term (pluggable)


Key rotation & retries

Provide one or many API keys. LLMClient cycles through them on RateLimitError:

client = LLMClient("openai", "gpt-4o", ["k1", "k2", "k3"], stdio=stdio)

If all keys fail you get the last captured exception.


Human‑in‑the‑loop

Because every model prompt can return kind = message, the agent pauses and calls StaIO.user_input_text()—letting a human decide before the loop continues.

Implement custom BaseStaIO subclasses to:

  • stream to a React frontend
  • post to Slack
  • collect feedback for RLHF

Django Channels integration

For real‑time websockets drop this into your consumers.py (excerpt from the demo project):

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.accept()

        self.agent: LLMAgent = None
        self.queue: Queue = Queue(maxsize=1)
        self.stdio = ChannelWebsocketStaIO('', self, self.queue)
        self.thread: Thread = None

    def configure_agent(self, initial_message: str) -> LLMAgent:
        """Configure the main agent and run it with the initial message"""
        client = LLMClient(settings.LLM_PROVIDER, settings.LLM_MODEL, settings.OPENAI_API_KEYS,
                           stdio=self.stdio)
        weather_agent = WeatherAgent("Weather Agent", "return the weather of cities", client,
                                     short_term_memory=SessionMemory(), stdio=self.stdio)
        assistant = Assistant("Assistant", "chat with user and help him as much as you can", client,
                              short_term_memory=SessionMemory(), stdio=self.stdio, work_mode=WorkMode.CHAT)
        assistant.register_agent_as_tool(weather_agent)

        self.agent = assistant
        self.agent.run(initial_message)

    def disconnect(self, close_code):
        pass

    def receive(self, text_data):
        self.stdio.log_debug(f"{text_data = }")
        text_data_json = json.loads(text_data)
        message = text_data_json["message"]

        if self.agent:
            self.stdio.log_debug("Putting message in the queue")
            self.queue.put(message)
        else:
            self.thread = Thread(target=self.configure_agent, args=[message])
            self.thread.setDaemon(True)
            self.thread.setName("Assistant Agent Thread")
            self.thread.start()

The full example lives in consumers.py. citeturn1file2


Environment variables

Variable Default Description
LLM_PROVIDER openai Provider identifier used by litellm
LLM_MODEL gpt-4o Model name (or anthropic/claude‑...)
OPENAI_API_KEYS Comma‑separated list of keys for rotation

Set them in .env and load with python‑decouple:

OPENAI_API_KEYS=sk‑primary,sk‑backup
LLM_PROVIDER=openai
LLM_MODEL=gpt-4o

Running the test suite

python -m unittest tests

The tests exercise:

  • prompt encoding/decoding (test_prompting.py)
  • agent collaboration and tool calls (test_agent.py)

Roadmap

  • Async I/O & streaming partial tokens
  • Built‑in Redis + SQLite memory backends

License

Sweet Agent is released under the MIT License. See LICENSE for the full text.

SweetAgent is developed under Connectivo AB.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sweetagent-0.1.3.tar.gz (360.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sweetagent-0.1.3-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file sweetagent-0.1.3.tar.gz.

File metadata

  • Download URL: sweetagent-0.1.3.tar.gz
  • Upload date:
  • Size: 360.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.4

File hashes

Hashes for sweetagent-0.1.3.tar.gz
Algorithm Hash digest
SHA256 17a44b5d3abe1423b8245b56b5bcf32dcabb70bf3940be254f021804601720f6
MD5 9d83fb743d8778a217767fd08f5ad088
BLAKE2b-256 ef818a6ac1411f13eb4cd5a7a6edcce20875dd33636b2965d20b705a13cef798

See more details on using hashes here.

File details

Details for the file sweetagent-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: sweetagent-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.4

File hashes

Hashes for sweetagent-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ceddc4f587adf4f52cebbe1dcc9072c0973a3af55e006a83dc2a6e89e392a01f
MD5 8372ab061cb9c6de3fd4c7e9d62da841
BLAKE2b-256 f7f78173876292750bdb4c8c3afc42a36aefe3b93cbd369e06b792671c5bbb9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page