This repository showcases how to use the Salesforce Einstein Platform API using an Apex based wrapper.
Please check the product documentation for general information about what the Salesforce Einstein Platform API is, how to use it and when it'll be available for you.
The wrapper supersedes the old wrapper for the Salesforce Einstein Vision API. Besided breaking changes compared to the old wrapper this repo contains the v2 of the API (including image-multi-label, language intent and language sentiment).
See the included Playground in action.
Check out the full changelog.
For using the wrapper you'll need to fulfill the following requirements:
Please find the detailed instructions for how to setup access to the Einstein Platform API here.
Clone the repo to your local file system.
git clone https://github.com/muenzpraeger/salesforce-einstein-platform-apex
run the orgInit.sh script
./orgInit
Again thanks to Wade for creating this neat feature.
You can use the Salesforce CLI to deploy the source into a regular Salesforce org using the Metatdata API.
Authenticate against the deployment org
sfdx force:auth:web:login -a yourOrgAlias
Create an output directory for the to be converted source
mkdir mdapi
Convert the source from Salesforce DX format to Metatdata API format
sfdx force:source:convert -r force-app -d mdapi
Deploy the source
sfdx force:mdapi:deploy -d mdapi -u yourOrgAlias
After you've added the wrapper files two steps are required:
If you went through my Trailhead project you likely went through that excercise already.
The installation adds a new Lightning App to your Salesforce org for the included Playground.
If you want to use this in production make sure that the certificate isn't shared publicly. Options are for example to create and store a custom encrypted blob, and then decrypt via code.
The foundation for everything is the PredictionService
. As the communication with the API is based on a valid OAuth2 token (see MetaMind documentation) you can initiate a new PredictionService in the following way.
Einstein_PredictionService predictionService = new Einstein_PredictionService(Einstein_PredictionService.Types.IMAGE);
This creates a new prediction service for working with images. You can switch the type so that you can reuse it for other predictions.
service.setType(Einstein_PredictionService.Types.SENTIMENT);
Einstein_PredictionService service = new Einstein_PredictionService(Einstein_PredictionService.Types.IMAGE);
List<Einstein_Dataset> datasets = service.getDatasets();
Einstein_PredictionService service = new Einstein_PredictionService(Einstein_PredictionService.Types.INTENT);
Einstein_Model model = service.trainDataset(datasetId, 'the dataset name', 0, 0, '';
You can predict images either by sending Base64, uploading a Blob or a remote (publicly available!) URL. See this example how to validate a remote URL.
Einstein_PredictionResult result = service.predictImageUrl('GeneralImageClassifier', 'yourUrl', 5, '');
The prediction for intent or sentiment is similar. Like this example for intent.
Einstein_PredictionResult result = service.predictIntent('yourModelId', 'theText', 0, '');
Feel free to contribute to this project via pull requests. Please read the contribution before you start working on something.
For licensing see the included license file.