Skip to main content

A flexible framework for building and deploying LLM-powered agents with multiple provider support, inspired by the mihrab that guides prayer in a mosque

Project description

MihrabAI

MihrabAI A flexible and extensible framework for building AI agents powered by large language models (LLMs). Like the mihrab that guides prayer in a mosque, this framework provides direction and guidance through seamless integration with multiple LLM providers, intelligent provider fallback, and memory-enabled agents.

License: MIT Python 3.9+

Features

  • Multi-Provider Support: Seamlessly integrate with OpenAI, Anthropic, Groq, and other LLM providers through a unified interface
  • Automatic Fallback: Gracefully handle API errors with configurable fallback strategies
  • Cost Optimization: Intelligently select providers based on cost, performance, or reliability
  • Memory-Enabled Agents: Build agents with persistent memory for contextual conversations
  • Streaming Support: Stream responses for real-time interaction
  • Tool Integration: Easily extend agents with custom tools and functions
  • Provider Statistics: Track usage, costs, and performance across providers

New in Version 0.2.0

  • Command-Line Interface: Interact with agents directly from the terminal
  • Memory Task Agents: Enhanced agents with built-in memory capabilities
  • Improved Factory Functions: Easily create specialized agents with a single function call
  • Extended Provider Support: Better integration with more LLM providers, including Groq
  • Enhanced Documentation: More examples and clearer usage instructions
  • Groq Integration: Full support for Groq's high-performance LLM models, including Llama 3

Installation

pip install mihrab-ai-agent

Quick Start

Basic Agent

import asyncio
import os
from mihrabai.core.agent import SimpleAgent
from mihrabai.models import create_model
from mihrabai.runtime.runner import AgentRunner

async def main():
    # Create a model using OpenAI
    model = await create_model(
        provider_name="openai",
        model_name="gpt-3.5-turbo",
        api_key=os.environ.get("OPENAI_API_KEY")
    )
    
    # Create a simple agent
    agent = SimpleAgent(model=model)
    
    # Create a runner
    runner = AgentRunner(agent=agent)
    
    # Run a conversation
    response = await runner.run("Hello, who are you?")
    print(response.content)

if __name__ == "__main__":
    asyncio.run(main())

Multi-Provider Agent

import asyncio
import os
from mihrabai.models.multi_provider import MultiProviderModel, OptimizationStrategy
from mihrabai.core.agent import SimpleAgent
from mihrabai.runtime.runner import AgentRunner

async def main():
    # Create a multi-provider model
    model = await MultiProviderModel.create(
        primary_model="gpt-3.5-turbo",
        fallback_models=["claude-3-sonnet", "llama2-70b-4096"],
        required_capabilities={"chat"},
        optimize_for=OptimizationStrategy.COST,
        api_keys={
            "openai": os.environ.get("OPENAI_API_KEY"),
            "anthropic": os.environ.get("ANTHROPIC_API_KEY"),
            "groq": os.environ.get("GROQ_API_KEY")
        }
    )
    
    # Create an agent with the multi-provider model
    agent = SimpleAgent(model=model)
    
    # Create a runner
    runner = AgentRunner(agent=agent)
    
    # Run a conversation
    response = await runner.run("What's the capital of Morocco?")
    print(f"Response from {model.current_provider}: {response.content}")

if __name__ == "__main__":
    asyncio.run(main())

Memory-Enabled Agent

import asyncio
import os
from mihrabai.core.memory_task_agent import MemoryEnabledTaskAgent
from mihrabai.models import create_model
from mihrabai.runtime.memory_runner import MemoryAgentRunner

async def main():
    # Create a model
    model = await create_model(
        provider_name="anthropic",
        model_name="claude-3-sonnet",
        api_key=os.environ.get("ANTHROPIC_API_KEY")
    )
    
    # Create a memory-enabled agent
    agent = MemoryEnabledTaskAgent(
        model=model,
        system_message="You are a helpful assistant with memory like the ancient scholars of the House of Wisdom.",
        max_memory_items=50,
        memory_retrieval_count=5
    )
    
    # Create a memory runner
    runner = MemoryAgentRunner(
        agent=agent,
        memory_persistence_path="./manuscripts"
    )
    
    # Start a conversation with a session ID
    session_id = "scholar123"
    
    # First interaction
    await runner.run("My name is Hassan", session_id=session_id)
    
    # Save memory
    await runner.save_memory(session_id)
    
    # Later interaction (memory will be loaded automatically)
    response = await runner.run("What's my name?", session_id=session_id)
    print(response.content)  # Should remember the name "Hassan"

if __name__ == "__main__":
    asyncio.run(main())

Architecture

The framework is built around a modular architecture like the geometric patterns of Islamic art:

  • Core: Base classes for agents, messages, and memory
  • Models: Provider integrations and model abstractions
  • Runtime: Execution environment for agents
  • Utils: Logging, tracing, and other utilities

Supported Providers

  • OpenAI: GPT-3.5, GPT-4, and other models
  • Anthropic: Claude 3 Opus, Claude 3 Sonnet, and other models
  • Groq: Llama 3-70B, Llama 3-8B, Llama 2-70B, Mixtral, and other models
    • High-performance inference with low latency
    • Cost-effective alternative to other providers
    • See our Groq Integration Guide for details
  • Easily extensible to other providers

Advanced Usage

Custom Tools

from mihrabai.core.task_agent import TaskAgent, ToolConfig
from mihrabai.core.message import Message, MessageRole

# Define a tool function
def get_weather(params):
    location = params.get("location", "Marrakech")
    return f"The weather in {location} is sunny and 85°F"

# Create a tool configuration
weather_tool = ToolConfig(
    name="get_weather",
    description="Get the current weather for a location",
    function=get_weather,
    parameters={
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city and country, e.g. Marrakech, Morocco"
            }
        },
        "required": ["location"]
    }
)

# Create an agent with the tool
agent = TaskAgent(
    model=model,
    system_message="You are a helpful assistant that can check the weather.",
    tools=[weather_tool]
)

Provider Statistics

from mihrabai.models.provider_stats import ProviderStatsManager

# Create a stats manager
stats_manager = ProviderStatsManager()

# Record usage
stats_manager.record_request(
    provider="openai",
    model="gpt-4",
    prompt_tokens=500,
    completion_tokens=200,
    cost=0.01
)

# Get usage report
report = stats_manager.get_usage_report()
print(f"Total cost: ${report['total_cost']}")
print(f"Total tokens: {report['total_tokens']}")

Configuration

You can configure the framework using environment variables:

# API Keys
export OPENAI_API_KEY="your-openai-key"
export ANTHROPIC_API_KEY="your-anthropic-key"
export GROQ_API_KEY="your-groq-key"

# Logging
export LOG_LEVEL="INFO"
export LOG_FILE="logs/mihrabai.log"

# Runtime configuration
export MAX_RETRIES=3
export RETRY_DELAY=2
export REQUEST_TIMEOUT=30

Documentation

Comprehensive documentation is available in the src/mihrabai/docs directory:

Development

Setup

# Clone the repository
git clone https://github.com/yourusername/mihrabai.git
cd mihrabai

# Create a virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -e ".[dev]"

Running Tests

# Run all tests
pytest

# Run specific tests
pytest tests/unit/
pytest tests/integration/

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some 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 - see the LICENSE file for details.

Acknowledgments

  • Thanks to all the LLM providers for their amazing models
  • Inspired by LangChain, AutoGPT, and other agent frameworks
  • Named after the mihrab, the niche in a mosque that indicates the direction of prayer, symbolizing guidance and direction

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

mihrab_ai_agent-0.2.0.tar.gz (194.1 kB view details)

Uploaded Source

Built Distribution

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

mihrab_ai_agent-0.2.0-py3-none-any.whl (261.2 kB view details)

Uploaded Python 3

File details

Details for the file mihrab_ai_agent-0.2.0.tar.gz.

File metadata

  • Download URL: mihrab_ai_agent-0.2.0.tar.gz
  • Upload date:
  • Size: 194.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for mihrab_ai_agent-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3c8bb83049a5455c69999cc81c5588bda091f51f7350e8d51869a89fa1c0230e
MD5 3347537654f8571e3fa8b9e682f96351
BLAKE2b-256 14e4d798f21a42c08c41fb2bcba02be05743b5971b00a4d294eab46baf7d700f

See more details on using hashes here.

File details

Details for the file mihrab_ai_agent-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mihrab_ai_agent-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1086787307b3b384c25d0c6da68b4653063bb4c67f862b45e981969399e3faf5
MD5 6cee79de15740dbad192c606ffc82a22
BLAKE2b-256 be1a72b0ab414ac0c1e0d6becc293e5b9d39ebc8dbe33bf67a5b9c8f39c2956b

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