-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.js
190 lines (166 loc) · 5.53 KB
/
example.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict'
const IntentoConnector = require('./src/index')
// Quickly load .env files into the environment
// The next line requires `dotenv` installed (`yarn add -D dotenv` or `npm install --save-dev dotenv`)
//require('dotenv').config()
const apikey = process.env.INTENTO_API_KEY
const google_api_key = process.env.GOOGLE_API_KEY
if (!google_api_key) {
console.warn('Missing Google api key. Some examples might not work.')
}
const client = new IntentoConnector({ apikey })
if (client.error) {
console.error(client.error)
if (!apikey) {
console.error(
'Consider one of the options https://github.com/intento/intento-nodejs#how-to-pass-your-api-keys-to-your-environment'
)
}
process.exit(1)
}
// This is an intent to translate text from one language to another.
// More on that in the documentation here https://github.com/intento/intento-api/blob/master/ai.text.translate.md
client.ai.text.translate
.fulfill({ text: "How's it going?", to: 'es' })
.then(defaultCallback)
.catch(prettyCatch)
// This is an intent to analyze the sentiment of the provided text.
// More on that in the documentation here https://github.com/intento/intento-api/blob/master/ai.text.sentiment.md
client.ai.text.sentiment
.fulfill({
text: 'We love this place',
lang: 'en',
provider: 'ai.text.sentiment.ibm.natural_language_understanding',
})
.then(defaultCallback)
.catch(prettyCatch)
// This is an intent to get meanings of text in selected language.
// More on that in the documentation here https://github.com/intento/intento-api/blob/master/ai.text.dictionary.md
client.ai.text.dictionary
.fulfill({
text: 'meaning',
from: 'en',
to: 'ru',
})
.then(data => {
console.log(
'Dictionary results:\n',
JSON.stringify(data, null, 4),
'\n'
)
})
/* Explore providers */
// Documentation on providers:
// - Translation: https://github.com/intento/intento-api/blob/master/ai.text.translate.md#getting-available-providers
// - Sentiment analysis: https://github.com/intento/intento-api/blob/master/ai.text.sentiment.md#getting-available-providers
// - Text meanings: https://github.com/intento/intento-api/blob/master/ai.text.dictionary.md#getting-available-providers
client.ai.text.translate
.providers()
.then(prettyPrintProviders)
.catch(prettyCatch)
// Providers for sentiment analysis
client.ai.text.sentiment
.providers()
.then(prettyPrintProviders)
.catch(prettyCatch)
// Providers to get meanings of texts in selected languages
client.ai.text.dictionary
.providers()
.then(prettyPrintProviders)
.catch(prettyCatch)
// more examples with exploring providers here https://github.com/intento/intento-nodejs/blob/master/samples/server-side-app/explore-providers.js
/* Advanced usage */
// Translate with specific strategy
client.ai.text.translate
.fulfill({
text: "How's it going?",
to: 'pt',
routing: 'best_price',
})
.then(data => {
console.log('\nTranslate with a "best_price" strategy \n')
defaultCallback(data)
})
.catch(prettyCatch)
/* Advanced general examples */
client
.makeRequest({
path: '/ai/text/translate',
method: 'POST',
content: {
context: {
text: 'A sample text',
to: 'es',
},
service: {
provider: 'ai.text.translate.microsoft.translator_text_api.2-0',
},
},
})
.then(data => {
console.log('Basic usage example\n')
defaultCallback(data)
})
.catch(prettyCatch)
// With custom google api key
client
.makeRequest({
path: '/ai/text/translate',
method: 'POST',
content: {
context: {
text: 'A sample text!',
to: 'es',
},
service: {
provider: 'ai.text.translate.google.translate_api.2-0',
auth: {
'ai.text.translate.google.translate_api.2-0': [
{ key: `${google_api_key}` },
],
},
},
},
})
.then(data => {
console.log('Example with your google api key\n')
defaultCallback(data)
})
.catch(prettyCatch)
// One can pass request parameters as raw json specified as a `data` parameter
// Make sure your json is valid. For example one can validate json online here https://jsonformatter.curiousconcept.com/
client
.makeRequest({
path: '/ai/text/translate',
method: 'POST',
data: `{
"context": {
"text": "Validation is the king",
"to": "es"
},
"service": {
"provider": "ai.text.translate.microsoft.translator_text_api.2-0"
}
}`,
})
.then(defaultCallback)
.catch(prettyCatch)
/* helpers */
function defaultCallback(data) {
console.log('API response:\n', JSON.stringify(data, null, 4), '\n\n')
}
function prettyPrintProviders(data) {
if (!data || !data.length) {
console.log(`\nThere are no providers.`)
} else if (data.length === 1) {
console.log(`\nThere is one provider:`)
} else {
console.log(`\nThere are overall ${data.length} providers:`)
}
data.forEach((p, i) => console.log(` ${i + 1}. ${p.name}`))
console.log('\n\n')
}
function prettyCatch(errorResponse) {
console.log('\nError: ' + errorResponse.message)
console.log('\n\n')
}