@tavily/core
Version:
Official JavaScript library for Tavily.
575 lines (567 loc) • 18 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/index.ts
var index_exports = {};
__export(index_exports, {
tavily: () => tavily
});
module.exports = __toCommonJS(index_exports);
// src/search.ts
var import_axios2 = require("axios");
// src/utils.ts
var import_axios = __toESM(require("axios"));
var import_js_tiktoken = require("js-tiktoken");
var import_https_proxy_agent = require("https-proxy-agent");
var BASE_URL = "https://api.tavily.com";
var DEFAULT_MODEL_ENCODING = "gpt-3.5-turbo";
var DEFAULT_MAX_TOKENS = 4e3;
function post(endpoint, body, apiKey, proxies, timeout = 60) {
return __async(this, null, function* () {
const url = `${BASE_URL}/${endpoint}`;
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
"X-Client-Source": "tavily-js"
};
const timeoutInMillis = timeout * 1e3;
const config = { headers, timeout: timeoutInMillis };
if (proxies) {
if (proxies.http) {
config.httpAgent = new import_https_proxy_agent.HttpsProxyAgent(proxies.http);
}
if (proxies.https) {
config.httpsAgent = new import_https_proxy_agent.HttpsProxyAgent(proxies.https);
}
}
return import_axios.default.post(url, body, config);
});
}
function getTotalTokensFromString(str, encodingName = DEFAULT_MODEL_ENCODING) {
const encoding = (0, import_js_tiktoken.encodingForModel)(encodingName);
return encoding.encode(str).length;
}
function getMaxTokensFromList(data, maxTokens = DEFAULT_MAX_TOKENS) {
var result = [];
let currentTokens = 0;
for (let item of data) {
let itemString = JSON.stringify(item);
let newTotalTokens = currentTokens + getTotalTokensFromString(itemString);
if (newTotalTokens > maxTokens) {
break;
}
result.push(item);
currentTokens = newTotalTokens;
}
return JSON.stringify(result);
}
function handleRequestError(res) {
var _a, _b, _c, _d;
const status = res.status;
const message = (_b = (_a = res.data) == null ? void 0 : _a.detail) == null ? void 0 : _b.error;
if (!message) {
throw new Error(`${status} Error: ${JSON.stringify(res.data)}`);
}
throw new Error(`${(_d = (_c = res.data) == null ? void 0 : _c.detail) == null ? void 0 : _d.error}`);
}
function handleTimeoutError(timeout) {
throw new Error(`Request timed out after ${timeout} seconds.`);
}
// src/search.ts
function _search(apiKey, proxies) {
return function search(_0) {
return __async(this, arguments, function* (query, options = {}) {
const _a = options, {
searchDepth,
topic,
days,
maxResults,
includeImages,
includeImageDescriptions,
includeAnswer,
includeRawContent,
includeDomains,
excludeDomains,
timeRange,
chunksPerSource,
country,
timeout
} = _a, kwargs = __objRest(_a, [
"searchDepth",
"topic",
"days",
"maxResults",
"includeImages",
"includeImageDescriptions",
"includeAnswer",
"includeRawContent",
"includeDomains",
"excludeDomains",
"timeRange",
"chunksPerSource",
"country",
"timeout"
]);
const requestTimeout = timeout ? Math.min(timeout, 120) : 60;
try {
const response = yield post(
"search",
__spreadValues({
query,
search_depth: searchDepth,
topic,
days,
max_results: maxResults,
include_images: includeImages,
include_image_descriptions: includeImageDescriptions,
include_answer: includeAnswer,
include_raw_content: includeRawContent,
include_domains: includeDomains,
exclude_domains: excludeDomains,
time_range: timeRange,
chunks_per_source: chunksPerSource,
country
}, kwargs),
apiKey,
proxies,
requestTimeout
);
return {
query,
responseTime: response.data.response_time,
images: response.data.images.map((image) => {
return {
url: (image == null ? void 0 : image.url) || image,
description: image.description
};
}),
results: response.data.results.map((result) => {
return {
title: result.title,
url: result.url,
content: result.content,
rawContent: result.raw_content,
score: result.score,
publishedDate: result.published_date
};
}),
answer: response.data.answer
};
} catch (err) {
if (err instanceof import_axios2.AxiosError) {
if (err.code === "ECONNABORTED") {
handleTimeoutError(requestTimeout);
}
if (err.response) {
handleRequestError(err.response);
}
}
throw new Error(
`An unexpected error occurred while making the request. Error: ${err}`
);
}
});
};
}
function _searchQNA(apiKey, proxies) {
return function searchQNA(_0) {
return __async(this, arguments, function* (query, options = {}) {
var _a;
const requestTimeout = (options == null ? void 0 : options.timeout) ? Math.min(options.timeout, 120) : 60;
try {
const response = yield post(
"search",
{
query,
search_depth: (_a = options.searchDepth) != null ? _a : "advanced",
topic: options.topic,
days: options.days,
max_results: options.maxResults,
include_answer: true,
include_domains: options.includeDomains,
exclude_domains: options.excludeDomains,
chunks_per_source: options.chunksPerSource
},
apiKey,
proxies,
requestTimeout
);
const answer = response.data.answer;
return answer;
} catch (err) {
if (err instanceof import_axios2.AxiosError) {
if (err.code === "ECONNABORTED") {
handleTimeoutError(requestTimeout);
}
if (err.response) {
handleRequestError(err.response);
}
}
throw new Error(
`An unexpected error occurred while making the request. Error: ${err}`
);
}
});
};
}
function _searchContext(apiKey, proxies) {
return function searchContext(_0) {
return __async(this, arguments, function* (query, options = {}) {
var _a, _b;
const timeout = (options == null ? void 0 : options.timeout) ? Math.min(options.timeout, 120) : 60;
try {
const response = yield post(
"search",
{
query,
search_depth: options.searchDepth,
topic: options.topic,
days: options.days,
max_results: options.maxResults,
include_domains: options.includeDomains,
exclude_domains: options.excludeDomains,
chunks_per_source: options.chunksPerSource
},
apiKey,
proxies,
timeout
);
const sources = ((_a = response.data) == null ? void 0 : _a.results) || [];
const context = sources.map((source) => {
return {
url: source.url,
content: source.content
};
});
return JSON.stringify(
getMaxTokensFromList(context, (_b = options.maxTokens) != null ? _b : DEFAULT_MAX_TOKENS)
);
} catch (err) {
if (err instanceof import_axios2.AxiosError) {
if (err.code === "ECONNABORTED") {
handleTimeoutError(timeout);
}
if (err.response) {
handleRequestError(err.response);
}
}
throw new Error(
`An unexpected error occurred while making the request. Error: ${err}`
);
}
});
};
}
// src/extract.ts
var import_axios3 = require("axios");
function _extract(apiKey, proxies) {
return function extract(_0) {
return __async(this, arguments, function* (urls, options = {}) {
const _a = options, { includeImages, extractDepth, format, timeout } = _a, kwargs = __objRest(_a, ["includeImages", "extractDepth", "format", "timeout"]);
const requestTimeout = timeout ? Math.min(timeout, 120) : 60;
try {
const response = yield post(
"extract",
__spreadValues({
urls,
include_images: includeImages,
extract_depth: extractDepth,
format
}, kwargs),
apiKey,
proxies,
requestTimeout
);
return {
responseTime: response.data.response_time,
results: response.data.results.map((result) => {
return {
url: result.url,
rawContent: result.raw_content,
images: result.images
};
}),
failedResults: response.data.failed_results.map((result) => {
return {
url: result.url,
error: result.error
};
})
};
} catch (err) {
if (err instanceof import_axios3.AxiosError) {
if (err.code === "ECONNABORTED") {
handleTimeoutError(requestTimeout);
}
if (err.response) {
handleRequestError(err.response);
}
}
throw new Error(
`An unexpected error occurred while making the request. Error: ${err}`
);
}
});
};
}
// src/crawl.ts
var import_axios4 = require("axios");
function _crawl(apiKey, proxies) {
return function crawl(_0) {
return __async(this, arguments, function* (url, options = {}) {
const _a = options, {
maxDepth,
maxBreadth,
limit,
extractDepth,
selectPaths,
selectDomains,
excludePaths,
excludeDomains,
allowExternal,
includeImages,
categories,
instructions,
format,
timeout
} = _a, kwargs = __objRest(_a, [
"maxDepth",
"maxBreadth",
"limit",
"extractDepth",
"selectPaths",
"selectDomains",
"excludePaths",
"excludeDomains",
"allowExternal",
"includeImages",
"categories",
"instructions",
"format",
"timeout"
]);
const requestTimeout = timeout ? Math.min(timeout, 120) : 60;
try {
const response = yield post(
"crawl",
__spreadValues({
url,
max_depth: maxDepth,
max_breadth: maxBreadth,
limit,
extract_depth: extractDepth,
select_paths: selectPaths,
select_domains: selectDomains,
exclude_paths: excludePaths,
exclude_domains: excludeDomains,
allow_external: allowExternal,
include_images: includeImages,
categories,
instructions,
format
}, kwargs),
apiKey,
proxies,
requestTimeout
);
return {
responseTime: response.data.response_time,
baseUrl: response.data.base_url,
results: response.data.results.map((item) => {
return {
url: item.url,
rawContent: item.raw_content,
images: item.images
};
})
};
} catch (err) {
if (err instanceof import_axios4.AxiosError) {
if (err.code === "ECONNABORTED") {
handleTimeoutError(requestTimeout);
}
if (err.response) {
handleRequestError(err.response);
}
}
throw new Error(
`An unexpected error occurred while making the request. Error: ${err}`
);
}
});
};
}
// src/map.ts
var import_axios5 = require("axios");
function _map(apiKey, proxies) {
return function map(_0) {
return __async(this, arguments, function* (url, options = {}) {
const _a = options, {
maxDepth,
maxBreadth,
limit,
selectPaths,
selectDomains,
excludePaths,
excludeDomains,
allowExternal,
categories,
instructions,
timeout
} = _a, kwargs = __objRest(_a, [
"maxDepth",
"maxBreadth",
"limit",
"selectPaths",
"selectDomains",
"excludePaths",
"excludeDomains",
"allowExternal",
"categories",
"instructions",
"timeout"
]);
const requestTimeout = timeout ? Math.min(timeout, 120) : 60;
try {
const response = yield post(
"map",
__spreadValues({
url,
max_depth: maxDepth,
max_breadth: maxBreadth,
limit,
select_paths: selectPaths,
select_domains: selectDomains,
exclude_paths: excludePaths,
exclude_domains: excludeDomains,
allow_external: allowExternal,
categories,
instructions
}, kwargs),
apiKey,
proxies,
requestTimeout
);
return {
responseTime: response.data.response_time,
baseUrl: response.data.base_url,
results: response.data.results
};
} catch (err) {
if (err instanceof import_axios5.AxiosError) {
if (err.code === "ECONNABORTED") {
handleTimeoutError(requestTimeout);
}
if (err.response) {
handleRequestError(err.response);
}
}
throw new Error(
`An unexpected error occurred while making the request. Error: ${err}`
);
}
});
};
}
// src/client.ts
function tavily(options) {
const apiKey = (options == null ? void 0 : options.apiKey) || process.env.TAVILY_API_KEY;
const proxies = (() => {
var _a, _b;
const http = ((_a = options == null ? void 0 : options.proxies) == null ? void 0 : _a.http) || process.env.TAVILY_HTTP_PROXY;
const https = ((_b = options == null ? void 0 : options.proxies) == null ? void 0 : _b.https) || process.env.TAVILY_HTTPS_PROXY;
const result = {};
if (http) result.http = http;
if (https) result.https = https;
return Object.keys(result).length > 0 ? result : void 0;
})();
if (!apiKey) {
throw new Error(
"No API key provided. Please provide the api_key attribute or set the TAVILY_API_KEY environment variable."
);
}
return {
search: _search(apiKey, proxies),
extract: _extract(apiKey, proxies),
searchQNA: _searchQNA(apiKey, proxies),
searchContext: _searchContext(apiKey, proxies),
crawl: _crawl(apiKey, proxies),
map: _map(apiKey, proxies)
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
tavily
});
//# sourceMappingURL=index.js.map