/
classify_summarize.js
64 lines (54 loc) · 2.23 KB
/
classify_summarize.js
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
//Do not share this token
//TODO input your own API token
const TOKEN = ''
// this is where we call the API to classify our unlabeled text based on the training data we pass in as a prompt
function classify(s_args, s_input) {
// we start by extracting the training data from the specified range in s_args and packaging it into a prompt
let inputs = [s_input];
let examples = [];
for (let i = 0; i< s_args.length; i++) {
//this is assuming label is always the 2nd parameter
examples.push({"text" : s_args[i][0] , "label" : s_args[i][1]})
}
const payload_for_classification = {
examples,
inputs,
"model" : "embed-english-v2.0",
};
const options = {
'headers' : { 'Authorization' : 'Bearer ' + TOKEN },
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(payload_for_classification)
};
const response = UrlFetchApp.fetch('https://api.cohere.ai/v1/classify', options);
const responseContent = response.getContentText();
const responseContentText = JSON.parse(response.getContentText());
const classificationContent = responseContentText.classifications[0];
const ourPrediction = classificationContent.prediction;
const confidence = (classificationContent.confidence * 100).toFixed(2) + "%";
let displayString = "Prediction: " + ourPrediction.toUpperCase() + " review\n" + "Confidence Level: " + confidence + "\n";
return displayString;
}
// this is where we call the API to summarize the review
function summarize(cell_value) {
let raw = JSON.stringify({
"message": "Summarize this in about 10 words: " + cell_value,
"temperature": 0.2,
"model": "command-r",
});
let requestOptions = {
'method': 'post',
'muteHttpExceptions': true,
'contentType': 'application/json',
'headers': {
'Authorization': 'Bearer ' + TOKEN
},
'payload': raw,
redirect: 'follow'
};
let response = UrlFetchApp.fetch("https://api.cohere.ai/v1/chat", requestOptions)
let responseContentTxt = JSON.parse(response.getContentText());
let summarizedTxt = "SUMMARY: " + responseContentTxt.text + "\n";
return summarizedTxt;
}