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
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.
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
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 2, Llama 3, Mixtral, and other models
- 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
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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mihrab_ai_agent-0.1.0.tar.gz.
File metadata
- Download URL: mihrab_ai_agent-0.1.0.tar.gz
- Upload date:
- Size: 163.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09e33d969c83e0fac94baab2f40f4174cf2206ae2c9e308655b75d1dd072ad2d
|
|
| MD5 |
4b2da405af12d7fb13b7ef7ab74a8bf5
|
|
| BLAKE2b-256 |
ebfe78f2f4ae1111f53b026106f16c695c6c665b2a3aca685d182e57847d9d1f
|
File details
Details for the file mihrab_ai_agent-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mihrab_ai_agent-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23de923810b8642f4417d0a9797354c12881ba1085111595c3a0c44ba8fffd0b
|
|
| MD5 |
3761d0e5c371bdf971fd61b77128dfce
|
|
| BLAKE2b-256 |
8d147dd48c20aee4ea55e7029c6fc8e8e39a6d2694615e53ac58689f6105266d
|