langchain 0.0.159 → 0.0.161

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.
Files changed (62) hide show
  1. package/dist/embeddings/bedrock.cjs +73 -0
  2. package/dist/embeddings/bedrock.d.ts +47 -0
  3. package/dist/embeddings/bedrock.js +69 -0
  4. package/dist/experimental/hubs/makersuite/googlemakersuitehub.cjs +297 -0
  5. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +183 -0
  6. package/dist/experimental/hubs/makersuite/googlemakersuitehub.js +291 -0
  7. package/dist/load/import_constants.cjs +2 -0
  8. package/dist/load/import_constants.js +2 -0
  9. package/dist/load/import_map.cjs +4 -1
  10. package/dist/load/import_map.d.ts +3 -0
  11. package/dist/load/import_map.js +3 -0
  12. package/dist/retrievers/chaindesk.cjs +71 -0
  13. package/dist/retrievers/chaindesk.d.ts +18 -0
  14. package/dist/retrievers/chaindesk.js +67 -0
  15. package/dist/retrievers/contextual_compression.cjs +2 -2
  16. package/dist/retrievers/contextual_compression.js +2 -2
  17. package/dist/retrievers/databerry.cjs +1 -0
  18. package/dist/retrievers/databerry.d.ts +1 -0
  19. package/dist/retrievers/databerry.js +1 -0
  20. package/dist/retrievers/document_compressors/embeddings_filter.cjs +78 -0
  21. package/dist/retrievers/document_compressors/embeddings_filter.d.ts +39 -0
  22. package/dist/retrievers/document_compressors/embeddings_filter.js +74 -0
  23. package/dist/retrievers/document_compressors/index.cjs +33 -1
  24. package/dist/retrievers/document_compressors/index.d.ts +14 -1
  25. package/dist/retrievers/document_compressors/index.js +31 -0
  26. package/dist/retrievers/document_compressors/test/document_compressor.int.test.cjs +28 -0
  27. package/dist/retrievers/document_compressors/test/document_compressor.int.test.d.ts +1 -0
  28. package/dist/retrievers/document_compressors/test/document_compressor.int.test.js +26 -0
  29. package/dist/retrievers/tavily_search_api.cjs +144 -0
  30. package/dist/retrievers/tavily_search_api.d.ts +38 -0
  31. package/dist/retrievers/tavily_search_api.js +140 -0
  32. package/dist/schema/runnable/base.cjs +3 -0
  33. package/dist/schema/runnable/base.d.ts +1 -0
  34. package/dist/schema/runnable/base.js +3 -0
  35. package/dist/text_splitter.cjs +22 -8
  36. package/dist/text_splitter.d.ts +1 -0
  37. package/dist/text_splitter.js +22 -8
  38. package/dist/types/googlevertexai-types.d.ts +7 -5
  39. package/dist/util/googlevertexai-connection.cjs +35 -27
  40. package/dist/util/googlevertexai-connection.d.ts +12 -8
  41. package/dist/util/googlevertexai-connection.js +33 -26
  42. package/dist/util/googlevertexai-webauth.d.ts +2 -2
  43. package/dist/vectorstores/googlevertexai.d.ts +4 -4
  44. package/dist/vectorstores/pinecone.cjs +21 -34
  45. package/dist/vectorstores/pinecone.d.ts +6 -9
  46. package/dist/vectorstores/pinecone.js +21 -34
  47. package/embeddings/bedrock.cjs +1 -0
  48. package/embeddings/bedrock.d.ts +1 -0
  49. package/embeddings/bedrock.js +1 -0
  50. package/experimental/hubs/makersuite/googlemakersuitehub.cjs +1 -0
  51. package/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -0
  52. package/experimental/hubs/makersuite/googlemakersuitehub.js +1 -0
  53. package/package.json +48 -3
  54. package/retrievers/chaindesk.cjs +1 -0
  55. package/retrievers/chaindesk.d.ts +1 -0
  56. package/retrievers/chaindesk.js +1 -0
  57. package/retrievers/document_compressors/embeddings_filter.cjs +1 -0
  58. package/retrievers/document_compressors/embeddings_filter.d.ts +1 -0
  59. package/retrievers/document_compressors/embeddings_filter.js +1 -0
  60. package/retrievers/tavily_search_api.cjs +1 -0
  61. package/retrievers/tavily_search_api.d.ts +1 -0
  62. package/retrievers/tavily_search_api.js +1 -0
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EmbeddingsFilter = void 0;
4
+ const index_js_1 = require("./index.cjs");
5
+ const math_js_1 = require("../../util/math.cjs");
6
+ /**
7
+ * Class that represents a document compressor that uses embeddings to
8
+ * drop documents unrelated to the query.
9
+ */
10
+ class EmbeddingsFilter extends index_js_1.BaseDocumentCompressor {
11
+ constructor(params) {
12
+ super();
13
+ /**
14
+ * Embeddings to use for embedding document contents and queries.
15
+ */
16
+ Object.defineProperty(this, "embeddings", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ /**
23
+ * Similarity function for comparing documents.
24
+ */
25
+ Object.defineProperty(this, "similarityFn", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: math_js_1.cosineSimilarity
30
+ });
31
+ /**
32
+ * Threshold for determining when two documents are similar enough
33
+ * to be considered redundant. Must be specified if `k` is not set.
34
+ */
35
+ Object.defineProperty(this, "similarityThreshold", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
41
+ /**
42
+ * The number of relevant documents to return. Can be explicitly set to undefined, in which case
43
+ * similarity_threshold` must be specified. Defaults to 20
44
+ */
45
+ Object.defineProperty(this, "k", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: 20
50
+ });
51
+ this.embeddings = params.embeddings;
52
+ this.similarityFn = params.similarityFn ?? this.similarityFn;
53
+ this.similarityThreshold = params.similarityThreshold;
54
+ this.k = "k" in params ? params.k : this.k;
55
+ if (this.k === undefined && this.similarityThreshold === undefined) {
56
+ throw new Error(`Must specify one of "k" or "similarity_threshold".`);
57
+ }
58
+ }
59
+ async compressDocuments(documents, query) {
60
+ const embeddedDocuments = await this.embeddings.embedDocuments(documents.map((doc) => doc.pageContent));
61
+ const embeddedQuery = await this.embeddings.embedQuery(query);
62
+ const similarity = this.similarityFn([embeddedQuery], embeddedDocuments)[0];
63
+ let includedIdxs = Array.from({ length: embeddedDocuments.length }, (_, i) => i);
64
+ if (this.k !== undefined) {
65
+ includedIdxs = includedIdxs
66
+ .map((v, i) => [similarity[i], v])
67
+ .sort(([a], [b]) => b - a)
68
+ .slice(0, this.k)
69
+ .map(([, i]) => i);
70
+ }
71
+ if (this.similarityThreshold !== undefined) {
72
+ const threshold = this.similarityThreshold;
73
+ includedIdxs = includedIdxs.filter((i) => similarity[i] > threshold);
74
+ }
75
+ return includedIdxs.map((i) => documents[i]);
76
+ }
77
+ }
78
+ exports.EmbeddingsFilter = EmbeddingsFilter;
@@ -0,0 +1,39 @@
1
+ import { Document } from "../../document.js";
2
+ import { Embeddings } from "../../embeddings/base.js";
3
+ import { BaseDocumentCompressor } from "./index.js";
4
+ import { cosineSimilarity } from "../../util/math.js";
5
+ /**
6
+ * Interface for the parameters of the `EmbeddingsFilter` class.
7
+ */
8
+ export interface EmbeddingsFilterParams {
9
+ embeddings: Embeddings;
10
+ similarityFn?: (x: number[][], y: number[][]) => number[][];
11
+ similarityThreshold?: number;
12
+ k?: number;
13
+ }
14
+ /**
15
+ * Class that represents a document compressor that uses embeddings to
16
+ * drop documents unrelated to the query.
17
+ */
18
+ export declare class EmbeddingsFilter extends BaseDocumentCompressor {
19
+ /**
20
+ * Embeddings to use for embedding document contents and queries.
21
+ */
22
+ embeddings: Embeddings;
23
+ /**
24
+ * Similarity function for comparing documents.
25
+ */
26
+ similarityFn: typeof cosineSimilarity;
27
+ /**
28
+ * Threshold for determining when two documents are similar enough
29
+ * to be considered redundant. Must be specified if `k` is not set.
30
+ */
31
+ similarityThreshold?: number;
32
+ /**
33
+ * The number of relevant documents to return. Can be explicitly set to undefined, in which case
34
+ * similarity_threshold` must be specified. Defaults to 20
35
+ */
36
+ k?: number | undefined;
37
+ constructor(params: EmbeddingsFilterParams);
38
+ compressDocuments(documents: Document[], query: string): Promise<Document[]>;
39
+ }
@@ -0,0 +1,74 @@
1
+ import { BaseDocumentCompressor } from "./index.js";
2
+ import { cosineSimilarity } from "../../util/math.js";
3
+ /**
4
+ * Class that represents a document compressor that uses embeddings to
5
+ * drop documents unrelated to the query.
6
+ */
7
+ export class EmbeddingsFilter extends BaseDocumentCompressor {
8
+ constructor(params) {
9
+ super();
10
+ /**
11
+ * Embeddings to use for embedding document contents and queries.
12
+ */
13
+ Object.defineProperty(this, "embeddings", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ /**
20
+ * Similarity function for comparing documents.
21
+ */
22
+ Object.defineProperty(this, "similarityFn", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: cosineSimilarity
27
+ });
28
+ /**
29
+ * Threshold for determining when two documents are similar enough
30
+ * to be considered redundant. Must be specified if `k` is not set.
31
+ */
32
+ Object.defineProperty(this, "similarityThreshold", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ /**
39
+ * The number of relevant documents to return. Can be explicitly set to undefined, in which case
40
+ * similarity_threshold` must be specified. Defaults to 20
41
+ */
42
+ Object.defineProperty(this, "k", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: 20
47
+ });
48
+ this.embeddings = params.embeddings;
49
+ this.similarityFn = params.similarityFn ?? this.similarityFn;
50
+ this.similarityThreshold = params.similarityThreshold;
51
+ this.k = "k" in params ? params.k : this.k;
52
+ if (this.k === undefined && this.similarityThreshold === undefined) {
53
+ throw new Error(`Must specify one of "k" or "similarity_threshold".`);
54
+ }
55
+ }
56
+ async compressDocuments(documents, query) {
57
+ const embeddedDocuments = await this.embeddings.embedDocuments(documents.map((doc) => doc.pageContent));
58
+ const embeddedQuery = await this.embeddings.embedQuery(query);
59
+ const similarity = this.similarityFn([embeddedQuery], embeddedDocuments)[0];
60
+ let includedIdxs = Array.from({ length: embeddedDocuments.length }, (_, i) => i);
61
+ if (this.k !== undefined) {
62
+ includedIdxs = includedIdxs
63
+ .map((v, i) => [similarity[i], v])
64
+ .sort(([a], [b]) => b - a)
65
+ .slice(0, this.k)
66
+ .map(([, i]) => i);
67
+ }
68
+ if (this.similarityThreshold !== undefined) {
69
+ const threshold = this.similarityThreshold;
70
+ includedIdxs = includedIdxs.filter((i) => similarity[i] > threshold);
71
+ }
72
+ return includedIdxs.map((i) => documents[i]);
73
+ }
74
+ }
@@ -1,9 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseDocumentCompressor = void 0;
3
+ exports.DocumentCompressorPipeline = exports.BaseDocumentCompressor = void 0;
4
4
  /**
5
5
  * Base Document Compression class. All compressors should extend this class.
6
6
  */
7
7
  class BaseDocumentCompressor {
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ static isBaseDocumentCompressor(x) {
10
+ return x?.compressDocuments !== undefined;
11
+ }
8
12
  }
9
13
  exports.BaseDocumentCompressor = BaseDocumentCompressor;
14
+ /**
15
+ * Document compressor that uses a pipeline of Transformers.
16
+ */
17
+ class DocumentCompressorPipeline extends BaseDocumentCompressor {
18
+ constructor(fields) {
19
+ super();
20
+ Object.defineProperty(this, "transformers", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ this.transformers = fields.transformers;
27
+ }
28
+ async compressDocuments(documents, query, callbacks) {
29
+ let transformedDocuments = documents;
30
+ for (const transformer of this.transformers) {
31
+ if (BaseDocumentCompressor.isBaseDocumentCompressor(transformer)) {
32
+ transformedDocuments = await transformer.compressDocuments(transformedDocuments, query, callbacks);
33
+ }
34
+ else {
35
+ transformedDocuments = await transformer.transformDocuments(transformedDocuments);
36
+ }
37
+ }
38
+ return transformedDocuments;
39
+ }
40
+ }
41
+ exports.DocumentCompressorPipeline = DocumentCompressorPipeline;
@@ -1,4 +1,6 @@
1
1
  import { Document } from "../../document.js";
2
+ import { BaseDocumentTransformer } from "../../schema/document.js";
3
+ import { Callbacks } from "../../callbacks/manager.js";
2
4
  /**
3
5
  * Base Document Compression class. All compressors should extend this class.
4
6
  */
@@ -12,5 +14,16 @@ export declare abstract class BaseDocumentCompressor {
12
14
  * @param query A query string.
13
15
  * @returns A Promise that resolves with an array of compressed `Document` objects.
14
16
  */
15
- abstract compressDocuments(documents: Document[], query: string): Promise<Document[]>;
17
+ abstract compressDocuments(documents: Document[], query: string, callbacks?: Callbacks): Promise<Document[]>;
18
+ static isBaseDocumentCompressor(x: any): x is BaseDocumentCompressor;
19
+ }
20
+ /**
21
+ * Document compressor that uses a pipeline of Transformers.
22
+ */
23
+ export declare class DocumentCompressorPipeline extends BaseDocumentCompressor {
24
+ transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];
25
+ constructor(fields: {
26
+ transformers: (BaseDocumentTransformer | BaseDocumentCompressor)[];
27
+ });
28
+ compressDocuments(documents: Document[], query: string, callbacks?: Callbacks): Promise<Document[]>;
16
29
  }
@@ -2,4 +2,35 @@
2
2
  * Base Document Compression class. All compressors should extend this class.
3
3
  */
4
4
  export class BaseDocumentCompressor {
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ static isBaseDocumentCompressor(x) {
7
+ return x?.compressDocuments !== undefined;
8
+ }
9
+ }
10
+ /**
11
+ * Document compressor that uses a pipeline of Transformers.
12
+ */
13
+ export class DocumentCompressorPipeline extends BaseDocumentCompressor {
14
+ constructor(fields) {
15
+ super();
16
+ Object.defineProperty(this, "transformers", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ this.transformers = fields.transformers;
23
+ }
24
+ async compressDocuments(documents, query, callbacks) {
25
+ let transformedDocuments = documents;
26
+ for (const transformer of this.transformers) {
27
+ if (BaseDocumentCompressor.isBaseDocumentCompressor(transformer)) {
28
+ transformedDocuments = await transformer.compressDocuments(transformedDocuments, query, callbacks);
29
+ }
30
+ else {
31
+ transformedDocuments = await transformer.transformDocuments(transformedDocuments);
32
+ }
33
+ }
34
+ return transformedDocuments;
35
+ }
5
36
  }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const globals_1 = require("@jest/globals");
4
+ const openai_js_1 = require("../../../embeddings/openai.cjs");
5
+ const text_splitter_js_1 = require("../../../text_splitter.cjs");
6
+ const embeddings_filter_js_1 = require("../embeddings_filter.cjs");
7
+ const index_js_1 = require("../index.cjs");
8
+ const document_js_1 = require("../../../document.cjs");
9
+ (0, globals_1.test)("Test DocumentCompressorPipeline", async () => {
10
+ const embeddings = new openai_js_1.OpenAIEmbeddings();
11
+ const splitter = new text_splitter_js_1.RecursiveCharacterTextSplitter({
12
+ chunkSize: 30,
13
+ chunkOverlap: 0,
14
+ separators: [". "],
15
+ });
16
+ const relevantFilter = new embeddings_filter_js_1.EmbeddingsFilter({
17
+ embeddings,
18
+ similarityThreshold: 0.8,
19
+ });
20
+ const pipelineFilter = new index_js_1.DocumentCompressorPipeline({
21
+ transformers: [splitter, relevantFilter],
22
+ });
23
+ const texts = ["This sentence is about cows", "foo bar baz"];
24
+ const docs = [new document_js_1.Document({ pageContent: texts.join(". ") })];
25
+ const actual = await pipelineFilter.compressDocuments(docs, "Tell me about farm animals");
26
+ (0, globals_1.expect)(actual.length).toBe(1);
27
+ (0, globals_1.expect)(texts[0].includes(actual[0].pageContent)).toBeTruthy();
28
+ });
@@ -0,0 +1,26 @@
1
+ import { test, expect } from "@jest/globals";
2
+ import { OpenAIEmbeddings } from "../../../embeddings/openai.js";
3
+ import { RecursiveCharacterTextSplitter } from "../../../text_splitter.js";
4
+ import { EmbeddingsFilter } from "../embeddings_filter.js";
5
+ import { DocumentCompressorPipeline } from "../index.js";
6
+ import { Document } from "../../../document.js";
7
+ test("Test DocumentCompressorPipeline", async () => {
8
+ const embeddings = new OpenAIEmbeddings();
9
+ const splitter = new RecursiveCharacterTextSplitter({
10
+ chunkSize: 30,
11
+ chunkOverlap: 0,
12
+ separators: [". "],
13
+ });
14
+ const relevantFilter = new EmbeddingsFilter({
15
+ embeddings,
16
+ similarityThreshold: 0.8,
17
+ });
18
+ const pipelineFilter = new DocumentCompressorPipeline({
19
+ transformers: [splitter, relevantFilter],
20
+ });
21
+ const texts = ["This sentence is about cows", "foo bar baz"];
22
+ const docs = [new Document({ pageContent: texts.join(". ") })];
23
+ const actual = await pipelineFilter.compressDocuments(docs, "Tell me about farm animals");
24
+ expect(actual.length).toBe(1);
25
+ expect(texts[0].includes(actual[0].pageContent)).toBeTruthy();
26
+ });
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TavilySearchAPIRetriever = void 0;
4
+ const document_js_1 = require("../document.cjs");
5
+ const retriever_js_1 = require("../schema/retriever.cjs");
6
+ const env_js_1 = require("../util/env.cjs");
7
+ /**
8
+ * A class for retrieving documents related to a given search term
9
+ * using the Tavily Search API.
10
+ */
11
+ class TavilySearchAPIRetriever extends retriever_js_1.BaseRetriever {
12
+ static lc_name() {
13
+ return "TavilySearchAPIRetriever";
14
+ }
15
+ get lc_namespace() {
16
+ return ["langchain", "retrievers", "tavily_search_api"];
17
+ }
18
+ constructor(fields) {
19
+ super(fields);
20
+ Object.defineProperty(this, "k", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: 10
25
+ });
26
+ Object.defineProperty(this, "includeGeneratedAnswer", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: false
31
+ });
32
+ Object.defineProperty(this, "includeRawContent", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: false
37
+ });
38
+ Object.defineProperty(this, "includeImages", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: false
43
+ });
44
+ Object.defineProperty(this, "searchDepth", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: "basic"
49
+ });
50
+ Object.defineProperty(this, "includeDomains", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: void 0
55
+ });
56
+ Object.defineProperty(this, "excludeDomains", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: void 0
61
+ });
62
+ Object.defineProperty(this, "kwargs", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: {}
67
+ });
68
+ Object.defineProperty(this, "apiKey", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: void 0
73
+ });
74
+ this.k = fields?.k ?? this.k;
75
+ this.includeGeneratedAnswer =
76
+ fields?.includeGeneratedAnswer ?? this.includeGeneratedAnswer;
77
+ this.includeRawContent =
78
+ fields?.includeRawContent ?? this.includeRawContent;
79
+ this.includeImages = fields?.includeImages ?? this.includeImages;
80
+ this.searchDepth = fields?.searchDepth ?? this.searchDepth;
81
+ this.includeDomains = fields?.includeDomains ?? this.includeDomains;
82
+ this.excludeDomains = fields?.excludeDomains ?? this.excludeDomains;
83
+ this.kwargs = fields?.kwargs ?? this.kwargs;
84
+ this.apiKey = fields?.apiKey ?? (0, env_js_1.getEnvironmentVariable)("TAVILY_API_KEY");
85
+ if (this.apiKey === undefined) {
86
+ throw new Error(`No Tavily API key found. Either set an environment variable named "TAVILY_API_KEY" or pass an API key as "apiKey".`);
87
+ }
88
+ }
89
+ async _getRelevantDocuments(query, _runManager) {
90
+ const body = {
91
+ query,
92
+ include_answer: this.includeGeneratedAnswer,
93
+ include_raw_content: this.includeRawContent,
94
+ include_images: this.includeImages,
95
+ max_results: this.k,
96
+ search_depth: this.searchDepth,
97
+ api_key: this.apiKey,
98
+ };
99
+ if (this.includeDomains) {
100
+ body.include_domains = this.includeDomains;
101
+ }
102
+ if (this.excludeDomains) {
103
+ body.exclude_domains = this.excludeDomains;
104
+ }
105
+ const response = await fetch("https://api.tavily.com/search", {
106
+ method: "POST",
107
+ headers: {
108
+ "content-type": "application/json",
109
+ },
110
+ body: JSON.stringify({ ...body, ...this.kwargs }),
111
+ });
112
+ const json = await response.json();
113
+ if (!response.ok) {
114
+ throw new Error(`Request failed with status code ${response.status}: ${json.error}`);
115
+ }
116
+ if (!Array.isArray(json.results)) {
117
+ throw new Error(`Could not parse Tavily results. Please try again.`);
118
+ }
119
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
+ const docs = json.results.map((result) => {
121
+ const pageContent = this.includeRawContent
122
+ ? result.raw_content
123
+ : result.content;
124
+ const metadata = {
125
+ title: result.title,
126
+ source: result.url,
127
+ ...Object.fromEntries(Object.entries(result).filter(([k]) => !["content", "title", "url", "raw_content"].includes(k))),
128
+ images: json.images,
129
+ };
130
+ return new document_js_1.Document({ pageContent, metadata });
131
+ });
132
+ if (this.includeGeneratedAnswer) {
133
+ docs.push(new document_js_1.Document({
134
+ pageContent: json.answer,
135
+ metadata: {
136
+ title: "Suggested Answer",
137
+ source: "https://tavily.com/",
138
+ },
139
+ }));
140
+ }
141
+ return docs;
142
+ }
143
+ }
144
+ exports.TavilySearchAPIRetriever = TavilySearchAPIRetriever;
@@ -0,0 +1,38 @@
1
+ import { Document } from "../document.js";
2
+ import { CallbackManagerForRetrieverRun } from "../callbacks/manager.js";
3
+ import { BaseRetriever, type BaseRetrieverInput } from "../schema/retriever.js";
4
+ /**
5
+ * Options for the HydeRetriever class, which includes a BaseLanguageModel
6
+ * instance, a VectorStore instance, and an optional promptTemplate which
7
+ * can either be a BasePromptTemplate instance or a PromptKey.
8
+ */
9
+ export type TavilySearchAPIRetrieverFields = BaseRetrieverInput & {
10
+ k?: number;
11
+ includeGeneratedAnswer?: boolean;
12
+ includeRawContent?: boolean;
13
+ includeImages?: boolean;
14
+ searchDepth?: "basic" | "advanced";
15
+ includeDomains?: string[];
16
+ excludeDomains?: string[];
17
+ kwargs?: Record<string, unknown>;
18
+ apiKey?: string;
19
+ };
20
+ /**
21
+ * A class for retrieving documents related to a given search term
22
+ * using the Tavily Search API.
23
+ */
24
+ export declare class TavilySearchAPIRetriever extends BaseRetriever {
25
+ static lc_name(): string;
26
+ get lc_namespace(): string[];
27
+ k: number;
28
+ includeGeneratedAnswer: boolean;
29
+ includeRawContent: boolean;
30
+ includeImages: boolean;
31
+ searchDepth: string;
32
+ includeDomains?: string[];
33
+ excludeDomains?: string[];
34
+ kwargs: Record<string, unknown>;
35
+ apiKey?: string;
36
+ constructor(fields?: TavilySearchAPIRetrieverFields);
37
+ _getRelevantDocuments(query: string, _runManager?: CallbackManagerForRetrieverRun): Promise<Document[]>;
38
+ }
@@ -0,0 +1,140 @@
1
+ import { Document } from "../document.js";
2
+ import { BaseRetriever } from "../schema/retriever.js";
3
+ import { getEnvironmentVariable } from "../util/env.js";
4
+ /**
5
+ * A class for retrieving documents related to a given search term
6
+ * using the Tavily Search API.
7
+ */
8
+ export class TavilySearchAPIRetriever extends BaseRetriever {
9
+ static lc_name() {
10
+ return "TavilySearchAPIRetriever";
11
+ }
12
+ get lc_namespace() {
13
+ return ["langchain", "retrievers", "tavily_search_api"];
14
+ }
15
+ constructor(fields) {
16
+ super(fields);
17
+ Object.defineProperty(this, "k", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: 10
22
+ });
23
+ Object.defineProperty(this, "includeGeneratedAnswer", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: false
28
+ });
29
+ Object.defineProperty(this, "includeRawContent", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: false
34
+ });
35
+ Object.defineProperty(this, "includeImages", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: false
40
+ });
41
+ Object.defineProperty(this, "searchDepth", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: "basic"
46
+ });
47
+ Object.defineProperty(this, "includeDomains", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "excludeDomains", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "kwargs", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: {}
64
+ });
65
+ Object.defineProperty(this, "apiKey", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
71
+ this.k = fields?.k ?? this.k;
72
+ this.includeGeneratedAnswer =
73
+ fields?.includeGeneratedAnswer ?? this.includeGeneratedAnswer;
74
+ this.includeRawContent =
75
+ fields?.includeRawContent ?? this.includeRawContent;
76
+ this.includeImages = fields?.includeImages ?? this.includeImages;
77
+ this.searchDepth = fields?.searchDepth ?? this.searchDepth;
78
+ this.includeDomains = fields?.includeDomains ?? this.includeDomains;
79
+ this.excludeDomains = fields?.excludeDomains ?? this.excludeDomains;
80
+ this.kwargs = fields?.kwargs ?? this.kwargs;
81
+ this.apiKey = fields?.apiKey ?? getEnvironmentVariable("TAVILY_API_KEY");
82
+ if (this.apiKey === undefined) {
83
+ throw new Error(`No Tavily API key found. Either set an environment variable named "TAVILY_API_KEY" or pass an API key as "apiKey".`);
84
+ }
85
+ }
86
+ async _getRelevantDocuments(query, _runManager) {
87
+ const body = {
88
+ query,
89
+ include_answer: this.includeGeneratedAnswer,
90
+ include_raw_content: this.includeRawContent,
91
+ include_images: this.includeImages,
92
+ max_results: this.k,
93
+ search_depth: this.searchDepth,
94
+ api_key: this.apiKey,
95
+ };
96
+ if (this.includeDomains) {
97
+ body.include_domains = this.includeDomains;
98
+ }
99
+ if (this.excludeDomains) {
100
+ body.exclude_domains = this.excludeDomains;
101
+ }
102
+ const response = await fetch("https://api.tavily.com/search", {
103
+ method: "POST",
104
+ headers: {
105
+ "content-type": "application/json",
106
+ },
107
+ body: JSON.stringify({ ...body, ...this.kwargs }),
108
+ });
109
+ const json = await response.json();
110
+ if (!response.ok) {
111
+ throw new Error(`Request failed with status code ${response.status}: ${json.error}`);
112
+ }
113
+ if (!Array.isArray(json.results)) {
114
+ throw new Error(`Could not parse Tavily results. Please try again.`);
115
+ }
116
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
+ const docs = json.results.map((result) => {
118
+ const pageContent = this.includeRawContent
119
+ ? result.raw_content
120
+ : result.content;
121
+ const metadata = {
122
+ title: result.title,
123
+ source: result.url,
124
+ ...Object.fromEntries(Object.entries(result).filter(([k]) => !["content", "title", "url", "raw_content"].includes(k))),
125
+ images: json.images,
126
+ };
127
+ return new Document({ pageContent, metadata });
128
+ });
129
+ if (this.includeGeneratedAnswer) {
130
+ docs.push(new Document({
131
+ pageContent: json.answer,
132
+ metadata: {
133
+ title: "Suggested Answer",
134
+ source: "https://tavily.com/",
135
+ },
136
+ }));
137
+ }
138
+ return docs;
139
+ }
140
+ }