Skip to main content

A lightweight and developer-friendly library for building and orchestrating agents

Project description

SlimAgents

A lightweight and developer-friendly library for building and orchestrating AI agents

Install

Requires Python 3.10+

Latest stable release:

pip install slimagents

Latest development version:

pip install git+ssh://git@github.com/aremeis/slimagents.git

or

pip install git+https://github.com/aremeis/slimagents.git

Documentation

In SlimAgents, an Agent is simply a wrapper around a large language model, textual instructions, and a set of tools. Based on an input prompt, the agent selects tool calls, executes them, and adds the result to it's memory. This process is repeated until the LLM does not generate any more tool calls, in which case the agent returns the last message content generated from the LLM.

Here's a simple example:

from slimagents import Agent

def python_evaluator(expression: str) -> str:
    """Evaluate a Python expression. Always use this tool for calculations and other complex operations."""
    print(f"--- Evaluating {expression}")
    # Obviously not secure, but for the sake of this example we'll just eval the expression.
    ret = str(eval(expression))
    print(f"--> {ret}")
    return ret

agent = Agent(
    instructions="You are a helpful assistant. When given a task you always try to solve it by using tools, never rely on your own knowledge.",
    tools=[python_evaluator],
)

prompt = "How many R's are in the word 'STRAWBERRY'?"
print(f"User: {prompt}")
response = agent.run_sync(prompt)
print(f"Agent: {response.value}")

Result:

User: How many R's are in the word 'STRAWBERRY'?
--- Evaluating 'STRAWBERRY'.count('R')
--> 3
Agent: There are 3 'R's in the word 'STRAWBERRY'.

Tools

As you can see from the example above, a tool is simply a normal Python function! This means that it is very easy to integrate existing Python libraries with your agents. Use the tool's docstring to describe the tool and it's arguments to the LLM.

SlimAgents supports both synchronous and asynchronous tool calls. If the LLM generates several async tool calls, they will be executed in parallel.

NOTE: The method run_sync is used in the examples in this document. In async applications, you should use the run method instead.

Tools can also be implemented as methods. This allows for encapsulation of the agent's settings and logic into an Agent subclass:

# !pip install python-weather

from slimagents.core import Agent
import python_weather

class WeatherAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="You are a helpful assistant who answers questions about the weather.",
            tools=[self.get_temperature],
        )

    async def get_temperature(self, location: str) -> float:
        """Get the current temperature in a given location, in degrees Celsius."""
        async with python_weather.Client(unit=python_weather.METRIC) as client:
            print(f"--- Getting temperature for {location}")
            weather = await client.get(location)
            print(f"--> Temperature in {location}: {weather.temperature}")
            return weather.temperature

agent = WeatherAgent()
prompt = "What is the temperature difference between London and Paris?"
print(f"User: {prompt}")
response = agent.run_sync(prompt)
print(f"Agent: {response.value}")
User: What is the temperature difference between London and Paris?
--- Getting temperature for London
--- Getting temperature for Paris
--> Temperature in London: 4
--> Temperature in Paris: 3
Agent: The temperature difference between London and Paris is 1°C, with London being warmer.

LLMs

SlimAgents uses LiteLLM under the hood, which means that you can use virtually any LLM to power your agents! OpenAI's gpt-4o is used by default, but this example shows how to use Google's Gemini 1.5 Pro instead. See LiteLLM's documentation for more information about model support and how to specify models.

from slimagents import Agent

agent = Agent(
    model="gemini/gemini-1.5-pro",
)

response = agent.run_sync("Who are you?")
print(response.value)
I am a large language model, trained by Google.

Instructions

Instructions are passed to the LLM as the system message. They are used to guide the LLM's behavior and to provide context for the tools. SlimAgents does not come with pre-defined instructions, so your agent's behavior is entirely controlled by the information you provide as instructions and in the tool documentation.

Instructions can be dynamic, i.e. generated based on the agent's state. A typical use case is when you want the instructions to include information that change based on previous tool calls. To accomplish this, simply override the instructions property of the agent:

from slimagents import Agent
from slimagents.repl import run_demo_loop

class StrictAgent(Agent):
    def __init__(self, max_responses: int):
        super().__init__(
            tools=[self.update_responses_left],
        )
        self._answers_left = max_responses

    @property
    def instructions(self) -> str:
        if self._answers_left >= 0:
            return f"""You are a helpful assistant. 
You currently have {self._answers_left} responses left.
ALWAYS call the `update_responses_left` tool before you respond."""
        else:
            return "You always answer 'I can't answer that.'."

    def update_responses_left(self):
        """IMPORTANT! You ALWAYS call this tool before you respond, no matter what the user says."""
        self._answers_left -= 1
        return "Good! You may now answer the question."

agent = StrictAgent(2) # This agent will only respond 2 times.
run_demo_loop(agent)
Starting SlimAgents CLI 🪶
User: Hi
StrictAgent: update_responses_left()
StrictAgent: Hello! How can I assist you today?
User: How many answers left?
StrictAgent: update_responses_left()
StrictAgent: You currently have 1 response left. How else may I assist you?
User: 2 + 2?                        
StrictAgent: update_responses_left()
StrictAgent: I can't answer that.
User: Why not?
StrictAgent: update_responses_left()
StrictAgent: I can't answer that.

This example also illustrates the run_demo_loop function. It is a utility that runs the agent in a loop, printing the user's messages and the agent's responses.

Memory

WIP

Handoffs

Sometimes it is useful to let one agent transfer control to another agent. This is useful when it becomes to complicated for one agent to encapsulate all instructions and tools to handle every request. To accomplish such handoffs, simply return an Agent from a tool call:

sales_agent = Agent(name="Sales Agent")

def transfer_to_sales():
   return sales_agent

agent = Agent(functions=[transfer_to_sales])

response = agent.run("Transfer me to sales.")
print(response.agent.name)
Sales Agent

If you think this feature looks like it is borrowed from OpenAI's Swarm framework, you are right! In fact, SlimAgents started out as a fork of Swarm, so big shoutout to OpenAI and the Swarm team for the inspiration!

Major changes from Swarm:

  • Supports virtually any LLM
  • Designed for subclassing Agent to encapsulate agent behavior
  • Supports async, concurrent tool calls
  • Uses proper Python logging instead of print statements
  • Supports structured outputs with Pydantic

The response object

WIP

Response type

WIP

Handoff vs tool call

WIP

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

slimagents-0.1.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

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

slimagents-0.1.0-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file slimagents-0.1.0.tar.gz.

File metadata

  • Download URL: slimagents-0.1.0.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for slimagents-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f456608d9712a829120d494c6856184eb37b48412579c2277a86d39a0509358b
MD5 a2789c4472b13b82595ee75c1ad7fa96
BLAKE2b-256 ed0f7f6c02e85ae07013270be9ec8510e76770c6ce6bb50c62b0730e6e70bf2d

See more details on using hashes here.

File details

Details for the file slimagents-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slimagents-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for slimagents-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95156210c07ab05c97b02a03cdd22600b69a8afc6dad384a7c79da3b512c5c01
MD5 4629e9c77cd0beac282f994b951e17db
BLAKE2b-256 2ceda5642d7a0fd30fb60d869900db315ad3b96b32004997ab1ec3c3b30ed6ff

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