Skip to main content

Grasp Agents Library

Project description

Grasp Agents


Grasp Agents

PyPI version License: MIT PyPI downloads GitHub Stars GitHub Forks

Overview

Grasp Agents is a modular Python framework for building agentic AI pipelines and applications. It is meant to be minimalistic but functional, allowing for rapid experimentation while keeping full and granular low-level control over prompting, LLM handling, and inter-agent communication by avoiding excessive higher-level abstractions.

Features

  • Clean formulation of agents as generic entities over:
    • I/O schemas
    • Memory
    • Shared context
  • Transparent implementation of common agentic patterns:
    • Single-agent loops with an optional "ReAct mode" to enforce reasoning between the tool calls
    • Workflows (static communication topology), including loops
    • Agents-as-tools for task delegation
    • Freeform A2A communication via the in-process actor model
  • Batch processing support outside of agentic loops
  • Simple logging and usage/cost tracking

Project Structure

  • processor.py, comm_processor.py, llm_agent.py: Core processor and agent class implementations.
  • packet.py, packet_pool.py: Communication management.
  • llm_policy_executor.py: LLM actions and tool call loops.
  • prompt_builder.py: Tools for constructing prompts.
  • workflow/: Modules for defining and managing static agent workflows.
  • llm.py, cloud_llm.py: LLM integration and base LLM functionalities.
  • openai/: Modules specific to OpenAI API integration.
  • memory.py, llm_agent_memory.py: Memory management.
  • run_context.py: Shared context management for agent runs.
  • usage_tracker.py: Tracking of API usage and costs.
  • costs_dict.yaml: Dictionary for cost tracking (update if needed).
  • rate_limiting/: Basic rate limiting tools.

Quickstart & Installation Variants (UV Package manager)

Note: You can check this sample project code in the src/grasp_agents/examples/demo/uv folder. Feel free to copy and paste the code from there to a separate project. There are also examples for other package managers.

1. Prerequisites

Install the UV Package Manager:

curl -LsSf https://astral.sh/uv/install.sh | sh

2. Create Project & Install Dependencies

mkdir my-test-uv-app
cd my-test-uv-app
uv init .

Create and activate a virtual environment:

uv venv
source .venv/bin/activate

Add and sync dependencies:

uv add grasp_agents
uv sync

3. Example Usage

Ensure you have a .env file with your OpenAI and Google AI Studio API keys set

OPENAI_API_KEY=your_openai_api_key
GOOGLE_AI_STUDIO_API_KEY=your_google_ai_studio_api_key

Create a script, e.g., problem_recommender.py:

import asyncio
from pathlib import Path
from typing import Any

from dotenv import load_dotenv
from pydantic import BaseModel, Field

from grasp_agents.grasp_logging import setup_logging
from grasp_agents.openai import OpenAILLM, OpenAILLMSettings
from grasp_agents import LLMAgent, BaseTool, RunContext

load_dotenv()


# Configure the logger to output to the console and/or a file
setup_logging(
    logs_file_path="grasp_agents_demo.log",
    logs_config_path=Path().cwd() / "configs/logging/default.yaml",
)

sys_prompt_react = """
Your task is to suggest an exciting stats problem to the student. 
You should first ask the student about their education, interests, and preferences, then suggest a problem tailored specifically to them. 

# Instructions
* Use the provided tool to ask questions.
* Ask questions one by one.
* Provide your thinking before asking a question and after receiving a reply.
* Do not include your exact question as part of your thinking.
* The problem must have all the necessary data.
* Use the final answer tool to provide the problem.
"""

# Tool input must be a Pydantic model to infer the JSON schema used by the LLM APIs
class TeacherQuestion(BaseModel):
    question: str


StudentReply = str


ask_student_tool_description = """
"Ask the student a question and get their reply."

Args:
    question: str
        The question to ask the student.
Returns:
    reply: str
        The student's reply to the question.
"""


class AskStudentTool(BaseTool[TeacherQuestion, StudentReply, Any]):
    name: str = "ask_student"
    description: str = ask_student_tool_description

    async def run(
        self, inp: TeacherQuestion, ctx: RunContext[Any] | None = None
    ) -> StudentReply:
        return input(inp.question)


class Problem(BaseModel):
    problem: str


teacher = LLMAgent[None, Problem, None](
    name="teacher",
    llm=OpenAILLM(
        model_name="openai:gpt-4.1",
        llm_settings=OpenAILLMSettings(temperature=0.5),
    ),
    tools=[AskStudentTool()],
    react_mode=True,
    final_answer_as_tool_call=True,
    sys_prompt=sys_prompt_react,
)

async def main():
    ctx = RunContext[None](print_messages=True)
    out = await teacher.run("start", ctx=ctx)
    print(out.payloads[0])
    print(ctx.usage_tracker.total_usage)


asyncio.run(main())

Run your script:

uv run problem_recommender.py

You can find more examples in src/grasp_agents/examples/notebooks/agents_demo.ipynb.

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

grasp_agents-0.4.0.tar.gz (40.7 kB view details)

Uploaded Source

Built Distribution

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

grasp_agents-0.4.0-py3-none-any.whl (58.4 kB view details)

Uploaded Python 3

File details

Details for the file grasp_agents-0.4.0.tar.gz.

File metadata

  • Download URL: grasp_agents-0.4.0.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.15

File hashes

Hashes for grasp_agents-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d00724906f91faa36ca83f8ca4621dd383f5034b0e03cb391141cc8642c18ac2
MD5 de9c32c95810aab2b9a984713e34047a
BLAKE2b-256 4ce0d25e6347c57e8d7ec2483d17b0dcdf2c65e99a993929a91b5eaa62d8bfbd

See more details on using hashes here.

File details

Details for the file grasp_agents-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for grasp_agents-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae9846a8c0d915c93804899e01a5238a1f8d492e8193fd3a5e3da236071d4344
MD5 5678b0c2c90c3ccbbecb8fbcad471d7c
BLAKE2b-256 a03229b6fe21aff7e6e4fc65788ce87aac3323f8a9c97eb1f183675d7eb00760

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