Python client to interact with the KappaML platform 🐍
This SDK provides a simple interface for creating, training, and managing online machine learning models.
Platform: https://kappaml.com API Keys: https://app.kappaml.com/api-keys API Documentation: https://api.kappaml.com/docs OpenAPI Schema: https://api.kappaml.com/openapi.json
pip install kappamlimportasynciofromkappamlimportKappaMLasyncdefmain():
# Initialize client with your API keyasyncwithKappaML(api_key="your_api_key") asclient:
# Create and deploy a modelmodel_id=awaitclient.create_model("my-model", "regression")
# Make predictionspredictions=awaitclient.predict(
model_id, {"feature1": 1, "feature2": 2}
)
# Get model metricsmetrics=awaitclient.get_metrics(model_id)
print(f"Predictions: {predictions}")
print(f"Model metrics: {metrics}")
# Run the async functionasyncio.run(main())The SDK requires an API key for authentication. You can provide it in two ways:
- Directly in the constructor:
client=KappaML(api_key="your_api_key")- Through environment variable:
export KAPPAML_API_KEY="your_api_key"client=KappaML() # Will use KAPPAML_API_KEY env variableInitialize a new KappaML client. Using it as an async context manager ensures proper cleanup of resources.
async create_model(name: str, ml_type: str, wait_for_deployment: bool = True, timeout: int = 60) -> str
Create a new model on the KappaML platform.
name: Name of the modelml_type: Type of ML task ('regression' or 'classification')wait_for_deployment: Whether to wait for model deployment to completetimeout: Maximum time to wait for deployment in seconds Returns the model ID.
Make predictions using a deployed model.
model_id: The model IDfeatures: Dictionary of feature names and values Returns the model's predictions.
async learn(model_id: str, features: Dict[str, Any], target: Union[float, int, str]) -> Dict[str, Any]
Train the model with a new data point.
model_id: The model IDfeatures: Dictionary of feature names and valuestarget: The target value to learn from Returns the learning response.
Get current metrics for a model.
model_id: The model ID Returns the model metrics.
Delete a model.
model_id: The model ID to delete
The SDK provides specific exceptions for different error cases:
KappaMLError: Base exception for SDK errorsModelNotFoundError: Raised when a model is not foundModelDeploymentError: Raised when model deployment fails
Example:
fromkappamlimportKappaML, ModelNotFoundErrorasyncdefmain():
asyncwithKappaML() asclient:
try:
metrics=awaitclient.get_metrics("non_existent_model")
exceptModelNotFoundError:
print("Model not found!")