-
Notifications
You must be signed in to change notification settings - Fork 0
API Interface
The Cognitive Engine provides a programmatic API for integration with external applications.
The API interface is located in the api/ directory and provides:
- External access to Cognitive Engine functionality
- RESTful endpoints for web applications
- WebSocket support for real-time communication
- Authentication and authorization
- Rate limiting and throttling
Main API interface providing external access to the Cognitive Engine.
Location: api/interface.py
Key Features:
- Query processing endpoint
- Configuration management
- Session management
- Authentication
- Error handling
Process a query through the Cognitive Engine.
Request:
{
"query": "What is artificial intelligence?",
"config": {
"min_iterations": 3,
"max_iterations": 50,
"confidence_threshold": 0.7
}
}Response:
{
"answer": "AI is the simulation of human intelligence...",
"confidence": 0.87,
"iterations": 5,
"reasoning_trace": "...",
"timestamp": "2024-01-01T00:00:00Z"
}Health check endpoint.
Response:
{
"status": "healthy",
"timestamp": "2024-01-01T00:00:00Z",
"version": "1.0.0"
}Update configuration.
Request:
{
"min_iterations": 5,
"max_iterations": 30,
"confidence_threshold": 0.8
}Response:
{
"status": "success",
"config": { ... }
}Get current configuration.
Response:
{
"min_iterations": 3,
"max_iterations": 50,
"confidence_threshold": 0.7
}Access memory system.
Response:
{
"episodic_count": 1000,
"pattern_count": 50,
"rule_count": 20
}Create a new session.
Request:
{
"user_id": "user123",
"metadata": {}
}Response:
{
"session_id": "session_abc123",
"created_at": "2024-01-01T00:00:00Z"
}Get session information.
Response:
{
"session_id": "session_abc123",
"created_at": "2024-01-01T00:00:00Z",
"query_count": 10,
"last_activity": "2024-01-01T01:00:00Z"
}Connect to WebSocket endpoint:
ws://localhost:8000/ws
Client to Server:
{
"type": "query",
"session_id": "optional-session-id",
"query": "Your question here",
"config": { ... }
}Server to Client:
{
"type": "response",
"answer": "Response from Cognitive Engine",
"confidence": 0.87,
"iterations": 5
}-
query: Process a query -
response: Query response -
error: Error message -
status: Status update -
memory_update: Memory change notification
Include API key in request header:
Authorization: Bearer your-api-keyUse session ID for authentication:
X-Session-ID: session_abc123Default rate limits:
- 100 requests per minute per IP
- 1000 requests per hour per IP
- 10000 requests per day per IP
Rate limit headers:
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1609459200
{
"error": {
"code": "ERROR_CODE",
"message": "Human-readable error message",
"details": "Additional details"
}
}-
INVALID_REQUEST: Malformed request -
AUTHENTICATION_FAILED: Invalid credentials -
RATE_LIMIT_EXCEEDED: Too many requests -
INTERNAL_ERROR: Server error -
CONFIGURATION_ERROR: Invalid configuration
import requests
# Process query
response = requests.post(
'http://localhost:8000/api/query',
json={'query': 'What is AI?'},
headers={'Authorization': 'Bearer your-api-key'}
)
result = response.json()
print(result['answer'])# Process query
curl -X POST http://localhost:8000/api/query \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{"query": "What is AI?"}'fetch('http://localhost:8000/api/query', {
method: 'POST',
headers: {
'Authorization': 'Bearer your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({ query: 'What is AI?' })
})
.then(response => response.json())
.then(data => console.log(data.answer));const ws = new WebSocket('ws://localhost:8000/ws');
ws.onopen = () => {
ws.send(JSON.stringify({
type: 'query',
query: 'What is AI?'
}));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'response') {
console.log(data.answer);
}
};from api.interface import APIServer
server = APIServer(
host='0.0.0.0',
port=8000,
debug=False,
enable_auth=True,
rate_limit=100
)
server.start()API_HOST=0.0.0.0
API_PORT=8000
API_DEBUG=false
API_AUTH=true
API_RATE_LIMIT=100
API_KEY=your-api-keyfrom cognitive_engine import CognitiveEngineClient
client = CognitiveEngineClient(
api_key='your-api-key',
base_url='http://localhost:8000'
)
result = client.query('What is AI?')
print(result.answer)import { CognitiveEngineClient } from 'cognitive-engine-js';
const client = new CognitiveEngineClient({
apiKey: 'your-api-key',
baseUrl: 'http://localhost:8000'
});
const result = await client.query('What is AI?');
console.log(result.answer);FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "-m", "api.interface"]services:
api:
build: .
ports:
- "8000:8000"
environment:
- API_KEY=your-api-key
- API_HOST=0.0.0.0
- API_PORT=8000apiVersion: apps/v1
kind: Deployment
metadata:
name: cognitive-engine-api
spec:
replicas: 3
selector:
matchLabels:
app: cognitive-engine-api
template:
metadata:
labels:
app: cognitive-engine-api
spec:
containers:
- name: api
image: cognitive-engine:latest
ports:
- containerPort: 8000
env:
- name: API_KEY
valueFrom:
secretKeyRef:
name: api-secrets
key: api-keyGET /api/metrics
Returns Prometheus-compatible metrics.
GET /api/health
Returns health status.
API logs to cognitive_engine.log with prefix [API].
Use HTTPS in production:
server = APIServer(
ssl_keyfile='path/to/key.pem',
ssl_certfile='path/to/cert.pem'
)Configure CORS:
server = APIServer(
cors_origins=['https://yourdomain.com']
)All inputs are validated using Pydantic models.
All outputs are sanitized to prevent XSS.
For API issues:
- Email: autobotsolution@gmail.com
- Address: Flushing MI
- Check logs:
cognitive_engine.log - Review error responses