exa-js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2024 Hubert Yuan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # exasearch
@@ -0,0 +1,96 @@
1
+ type BaseSearchOptions = {
2
+ numResults?: number;
3
+ includeDomains?: string[];
4
+ excludeDomains?: string[];
5
+ startCrawlDate?: string;
6
+ endCrawlDate?: string;
7
+ startPublishedDate?: string;
8
+ endPublishedDate?: string;
9
+ category?: string;
10
+ };
11
+ type RegularSearchOptions = BaseSearchOptions & {
12
+ useAutoprompt?: boolean;
13
+ type?: string;
14
+ };
15
+ type FindSimilarOptions = BaseSearchOptions & {
16
+ excludeSourceDomain?: boolean;
17
+ };
18
+ type ContentsOptions = {
19
+ text?: TextContentsOptions | boolean;
20
+ highlights?: HighlightsContentsOptions | boolean;
21
+ };
22
+ type TextContentsOptions = {
23
+ maxCharacters?: number;
24
+ includeHTMLTags?: boolean;
25
+ };
26
+ type HighlightsContentsOptions = {
27
+ query?: string;
28
+ numSentences?: number;
29
+ highlightsPerURL?: number;
30
+ };
31
+ type TextResponse = {
32
+ text: string;
33
+ };
34
+ type HighlightsResponse = {
35
+ highlights: string[];
36
+ highlightScores: number[];
37
+ };
38
+ type ContentsResultComponent<T extends ContentsOptions> = (T['text'] extends (object | true) ? TextResponse : {}) & (T['highlights'] extends (object | true) ? HighlightsResponse : {});
39
+ type SearchResult<T extends ContentsOptions = {}> = {
40
+ title: string;
41
+ url: string;
42
+ publishedDate?: string;
43
+ author?: string;
44
+ score?: number;
45
+ id: string;
46
+ } & ContentsResultComponent<T>;
47
+ type SearchResponse<T extends ContentsOptions = {}> = {
48
+ results: SearchResult<T>[];
49
+ autopromptString?: string;
50
+ };
51
+ /**
52
+ * The Exa class encapsulates the API's endpoints.
53
+ */
54
+ declare class Exa {
55
+ private baseURL;
56
+ private headers;
57
+ /**
58
+ * Constructs the Exa API client.
59
+ * @param {string} apiKey - The API key for authentication.
60
+ * @param {string} [baseURL] - The base URL of the Exa API.
61
+ */
62
+ constructor(apiKey?: string, baseURL?: string);
63
+ /**
64
+ * Makes a request to the Exa API.
65
+ * @param {string} endpoint - The API endpoint to call.
66
+ * @param {string} method - The HTTP method to use.
67
+ * @param {any} [body] - The request body for POST requests.
68
+ * @returns {Promise<any>} The response from the API.
69
+ */
70
+ private request;
71
+ /**
72
+ * Performs a search with a Exa prompt-engineered query.
73
+ * @param {string} query - The query string.
74
+ * @param {SearchOptions} [options] - Additional search options.
75
+ * @returns {Promise<SearchResponse>} A list of relevant search results.
76
+ */
77
+ search(query: string, options: RegularSearchOptions): Promise<SearchResponse>;
78
+ searchAndContents<T extends ContentsOptions>(query: string, options: RegularSearchOptions & T): Promise<SearchResponse<T>>;
79
+ /**
80
+ * Finds similar links to the provided URL.
81
+ * @param {string} url - The URL for which to find similar links.
82
+ * @param {FindSimilarOptions} [options] - Additional options for finding similar links.
83
+ * @returns {Promise<SearchResponse>} A list of similar search results.
84
+ */
85
+ findSimilar(url: string, options?: FindSimilarOptions): Promise<SearchResponse>;
86
+ findSimilarAndContents<T extends ContentsOptions>(url: string, options: FindSimilarOptions & T): Promise<SearchResponse<T>>;
87
+ /**
88
+ * Retrieves contents of documents based on a list of document IDs.
89
+ * @param {string | string[] | SearchResult[]} ids - An array of document IDs.
90
+ * @param {ContentsOptions} [options] - Additional options for retrieving document contents.
91
+ * @returns {Promise<GetContentsResponse>} A list of document contents.
92
+ */
93
+ getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>>;
94
+ }
95
+
96
+ export { BaseSearchOptions, ContentsOptions, FindSimilarOptions, HighlightsContentsOptions, RegularSearchOptions, SearchResponse, SearchResult, TextContentsOptions, Exa as default };
package/dist/index.js ADDED
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ default: () => src_default
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+ var import_cross_fetch = __toESM(require("cross-fetch"));
37
+ var Exa = class {
38
+ /**
39
+ * Constructs the Exa API client.
40
+ * @param {string} apiKey - The API key for authentication.
41
+ * @param {string} [baseURL] - The base URL of the Exa API.
42
+ */
43
+ constructor(apiKey, baseURL = "https://api.exa.ai") {
44
+ this.baseURL = baseURL;
45
+ if (!apiKey) {
46
+ apiKey = process.env.EXASEARCH_API_KEY;
47
+ if (!apiKey) {
48
+ throw new Error("API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)");
49
+ }
50
+ }
51
+ this.headers = new import_cross_fetch.Headers({
52
+ "x-api-key": apiKey,
53
+ "Content-Type": "application/json",
54
+ "User-Agent": "exa-node 1.0.27"
55
+ });
56
+ }
57
+ /**
58
+ * Makes a request to the Exa API.
59
+ * @param {string} endpoint - The API endpoint to call.
60
+ * @param {string} method - The HTTP method to use.
61
+ * @param {any} [body] - The request body for POST requests.
62
+ * @returns {Promise<any>} The response from the API.
63
+ */
64
+ async request(endpoint, method, body) {
65
+ const response = await (0, import_cross_fetch.default)(this.baseURL + endpoint, {
66
+ method,
67
+ headers: this.headers,
68
+ body: body ? JSON.stringify(body) : void 0
69
+ });
70
+ if (!response.ok) {
71
+ const message = (await response.json()).error;
72
+ throw new Error(
73
+ `Request failed with status ${response.status}. ${message}`
74
+ );
75
+ }
76
+ return await response.json();
77
+ }
78
+ /**
79
+ * Performs a search with a Exa prompt-engineered query.
80
+ * @param {string} query - The query string.
81
+ * @param {SearchOptions} [options] - Additional search options.
82
+ * @returns {Promise<SearchResponse>} A list of relevant search results.
83
+ */
84
+ async search(query, options) {
85
+ return await this.request("/search", "POST", { query, ...options });
86
+ }
87
+ async searchAndContents(query, options) {
88
+ const { text, highlights, ...rest } = options;
89
+ return await this.request("/search", "POST", {
90
+ query,
91
+ contents: {
92
+ ...text ? { text } : {},
93
+ ...highlights ? { highlights } : {}
94
+ },
95
+ ...rest
96
+ });
97
+ }
98
+ /**
99
+ * Finds similar links to the provided URL.
100
+ * @param {string} url - The URL for which to find similar links.
101
+ * @param {FindSimilarOptions} [options] - Additional options for finding similar links.
102
+ * @returns {Promise<SearchResponse>} A list of similar search results.
103
+ */
104
+ async findSimilar(url, options) {
105
+ return await this.request("/findSimilar", "POST", { url, ...options });
106
+ }
107
+ async findSimilarAndContents(url, options) {
108
+ const { text, highlights, ...rest } = options;
109
+ return await this.request("/findSimilar", "POST", {
110
+ url,
111
+ contents: {
112
+ ...text ? { text } : {},
113
+ ...highlights ? { highlights } : {}
114
+ },
115
+ ...rest
116
+ });
117
+ }
118
+ /**
119
+ * Retrieves contents of documents based on a list of document IDs.
120
+ * @param {string | string[] | SearchResult[]} ids - An array of document IDs.
121
+ * @param {ContentsOptions} [options] - Additional options for retrieving document contents.
122
+ * @returns {Promise<GetContentsResponse>} A list of document contents.
123
+ */
124
+ async getContents(ids, options) {
125
+ if (ids.length === 0) {
126
+ throw new Error("Must provide at least one ID");
127
+ }
128
+ let requestIds;
129
+ if (typeof ids === "string") {
130
+ requestIds = [ids];
131
+ } else if (typeof ids[0] === "string") {
132
+ requestIds = ids;
133
+ } else {
134
+ requestIds = ids.map((result) => result.id);
135
+ }
136
+ return await this.request(`/contents`, "POST", { ids: requestIds, ...options });
137
+ }
138
+ };
139
+ var src_default = Exa;
package/dist/index.mjs ADDED
@@ -0,0 +1,108 @@
1
+ // src/index.ts
2
+ import fetch, { Headers } from "cross-fetch";
3
+ var Exa = class {
4
+ /**
5
+ * Constructs the Exa API client.
6
+ * @param {string} apiKey - The API key for authentication.
7
+ * @param {string} [baseURL] - The base URL of the Exa API.
8
+ */
9
+ constructor(apiKey, baseURL = "https://api.exa.ai") {
10
+ this.baseURL = baseURL;
11
+ if (!apiKey) {
12
+ apiKey = process.env.EXASEARCH_API_KEY;
13
+ if (!apiKey) {
14
+ throw new Error("API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)");
15
+ }
16
+ }
17
+ this.headers = new Headers({
18
+ "x-api-key": apiKey,
19
+ "Content-Type": "application/json",
20
+ "User-Agent": "exa-node 1.0.27"
21
+ });
22
+ }
23
+ /**
24
+ * Makes a request to the Exa API.
25
+ * @param {string} endpoint - The API endpoint to call.
26
+ * @param {string} method - The HTTP method to use.
27
+ * @param {any} [body] - The request body for POST requests.
28
+ * @returns {Promise<any>} The response from the API.
29
+ */
30
+ async request(endpoint, method, body) {
31
+ const response = await fetch(this.baseURL + endpoint, {
32
+ method,
33
+ headers: this.headers,
34
+ body: body ? JSON.stringify(body) : void 0
35
+ });
36
+ if (!response.ok) {
37
+ const message = (await response.json()).error;
38
+ throw new Error(
39
+ `Request failed with status ${response.status}. ${message}`
40
+ );
41
+ }
42
+ return await response.json();
43
+ }
44
+ /**
45
+ * Performs a search with a Exa prompt-engineered query.
46
+ * @param {string} query - The query string.
47
+ * @param {SearchOptions} [options] - Additional search options.
48
+ * @returns {Promise<SearchResponse>} A list of relevant search results.
49
+ */
50
+ async search(query, options) {
51
+ return await this.request("/search", "POST", { query, ...options });
52
+ }
53
+ async searchAndContents(query, options) {
54
+ const { text, highlights, ...rest } = options;
55
+ return await this.request("/search", "POST", {
56
+ query,
57
+ contents: {
58
+ ...text ? { text } : {},
59
+ ...highlights ? { highlights } : {}
60
+ },
61
+ ...rest
62
+ });
63
+ }
64
+ /**
65
+ * Finds similar links to the provided URL.
66
+ * @param {string} url - The URL for which to find similar links.
67
+ * @param {FindSimilarOptions} [options] - Additional options for finding similar links.
68
+ * @returns {Promise<SearchResponse>} A list of similar search results.
69
+ */
70
+ async findSimilar(url, options) {
71
+ return await this.request("/findSimilar", "POST", { url, ...options });
72
+ }
73
+ async findSimilarAndContents(url, options) {
74
+ const { text, highlights, ...rest } = options;
75
+ return await this.request("/findSimilar", "POST", {
76
+ url,
77
+ contents: {
78
+ ...text ? { text } : {},
79
+ ...highlights ? { highlights } : {}
80
+ },
81
+ ...rest
82
+ });
83
+ }
84
+ /**
85
+ * Retrieves contents of documents based on a list of document IDs.
86
+ * @param {string | string[] | SearchResult[]} ids - An array of document IDs.
87
+ * @param {ContentsOptions} [options] - Additional options for retrieving document contents.
88
+ * @returns {Promise<GetContentsResponse>} A list of document contents.
89
+ */
90
+ async getContents(ids, options) {
91
+ if (ids.length === 0) {
92
+ throw new Error("Must provide at least one ID");
93
+ }
94
+ let requestIds;
95
+ if (typeof ids === "string") {
96
+ requestIds = [ids];
97
+ } else if (typeof ids[0] === "string") {
98
+ requestIds = ids;
99
+ } else {
100
+ requestIds = ids.map((result) => result.id);
101
+ }
102
+ return await this.request(`/contents`, "POST", { ids: requestIds, ...options });
103
+ }
104
+ };
105
+ var src_default = Exa;
106
+ export {
107
+ src_default as default
108
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "exa-js",
3
+ "version": "1.0.0",
4
+ "description": "Exasearch SDK for Node.js and the browser",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "main": "./dist/index.js",
12
+ "module": "./dist/index.mjs",
13
+ "exports": {
14
+ "require": "./dist/index.js",
15
+ "import": "./dist/index.mjs"
16
+ },
17
+ "types": "./dist/index.d.ts",
18
+ "scripts": {
19
+ "build-fast": "tsup src/index.ts --format cjs,esm",
20
+ "build": "pnpm run build-fast --dts-resolve",
21
+ "test": "vitest run",
22
+ "prepublishOnly": "pnpm run build"
23
+ },
24
+ "license": "MIT",
25
+ "devDependencies": {
26
+ "prettier": "2.8.4",
27
+ "tsup": "6.6.3",
28
+ "typescript": "4.9.5",
29
+ "vitest": "0.28.5"
30
+ },
31
+ "dependencies": {
32
+ "cross-fetch": "^4.0.0"
33
+ },
34
+ "directories": {
35
+ "test": "test"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/HubertY/exanodesdk.git"
40
+ },
41
+ "keywords": [
42
+ "exa",
43
+ "metaphor",
44
+ "search",
45
+ "AI",
46
+ "LLMs",
47
+ "RAG",
48
+ "retrieval",
49
+ "augmented",
50
+ "generation"
51
+ ],
52
+ "author": "jeffzwang",
53
+ "bugs": {
54
+ "url": "https://github.com/HubertY/exanodesdk/issues"
55
+ },
56
+ "homepage": "https://github.com/HubertY/exanodesdk#readme"
57
+ }