File: /home/cafsindia/ai_allyindian_com/node_modules/sarvamai/api/resources/text/client/Client.js
"use strict";
/**
* This file was auto-generated by Fern from our API Definition.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Text = void 0;
const environments = __importStar(require("../../../../environments"));
const core = __importStar(require("../../../../core"));
const SarvamAI = __importStar(require("../../../index"));
const url_join_1 = __importDefault(require("url-join"));
const errors = __importStar(require("../../../../errors/index"));
class Text {
constructor(_options = {}) {
this._options = _options;
}
/**
* **Translation** converts text from one language to another while preserving its meaning.
* For Example: **'मैं ऑफिस जा रहा हूँ'** translates to **'I am going to the office'** in English, where the script and language change, but the original meaning remains the same.
*
* Available languages:
* - **`bn-IN`**: Bengali
* - **`en-IN`**: English
* - **`gu-IN`**: Gujarati
* - **`hi-IN`**: Hindi
* - **`kn-IN`**: Kannada
* - **`ml-IN`**: Malayalam
* - **`mr-IN`**: Marathi
* - **`od-IN`**: Odia
* - **`pa-IN`**: Punjabi
* - **`ta-IN`**: Tamil
* - **`te-IN`**: Telugu
*
* ### Newly added languages:
* - **`as-IN`**: Assamese
* - **`brx-IN`**: Bodo
* - **`doi-IN`**: Dogri
* - **`kok-IN`**: Konkani
* - **`ks-IN`**: Kashmiri
* - **`mai-IN`**: Maithili
* - **`mni-IN`**: Manipuri (Meiteilon)
* - **`ne-IN`**: Nepali
* - **`sa-IN`**: Sanskrit
* - **`sat-IN`**: Santali
* - **`sd-IN`**: Sindhi
* - **`ur-IN`**: Urdu
*
* For hands-on practice, you can explore the notebook tutorial on [Translate API Tutorial](https://github.com/sarvamai/sarvam-ai-cookbook/blob/main/notebooks/translate/Translate_API_Tutorial.ipynb).
*
* @param {SarvamAI.TranslationRequest} request
* @param {Text.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link SarvamAI.BadRequestError}
* @throws {@link SarvamAI.ForbiddenError}
* @throws {@link SarvamAI.UnprocessableEntityError}
* @throws {@link SarvamAI.TooManyRequestsError}
* @throws {@link SarvamAI.InternalServerError}
*
* @example
* await client.text.translate({
* input: "input",
* source_language_code: "auto",
* target_language_code: "bn-IN"
* })
*/
translate(request, requestOptions) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_b = (_a = (yield core.Supplier.get(this._options.baseUrl))) !== null && _a !== void 0 ? _a : (yield core.Supplier.get(this._options.environment))) !== null && _b !== void 0 ? _b : environments.SarvamAIEnvironment.Production, "translate"),
method: "POST",
headers: Object.assign(Object.assign({ "X-Fern-Language": "JavaScript", "X-Fern-SDK-Name": "sarvamai", "X-Fern-SDK-Version": "0.1.5-a1", "User-Agent": "sarvamai/0.1.5-a1", "X-Fern-Runtime": core.RUNTIME.type, "X-Fern-Runtime-Version": core.RUNTIME.version }, (yield this._getCustomAuthorizationHeaders())), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers),
contentType: "application/json",
requestType: "json",
body: request,
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
});
if (_response.ok) {
return _response.body;
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new SarvamAI.BadRequestError(_response.error.body);
case 403:
throw new SarvamAI.ForbiddenError(_response.error.body);
case 422:
throw new SarvamAI.UnprocessableEntityError(_response.error.body);
case 429:
throw new SarvamAI.TooManyRequestsError(_response.error.body);
case 500:
throw new SarvamAI.InternalServerError(_response.error.body);
default:
throw new errors.SarvamAIError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.SarvamAIError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.SarvamAITimeoutError("Timeout exceeded when calling POST /translate.");
case "unknown":
throw new errors.SarvamAIError({
message: _response.error.errorMessage,
});
}
});
}
/**
* Identifies the language (e.g., en-IN, hi-IN) and script (e.g., Latin, Devanagari) of the input text, supporting multiple languages.
*
* @param {SarvamAI.LanguageIdentificationRequest} request
* @param {Text.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link SarvamAI.BadRequestError}
* @throws {@link SarvamAI.ForbiddenError}
* @throws {@link SarvamAI.UnprocessableEntityError}
* @throws {@link SarvamAI.TooManyRequestsError}
* @throws {@link SarvamAI.InternalServerError}
*
* @example
* await client.text.identifyLanguage({
* input: "input"
* })
*/
identifyLanguage(request, requestOptions) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_b = (_a = (yield core.Supplier.get(this._options.baseUrl))) !== null && _a !== void 0 ? _a : (yield core.Supplier.get(this._options.environment))) !== null && _b !== void 0 ? _b : environments.SarvamAIEnvironment.Production, "text-lid"),
method: "POST",
headers: Object.assign(Object.assign({ "X-Fern-Language": "JavaScript", "X-Fern-SDK-Name": "sarvamai", "X-Fern-SDK-Version": "0.1.5-a1", "User-Agent": "sarvamai/0.1.5-a1", "X-Fern-Runtime": core.RUNTIME.type, "X-Fern-Runtime-Version": core.RUNTIME.version }, (yield this._getCustomAuthorizationHeaders())), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers),
contentType: "application/json",
requestType: "json",
body: request,
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
});
if (_response.ok) {
return _response.body;
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new SarvamAI.BadRequestError(_response.error.body);
case 403:
throw new SarvamAI.ForbiddenError(_response.error.body);
case 422:
throw new SarvamAI.UnprocessableEntityError(_response.error.body);
case 429:
throw new SarvamAI.TooManyRequestsError(_response.error.body);
case 500:
throw new SarvamAI.InternalServerError(_response.error.body);
default:
throw new errors.SarvamAIError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.SarvamAIError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.SarvamAITimeoutError("Timeout exceeded when calling POST /text-lid.");
case "unknown":
throw new errors.SarvamAIError({
message: _response.error.errorMessage,
});
}
});
}
/**
* **Transliteration** converts text from one script to another while preserving the original pronunciation. For example, **'नमस्ते'** becomes **'namaste'** in English, and **'how are you'** can be written as **'हाउ आर यू'** in Devanagari. This process ensures that the sound of the original text remains intact, even when written in a different script.
*
* Transliteration is useful when you want to represent words phonetically across different writing systems, such as converting **'मैं ऑफिस जा रहा हूँ'** to **'main office ja raha hun'** in English letters.
*
* **Translation**, on the other hand, converts text from one language to another while preserving the meaning rather than pronunciation. For example, **'मैं ऑफिस जा रहा हूँ'** translates to **'I am going to the office'** in English, changing both the script and the language while conveying the intended message.
* ### Examples of **Transliteration**:
* - **'Good morning'** becomes **'गुड मॉर्निंग'** in Hindi, where the pronunciation is preserved but the meaning is not translated.
* - **'सुप्रभात'** becomes **'suprabhat'** in English.
*
* Available languages:
* - **`en-IN`**: English
* - **`hi-IN`**: Hindi
* - **`bn-IN`**: Bengali
* - **`gu-IN`**: Gujarati
* - **`kn-IN`**: Kannada
* - **`ml-IN`**: Malayalam
* - **`mr-IN`**: Marathi
* - **`od-IN`**: Odia
* - **`pa-IN`**: Punjabi
* - **`ta-IN`**: Tamil
* - **`te-IN`**: Telugu
*
* For hands-on practice, you can explore the notebook tutorial on [Transliterate API Tutorial](https://github.com/sarvamai/sarvam-ai-cookbook/blob/main/notebooks/transliterate/Transliterate_API_Tutorial.ipynb).
*
* @param {SarvamAI.TransliterationRequest} request
* @param {Text.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws {@link SarvamAI.BadRequestError}
* @throws {@link SarvamAI.ForbiddenError}
* @throws {@link SarvamAI.UnprocessableEntityError}
* @throws {@link SarvamAI.TooManyRequestsError}
* @throws {@link SarvamAI.InternalServerError}
*
* @example
* await client.text.transliterate({
* input: "input",
* source_language_code: "auto",
* target_language_code: "bn-IN"
* })
*/
transliterate(request, requestOptions) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const _response = yield core.fetcher({
url: (0, url_join_1.default)((_b = (_a = (yield core.Supplier.get(this._options.baseUrl))) !== null && _a !== void 0 ? _a : (yield core.Supplier.get(this._options.environment))) !== null && _b !== void 0 ? _b : environments.SarvamAIEnvironment.Production, "transliterate"),
method: "POST",
headers: Object.assign(Object.assign({ "X-Fern-Language": "JavaScript", "X-Fern-SDK-Name": "sarvamai", "X-Fern-SDK-Version": "0.1.5-a1", "User-Agent": "sarvamai/0.1.5-a1", "X-Fern-Runtime": core.RUNTIME.type, "X-Fern-Runtime-Version": core.RUNTIME.version }, (yield this._getCustomAuthorizationHeaders())), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers),
contentType: "application/json",
requestType: "json",
body: request,
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
});
if (_response.ok) {
return _response.body;
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new SarvamAI.BadRequestError(_response.error.body);
case 403:
throw new SarvamAI.ForbiddenError(_response.error.body);
case 422:
throw new SarvamAI.UnprocessableEntityError(_response.error.body);
case 429:
throw new SarvamAI.TooManyRequestsError(_response.error.body);
case 500:
throw new SarvamAI.InternalServerError(_response.error.body);
default:
throw new errors.SarvamAIError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}
}
switch (_response.error.reason) {
case "non-json":
throw new errors.SarvamAIError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.SarvamAITimeoutError("Timeout exceeded when calling POST /transliterate.");
case "unknown":
throw new errors.SarvamAIError({
message: _response.error.errorMessage,
});
}
});
}
_getCustomAuthorizationHeaders() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const apiSubscriptionKeyValue = (_a = (yield core.Supplier.get(this._options.apiSubscriptionKey))) !== null && _a !== void 0 ? _a : process === null || process === void 0 ? void 0 : process.env["SARVAM_API_KEY"];
return { "api-subscription-key": apiSubscriptionKeyValue };
});
}
}
exports.Text = Text;