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.
The current version is 2.3.0 (git tag). Check out the full changelog.
For using the wrapper you'll need to fulfill the following requirements:
- Access to a Salesforce org that as minimum API version 41, i. e. a Developer Edition or a scratch org (you can signup here for free if you don't have one).
- An API account for Salesforce Einstein Platform.
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
Change into the git repo directory and create a new scratch org
sfdx force:org:create -s -f config/project-scratch-def.json
Push the source to the newly created org.
sfdx force:source:push
If you want to add the wrapper to an existing org you can either copy the contents manually from this repo.
Alternatively you can use Wade's OSS plugin for Salesforce DX.
Again thanks to Wade for creating this neat feature.
After you've added the wrapper files two steps are required:
- Set the value for Einstein EMail in Custom Settings => Einstein Settings (via the Manage button) for that org to the email address that you've used to sign up for Einstein Platform.
- Store the Einstein Platform file as File in the org. The name must be einstein_platform.
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.
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.