Skip to content

Commit 7656af4

Browse files
ai21 - text improvement & segmentation, grammar and answer
1 parent 6bd850b commit 7656af4

11 files changed

+374
-6
lines changed

contextualAnswers.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class ContextualAnswer {
2+
access_token: string;
3+
4+
constructor(access_token: string) {
5+
this.access_token = access_token;
6+
}
7+
8+
getContextualAnswer = async (context:string, question: string) => {
9+
try {
10+
// Make the GET request using fetch
11+
const response = await fetch("https://api.ai21.com/studio/v1/answer", {
12+
headers: {
13+
"Authorization": `Bearer ${this.access_token}`,
14+
"Content-Type": "application/json"
15+
},
16+
body: JSON.stringify({
17+
"context": context,
18+
"question": question
19+
}),
20+
method: "POST"
21+
});
22+
23+
// Parse the JSON response
24+
const data = await response.json();
25+
// Return the data
26+
return data;
27+
28+
} catch (error) {
29+
// Handle errors that occurred during the fetch call
30+
console.error('An error occurred while fetching data');
31+
return 'An error occurred while fetching data';
32+
}
33+
};
34+
}
35+
36+
export default ContextualAnswer;

dist/contextualAnswers.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
class ContextualAnswer {
13+
constructor(access_token) {
14+
this.getContextualAnswer = (context, question) => __awaiter(this, void 0, void 0, function* () {
15+
try {
16+
// Make the GET request using fetch
17+
const response = yield fetch("https://api.ai21.com/studio/v1/answer", {
18+
headers: {
19+
"Authorization": `Bearer ${this.access_token}`,
20+
"Content-Type": "application/json"
21+
},
22+
body: JSON.stringify({
23+
"context": context,
24+
"question": question
25+
}),
26+
method: "POST"
27+
});
28+
// Parse the JSON response
29+
const data = yield response.json();
30+
// Return the data
31+
return data;
32+
}
33+
catch (error) {
34+
// Handle errors that occurred during the fetch call
35+
console.error('An error occurred while fetching data');
36+
return 'An error occurred while fetching data';
37+
}
38+
});
39+
this.access_token = access_token;
40+
}
41+
}
42+
exports.default = ContextualAnswer;

dist/grammaticalCorrection.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
class GrammaticalCorrection {
13+
constructor(access_token) {
14+
this.getGrammaticalCorrection = (text) => __awaiter(this, void 0, void 0, function* () {
15+
try {
16+
// Make the GET request using fetch
17+
const response = yield fetch("https://api.ai21.com/studio/v1/gec", {
18+
headers: {
19+
"Authorization": `Bearer ${this.access_token}`,
20+
"Content-Type": "application/json"
21+
},
22+
body: JSON.stringify({
23+
"text": text
24+
}),
25+
method: "POST"
26+
});
27+
// Parse the JSON response
28+
const data = yield response.json();
29+
// Return the data
30+
return data;
31+
}
32+
catch (error) {
33+
// Handle errors that occurred during the fetch call
34+
console.error('An error occurred while fetching data');
35+
return 'An error occurred while fetching data';
36+
}
37+
});
38+
this.access_token = access_token;
39+
}
40+
}
41+
exports.default = GrammaticalCorrection;
42+
// import requests
43+
// fetch("https://api.ai21.com/studio/v1/paraphrase", {
44+
// headers: {
45+
// "Authorization": "Bearer YOUR_API_KEY",
46+
// "Content-Type": "application/json"
47+
// },
48+
// body: JSON.stringify({
49+
// "text": "Throughout this page, we will explore the advantages and features of the Paraphrase API."
50+
// }),
51+
// method: "POST"
52+
// });

dist/test.js

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/textImprovements.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
class TextImprovements {
13+
constructor(access_token) {
14+
this.getTextImprovements = (text, types) => __awaiter(this, void 0, void 0, function* () {
15+
try {
16+
// Make the GET request using fetch
17+
const response = yield fetch("https://api.ai21.com/studio/v1/improvements", {
18+
headers: {
19+
"Authorization": `Bearer ${this.access_token}`,
20+
"Content-Type": "application/json"
21+
},
22+
body: JSON.stringify({
23+
"text": text,
24+
"types": types
25+
}),
26+
method: "POST"
27+
});
28+
// Parse the JSON response
29+
const data = yield response.json();
30+
// Return the data
31+
return data;
32+
}
33+
catch (error) {
34+
// Handle errors that occurred during the fetch call
35+
console.error('An error occurred while fetching data');
36+
return 'An error occurred while fetching data';
37+
}
38+
});
39+
this.access_token = access_token;
40+
}
41+
}
42+
exports.default = TextImprovements;

dist/textSegmentation.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
class TextSegmentation {
13+
constructor(access_token) {
14+
this.getTextSegmentation = (text, type) => __awaiter(this, void 0, void 0, function* () {
15+
try {
16+
// Make the GET request using fetch
17+
const response = yield fetch("https://api.ai21.com/studio/v1/segmentation", {
18+
headers: {
19+
"Authorization": `Bearer ${this.access_token}`,
20+
"Content-Type": "application/json"
21+
},
22+
body: JSON.stringify({
23+
"source": text,
24+
"sourceType": type
25+
}),
26+
method: "POST"
27+
});
28+
// Parse the JSON response
29+
const data = yield response.json();
30+
// Return the data
31+
return data;
32+
}
33+
catch (error) {
34+
// Handle errors that occurred during the fetch call
35+
console.error('An error occurred while fetching data');
36+
return 'An error occurred while fetching data';
37+
}
38+
});
39+
this.access_token = access_token;
40+
}
41+
}
42+
exports.default = TextSegmentation;

grammaticalCorrection.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class GrammaticalCorrection {
2+
access_token: string;
3+
4+
constructor(access_token: string) {
5+
this.access_token = access_token;
6+
}
7+
8+
getGrammaticalCorrection = async (text:string) => {
9+
try {
10+
// Make the GET request using fetch
11+
const response = await fetch("https://api.ai21.com/studio/v1/gec", {
12+
headers: {
13+
"Authorization": `Bearer ${this.access_token}`,
14+
"Content-Type": "application/json"
15+
},
16+
body: JSON.stringify({
17+
"text": text
18+
}),
19+
method: "POST"
20+
});
21+
22+
// Parse the JSON response
23+
const data = await response.json();
24+
// Return the data
25+
return data;
26+
27+
} catch (error) {
28+
// Handle errors that occurred during the fetch call
29+
console.error('An error occurred while fetching data');
30+
return 'An error occurred while fetching data';
31+
}
32+
};
33+
}
34+
35+
export default GrammaticalCorrection;
36+
37+
// import requests
38+
39+
// fetch("https://api.ai21.com/studio/v1/paraphrase", {
40+
// headers: {
41+
// "Authorization": "Bearer YOUR_API_KEY",
42+
// "Content-Type": "application/json"
43+
// },
44+
// body: JSON.stringify({
45+
// "text": "Throughout this page, we will explore the advantages and features of the Paraphrase API."
46+
// }),
47+
// method: "POST"
48+
// });

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
"keywords": [
1515
"ai21",
1616
"artifical-intelligence",
17-
"paraphrase"
17+
"NLP",
18+
"Natural Language Processing",
19+
"paraphrase",
20+
"grammatical-correction-error",
21+
"contextual-answer",
22+
"text-improvement",
23+
"text-segmentation"
1824
],
1925
"author": "muhammad-usman-108",
2026
"license": "MIT",

test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import Paraphrase from './paraphrase';
2+
import GrammaticalCorrection from './grammaticalCorrection';
3+
import TextImprovements from './textImprovements';
4+
import TextSegmentation from './textSegmentation';
5+
import ContextualAnswer from './contextualAnswers';
6+
7+
const access_token = 'xxxxxxxxxxxxxxxxxxx';
28

3-
const access_token = 'xxxxxxxxxxxxxxxx';
49
// Usage example
5-
const tf = new Paraphrase(access_token);
10+
const paraphrase = new Paraphrase(access_token);
11+
const grammaticalCorrection = new GrammaticalCorrection(access_token);
12+
const textImprovements = new TextImprovements(access_token);
13+
const textSegmentation = new TextSegmentation(access_token);
14+
const contextualAnswer = new ContextualAnswer(access_token);
15+
16+
const context = 'In 2020 and 2021, enormous QE — approximately $4.4 trillion, or 18%, of 2021 gross domestic product (GDP) — and enormous fiscal stimulus (which has been and always will be inflationary) — approximately $5 trillion, or 21%, of 2021 GDP — stabilized markets and allowed companies to raise enormous amounts of capital. In addition, this infusion of capital saved many small businesses and put more than $2.5 trillion in the hands of consumers and almost $1 trillion into state and local coffers. These actions led to a rapid decline in unemployment, dropping from 15% to under 4% in 20 months — the magnitude and speed of which were both unprecedented. Additionally, the economy grew 7% in 2021 despite the arrival of the Delta and Omicron variants and the global supply chain shortages, which were largely fueled by the dramatic upswing in consumer spending and the shift in that spend from services to goods. Fortunately, during these two years, vaccines for COVID-19 were also rapidly developed and distributed.'
617

7-
tf.getParaphrase('I am Muhammad Usman and I am 30 years old from Pakistan').then( result => console.log(result));
18+
paraphrase.getParaphrase('I am Muhammad Usman and I am 30 years old from Pakistan').then( result => console.log(result));
19+
grammaticalCorrection.getGrammaticalCorrection('I am Muhammad Usman and I am 30 years old from Pakistan and Studing in Germay university RWTH aachen').then( result => console.log(result));
20+
textImprovements.getTextImprovements('I am Muhammad Usman and I 30 years old Pakistan Studing in Germay university RWTH aachen', ["fluency"]).then( result => console.log(result));
21+
textSegmentation.getTextSegmentation('https://www.nhs.uk/conditions/carpal-tunnel-syndrome/', 'URL').then( result => console.log(result));
22+
contextualAnswer.getContextualAnswer(context, 'Did the economy shrink?').then( result => console.log(result));

textImprovements.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class TextImprovements {
2+
access_token: string;
3+
4+
constructor(access_token: string) {
5+
this.access_token = access_token;
6+
}
7+
8+
getTextImprovements = async (text:string, types: Array<string>) => {
9+
try {
10+
// Make the GET request using fetch
11+
const response = await fetch("https://api.ai21.com/studio/v1/improvements", {
12+
headers: {
13+
"Authorization": `Bearer ${this.access_token}`,
14+
"Content-Type": "application/json"
15+
},
16+
body: JSON.stringify({
17+
"text": text,
18+
"types": types
19+
}),
20+
method: "POST"
21+
});
22+
23+
// Parse the JSON response
24+
const data = await response.json();
25+
// Return the data
26+
return data;
27+
28+
} catch (error) {
29+
// Handle errors that occurred during the fetch call
30+
console.error('An error occurred while fetching data');
31+
return 'An error occurred while fetching data';
32+
}
33+
};
34+
}
35+
36+
export default TextImprovements;

0 commit comments

Comments
 (0)