Skip to main content

A Python package to build AI agents on NEAR Protocol.

Project description

NEAR AI Agent

A Python package for building and managing AI agents on NEAR Protocol. This package provides a streamlined way to create, customize, and run AI agents using the NEAR AI Environment API, with support for both local development and NEAR AI deployment.

Features

  • Full integration with NEAR AI's Environment API
  • Support for both local development and NEAR AI deployment
  • Built-in tool registry for extending agent capabilities
  • Advanced message processing with LLM support
  • Configurable system prompts and model parameters
  • Modular design for custom task integration
  • Local development mode with mock environment

Installation

Install the package from PyPI:

pip install near_ai_agent

For development:

git clone https://github.com/joe-rlo/near_ai_agent.git
cd near_ai_agent
pip install -e .

Usage

Basic Usage with NEAR AI

  1. Create an agent directory in your NEAR AI registry:
mkdir -p ~/.nearai/registry/my-agent
  1. Initialize your agent with default configuration:
from near_ai_agent.agent import NearAIAgent

# The environment will be provided by NEAR AI
agent = NearAIAgent()
agent.run()
  1. Run your agent using NEAR AI CLI:
nearai agent interactive my-agent --local

Local Development

Create and Run a Custom Agent

from near_ai_agent.agent import NearAIAgent
from near_ai_agent.environment import NearAIEnvironment

# Create agent with mock environment for local testing
env = NearAIEnvironment()  # Creates a mock environment
agent = NearAIAgent(env=env)
agent.run()

Adding Custom Tools

from near_ai_agent.agent import NearAIAgent

class CustomAgent(NearAIAgent):
    def _register_tools(self):
        super()._register_tools()  # Register default tools
        
        @self.tool_registry.register_tool
        def calculate_sum(a: int, b: int) -> int:
            """Calculate the sum of two numbers"""
            return a + b

# Initialize and run
agent = CustomAgent()
agent.run()

Configuration

Create a config.json file to configure your agent:

{
    "agent_name": "MyCustomAgent",
    "version": "1.0.0",
    "system_prompt": {
        "role": "system",
        "content": "You are a helpful AI assistant that specializes in [your domain]."
    },
    "model": "qwen2p5-72b-instruct",
    "temperature": 0.7,
    "default_tasks": {
        "greet": "Hello! I'm MyCustomAgent, here to assist you.",
        "help": "I can help you with various tasks including file operations and calculations."
    }
}

NEAR AI Deployment

  1. Create metadata.json in your agent directory:
{
    "category": "agent",
    "description": "Your agent description",
    "tags": ["python", "assistant"],
    "details": {
        "agent": {
            "defaults": {
                "model": "qwen2p5-72b-instruct",
                "model_max_tokens": 16384,
                "model_provider": "fireworks",
                "model_temperature": 0.7
            }
        }
    },
    "show_entry": true,
    "name": "my-custom-agent",
    "version": "0.1.0"
}
  1. Upload your agent:
nearai registry upload ~/.nearai/registry/my-agent

Advanced Features

Message Processing

The agent handles messages through a sophisticated processing loop that can:

  • Execute multiple iterations of thought
  • Use tools when needed
  • Request user input at appropriate times
  • Maintain conversation context
from near_ai_agent.agent import NearAIAgent

agent = NearAIAgent()
agent.process_message(max_iterations=5)  # Process with up to 5 iterations

Tool Registry

Built-in tools include:

  • File operations (read/write)
  • Command execution
  • Vector store queries
  • Custom task execution

Add your own tools:

from near_ai_agent.agent import NearAIAgent

class CustomAgent(NearAIAgent):
    def _register_tools(self):
        super()._register_tools()
        
        @self.tool_registry.register_tool
        def custom_tool(param: str) -> str:
            """Tool description for the LLM"""
            return f"Processed {param}"

Testing

Run the test suite:

python -m unittest discover tests

For local development testing:

python examples/run_agent.py --local

Project Structure

near_ai_agent/
├── near_ai_agent/
│   ├── __init__.py
│   ├── agent.py          # Core agent implementation
│   ├── environment.py    # Environment wrapper
│   ├── tasks.py         # Task implementations
│   └── config.json      # Default configuration
├── examples/
│   └── run_agent.py     # Example usage
├── tests/
├── setup.py
├── README.md
└── requirements.txt

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License.

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

near_ai_agent-1.0.0a2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

near_ai_agent-1.0.0a2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file near_ai_agent-1.0.0a2.tar.gz.

File metadata

  • Download URL: near_ai_agent-1.0.0a2.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.1

File hashes

Hashes for near_ai_agent-1.0.0a2.tar.gz
Algorithm Hash digest
SHA256 d1a0f525c94f51d058ba07016309710c692ef0ec05f1b6a54e515acb802590b7
MD5 0f90812c1421f22e930e61084dacf24e
BLAKE2b-256 fec7c19423a2ee389751759b42a99ee70d52f13cfe85e1eb52dec854b65ca43a

See more details on using hashes here.

File details

Details for the file near_ai_agent-1.0.0a2-py3-none-any.whl.

File metadata

File hashes

Hashes for near_ai_agent-1.0.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 265637a2d8c5f4a47c5218c40dd85d6ae66d1bca637740c7da8b6713df174fd0
MD5 d6171bfce10a319f91ab7a7071992b2e
BLAKE2b-256 b3e23040383b82b0876e70629ab31c6b8eeb6a62b14d4c1d3c601b3844058f18

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