Skip to content

Commit

Permalink
add translate field
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Nicpon committed Jul 16, 2024
1 parent b6b6ec9 commit 7c57cc6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions examples/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import * as pg from '../dist/index.js';
const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);

async function Translate() {
const text = `The rain in Spain stays mainly in the plain`;
const sourceLang = pg.Languages.English;
const targetLang = pg.Languages.Spanish;
const text = `The rain in Spain stays mainly in the plain`;
const useThirdPartyEngine = false;

var [result, err] = await client.Translate(text, sourceLang, targetLang);
var [result, err] = await client.Translate(text, sourceLang, targetLang, useThirdPartyEngine);
if (err != null) {
console.log('ERROR:' + err.error);
return;
Expand Down
10 changes: 7 additions & 3 deletions src/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,11 +997,12 @@ export class Client {
* const client = new pg.Client('https://api.predictionguard.com', process.env.PGKEY);
*
* async function Translate() {
* const text = `The rain in Spain stays mainly in the plain`;
* const sourceLang = pg.Languages.English;
* const targetLang = pg.Languages.Spanish;
* const text = `The rain in Spain stays mainly in the plain`;
* const useThirdPartyEngine = false;
*
* var [result, err] = await client.Translate(text, sourceLang, targetLang);
* var [result, err] = await client.Translate(text, sourceLang, targetLang, useThirdPartyEngine);
* if (err != null) {
* console.log('ERROR:' + err.error);
* return;
Expand All @@ -1018,11 +1019,13 @@ export class Client {
* language of the text.
* @param {model.Languages} targetLang - targetLang represents the target
* language of the text.
* @param {boolean} useThirdPartyEngine - Use third-party translation
* engines such as OpenAI, DeepL, and Google. Defaults to false.
*
* @returns - A Promise with a Translate object and a Error
* object if the error is not null.
*/
async Translate(text: string, sourceLang: model.Languages, targetLang: model.Languages): Promise<[model.Translate, model.Error | null]> {
async Translate(text: string, sourceLang: model.Languages, targetLang: model.Languages, useThirdPartyEngine: boolean): Promise<[model.Translate, model.Error | null]> {
const zero: model.Translate = {
id: '',
object: '',
Expand All @@ -1041,6 +1044,7 @@ export class Client {
text: text,
source_lang: sourceLang,
target_lang: targetLang,
use_third_party_engine: useThirdPartyEngine,
};

const [result, err] = await this.RawDoPost('translate', body);
Expand Down
6 changes: 4 additions & 2 deletions test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,9 @@ async function testTranslateBasic() {
const text = 'The rain in Spain stays mainly in the plain';
const sourceLang = pg.Languages.English;
const targetLang = pg.Languages.Spanish;
const useThirdPartyEngine = false;

var [result, err] = await client.Translate(text, sourceLang, targetLang);
var [result, err] = await client.Translate(text, sourceLang, targetLang, useThirdPartyEngine);
if (err != null) {
assert.fail('ERROR:' + err.error);
}
Expand All @@ -764,8 +765,9 @@ async function testTranslateBadkey() {
const text = 'The rain in Spain stays mainly in the plain';
const sourceLang = pg.Languages.English;
const targetLang = pg.Languages.Spanish;
const useThirdPartyEngine = false;

var [, err] = await client.Translate(text, sourceLang, targetLang);
var [, err] = await client.Translate(text, sourceLang, targetLang, useThirdPartyEngine);
if (err == null) {
assert.fail("didn't get an error");
}
Expand Down

0 comments on commit 7c57cc6

Please sign in to comment.