UNPKG

voyageai

Version:

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-Built%20with%20Fern-brightgreen)](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fvoyage-ai%2Ftypescript-sdk) [![npm shield](

258 lines (257 loc) 12.9 kB
"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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __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.VoyageAIClient = void 0; const environments = __importStar(require("./environments")); const core = __importStar(require("./core")); const serializers = __importStar(require("./serialization/index")); const url_join_1 = __importDefault(require("url-join")); const errors = __importStar(require("./errors/index")); class VoyageAIClient { constructor(_options = {}) { this._options = _options; } /** * Voyage embedding endpoint receives as input a string (or a list of strings) and other arguments such as the preferred model name, and returns a response containing a list of embeddings. * * @param {VoyageAI.EmbedRequest} request * @param {VoyageAIClient.RequestOptions} requestOptions - Request-specific configuration. * * @example * await client.embed({ * input: "input", * model: "model" * }) */ embed(request, requestOptions) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const _response = yield ((_a = this._options.fetcher) !== null && _a !== void 0 ? _a : core.fetcher)({ url: (0, url_join_1.default)((_b = (yield core.Supplier.get(this._options.environment))) !== null && _b !== void 0 ? _b : environments.VoyageAIEnvironment.Default, "embeddings"), method: "POST", headers: { Authorization: yield this._getAuthorizationHeader(), "X-Fern-Language": "JavaScript", "X-Fern-SDK-Name": "voyageai", "X-Fern-SDK-Version": "0.0.4", "User-Agent": "voyageai/0.0.4", "X-Fern-Runtime": core.RUNTIME.type, "X-Fern-Runtime-Version": core.RUNTIME.version, }, contentType: "application/json", requestType: "json", body: serializers.EmbedRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }), 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 serializers.EmbedResponse.parseOrThrow(_response.body, { unrecognizedObjectKeys: "passthrough", allowUnrecognizedUnionMembers: true, allowUnrecognizedEnumValues: true, skipValidation: true, breadcrumbsPrefix: ["response"], }); } if (_response.error.reason === "status-code") { throw new errors.VoyageAIError({ statusCode: _response.error.statusCode, body: _response.error.body, }); } switch (_response.error.reason) { case "non-json": throw new errors.VoyageAIError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, }); case "timeout": throw new errors.VoyageAITimeoutError(); case "unknown": throw new errors.VoyageAIError({ message: _response.error.errorMessage, }); } }); } /** * Voyage reranker endpoint receives as input a query, a list of documents, and other arguments such as the model name, and returns a response containing the reranking results. * * @param {VoyageAI.RerankRequest} request * @param {VoyageAIClient.RequestOptions} requestOptions - Request-specific configuration. * * @example * await client.rerank({ * query: "query", * documents: ["documents"], * model: "model" * }) */ rerank(request, requestOptions) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const _response = yield ((_a = this._options.fetcher) !== null && _a !== void 0 ? _a : core.fetcher)({ url: (0, url_join_1.default)((_b = (yield core.Supplier.get(this._options.environment))) !== null && _b !== void 0 ? _b : environments.VoyageAIEnvironment.Default, "rerank"), method: "POST", headers: { Authorization: yield this._getAuthorizationHeader(), "X-Fern-Language": "JavaScript", "X-Fern-SDK-Name": "voyageai", "X-Fern-SDK-Version": "0.0.4", "User-Agent": "voyageai/0.0.4", "X-Fern-Runtime": core.RUNTIME.type, "X-Fern-Runtime-Version": core.RUNTIME.version, }, contentType: "application/json", requestType: "json", body: serializers.RerankRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }), 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 serializers.RerankResponse.parseOrThrow(_response.body, { unrecognizedObjectKeys: "passthrough", allowUnrecognizedUnionMembers: true, allowUnrecognizedEnumValues: true, skipValidation: true, breadcrumbsPrefix: ["response"], }); } if (_response.error.reason === "status-code") { throw new errors.VoyageAIError({ statusCode: _response.error.statusCode, body: _response.error.body, }); } switch (_response.error.reason) { case "non-json": throw new errors.VoyageAIError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, }); case "timeout": throw new errors.VoyageAITimeoutError(); case "unknown": throw new errors.VoyageAIError({ message: _response.error.errorMessage, }); } }); } /** * The Voyage multimodal embedding endpoint returns vector representations for a given list of multimodal inputs consisting of text, images, or an interleaving of both modalities. * * @param {VoyageAI.MultimodalEmbedRequest} request * @param {VoyageAIClient.RequestOptions} requestOptions - Request-specific configuration. * * @example * await client.multimodalEmbed({ * inputs: [{}], * model: "model" * }) */ multimodalEmbed(request, requestOptions) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const _response = yield ((_a = this._options.fetcher) !== null && _a !== void 0 ? _a : core.fetcher)({ url: (0, url_join_1.default)((_b = (yield core.Supplier.get(this._options.environment))) !== null && _b !== void 0 ? _b : environments.VoyageAIEnvironment.Default, "multimodalembeddings"), method: "POST", headers: { Authorization: yield this._getAuthorizationHeader(), "X-Fern-Language": "JavaScript", "X-Fern-SDK-Name": "voyageai", "X-Fern-SDK-Version": "0.0.4", "User-Agent": "voyageai/0.0.4", "X-Fern-Runtime": core.RUNTIME.type, "X-Fern-Runtime-Version": core.RUNTIME.version, }, contentType: "application/json", requestType: "json", body: serializers.MultimodalEmbedRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }), 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 serializers.MultimodalEmbedResponse.parseOrThrow(_response.body, { unrecognizedObjectKeys: "passthrough", allowUnrecognizedUnionMembers: true, allowUnrecognizedEnumValues: true, skipValidation: true, breadcrumbsPrefix: ["response"], }); } if (_response.error.reason === "status-code") { throw new errors.VoyageAIError({ statusCode: _response.error.statusCode, body: _response.error.body, }); } switch (_response.error.reason) { case "non-json": throw new errors.VoyageAIError({ statusCode: _response.error.statusCode, body: _response.error.rawBody, }); case "timeout": throw new errors.VoyageAITimeoutError(); case "unknown": throw new errors.VoyageAIError({ message: _response.error.errorMessage, }); } }); } _getAuthorizationHeader() { var _a; return __awaiter(this, void 0, void 0, function* () { const bearer = (_a = (yield core.Supplier.get(this._options.apiKey))) !== null && _a !== void 0 ? _a : process === null || process === void 0 ? void 0 : process.env["VOYAGE_API_KEY"]; if (bearer == null) { throw new errors.VoyageAIError({ message: "Please specify VOYAGE_API_KEY when instantiating the client.", }); } return `Bearer ${bearer}`; }); } } exports.VoyageAIClient = VoyageAIClient;