authors are vetted experts in their fields and write on topics in which they have demonstrated experience. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.
法赫德·穆纳瓦尔·汗的头像

法赫德·穆纳瓦尔·汗

Fahad是Salesforce认证人员.com Developer and web developer since 2011, including being a senior dev for clients like Honeywell.

工作经验

11

Share

Non-Salesforce应用程序开发人员可能会错过人工智能世界中隐藏的宝石.

当开发人员考虑将云用于人工智能时, 他们可能会想到IBM沃森, 微软Azure认知服务, Google Cloud, or Amazon AI. When they hear of Salesforce Einstein, they might automatically assume it’s limited to the Salesforce开发人员 specialization.

Not so! Any app, 无论是否与salesforce相关, 如何利用Salesforce收购的复杂人工智能云技术. They’ve entered the AI market with Salesforce Einstein, their own orchestration of AI cloud services. 值得注意的是,这包括提供语言和图像识别服务.

与其他人工智能云解决方案一样, 你不需要有博士学位就可以使用下面的重量级技术. 在这个Salesforce Einstein API教程中, 我将向您展示如何设置帐户并进行第一次AI云API调用. 对于呼叫本身, 我们将和cURL和Postman一起玩, but you could also your own back-end prototype or whatever other technology you’re most comfortable with.

从那里开始,天空就是极限.

创建Salesforce Einstein API帐户

要进行Einstein Platform API调用,首先需要 创建一个Einstein API帐户,下载密钥,并使用该密钥生成Salesforce OAuth令牌. The process only needs to be done once to be able to use both Einstein Vision and Einstein Language.

Salesforce Einstein lets you sign up directly, via social authentication, or via Heroku.

您可以使用Salesforce或 Heroku credentials. On selecting any of the above option, you will be redirected to their respective login pages. 如果你用Heroku登录, they require you to set up a credit card with them and attach the service to a specific Heroku instance of yours.

Heroku Einstein集成页面

如果您是Salesforce的新手,并且没有Heroku帐户, setting up an account with them is fairly quick—even quicker if you want to sign up via a preexisting social account like one with Google.

We’ll assume from here that you’re using Salesforce (via a social account or not) instead of Heroku. The process involves a bit of backtracking, so you’ll want to pay close attention to these steps.

Salesforce登录页面.

一旦你登录到Salesforce, 你会看到一个与爱因斯坦没有多大关系的屏幕. At this point, you should check your email and click their verification link; otherwise, 下一步将导致错误.

获得爱因斯坦API令牌

下一步是循环回到 最初的爱因斯坦API注册链接 然后再按一下Salesforce登录按钮. After that, you’ll set a new password—even if you created your account with the help of an external authorization partner like Google—and be redirected, again, 转到游览页面.

Now, circle back a third time to the API signup page, and click the Salesforce login button again. 这一次,您将得到如下所示的页面. 在下载您的私钥之前,请不要离开此页面,即使它可能会说你需要验证你的电子邮件! If you do, there will be no way to get your private key without manual help from their support team.

You only get one chance to download your Einstein API private key during the signup process.

You can either download the file to your local machine or copy and paste the key into a text editor and save the file as einstein_platform.pem.

Meanwhile, as mentioned, 您将收到另一封验证邮件, 这是爱因斯坦特有的. 也点击那个验证链接.

现在您有了私钥,就可以生成有时间限制的令牌了. 从创建数据集开始的每个API调用, 训练模型, 要建模预测—需要在请求标头中使用有效的OAuth令牌. 要获得令牌,你需要去他们的 令牌生成器 使用你登录时使用的邮箱地址. 粘贴或上传您在上面收到的私钥文件.

使用您的私钥生成Einstein平台服务OAuth令牌.

亲身体验Salesforce Einstein API

Using the AI cloud via Salesforce Einstein involves some basic concepts on how to train their artificial intelligence network by uploading sample data. 如果这听起来不熟悉, 我之前的教程 gives some examples of working with Salesforce Einstein—both for Einstein Language and Einstein Vision.

假设你能接受的话, 现在我们将通过cURL或Postman使用Einstein图像分类REST API. If you’re using Postman, wherever we have a cURL call, you can use Postman’s import feature:

将cURL语句导入到Postman.

Suppose you came across a useful business requirement where you want to distinguish between the smartphone and landline phone based on images and, 使用这个预测, 您想要更新您的领先分数或处理您的用例.

下一步是创建我们自己的数据集. 请注意,您需要至少40个已经分类的示例. (如果你现在不想投入更多的时间, 您可以跳到下面的预测部分. Simply use a modelId of FoodImageClassifier, GeneralImageClassifier, SceneClassifier, or MultiLabelImageClassifier.)

在我们的例子中,我们有两类:智能手机和固定电话. 我们创建两个文件夹, 给它们贴上智能手机和固定电话的标签, 并在每个文件夹中添加图像. We then create a zip file (zip only: 7z does not work, for example) containing these folders.

下面是用于创建数据集的Einstein API端点:

curl -X POST \
     -H "Authorization: Bearer " \
     -H "Cache-Control: no-cache
     -H "Content-Type: multipart/form-data" \
     -F "type=image
     -F "" \
     http://api.einstein.ai / v2 /视觉/数据/上传/同步

Here, 可以像以下任何一个例子:

  1. data = @ /道路/ /你/地方/文件.zip
  2. 路径= http://somedomain/file.zip

In Postman, without importing, you would need to fill out the header and body tabs as shown below:

在Postman中填写标题.

用邮差填写尸体.

上传所有图片需要一些时间. 假设所有图像都已成功上传,则响应将具有 datasetId (重复作为主要 id 每个类别一次),这将在以后的通话中使用.

Once your data set is uploaded, you have to train the model using the data you just uploaded. 要训练模型,使用以下调用:

curl -X POST \
     -H "Authorization: Bearer " \
     -H "Cache-Control: no-cache
     -H "Content-Type: multipart/form-data" \
     -F "name=座机和智能手机型号" \
     -F "datasetId=" \
     http://api.einstein.ai / v2 /视觉/火车

训练你的爱因斯坦视觉模型通过邮差.

Training of the data set is normally placed in their queue, and in response, we will get the modelId. After that, we can check another endpoint to know whether the model has been trained or not yet:

curl -X GET \
     -H "Authorization: Bearer " \
     -H "Cache-Control: no-cache
     http://api.einstein.ai / v2 /视觉/火车/

当模型被训练时,你会得到这样的响应:

通过Postman检查你的模型的训练进度.

使用爱因斯坦视觉进行图像预测

这是它的核心. 一旦模型被训练, 现在可以发送图像了, 模型将返回我们定义的每个类别的概率值. 对于当前型号,我们选择了iPhone X的库存图像进行预测.

对于预测本身,我们使用以下端点:

curl -X POST \
     -H "Authorization: Bearer " \
     -H "Cache-Control: no-cache
     -H "Content-Type: multipart/form-data" \
     -F "" \
     -F "modelId=" \
     http://api.einstein.ai / v2 /视觉/预测

This time, is similar to ,但使用了不同的密钥,还有第三种选择:

  1. sampleContent = @ / / /你/地方/文件路径.zip
  2. sampleLocation = http://somedomain/file.zip
  3. sampleBase64Content = iVBORw0KGgoAAAANSUhEUgAAAC0... (换句话说, 你不需要任何前缀, 就是64进制的部分, 如果你想使用这个上传方法.)

通过邮差获得爱因斯坦视觉预测.

看看截图和概率值, the model successfully predicted that the iPhone X image is classified under the smartphone category. Success!

不管用例是什么, you’ll want to explore whether Einstein Vision assumes you’re providing an image that falls into one of the categories you trained it on. In testing, 当我们给上面的模特发了一张帆船的照片时,我们发现了这一点, 它在智能手机和固定电话之间做出了最好的猜测, 而不是说两者似乎都不是. In other words, the ratings it gives for your sailboat picture being a landline or smartphone still add up to 1, 就像他们输入合法信息一样.

然而,一些预构建的模型有类似的类别 Other (for the SceneClassifier model) and UNKNOWN (for theFoodImageClassifier). So it’s worth experimenting for your particular context so you can know what to expect if you will want to feed it images that don’t fit the categories given to it.

还有“多标签”类型的模式, 它返回所有类别, 按概率排序, 假设有多个类别适用- i.e.,概率加起来不等于1. If that sounds more like what you’re doing, it would be worth looking into the newly released 爱因斯坦物体探测. Instead of just telling you what might be in an image—overall—it actually gives you bounding boxes along with the predictions. 这类似于您在Facebook上看到的自动标记, 只不过它并不局限于人脸.

Salesforce爱因斯坦语言:意图和情感

如果你想训练自己的模型, 理论上,Salesforce Einstein可以同时训练意图和情感, 但更常见的是只训练意图. 训练意图模型类似于我们上面所经历的, 但不是图像文件夹, 您提供一个两列的CSV文件, A栏为文本,B栏为相应的类别. (它们也支持TSV或JSON.)

因为训练步骤基本上是一样的, we’ll assume at this point that you’ve already trained an Einstein Intent model with the training data they supply in case_routing_intent.csv 并且可以使用爱因斯坦情感的标准预建模型.

爱因斯坦意图预测调用很简单:

curl -X POST \
     -H "Authorization: Bearer " \
     -H "Cache-Control: no-cache
     -H "Content-Type: multipart/form-data" \
     -F "modelId=" \
     -F "document=" \
    http://api.einstein.ai / v2 /语言/意图

Here, 可以是这样的,“我怎样才能得到我的货物的运单号??”

The API call is the same with Einstein Sentiment, except that you can use the pre-built modelId CommunitySentiment (注意不同的端点):

curl -X POST \
     -H "Authorization: Bearer " \
     -H "Cache-Control: no-cache
     -H "Content-Type: multipart/form-data" \
     -F "modelId=CommunitySentiment" \
     -F "document=我怎样才能得到我的货物的运单号?" \
    http://api.einstein.ai / v2 /语言/情绪

The JSON output in both cases looks exactly like the prediction response format for Einstein Image Classification: The main substance is in an array associated with the key probabilities,每个数组元素给你一个标签和一个概率. 这就是它的全部!

简单的AI与Salesforce爱因斯坦

你现在已经看到使用爱因斯坦视觉和爱因斯坦语言是多么简单, and how the Einstein APIs don’t have anything to do with the rest of the Salesforce开发人员 APIs, 除了名字. What will you 用它们来创作?

了解基本知识

  • Salesforce中的API访问是什么样的?

    Access to the AI cloud via the Salesforce Einstein API requires signing up for an account, 获取私钥, 然后用它来生成一个令牌. 从那里,令牌可以用于任何Einstein API调用.

  • 如何在Salesforce中启用API访问?

    一旦你有了Salesforce(或Heroku)账户, enabling API access (and 获取私钥) is done via the Einstein signup page at http://api.einstein.您是否计划在Salesforce应用程序或非Salesforce应用程序中使用API.

聘请Toptal这方面的专家.
Hire Now
法赫德·穆纳瓦尔·汗的头像
法赫德·穆纳瓦尔·汗

Located in 伊斯兰堡,伊斯兰堡首都地区,巴基斯坦

Member since June 14, 2016

作者简介

Fahad是Salesforce认证人员.com Developer and web developer since 2011, including being a senior dev for clients like Honeywell.

Toptalauthors are vetted experts in their fields and write on topics in which they have demonstrated experience. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.

工作经验

11

世界级的文章,每周发一次.

订阅意味着同意我们的 隐私政策

世界级的文章,每周发一次.

订阅意味着同意我们的 隐私政策

Toptal开发者

加入总冠军® community.