作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.
Fahad Munawar Khan's profile image

Fahad Munawar Khan

Fahad is a Salesforce Certified Force.自2011年以来,他一直是网站开发人员和网络开发人员,包括为霍尼韦尔等客户担任高级开发人员.

Years of Experience

11

Share

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

当开发人员考虑将云用于人工智能时, they might think of IBM Watson, Microsoft Azure Cognitive Services, Google Cloud, or Amazon AI. 当他们听说Salesforce Einstein时,他们可能会自动认为它仅限于 Salesforce developer specialization.

Not so! Any app, whether Salesforce-related or not, 如何利用Salesforce收购的复杂人工智能云技术. 他们已经通过Salesforce Einstein进入了人工智能市场,这是他们自己的人工智能云服务编排. 值得注意的是,这包括提供语言和图像识别服务.

As with other AI cloud solutions, 你不需要有博士学位就可以使用下面的重量级技术. In this Salesforce Einstein API tutorial, 我将向您展示如何设置帐户并进行第一次AI云API调用. For the calls themselves, we’ll play with cURL and Postman, 但你也可以创建自己的后端原型或任何其他你最熟悉的技术.

From there, the sky’s the limit.

Creating a Salesforce Einstein API Account

要进行Einstein Platform API调用,首先需要 create an Einstein API account,下载密钥,并使用该密钥生成Salesforce OAuth令牌. 这个过程只需要完成一次,就可以同时使用爱因斯坦视觉和爱因斯坦语言.

Salesforce Einstein允许您通过社会认证或Heroku直接注册.

You can log in using your Salesforce or Heroku credentials. 在选择上述任何一个选项后,您将被重定向到各自的登录页面. If you log in with Heroku, 他们需要你在他们那里建立一张信用卡,并将服务附加到你的特定Heroku实例上.

The Heroku Einstein integration page

如果您是Salesforce的新手,并且没有Heroku帐户, 在他们那里建立一个账户相当快——如果你想通过一个已有的社交账户注册,比如谷歌的账户,甚至会更快.

从这里开始,我们假设您使用的是Salesforce(无论是否通过社交帐户),而不是Heroku. 这个过程涉及到一些回溯,所以你需要密切关注这些步骤.

The Salesforce login page.

Once you’re logged into Salesforce, 你会看到一个与爱因斯坦没有多大关系的屏幕. At this point, you should check your email and click their verification link; otherwise, the next step will result in an error.

Getting an Einstein API Token

The next step is to circle back to that initial Einstein API signup link and try the Salesforce login button again. After that, 您将设置一个新密码——即使您是在外部授权合作伙伴(如google)的帮助下创建帐户的——并被重定向, again, to the tour page.

现在,第三次转回API注册页面,再次单击Salesforce登录按钮. This time you’ll get a page as shown below. 在下载您的私钥之前,请不要离开此页面,即使它可能会说你需要验证你的电子邮件! 如果您这样做了,那么如果没有他们的支持团队的手动帮助,就无法获得您的私钥.

在注册过程中,您只有一次机会下载Einstein API私钥.

您可以将文件下载到本地机器,也可以将密钥复制并粘贴到文本编辑器中,并将文件保存为 einstein_platform.pem.

Meanwhile, as mentioned, 您将收到另一封验证邮件, this one being Einstein-specific. Click that verification link too.

现在您有了私钥,就可以生成有时间限制的令牌了. 从创建数据集开始的每个API调用, to training models, 要建模预测—需要在请求标头中使用有效的OAuth令牌. To get the token, you need to go to their token generator 使用你登录时使用的邮箱地址. 粘贴或上传您在上面收到的私钥文件.

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

Hands-on with the Salesforce Einstein API

通过Salesforce Einstein使用人工智能云涉及如何通过上传样本数据来训练他们的人工智能网络的一些基本概念. If that doesn’t sound familiar, my previous tutorial 给出了一些与Salesforce Einstein合作的例子,包括爱因斯坦语言和爱因斯坦视觉.

Assuming you’re comfortable with that, 现在我们将通过cURL或Postman使用Einstein图像分类REST API. 如果你使用的是Postman,只要我们有cURL调用,你就可以使用Postman的import特性:

Importing cURL statements into Postman.

假设您遇到了一个有用的业务需求,您希望根据图像和图像来区分智能手机和固定电话, using that predication, 您想要更新您的领先分数或处理您的用例.

The next step is creating our own data set. 请注意,您需要至少40个已经分类的示例. (如果你现在不想投入更多的时间, 您可以跳到下面的预测部分. Simply use a modelId of FoodImageClassifier, GeneralImageClassifier, SceneClassifier, or MultiLabelImageClassifier.)

在我们的例子中,我们有两类:智能手机和固定电话. We create two folders, 给它们贴上智能手机和固定电话的标签, and add images in each folder. 然后,我们创建一个包含这些文件夹的zip文件(例如,只压缩:7z不能工作).

下面是用于创建数据集的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/vision/datasets/upload/sync

Here, can be like either of these examples:

  1. data=@/path/to/your/local/file.zip
  2. path=http://somedomain/file.zip

在Postman中,如果没有导入,你需要填写标题和正文选项卡,如下所示:

Filling out the headers in Postman.

Filling out the body in Postman.

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

一旦你的数据集被上传,你必须使用你刚刚上传的数据来训练模型. 要训练模型,使用以下调用:

curl -X POST \
     -H "Authorization: Bearer " \
     -H "Cache-Control: no-cache" \
     -H "Content-Type: multipart/form-data" \
     -F "name=Landline and SmartPhone Model" \
     -F "datasetId=" \
     http://api.einstein.ai/v2/vision/train

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

训练的数据集通常放在它们的队列中,作为响应,我们将得到 modelId. 之后,我们可以检查另一个端点来知道模型是否已经训练:

curl -X GET \
     -H "Authorization: Bearer " \
     -H "Cache-Control: no-cache" \
     http://api.einstein.ai/v2/vision/train/

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

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

Using Einstein Vision for Image Prediction

Here’s the heart of it. Once the model is trained, you can now send an image, 模型将返回我们定义的每个类别的概率值. 对于当前型号,我们选择了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/vision/predict

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

  1. sampleContent=@/path/to/your/local/file.zip
  2. sampleLocation=http://somedomain/file.zip
  3. sampleBase64Content = iVBORw0KGgoAAAANSUhEUgAAAC0... (In other words, you don’t need any prefix, just the raw base 64 part, if you want to use this upload method.)

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

看看截图和概率值, 该模型成功预测出iPhone X的图像属于智能手机类别. Success!

Whatever your use case, 你需要探索爱因斯坦视觉是否假设你提供的图像属于你训练它的类别之一. In testing, 当我们给上面的模特发了一张帆船的照片时,我们发现了这一点, 它在智能手机和固定电话之间做出了最好的猜测, 而不是说两者似乎都不是. In other words, 如果你的帆船照片是固定电话或智能手机拍摄的,它给你的评分加起来仍然是1, just like they would with legitimate input.

然而,一些预构建的模型有类似的类别 Other (for the SceneClassifier model) and UNKNOWN (for theFoodImageClassifier). 所以在你的特定环境下进行实验是值得的,这样你就可以知道如果你想给它提供不符合给定类别的图像会发生什么.

还有“多标签”类型的模式, which returns all categories, sorted by probability, 假设有多个类别适用- i.e., the probabilities do not add up to 1. 如果这听起来更像是你正在做的事情,那么值得看看新发布的版本 Einstein Object Detection. 它不只是告诉你图像中可能有什么,而是在给出预测的同时给出边界框. 这类似于您在Facebook上看到的自动标记, except that it’s not limited to faces.

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

If you want to train your own model, 理论上,Salesforce Einstein可以同时训练意图和情感, 但更常见的是只训练意图. 训练意图模型类似于我们上面所经历的, but instead of folders of images, you supply a two-column CSV file, A栏为文本,B栏为相应的类别. (They also support TSV or JSON.)

因为训练步骤基本上是一样的, 在这一点上,我们假设你已经用他们提供的训练数据训练了一个爱因斯坦意图模型 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/language/intent

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

API调用与Einstein Sentiment相同,只是您可以使用预先构建的modelId CommunitySentiment (and note the different endpoint):

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/language/sentiment

这两种情况下的JSON输出看起来与Einstein Image Classification的预测响应格式完全相同:主要内容位于与键相关的数组中 probabilities,每个数组元素给你一个标签和一个概率. That’s all there is to it!

Easy AI with Salesforce Einstein

你现在已经看到使用爱因斯坦视觉和爱因斯坦语言是多么简单, 爱因斯坦api与Salesforce其他开发者api没有任何关系, except in name. What will you create with them?

Understanding the basics

  • What is API access like in Salesforce?

    通过Salesforce Einstein API访问人工智能云需要注册一个帐户, obtaining a private key, and using it to generate a token. 从那里,令牌可以用于任何Einstein API调用.

  • How do I enable API access in Salesforce?

    一旦你有了Salesforce(或Heroku)账户, 启用API访问(并获取私钥)可以通过Einstein注册页面http://api完成.einstein.您是否计划在Salesforce应用程序或非Salesforce应用程序中使用API.

Hire a Toptal expert on this topic.
Hire Now
Fahad Munawar Khan's profile image
Fahad Munawar Khan

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

Member since June 14, 2016

About the author

Fahad is a Salesforce Certified Force.自2011年以来,他一直是网站开发人员和网络开发人员,包括为霍尼韦尔等客户担任高级开发人员.

Toptal作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.

Years of Experience

11

World-class articles, delivered weekly.

Subscription implies consent to our privacy policy

World-class articles, delivered weekly.

Subscription implies consent to our privacy policy

Toptal Developers

Join the Toptal® community.