muenzpraeger / salesforce-einstein-platform-apex

Apache License 2.0
88 stars 51 forks source link

This project is no longer actively maintained. The functionality and future maintenance is available via the Einstein Vision and Language Model Builder in the AppExchange.

Build Status

salesforce-einstein-platform-apex

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.

Playground

Version history

Check out the full changelog.

Prerequisites

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.

Installation

Salesforce DX - new scratch org

Clone the repo to your local file system.

git clone https://github.com/muenzpraeger/salesforce-einstein-platform-apex

run the orgInit.sh script

./orgInit

Salesforce DX - deploy to an org from your hub

Again thanks to Wade for creating this neat feature.

Deploy

Salesforce DX - deploy into developer edition or production org

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

Configuration

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. 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.

Usage examples

Creating a PredictionService

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);

Fetch all trained image datasets

Einstein_PredictionService service = new Einstein_PredictionService(Einstein_PredictionService.Types.IMAGE);
List<Einstein_Dataset> datasets = service.getDatasets();

Train an intent dataset

Einstein_PredictionService service = new Einstein_PredictionService(Einstein_PredictionService.Types.INTENT);
Einstein_Model model = service.trainDataset(datasetId, 'the dataset name', 0, 0, '';

Prediction

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, '');

Contribution

Feel free to contribute to this project via pull requests. Please read the contribution before you start working on something.

License

For licensing see the included license file.