exa-js 1.0.6 → 1.0.8
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/dist/index.d.ts +65 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Search options for performing a search query.
|
|
3
|
+
* @typedef {Object} SearchOptions
|
|
4
|
+
* @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.
|
|
5
|
+
* @property {string[]} [includeDomains] - List of domains to include in the search.
|
|
6
|
+
* @property {string[]} [excludeDomains] - List of domains to exclude in the search.
|
|
7
|
+
* @property {string} [startCrawlDate] - Start date for results based on crawl date.
|
|
8
|
+
* @property {string} [endCrawlDate] - End date for results based on crawl date.
|
|
9
|
+
* @property {string} [startPublishedDate] - Start date for results based on published date.
|
|
10
|
+
* @property {string} [endPublishedDate] - End date for results based on published date.
|
|
11
|
+
* @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.
|
|
12
|
+
* @property {string} [type] - Type of search, 'keyword' or 'neural'.
|
|
13
|
+
* @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.
|
|
14
|
+
*/
|
|
1
15
|
type BaseSearchOptions = {
|
|
2
16
|
numResults?: number;
|
|
3
17
|
includeDomains?: string[];
|
|
@@ -8,13 +22,36 @@ type BaseSearchOptions = {
|
|
|
8
22
|
endPublishedDate?: string;
|
|
9
23
|
category?: string;
|
|
10
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Search options for performing a search query.
|
|
27
|
+
* @typedef {Object} ContentsOptions
|
|
28
|
+
* @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.
|
|
29
|
+
*/
|
|
11
30
|
type RegularSearchOptions = BaseSearchOptions & {
|
|
12
31
|
useAutoprompt?: boolean;
|
|
13
32
|
type?: string;
|
|
14
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* Options for finding similar links.
|
|
36
|
+
* @typedef {Object} FindSimilarOptions
|
|
37
|
+
* @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.
|
|
38
|
+
* @property {string[]} [includeDomains] - List of domains to include in the search.
|
|
39
|
+
* @property {string[]} [excludeDomains] - List of domains to exclude from the search.
|
|
40
|
+
* @property {string} [startCrawlDate] - Start date for results based on crawl date.
|
|
41
|
+
* @property {string} [endCrawlDate] - End date for results based on crawl date.
|
|
42
|
+
* @property {string} [startPublishedDate] - Start date for results based on published date.
|
|
43
|
+
* @property {string} [endPublishedDate] - End date for results based on published date.
|
|
44
|
+
* @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.
|
|
45
|
+
* @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.
|
|
46
|
+
*/
|
|
15
47
|
type FindSimilarOptions = BaseSearchOptions & {
|
|
16
48
|
excludeSourceDomain?: boolean;
|
|
17
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* Search options for performing a search query.
|
|
52
|
+
* @typedef {Object} ContentsOptions
|
|
53
|
+
* @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.
|
|
54
|
+
*/
|
|
18
55
|
type ContentsOptions = {
|
|
19
56
|
text?: TextContentsOptions | boolean;
|
|
20
57
|
highlights?: HighlightsContentsOptions | boolean;
|
|
@@ -36,6 +73,16 @@ type HighlightsResponse = {
|
|
|
36
73
|
highlightScores: number[];
|
|
37
74
|
};
|
|
38
75
|
type ContentsResultComponent<T extends ContentsOptions> = (T['text'] extends (object | true) ? TextResponse : {}) & (T['highlights'] extends (object | true) ? HighlightsResponse : {});
|
|
76
|
+
/**
|
|
77
|
+
* Represents a search result object.
|
|
78
|
+
* @typedef {Object} Result
|
|
79
|
+
* @property {string} title - The title of the search result.
|
|
80
|
+
* @property {string} url - The URL of the search result.
|
|
81
|
+
* @property {string} [publishedDate] - The estimated creation date of the content.
|
|
82
|
+
* @property {string} [author] - The author of the content, if available.
|
|
83
|
+
* @property {number} [score] - Similarity score between the query/url and the result.
|
|
84
|
+
* @property {string} id - The temporary ID for the document.
|
|
85
|
+
*/
|
|
39
86
|
type SearchResult<T extends ContentsOptions = {}> = {
|
|
40
87
|
title: string | null;
|
|
41
88
|
url: string;
|
|
@@ -44,6 +91,12 @@ type SearchResult<T extends ContentsOptions = {}> = {
|
|
|
44
91
|
score?: number;
|
|
45
92
|
id: string;
|
|
46
93
|
} & ContentsResultComponent<T>;
|
|
94
|
+
/**
|
|
95
|
+
* Represents a search response object.
|
|
96
|
+
* @typedef {Object} SearchResponse
|
|
97
|
+
* @property {Result[]} results - The list of search results.
|
|
98
|
+
* @property {string} [autopromptString] - The autoprompt string, if applicable.
|
|
99
|
+
*/
|
|
47
100
|
type SearchResponse<T extends ContentsOptions = {}> = {
|
|
48
101
|
results: SearchResult<T>[];
|
|
49
102
|
autopromptString?: string;
|
|
@@ -75,6 +128,12 @@ declare class Exa {
|
|
|
75
128
|
* @returns {Promise<SearchResponse>} A list of relevant search results.
|
|
76
129
|
*/
|
|
77
130
|
search(query: string, options?: RegularSearchOptions): Promise<SearchResponse>;
|
|
131
|
+
/**
|
|
132
|
+
* Performs a search with a Exa prompt-engineered query and returns the contents of the documents.
|
|
133
|
+
* @param {string} query - The query string.
|
|
134
|
+
* @param {SearchOptions} [options] - Additional search options.
|
|
135
|
+
* @returns {Promise<SearchResponse>} A list of relevant search results.
|
|
136
|
+
*/
|
|
78
137
|
searchAndContents<T extends ContentsOptions>(query: string, options?: RegularSearchOptions & T): Promise<SearchResponse<T>>;
|
|
79
138
|
/**
|
|
80
139
|
* Finds similar links to the provided URL.
|
|
@@ -83,6 +142,12 @@ declare class Exa {
|
|
|
83
142
|
* @returns {Promise<SearchResponse>} A list of similar search results.
|
|
84
143
|
*/
|
|
85
144
|
findSimilar(url: string, options?: FindSimilarOptions): Promise<SearchResponse>;
|
|
145
|
+
/**
|
|
146
|
+
* Finds similar links to the provided URL and returns the contents of the documents.
|
|
147
|
+
* @param {string} url - The URL for which to find similar links.
|
|
148
|
+
* @param {FindSimilarOptions} [options] - Additional options for finding similar links.
|
|
149
|
+
* @returns {Promise<SearchResponse>} A list of similar search results.
|
|
150
|
+
*/
|
|
86
151
|
findSimilarAndContents<T extends ContentsOptions>(url: string, options?: FindSimilarOptions & T): Promise<SearchResponse<T>>;
|
|
87
152
|
/**
|
|
88
153
|
* Retrieves contents of documents based on a list of document IDs.
|
package/dist/index.js
CHANGED
|
@@ -84,6 +84,12 @@ var Exa = class {
|
|
|
84
84
|
async search(query, options) {
|
|
85
85
|
return await this.request("/search", "POST", { query, ...options });
|
|
86
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Performs a search with a Exa prompt-engineered query and returns the contents of the documents.
|
|
89
|
+
* @param {string} query - The query string.
|
|
90
|
+
* @param {SearchOptions} [options] - Additional search options.
|
|
91
|
+
* @returns {Promise<SearchResponse>} A list of relevant search results.
|
|
92
|
+
*/
|
|
87
93
|
async searchAndContents(query, options) {
|
|
88
94
|
const { text, highlights, ...rest } = options || {};
|
|
89
95
|
return await this.request("/search", "POST", {
|
|
@@ -104,6 +110,12 @@ var Exa = class {
|
|
|
104
110
|
async findSimilar(url, options) {
|
|
105
111
|
return await this.request("/findSimilar", "POST", { url, ...options });
|
|
106
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Finds similar links to the provided URL and returns the contents of the documents.
|
|
115
|
+
* @param {string} url - The URL for which to find similar links.
|
|
116
|
+
* @param {FindSimilarOptions} [options] - Additional options for finding similar links.
|
|
117
|
+
* @returns {Promise<SearchResponse>} A list of similar search results.
|
|
118
|
+
*/
|
|
107
119
|
async findSimilarAndContents(url, options) {
|
|
108
120
|
const { text, highlights, ...rest } = options || {};
|
|
109
121
|
return await this.request("/findSimilar", "POST", {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from 'cross-fetch';\n\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n};\n\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n}\n\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n}\n\nexport type ContentsOptions = {\n text?: TextContentsOptions | boolean;\n highlights?: HighlightsContentsOptions | boolean;\n};\n\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n}\n\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n}\n\ntype TextResponse = { text: string };\ntype HighlightsResponse = { highlights: string[], highlightScores: number[] };\n\ntype ContentsResultComponent<T extends ContentsOptions> =\n (T['text'] extends (object | true) ? TextResponse : {}) &\n (T['highlights'] extends (object | true) ? HighlightsResponse : {});\n\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n}\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(\n apiKey?: string,\n baseURL: string = \"https://api.exa.ai\"\n ) {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\");\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.0.27\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(query: string, options?: RegularSearchOptions): Promise<SearchResponse> {\n return await this.request(\"/search\", 'POST', { query, ...options });\n }\n\n async searchAndContents<T extends ContentsOptions>(query: string, options?: RegularSearchOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, ...rest } = options || {};\n return await this.request(\"/search\", 'POST', {\n query,\n contents: {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {})\n },\n ...rest\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", 'POST', { url, ...options });\n }\n\n async findSimilarAndContents<T extends ContentsOptions>(url: string, options?: FindSimilarOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, ...rest } = options || {};\n return await this.request(\"/findSimilar\", 'POST', {\n url,\n contents: {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {})\n },\n ...rest\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`, 'POST', { ids: requestIds, ...options, });\n }\n}\n\nexport default Exa;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAA+B;AA8D/B,IAAM,MAAN,MAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASR,YACE,QACA,UAAkB,sBAClB;AACA,SAAK,UAAU;AACf,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACT,cAAM,IAAI,MAAM,2FAA2F;AAAA,MAC/G;AAAA,IACF;AACA,SAAK,UAAU,IAAI,2BAAQ;AAAA,MACzB,aAAa;AAAA,MACb,gBAAgB;AAAA,MAChB,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAc,QACZ,UACA,QACA,MACc;AACd,UAAM,WAAW,UAAM,mBAAAA,SAAM,KAAK,UAAU,UAAU;AAAA,MACpD;AAAA,MACA,SAAS,KAAK;AAAA,MACd,MAAM,OAAO,KAAK,UAAU,IAAI,IAAI;AAAA,IACtC,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,WAAW,MAAM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AI,MAAM,8BAA8B;AAAA,IAChD;AACA,QAAI;AACJ,QAAI,OAAO,QAAQ,UAAU;AAC3B,mBAAa,CAAC,GAAG;AAAA,IACnB,WAAW,OAAO,IAAI,CAAC,MAAM,UAAU;AACrC,mBAAa;AAAA,IACf,OAAO;AACL,mBAAc,IAAuB,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IAChE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ,EAAE,KAAK,YAAY,GAAG,QAAS,CAAC;AAAA,EACjF;AACJ;AAEA,IAAO,cAAQ;","names":["fetch"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from 'cross-fetch';\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n}\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n}\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.\n */\nexport type ContentsOptions = {\n text?: TextContentsOptions | boolean;\n highlights?: HighlightsContentsOptions | boolean;\n};\n\n// TODO; fill in\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n}\n\n// TODO; fill in docs\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n}\n\n// TODO: fill in docs\ntype TextResponse = { text: string };\n// TODO: fill in docs\ntype HighlightsResponse = { highlights: string[], highlightScores: number[] };\n\n// TODO; fill in docs\ntype ContentsResultComponent<T extends ContentsOptions> =\n (T['text'] extends (object | true) ? TextResponse : {}) &\n (T['highlights'] extends (object | true) ? HighlightsResponse : {});\n\n/**\n * Represents a search result object.\n * @typedef {Object} Result\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n */\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n}\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(\n apiKey?: string,\n baseURL: string = \"https://api.exa.ai\"\n ) {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\");\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.0.27\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(query: string, options?: RegularSearchOptions): Promise<SearchResponse> {\n return await this.request(\"/search\", 'POST', { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(query: string, options?: RegularSearchOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, ...rest } = options || {};\n return await this.request(\"/search\", 'POST', {\n query,\n contents: {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {})\n },\n ...rest\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", 'POST', { url, ...options });\n }\n\n /**\n * Finds similar links to the provided URL and returns the contents of the documents.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilarAndContents<T extends ContentsOptions>(url: string, options?: FindSimilarOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, ...rest } = options || {};\n return await this.request(\"/findSimilar\", 'POST', {\n url,\n contents: {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {})\n },\n ...rest\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`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HANGED
|
@@ -50,6 +50,12 @@ var Exa = class {
|
|
|
50
50
|
async search(query, options) {
|
|
51
51
|
return await this.request("/search", "POST", { query, ...options });
|
|
52
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Performs a search with a Exa prompt-engineered query and returns the contents of the documents.
|
|
55
|
+
* @param {string} query - The query string.
|
|
56
|
+
* @param {SearchOptions} [options] - Additional search options.
|
|
57
|
+
* @returns {Promise<SearchResponse>} A list of relevant search results.
|
|
58
|
+
*/
|
|
53
59
|
async searchAndContents(query, options) {
|
|
54
60
|
const { text, highlights, ...rest } = options || {};
|
|
55
61
|
return await this.request("/search", "POST", {
|
|
@@ -70,6 +76,12 @@ var Exa = class {
|
|
|
70
76
|
async findSimilar(url, options) {
|
|
71
77
|
return await this.request("/findSimilar", "POST", { url, ...options });
|
|
72
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Finds similar links to the provided URL and returns the contents of the documents.
|
|
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
|
+
*/
|
|
73
85
|
async findSimilarAndContents(url, options) {
|
|
74
86
|
const { text, highlights, ...rest } = options || {};
|
|
75
87
|
return await this.request("/findSimilar", "POST", {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from 'cross-fetch';\n\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n};\n\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n}\n\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n}\n\nexport type ContentsOptions = {\n text?: TextContentsOptions | boolean;\n highlights?: HighlightsContentsOptions | boolean;\n};\n\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n}\n\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n}\n\ntype TextResponse = { text: string };\ntype HighlightsResponse = { highlights: string[], highlightScores: number[] };\n\ntype ContentsResultComponent<T extends ContentsOptions> =\n (T['text'] extends (object | true) ? TextResponse : {}) &\n (T['highlights'] extends (object | true) ? HighlightsResponse : {});\n\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n}\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(\n apiKey?: string,\n baseURL: string = \"https://api.exa.ai\"\n ) {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\");\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.0.27\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(query: string, options?: RegularSearchOptions): Promise<SearchResponse> {\n return await this.request(\"/search\", 'POST', { query, ...options });\n }\n\n async searchAndContents<T extends ContentsOptions>(query: string, options?: RegularSearchOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, ...rest } = options || {};\n return await this.request(\"/search\", 'POST', {\n query,\n contents: {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {})\n },\n ...rest\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", 'POST', { url, ...options });\n }\n\n async findSimilarAndContents<T extends ContentsOptions>(url: string, options?: FindSimilarOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, ...rest } = options || {};\n return await this.request(\"/findSimilar\", 'POST', {\n url,\n contents: {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {})\n },\n ...rest\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/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AAA,QACvB,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;AAAA,MACrC;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,YACJ,KACA,SACyB;AACzB,WAAO,MAAM,KAAK,QAAQ,gBAAgB,QAAQ,EAAE,KAAK,GAAG,QAAQ,CAAC;AAAA,EACvE;AAAA,EAEA,MAAM,uBAAkD,KAAa,SAA8D;AACjI,UAAM,EAAE,MAAM,YAAY,GAAG,KAAK,IAAI,WAAW,CAAC;AAClD,WAAO,MAAM,KAAK,QAAQ,gBAAgB,QAAQ;AAAA,MAChD;AAAA,MACA,UAAU;AAAA,QACR,GAAI,OAAO,EAAE,KAAK,IAAI,CAAC;AAAA,QACvB,GAAI,aAAa,EAAE,WAAW,IAAI,CAAC;AAAA,MACrC;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,YAAuC,KAAyC,SAAyC;AAC7H,QAAI,IAAI,WAAW,GAAG;AACpB,YAAM,IAAI,MAAM,8BAA8B;AAAA,IAChD;AACA,QAAI;AACJ,QAAI,OAAO,QAAQ,UAAU;AAC3B,mBAAa,CAAC,GAAG;AAAA,IACnB,WAAW,OAAO,IAAI,CAAC,MAAM,UAAU;AACrC,mBAAa;AAAA,IACf,OAAO;AACL,mBAAc,IAAuB,IAAI,CAAC,WAAW,OAAO,EAAE;AAAA,IAChE;AACA,WAAO,MAAM,KAAK,QAAQ,aAAa,QAAQ,EAAE,KAAK,YAAY,GAAG,QAAS,CAAC;AAAA,EACjF;AACJ;AAEA,IAAO,cAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import fetch, { Headers } from 'cross-fetch';\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} SearchOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude in the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [useAutoprompt] - If true, converts query to a Metaphor query.\n * @property {string} [type] - Type of search, 'keyword' or 'neural'.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n */\nexport type BaseSearchOptions = {\n numResults?: number;\n includeDomains?: string[];\n excludeDomains?: string[];\n startCrawlDate?: string;\n endCrawlDate?: string;\n startPublishedDate?: string;\n endPublishedDate?: string;\n category?: string;\n};\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.\n */\nexport type RegularSearchOptions = BaseSearchOptions & {\n useAutoprompt?: boolean;\n type?: string;\n}\n\n/**\n * Options for finding similar links.\n * @typedef {Object} FindSimilarOptions\n * @property {number} [numResults] - Number of search results to return. Default 10. Max 10 for basic plans.\n * @property {string[]} [includeDomains] - List of domains to include in the search.\n * @property {string[]} [excludeDomains] - List of domains to exclude from the search.\n * @property {string} [startCrawlDate] - Start date for results based on crawl date.\n * @property {string} [endCrawlDate] - End date for results based on crawl date.\n * @property {string} [startPublishedDate] - Start date for results based on published date.\n * @property {string} [endPublishedDate] - End date for results based on published date.\n * @property {boolean} [excludeSourceDomain] - If true, excludes links from the base domain of the input.\n * @property {string} [category] - A data category to focus on, with higher comprehensivity and data cleanliness. Currently, the only category is company.\n */\nexport type FindSimilarOptions = BaseSearchOptions & {\n excludeSourceDomain?: boolean;\n}\n\n/**\n * Search options for performing a search query.\n * @typedef {Object} ContentsOptions\n * @property {string[]} [formats] - An array of format types asked for. Currently supports `extract` (first 1000 tokens) and `text` (full parsed HTML text). If this isn't specified, defaults to `extract`.\n */\nexport type ContentsOptions = {\n text?: TextContentsOptions | boolean;\n highlights?: HighlightsContentsOptions | boolean;\n};\n\n// TODO; fill in\nexport type TextContentsOptions = {\n maxCharacters?: number;\n includeHtmlTags?: boolean;\n}\n\n// TODO; fill in docs\nexport type HighlightsContentsOptions = {\n query?: string;\n numSentences?: number;\n highlightsPerUrl?: number;\n}\n\n// TODO: fill in docs\ntype TextResponse = { text: string };\n// TODO: fill in docs\ntype HighlightsResponse = { highlights: string[], highlightScores: number[] };\n\n// TODO; fill in docs\ntype ContentsResultComponent<T extends ContentsOptions> =\n (T['text'] extends (object | true) ? TextResponse : {}) &\n (T['highlights'] extends (object | true) ? HighlightsResponse : {});\n\n/**\n * Represents a search result object.\n * @typedef {Object} Result\n * @property {string} title - The title of the search result.\n * @property {string} url - The URL of the search result.\n * @property {string} [publishedDate] - The estimated creation date of the content.\n * @property {string} [author] - The author of the content, if available.\n * @property {number} [score] - Similarity score between the query/url and the result.\n * @property {string} id - The temporary ID for the document.\n */\nexport type SearchResult<T extends ContentsOptions = {}> = {\n title: string | null;\n url: string;\n publishedDate?: string;\n author?: string;\n score?: number;\n id: string;\n} & ContentsResultComponent<T>;\n\n/**\n * Represents a search response object.\n * @typedef {Object} SearchResponse\n * @property {Result[]} results - The list of search results.\n * @property {string} [autopromptString] - The autoprompt string, if applicable.\n */\nexport type SearchResponse<T extends ContentsOptions = {}> = {\n results: SearchResult<T>[];\n autopromptString?: string;\n}\n\n/**\n * The Exa class encapsulates the API's endpoints.\n */\nclass Exa {\n private baseURL: string;\n private headers: Headers;\n\n /**\n * Constructs the Exa API client.\n * @param {string} apiKey - The API key for authentication.\n * @param {string} [baseURL] - The base URL of the Exa API.\n */\n constructor(\n apiKey?: string,\n baseURL: string = \"https://api.exa.ai\"\n ) {\n this.baseURL = baseURL;\n if (!apiKey) {\n apiKey = process.env.EXASEARCH_API_KEY;\n if (!apiKey) {\n throw new Error(\"API key must be provided as an argument or as an environment variable (EXASEARCH_API_KEY)\");\n }\n }\n this.headers = new Headers({\n \"x-api-key\": apiKey,\n \"Content-Type\": \"application/json\",\n \"User-Agent\": \"exa-node 1.0.27\",\n });\n }\n\n /**\n * Makes a request to the Exa API.\n * @param {string} endpoint - The API endpoint to call.\n * @param {string} method - The HTTP method to use.\n * @param {any} [body] - The request body for POST requests.\n * @returns {Promise<any>} The response from the API.\n */\n private async request(\n endpoint: string,\n method: string,\n body?: any\n ): Promise<any> {\n const response = await fetch(this.baseURL + endpoint, {\n method,\n headers: this.headers,\n body: body ? JSON.stringify(body) : undefined,\n });\n\n if (!response.ok) {\n const message = (await response.json()).error;\n throw new Error(\n `Request failed with status ${response.status}. ${message}`\n );\n }\n\n return await response.json();\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async search(query: string, options?: RegularSearchOptions): Promise<SearchResponse> {\n return await this.request(\"/search\", 'POST', { query, ...options });\n }\n\n /**\n * Performs a search with a Exa prompt-engineered query and returns the contents of the documents.\n * @param {string} query - The query string.\n * @param {SearchOptions} [options] - Additional search options.\n * @returns {Promise<SearchResponse>} A list of relevant search results.\n */\n async searchAndContents<T extends ContentsOptions>(query: string, options?: RegularSearchOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, ...rest } = options || {};\n return await this.request(\"/search\", 'POST', {\n query,\n contents: {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {})\n },\n ...rest\n });\n }\n\n /**\n * Finds similar links to the provided URL.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilar(\n url: string,\n options?: FindSimilarOptions\n ): Promise<SearchResponse> {\n return await this.request(\"/findSimilar\", 'POST', { url, ...options });\n }\n\n /**\n * Finds similar links to the provided URL and returns the contents of the documents.\n * @param {string} url - The URL for which to find similar links.\n * @param {FindSimilarOptions} [options] - Additional options for finding similar links.\n * @returns {Promise<SearchResponse>} A list of similar search results.\n */\n async findSimilarAndContents<T extends ContentsOptions>(url: string, options?: FindSimilarOptions & T): Promise<SearchResponse<T>> {\n const { text, highlights, ...rest } = options || {};\n return await this.request(\"/findSimilar\", 'POST', {\n url,\n contents: {\n ...(text ? { text } : {}),\n ...(highlights ? { highlights } : {})\n },\n ...rest\n });\n }\n\n /**\n * Retrieves contents of documents based on a list of document IDs.\n * @param {string | string[] | SearchResult[]} ids - An array of document IDs.\n * @param {ContentsOptions} [options] - Additional options for retrieving document contents.\n * @returns {Promise<GetContentsResponse>} A list of document contents.\n */\n async getContents<T extends ContentsOptions>(ids: string | string[] | SearchResult[], options?: T): Promise<SearchResponse<T>> {\n if (ids.length === 0) {\n throw new Error(\"Must provide at least one ID\");\n }\n let requestIds: string[];\n if (typeof ids === \"string\") {\n requestIds = [ids];\n } else if (typeof ids[0] === \"string\") {\n requestIds = ids as string[];\n } else {\n requestIds = (ids as SearchResult[]).map((result) => result.id);\n }\n return await this.request(`/contents`, 'POST', { ids: requestIds, ...options, });\n }\n}\n\nexport default Exa;\n"],"mappings":";AAAA,OAAO,SAAS,eAAe;AAwH/B,IAAM,MAAN,MAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASR,YACE,QACA,UAAkB,sBAClB;AACA,SAAK,UAAU;AACf,QAAI,CAAC,QAAQ;AACX,eAAS,QAAQ,IAAI;AACrB,UAAI,CAAC,QAAQ;AACT,cAAM,IAAI,MAAM,2FAA2F;AAAA,MAC/G;AAAA,IACF;AACA,SAAK,UAAU,IAAI,QAAQ;AAAA,MACzB,aAAa;AAAA,MACb,gBAAgB;AAAA,MAChB,cAAc;AAAA,IAChB,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAc,QACZ,UACA,QACA,MACc;AACd,UAAM,W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