Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

17 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Python AI Agent

A powerful, extensible AI agent built in Python that can use various tools to help you with calculations, web searches, file operations, and more. The agent supports both OpenAI and Anthropic (Claude) language models.

Features

  • ๐Ÿค– Multi-Provider LLM Support: Works with both OpenAI GPT and Anthropic Claude
  • ๐Ÿ”ง Extensible Tool System: Easy to add new tools and capabilities
  • ๐Ÿงฎ Built-in Tools:
    • Calculator for mathematical operations
    • Web search for finding information online
    • File operations for reading and writing files
  • ๐Ÿ’ฌ Interactive CLI: User-friendly command-line interface
  • ๐Ÿ”’ Security: File operations with path validation and size limits
  • ๐Ÿ“š Conversation Memory: Maintains context across the conversation

Quick Start

1. Installation

# Clone or download the project
cd python-ai-agent

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate  # On macOS/Linux
# or
venv\Scripts\activate     # On Windows

# Install dependencies
pip install -r requirements.txt

2. Configuration

Copy the example environment file and add your API keys:

cp .env.example .env

Edit .env file and add your API key:

# Choose your preferred provider
LLM_PROVIDER=anthropic  # or "openai"

# Add your API key
ANTHROPIC_API_KEY=your_anthropic_api_key_here
# OR
OPENAI_API_KEY=your_openai_api_key_here

# Optional: Customize models
ANTHROPIC_MODEL=claude-3-sonnet-20240229
OPENAI_MODEL=gpt-4-turbo-preview

3. Running the Agent

# Start the interactive agent
python main.py

# Or with specific options
python main.py --provider anthropic --verbose

Getting API Keys

Anthropic Claude API Key

  1. Go to Anthropic Console
  2. Sign up or log in
  3. Navigate to API Keys section
  4. Create a new API key
  5. Copy the key to your .env file

OpenAI API Key

  1. Go to OpenAI Platform
  2. Sign up or log in
  3. Navigate to API Keys section
  4. Create a new API key
  5. Copy the key to your .env file

Usage Examples

Basic Conversation

๐Ÿ‘ค You: Hello! What can you help me with?
๐Ÿค– Assistant: Hi! I'm an AI assistant with access to several useful tools...

Calculator

๐Ÿ‘ค You: What's 15% of 240?
๐Ÿค– Assistant: I'll calculate 15% of 240 for you.
[Uses calculator tool]
The result is 36.

Web Search

๐Ÿ‘ค You: What's the latest news about AI?
๐Ÿค– Assistant: I'll search for the latest AI news for you.
[Uses web search tool]
Here are some recent AI developments...

File Operations

๐Ÿ‘ค You: Create a file called notes.txt with "Hello World"
๐Ÿค– Assistant: I'll create that file for you.
[Uses file operations tool]
Successfully created notes.txt with your content.

Available Commands

In the interactive mode, you can use these special commands:

  • exit, quit, bye - Exit the agent
  • clear - Clear conversation history
  • tools - List available tools
  • help - Show help message

Command Line Options

python main.py [options]

Options:
  --provider {anthropic,openai}  LLM provider to use
  --model MODEL                  Model to use
  --verbose                      Enable verbose logging
  --help                         Show help message

Project Structure

python-ai-agent/
โ”œโ”€โ”€ docs/
โ”‚   โ””โ”€โ”€ implementation-plan.md     # Development plan and progress
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ agent.py                   # Main Agent class
โ”‚   โ”œโ”€โ”€ llm_client.py             # LLM client wrapper
โ”‚   โ”œโ”€โ”€ config.py                 # Configuration management
โ”‚   โ””โ”€โ”€ tools/
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ base.py               # Base tool classes
โ”‚       โ”œโ”€โ”€ calculator.py         # Calculator tool
โ”‚       โ”œโ”€โ”€ web_search.py         # Web search tool
โ”‚       โ””โ”€โ”€ file_ops.py           # File operations tool
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ test_agent.py
โ”‚   โ”œโ”€โ”€ test_tools.py
โ”‚   โ””โ”€โ”€ test_integration.py
โ”œโ”€โ”€ main.py                       # CLI interface
โ”œโ”€โ”€ requirements.txt              # Python dependencies
โ”œโ”€โ”€ .env.example                  # Environment variables template
โ”œโ”€โ”€ .gitignore                    # Git ignore file
โ””โ”€โ”€ README.md                     # This file

Adding New Tools

To add a new tool, follow these steps:

  1. Create a new file in src/tools/ (e.g., weather.py)
  2. Inherit from the Tool base class
  3. Implement the required methods
  4. Register the tool in src/agent.py

Example:

from .base import Tool

class WeatherTool(Tool):
    def __init__(self):
        super().__init__(
            name="weather",
            description="Get current weather information for a location"
        )
    
    def execute(self, location: str) -> str:
        # Implement weather API call
        return f"Weather for {location}: ..."
    
    def get_schema(self) -> dict:
        return {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City or location name"
                }
            },
            "required": ["location"]
        }

Security Features

  • File Path Validation: File operations are restricted to safe paths
  • File Size Limits: Maximum 1MB file size for safety
  • Extension Filtering: Only allowed file types can be processed
  • Safe Expression Evaluation: Calculator uses AST parsing for safety

Troubleshooting

Common Issues

  1. API Key Error: Make sure your .env file has the correct API key
  2. Import Errors: Ensure you're in the virtual environment and dependencies are installed
  3. Permission Errors: For file operations, check file/directory permissions

Debug Mode

Run with --verbose flag to see detailed error messages:

python main.py --verbose

Development

Running Tests

# Run all tests
pytest tests/

# Run specific test file
pytest tests/test_tools.py

# Run with coverage
pytest --cov=src tests/

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

License

This project is open source. Feel free to use and modify as needed.

Support

For issues or questions:

  1. Check the troubleshooting section
  2. Review the implementation plan in docs/
  3. Create an issue with detailed information

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages