Skip to main content

A flexible agent framework.

Project description

Reflex Agent

reflex-agent is a flexible framework to create AI agents with modular capabilities and tool integrations.

Installation

pip install reflex-agent

Features

  • Zero Dependencies: The core install has no extra dependencies - bring your own LLM.
  • Modular Capabilities: The base agent loop is simple and can be modified through capabilities to add features like memory and chain of thought.
  • Simple Tool Use: Use any Python function as a tool that the agent can call.
  • Client-Server Architecture: Designed for easy use in a client-server architecture - allowing for stepping through the agent loop.
  • Async by Default: All functions are async by default.

Usage

Agents

Agents are entities that you can chat with. To create a basic agent, by specifiying an LLM and a system prompt.

We currently have built-in support for OpenAI and Anthropic models.

from flexai import Agent
from flexai.llm.openai import OpenAIClient
from flexai.llm.anthropic import AnthropicClient


openai_agent = Agent(
    llm=OpenAIClient(model="gpt-4o-mini"),
    prompt="You are a helpful assistant.",
)

anthropic_client = Agent(
    llm=AnthropicClient(model="claude-3-5-sonnet-20240620"),
    prompt="You are a helpful assistant.",
)

To interact with an agent, pass in a list of messages. There are two ways to interact with an agent - stream and step.

Streaming allows the agent to use multiple messages (such as inner tool uses) before returning a final response.

import asyncio
from flexai import Agent, UserMessage
from flexai.llm.openai import OpenAIClient

agent = Agent(
    llm=OpenAIClient(model="gpt-4o-mini"),
    prompt="You are an expert mathematician.",
)

async def get_agent_response(messages):
    async for response in agent.stream(messages):
        print(response)


asyncio.run(get_agent_response([UserMessage("What's 2 + 2?")]))

Stepping allows you to step through the agent loop, allowing you to see the agent's internal state at each step.

import asyncio
from flexai import Agent, UserMessage
from flexai.llm.openai import OpenAIClient

agent = Agent(
    llm=OpenAIClient(model="gpt-4o-mini"),
    prompt="You are an expert mathematician.",
)

async def get_agent_response(messages):
    response = await agent.step(messages)
    print(response)

asyncio.run(get_agent_response([UserMessage("What's 2 + 2?")]))

Memory

Agents are stateless by default - all state management is done on the user end. You can save the agent's output messages to have an extended conversation.

import asyncio
from flexai import Agent, UserMessage
from flexai.llm.openai import OpenAIClient

agent = Agent(
    llm=OpenAIClient(model="gpt-4o-mini"),
    prompt="You are an expert mathematician.",
)

async def get_agent_response(messages):
    response = await agent.step(messages):
    print(response)
    messages.append(response)
    messages.append(UserMessage("Tell me some key themes from your story."))
    response = await agent.step(messages):
    print(response)

asyncio.run(get_agent_response([UserMessage("Tell me a random story")]))

Tools

Tools are Python functions that the agent can call. The function's signature and docstring are used to determine when and how the tool is called.

import asyncio
from flexai import Agent, UserMessage
from flexai.llm.openai import OpenAIClient

def read_file(file_path: str) -> str:
    """Read a file and get the contents."""
    with open(file_path, "r") as file:
        return file.read()

def write_file(file_path: str, contents: str) -> None:
    """Write contents to a file."""
    with open(file_path, "w") as file:
        file.write(contents)

agent = Agent(
    llm=OpenAIClient(model="gpt-4o-mini"),
    prompt="You are an expert mathematician.",
    tools=[
        read_file,
        write_file,
    ]
)


async def get_agent_response(messages):
    # Stream allows the agent to use multiple tool uses in one go.
    async for message in agent.stream(messages):
        print(message)

asyncio.run(get_agent_response([UserMessage("Read the README.md file and create a copy at README2.md with a high level summary.")]))

Capabilities

Capabilities allow you to modify the core agent loop and change the behavior of the agent.

You can plug in to the agent loop to modify messages, responses, and system messages. For example, the TruncateMessages capability truncates the input messages to the LLM to a maximum.

@dataclass
class TruncateMessages(Capability):
    """Truncate the input messages to the LLM to a maximum number."""

    # The maximum number of messages to keep.
    max_messages: int

    async def modify_messages(self, messages: list[Message]) -> list[Message]:
        return messages[-self.max_messages :]

This capability is built-in to the framework to use:

import asyncio
from flexai import Agent, UserMessage
from flexai.llm.openai import OpenAIClient
from flexai.capabilities import TruncateMessages

agent = Agent(
    llm=OpenAIClient(model="gpt-4o-mini"),
    prompt="You are an expert mathematician.",
    capabilities=[TruncateMessages(max_messages=3)],
)

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

reflex_agent-0.1.0a37.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

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

reflex_agent-0.1.0a37-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file reflex_agent-0.1.0a37.tar.gz.

File metadata

  • Download URL: reflex_agent-0.1.0a37.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Darwin/23.6.0

File hashes

Hashes for reflex_agent-0.1.0a37.tar.gz
Algorithm Hash digest
SHA256 38476936cd437a252b40ef6f2434749ab8a5e78805f7ed6c4a578a1ca0efab91
MD5 5af3222cfdebd1af53de5da616fcf54c
BLAKE2b-256 3a2090d19f06b6b1f9465d8667d4ae5e261533d732e71c1b375892df1b43870d

See more details on using hashes here.

File details

Details for the file reflex_agent-0.1.0a37-py3-none-any.whl.

File metadata

  • Download URL: reflex_agent-0.1.0a37-py3-none-any.whl
  • Upload date:
  • Size: 22.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.6 Darwin/23.6.0

File hashes

Hashes for reflex_agent-0.1.0a37-py3-none-any.whl
Algorithm Hash digest
SHA256 f9d2a18bc3ec3aa945aee980973ba5292457c48e3a20d90777a87c51bdf8fc90
MD5 5fab05ea012aeb2f9d752f818bae1b3e
BLAKE2b-256 fcada6eef52160de16b7384bb7d85b4b377a3f5c55f543d2dbb5c5df70c85eb4

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