Repository navigation
Open AGI DApp Development
This guide explains how to develop decentralized applications (DApps) using the Open-A.G.I framework and its integration capabilities.
Open-A.G.I (Open Autonomous General Intelligence) is a decentralized framework for Artificial General Intelligence that provides the infrastructure and consensus mechanisms for distributed AI systems. It combines:
- Decentralized P2P Network: Secure peer-to-peer communication
- Consensus Protocols: PBFT-based consensus mechanisms
- AI Orchestration: Multi-model AI capabilities
- Security Framework: Post-quantum cryptography and TOR integration
A decentralized application (DApp) built with Open-A.G.I leverages the framework's distributed AI capabilities to create applications that:
- Operate without central authority
- Utilize collective AI intelligence
- Maintain user privacy through TOR
- Achieve consensus across distributed nodes
Before developing a DApp with Open-A.G.I, ensure you have:
- Python 3.9 or higher
- Docker (for containerized deployment)
- Git for version control
- Node.js 16+ (for blockchain components)
- Basic understanding of REST APIs
- Familiarity with blockchain concepts
git clone https://github.com/KaseMaster/Open-A.G.I.git
cd Open-A.G.I# Install Python dependencies
pip install -r requirements.txt
# For development tools
pip install -r requirements-dev.txt
# Install Node.js dependencies for blockchain components
cd dapps/aegis-token
npm installCopy the example environment file and configure as needed:
cp .env.example .envKey environment variables:
-
NODE_ID: Unique identifier for your node -
P2P_PORT: Port for P2P communication (default: 8080) -
API_PORT: Port for API access (default: 5000) -
TOR_ENABLED: Enable TOR integration (default: false)
import asyncio
from open_agi import NetworkManager, Config
async def initialize_network():
# Configure the network
config = Config(
node_id="dapp-node-001",
p2p_port=8080,
api_port=5000,
tor_enabled=False # Set to True for anonymous communication
)
# Initialize network manager
network_manager = NetworkManager(config)
# Start the network
await network_manager.start()
return network_manager
# Run the initialization
network_manager = asyncio.run(initialize_network())async def connect_to_peers(network_manager):
# Connect to existing peers
peers = [
"peer1.openagi.network:8080",
"peer2.openagi.network:8080"
]
for peer in peers:
try:
await network_manager.connect_to_peer(peer)
print(f"Connected to {peer}")
except Exception as e:
print(f"Failed to connect to {peer}: {e}")
# Connect to peers
asyncio.run(connect_to_peers(network_manager))import requests
import json
class OpenAGIDApp:
def __init__(self, api_base_url="http://localhost:5000"):
self.api_base_url = api_base_url
def get_system_health(self):
"""Check the health of the Open-A.G.I network"""
try:
response = requests.get(f"{self.api_base_url}/health")
return response.json()
except Exception as e:
return {"error": str(e)}
def send_chat_message(self, message, session_id=None):
"""Send a message to the AI chat system"""
try:
payload = {
"message": message,
"session_id": session_id or "default_session"
}
response = requests.post(
f"{self.api_base_url}/chat",
json=payload,
headers={"Content-Type": "application/json"}
)
return response.json()
except Exception as e:
return {"error": str(e)}
def process_document(self, document_content, document_type="text"):
"""Process a document using Open-A.G.I capabilities"""
try:
payload = {
"content": document_content,
"type": document_type
}
response = requests.post(
f"{self.api_base_url}/process",
json=payload,
headers={"Content-Type": "application/json"}
)
return response.json()
except Exception as e:
return {"error": str(e)}
def get_available_models(self):
"""Get list of available AI models"""
try:
response = requests.get(f"{self.api_base_url}/models")
return response.json()
except Exception as e:
return {"error": str(e)}
# Initialize the DApp
dapp = OpenAGIDApp()
# Example usage
health = dapp.get_system_health()
print("System Health:", health)
models = dapp.get_available_models()
print("Available Models:", models)Open-A.G.I DApps can leverage the built-in web interface or create custom frontends:
- React/Vue.js Interface: Modern web UI for user interaction
- Web3 Integration: Blockchain wallet integration (MetaMask)
- Real-time Updates: WebSocket connections for live data
- Secure Authentication: JWT or certificate-based authentication
The backend consists of several integrated services:
- Network Manager: Handles P2P connections and consensus
- AI Orchestrator: Manages AI models and processing
- Security Layer: Implements cryptographic security
- API Layer: Exposes RESTful endpoints for DApp integration
Your DApp can interact with Open-A.G.I through the following endpoints:
-
GET /health- Check system health -
GET /status- Get node status -
GET /peers- List connected peers -
POST /chat- Send chat messages to AI -
POST /process- Process documents -
GET /models- List available models -
POST /train- Initiate federated learning
Open-A.G.I includes blockchain components for DApp development:
// JavaScript example for interacting with AEGIS token
import { ethers } from 'ethers';
class AEGISTokenInterface {
constructor(contractAddress, provider) {
this.contractAddress = contractAddress;
this.provider = provider;
this.contract = new ethers.Contract(
contractAddress,
AEGIS_TOKEN_ABI,
provider
);
}
async getBalance(address) {
try {
const balance = await this.contract.balanceOf(address);
return ethers.utils.formatEther(balance);
} catch (error) {
console.error("Error getting balance:", error);
return "0";
}
}
async requestFromFaucet(walletAddress) {
try {
const tx = await this.contract.requestTokens(walletAddress);
await tx.wait();
return { success: true, transaction: tx.hash };
} catch (error) {
console.error("Error requesting tokens:", error);
return { success: false, error: error.message };
}
}
}// Example Solidity contract for DApp integration
pragma solidity ^0.8.19;
contract DAppRegistry {
mapping(address => bool) public registeredDApps;
mapping(address => string) public dAppMetadata;
event DAppRegistered(address indexed dappAddress, string metadata);
function registerDApp(string memory metadata) public {
registeredDApps[msg.sender] = true;
dAppMetadata[msg.sender] = metadata;
emit DAppRegistered(msg.sender, metadata);
}
function isRegisteredDApp(address dappAddress) public view returns (bool) {
return registeredDApps[dappAddress];
}
}Implement proper authentication mechanisms:
import jwt
import datetime
def generate_auth_token(user_id, permissions, secret_key):
"""Generate JWT token for API access"""
payload = {
'user_id': user_id,
'permissions': permissions,
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=24),
'iat': datetime.datetime.utcnow()
}
return jwt.encode(payload, secret_key, algorithm='HS256')
def verify_auth_token(token, secret_key):
"""Verify JWT token"""
try:
payload = jwt.decode(token, secret_key, algorithms=['HS256'])
return payload
except jwt.ExpiredSignatureError:
return None
except jwt.InvalidTokenError:
return None- Use TOR integration for anonymous communication
- Encrypt sensitive data in transit and at rest
- Implement proper access controls
- Configure firewalls to restrict access
- Use secure communication protocols (HTTPS, WSS)
- Regularly update dependencies and components
For development and production:
# Clone the repository
git clone https://github.com/KaseMaster/Open-A.G.I.git
cd Open-A.G.I
# Configure environment
cp .env.example .env
# Start all services
docker-compose up -d
# Check service status
docker-compose psFor maximum performance:
# Install dependencies
pip install -r requirements.txt
# Configure the application
python setup.py install
# Start the framework
python main.py start-dashboard --config config/app_config.jsonPlanned for future releases with Helm charts for easy deployment.
Regularly check system health:
# Run built-in health checks
./scripts/health_check.sh --verbose
# Or use API endpoint
curl http://localhost:5000/healthMonitor logs for issues:
# Application logs
tail -f /logs/application.log
# Docker logs
docker logs <container_name>
# System metrics
./scripts/monitor_system.sh- Monitor resource usage with built-in tools
- Optimize configurations based on load
- Implement auto-scaling (planned feature)
Always implement proper error handling:
import requests
from requests.exceptions import RequestException
def safe_api_call(url, method="GET", data=None):
try:
if method == "GET":
response = requests.get(url)
elif method == "POST":
response = requests.post(url, json=data)
response.raise_for_status()
return response.json()
except RequestException as e:
print(f"API request failed: {e}")
return {"error": str(e)}
except Exception as e:
print(f"Unexpected error: {e}")
return {"error": "Unexpected error occurred"}- Close connections properly
- Handle rate limiting
- Implement retry logic for transient failures
- Document your DApp's functionality
- Provide clear installation and usage instructions
- Include troubleshooting guides
Here's a complete example of a simple DApp that provides an AI chat interface:
import asyncio
import requests
import json
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
class AIChatDApp:
def __init__(self, api_base_url="http://localhost:5000"):
self.api_base_url = api_base_url
def send_message(self, message, session_id="default"):
"""Send a message to the AI and get a response"""
try:
payload = {
"message": message,
"session_id": session_id
}
response = requests.post(
f"{self.api_base_url}/chat",
json=payload,
headers={"Content-Type": "application/json"},
timeout=30
)
if response.status_code == 200:
return response.json()
else:
return {
"error": f"API returned status {response.status_code}",
"details": response.text
}
except Exception as e:
return {"error": str(e)}
# Initialize the DApp
chat_dapp = AIChatDApp()
@app.route('/')
def index():
"""Serve the main chat interface"""
return render_template('chat.html')
@app.route('/api/chat', methods=['POST'])
def chat_api():
"""API endpoint for chat messages"""
data = request.get_json()
if not data or 'message' not in data:
return jsonify({"error": "Message is required"}), 400
message = data['message']
session_id = data.get('session_id', 'default')
response = chat_dapp.send_message(message, session_id)
return jsonify(response)
@app.route('/api/health')
def health_check():
"""Health check endpoint"""
try:
response = requests.get(f"http://localhost:5000/health", timeout=5)
return jsonify({
"status": "healthy",
"open_agi_status": response.json()
})
except Exception as e:
return jsonify({
"status": "degraded",
"error": str(e)
}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8090, debug=True)And the corresponding HTML template (templates/chat.html):
<!DOCTYPE html>
<html>
<head>
<title>Open-A.G.I Chat DApp</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.chat-container { max-width: 800px; margin: 0 auto; }
.messages { border: 1px solid #ccc; height: 400px; overflow-y: scroll; padding: 10px; margin-bottom: 10px; }
.message { margin-bottom: 10px; padding: 8px; border-radius: 4px; }
.user-message { background-color: #e3f2fd; text-align: right; }
.ai-message { background-color: #f5f5f5; }
.input-area { display: flex; }
.input-area input { flex: 1; padding: 10px; margin-right: 10px; }
.input-area button { padding: 10px 20px; }
.status { text-align: center; color: #666; margin-bottom: 10px; }
</style>
</head>
<body>
<div class="chat-container">
<h1>Open-A.G.I Chat DApp</h1>
<div class="status" id="status">Connecting...</div>
<div class="messages" id="messages"></div>
<div class="input-area">
<input type="text" id="messageInput" placeholder="Type your message here..." />
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script>
const messagesDiv = document.getElementById('messages');
const messageInput = document.getElementById('messageInput');
const statusDiv = document.getElementById('status');
// Check system health
async function checkHealth() {
try {
const response = await fetch('/api/health');
const data = await response.json();
if (data.status === 'healthy') {
statusDiv.textContent = 'Connected to Open-A.G.I Network';
statusDiv.style.color = 'green';
} else {
statusDiv.textContent = 'Connection issues: ' + data.error;
statusDiv.style.color = 'red';
}
} catch (error) {
statusDiv.textContent = 'Cannot connect to DApp server';
statusDiv.style.color = 'red';
}
}
// Send message to AI
async function sendMessage() {
const message = messageInput.value.trim();
if (!message) return;
// Add user message to UI
addMessage(message, 'user');
messageInput.value = '';
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message: message })
});
const data = await response.json();
if (data.error) {
addMessage('Error: ' + data.error, 'ai');
} else {
addMessage(data.response || data.message || 'No response', 'ai');
}
} catch (error) {
addMessage('Error sending message: ' + error.message, 'ai');
}
}
// Add message to UI
function addMessage(text, sender) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}-message`;
messageDiv.textContent = text;
messagesDiv.appendChild(messageDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
// Handle Enter key
messageInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
sendMessage();
}
});
// Initialize
checkHealth();
setInterval(checkHealth, 30000); // Check health every 30 seconds
</script>
</body>
</html>- Verify that Open-A.G.I is running
- Check port configurations
- Ensure firewall settings allow connections
- Confirm TOR settings if enabled
- Verify API keys or tokens
- Check authentication headers
- Ensure proper permissions
- Monitor system resources
- Check network connectivity
- Review logs for errors or warnings
- Verify wallet connection
- Check contract addresses
- Ensure sufficient token balance
- Confirm network connectivity
The Open-A.G.I DApp development platform is continuously evolving with planned enhancements:
- Kubernetes Support: Helm charts for easy deployment
- Advanced Monitoring: Grafana dashboards for system metrics
- Auto-scaling: Automatic node scaling based on load
- Enhanced Security: More sophisticated TOR controls
- Performance Optimization: Integration with Open-A.G.I's performance optimizer
- Fork the Open-A.G.I repository from https://github.com/KaseMaster/Open-A.G.I
- Create a feature branch for your DApp
- Implement your DApp following the guidelines
- Add tests and documentation
- Submit a pull request