|
| 1 | +# Sentrius Python Agent |
| 2 | + |
| 3 | +This Python agent provides the same APIs and operations as the Java agent, enabling integration with the Sentrius platform for authentication, registration, heartbeat monitoring, and provenance event submission. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Keycloak Integration**: Full authentication support using Keycloak JWT tokens |
| 8 | +- **Agent Registration**: Automatic registration with the Sentrius API server |
| 9 | +- **Heartbeat Monitoring**: Continuous heartbeat mechanism to maintain connection |
| 10 | +- **Provenance Events**: Submit detailed provenance events for audit trails |
| 11 | +- **RSA Encryption**: Secure communication using ephemeral RSA keys |
| 12 | +- **Configurable**: Support for both YAML configuration files and environment variables |
| 13 | +- **Extensible**: Base agent framework for creating custom agents |
| 14 | + |
| 15 | +## Architecture |
| 16 | + |
| 17 | +The Python agent mirrors the Java agent architecture with these key components: |
| 18 | + |
| 19 | +### Services |
| 20 | +- **KeycloakService**: Handles authentication and token management |
| 21 | +- **AgentClientService**: Manages API communication with Sentrius server |
| 22 | +- **EphemeralKeyGen**: RSA key generation and cryptographic operations |
| 23 | +- **SentriusAgent**: Main agent framework coordinating all services |
| 24 | + |
| 25 | +### Agent Framework |
| 26 | +- **BaseAgent**: Abstract base class for all agents |
| 27 | +- **SQLAgent**: Example implementation for SQL operations |
| 28 | + |
| 29 | +## Configuration |
| 30 | + |
| 31 | +### YAML Configuration |
| 32 | +```yaml |
| 33 | +keycloak: |
| 34 | + server_url: "http://localhost:8080" |
| 35 | + realm: "sentrius" |
| 36 | + client_id: "python-agent" |
| 37 | + client_secret: "your-client-secret" |
| 38 | + |
| 39 | +agent: |
| 40 | + name_prefix: "python-agent" |
| 41 | + agent_type: "python" |
| 42 | + callback_url: "http://localhost:8081" |
| 43 | + api_url: "http://localhost:8080" |
| 44 | + heartbeat_interval: 30 |
| 45 | + |
| 46 | +llm: |
| 47 | + enabled: false |
| 48 | + provider: "openai" |
| 49 | + model: "gpt-3.5-turbo" |
| 50 | + api_key: null |
| 51 | + endpoint: null |
| 52 | +``` |
| 53 | +
|
| 54 | +### Environment Variables |
| 55 | +```bash |
| 56 | +KEYCLOAK_SERVER_URL=http://localhost:8080 |
| 57 | +KEYCLOAK_REALM=sentrius |
| 58 | +KEYCLOAK_CLIENT_ID=python-agent |
| 59 | +KEYCLOAK_CLIENT_SECRET=your-client-secret |
| 60 | +AGENT_NAME_PREFIX=python-agent |
| 61 | +AGENT_API_URL=http://localhost:8080 |
| 62 | +AGENT_CALLBACK_URL=http://localhost:8081 |
| 63 | +AGENT_HEARTBEAT_INTERVAL=30 |
| 64 | +``` |
| 65 | + |
| 66 | +## Usage |
| 67 | + |
| 68 | +### Running the SQL Agent |
| 69 | +```bash |
| 70 | +# With configuration file |
| 71 | +python main.py sql_agent --config config.yaml |
| 72 | + |
| 73 | +# With default configuration (uses environment variables) |
| 74 | +python main.py sql_agent |
| 75 | +``` |
| 76 | + |
| 77 | +### Creating Custom Agents |
| 78 | +```python |
| 79 | +from agents.base import BaseAgent |
| 80 | + |
| 81 | +class MyCustomAgent(BaseAgent): |
| 82 | + def __init__(self, config_path=None): |
| 83 | + super().__init__("My Custom Agent", config_path=config_path) |
| 84 | + |
| 85 | + def execute_task(self): |
| 86 | + # Your custom agent logic here |
| 87 | + self.submit_provenance( |
| 88 | + event_type="CUSTOM_TASK", |
| 89 | + details={"task": "custom_operation"} |
| 90 | + ) |
| 91 | +``` |
| 92 | + |
| 93 | +## API Operations |
| 94 | + |
| 95 | +The Python agent supports all the same API operations as the Java agent: |
| 96 | + |
| 97 | +### Agent Registration |
| 98 | +- **Endpoint**: `POST /api/v1/agent/register` |
| 99 | +- **Purpose**: Register the agent with the Sentrius API server |
| 100 | +- **Authentication**: Keycloak JWT token required |
| 101 | + |
| 102 | +### Heartbeat |
| 103 | +- **Endpoint**: `POST /api/v1/agent/heartbeat` |
| 104 | +- **Purpose**: Send periodic status updates to maintain connection |
| 105 | +- **Frequency**: Configurable (default: 30 seconds) |
| 106 | + |
| 107 | +### Provenance Submission |
| 108 | +- **Endpoint**: `POST /api/v1/agent/provenance/submit` |
| 109 | +- **Purpose**: Submit detailed provenance events for audit trails |
| 110 | +- **Data**: Event type, timestamp, agent ID, and custom details |
| 111 | + |
| 112 | +## Dependencies |
| 113 | + |
| 114 | +- `requests`: HTTP client for API communication |
| 115 | +- `PyJWT`: JWT token handling |
| 116 | +- `cryptography`: RSA key generation and encryption |
| 117 | +- `pyyaml`: YAML configuration parsing |
| 118 | +- `langchain`: LLM integration (for SQL agent) |
| 119 | + |
| 120 | +## Installation |
| 121 | + |
| 122 | +```bash |
| 123 | +pip install -r requirements.txt |
| 124 | +``` |
| 125 | + |
| 126 | +## Testing |
| 127 | + |
| 128 | +Run the test suite: |
| 129 | +```bash |
| 130 | +python tests/test_services.py |
| 131 | +``` |
| 132 | + |
| 133 | +## Security |
| 134 | + |
| 135 | +- Uses ephemeral RSA key pairs for secure communication |
| 136 | +- Validates JWT tokens using Keycloak public keys |
| 137 | +- Supports encrypted data exchange with the API server |
| 138 | +- Maintains secure token management throughout agent lifecycle |
| 139 | + |
| 140 | +## Integration with Java Ecosystem |
| 141 | + |
| 142 | +This Python agent is designed to work seamlessly with the existing Java-based Sentrius infrastructure: |
| 143 | + |
| 144 | +- Compatible with the same API endpoints |
| 145 | +- Uses identical authentication mechanisms |
| 146 | +- Submits provenance events in the same format |
| 147 | +- Supports the same agent lifecycle management |
| 148 | +- Can be launched using the same agent launcher service |
| 149 | + |
| 150 | +## Example Provenance Events |
| 151 | + |
| 152 | +The agent automatically submits various provenance events: |
| 153 | + |
| 154 | +```json |
| 155 | +{ |
| 156 | + "event_type": "AGENT_REGISTRATION", |
| 157 | + "timestamp": "2024-01-01T12:00:00.000Z", |
| 158 | + "agent_id": "python-agent-abc123", |
| 159 | + "details": { |
| 160 | + "agent_id": "python-agent-abc123", |
| 161 | + "callback_url": "http://localhost:8081", |
| 162 | + "agent_type": "python" |
| 163 | + } |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +```json |
| 168 | +{ |
| 169 | + "event_type": "SQL_QUERY_SUCCESS", |
| 170 | + "timestamp": "2024-01-01T12:01:00.000Z", |
| 171 | + "agent_id": "python-agent-abc123", |
| 172 | + "details": { |
| 173 | + "question_number": 1, |
| 174 | + "question": "What are the top 5 customers by revenue?", |
| 175 | + "response_length": 245 |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
0 commit comments