forked from oemkad/apex-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguage.apex
More file actions
80 lines (69 loc) · 3.54 KB
/
Copy pathLanguage.apex
File metadata and controls
80 lines (69 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class Language {
public static String LANGUAGE_API = 'https://api.einstein.ai/v2/language';
public static String SENTIMENT = LANGUAGE_API + '/sentiment';
public static String INTENT = LANGUAGE_API + '/intent';
public static List<Prediction> sentiment(String document, String access_token, String model) {
return apiCall(SENTIMENT, document, access_token, model);
}
public static List<Prediction> intent(String document, String access_token, String model) {
return apiCall(INTENT, document, access_token, model);
}
private static List<Prediction> apiCall(String endpoint, String document, String access_token, String model) {
string contentType = HttpFormBuilder.GetContentType();
// Compose the form
string form64 = '';
form64 += HttpFormBuilder.WriteBoundary();
form64 += HttpFormBuilder.WriteBodyParameter('modelId', EncodingUtil.urlEncode(model, 'UTF-8'));
form64 += HttpFormBuilder.WriteBoundary();
form64 += HttpFormBuilder.WriteBodyParameter('document', document);
form64 += HttpFormBuilder.WriteBoundary(null);
blob formBlob = EncodingUtil.base64Decode(form64);
string contentLength = string.valueOf(formBlob.size());
// Compose the http request
HttpRequest httpRequest = new HttpRequest();
httpRequest.setBodyAsBlob(formBlob);
httpRequest.setHeader('Connection', 'keep-alive');
httpRequest.setHeader('Content-Length', contentLength);
httpRequest.setHeader('Content-Type', contentType);
httpRequest.setMethod('POST');
httpRequest.setTimeout(120000);
httpRequest.setHeader('Authorization','Bearer ' + access_token);
httpRequest.setEndpoint(endpoint);
Http http = new Http();
List<Prediction> predictions = new List<Prediction>();
try {
HTTPResponse res = http.send(httpRequest);
if (res.getStatusCode() == 200) {
System.JSONParser parser = System.JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'probabilities')) {
parser.nextToken();
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
while (parser.nextToken() != null) {
// Advance to the start object marker to
// find next probability object.
if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
// Read entire probability object
Prediction probability = (Prediction)parser.readValueAs(Prediction.class);
predictions.add(probability);
}
}
}
break;
}
}
}
System.debug(predictions);
//System.debug(res.toString());
//System.debug('STATUS:'+res.getStatus());
//System.debug('STATUS_CODE:'+res.getStatusCode());
} catch(System.CalloutException e) {
System.debug('ERROR:' + e);
}
return(predictions);
}
public class Prediction {
public String label {get;set;}
public Double probability {get;set;}
}
}