langchain 0.0.146 → 0.0.148

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 (168) hide show
  1. package/chat_models/googlevertexai/web.cjs +1 -0
  2. package/chat_models/googlevertexai/web.d.ts +1 -0
  3. package/chat_models/googlevertexai/web.js +1 -0
  4. package/chat_models/googlevertexai.cjs +1 -1
  5. package/chat_models/googlevertexai.d.ts +1 -1
  6. package/chat_models/googlevertexai.js +1 -1
  7. package/dist/base_language/index.cjs +2 -2
  8. package/dist/base_language/index.d.ts +2 -1
  9. package/dist/base_language/index.js +1 -1
  10. package/dist/chains/base.d.ts +1 -1
  11. package/dist/chains/constitutional_ai/constitutional_principle.cjs +272 -1
  12. package/dist/chains/constitutional_ai/constitutional_principle.js +272 -1
  13. package/dist/chains/openai_functions/openapi.cjs +32 -27
  14. package/dist/chains/openai_functions/openapi.d.ts +9 -0
  15. package/dist/chains/openai_functions/openapi.js +31 -27
  16. package/dist/chat_models/base.d.ts +1 -1
  17. package/dist/chat_models/{googlevertexai.cjs → googlevertexai/common.cjs} +14 -26
  18. package/dist/chat_models/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -22
  19. package/dist/chat_models/{googlevertexai.js → googlevertexai/common.js} +12 -24
  20. package/dist/chat_models/googlevertexai/index.cjs +36 -0
  21. package/dist/chat_models/googlevertexai/index.d.ts +21 -0
  22. package/dist/chat_models/googlevertexai/index.js +31 -0
  23. package/dist/chat_models/googlevertexai/web.cjs +33 -0
  24. package/dist/chat_models/googlevertexai/web.d.ts +19 -0
  25. package/dist/chat_models/googlevertexai/web.js +28 -0
  26. package/dist/chat_models/openai.cjs +1 -1
  27. package/dist/chat_models/openai.js +1 -1
  28. package/dist/document_loaders/web/notionapi.cjs +93 -70
  29. package/dist/document_loaders/web/notionapi.d.ts +33 -1
  30. package/dist/document_loaders/web/notionapi.js +89 -71
  31. package/dist/embeddings/googlevertexai.cjs +5 -1
  32. package/dist/embeddings/googlevertexai.d.ts +2 -1
  33. package/dist/embeddings/googlevertexai.js +5 -1
  34. package/dist/evaluation/agents/index.cjs +17 -0
  35. package/dist/evaluation/agents/index.d.ts +1 -0
  36. package/dist/evaluation/agents/index.js +1 -0
  37. package/dist/evaluation/agents/prompt.cjs +132 -0
  38. package/dist/evaluation/agents/prompt.d.ts +6 -0
  39. package/dist/evaluation/agents/prompt.js +129 -0
  40. package/dist/evaluation/agents/trajectory.cjs +189 -0
  41. package/dist/evaluation/agents/trajectory.d.ts +54 -0
  42. package/dist/evaluation/agents/trajectory.js +184 -0
  43. package/dist/evaluation/base.cjs +274 -0
  44. package/dist/evaluation/base.d.ts +232 -0
  45. package/dist/evaluation/base.js +263 -0
  46. package/dist/evaluation/comparison/index.cjs +17 -0
  47. package/dist/evaluation/comparison/index.d.ts +1 -0
  48. package/dist/evaluation/comparison/index.js +1 -0
  49. package/dist/evaluation/comparison/pairwise.cjs +244 -0
  50. package/dist/evaluation/comparison/pairwise.d.ts +50 -0
  51. package/dist/evaluation/comparison/pairwise.js +238 -0
  52. package/dist/evaluation/comparison/prompt.cjs +74 -0
  53. package/dist/evaluation/comparison/prompt.d.ts +21 -0
  54. package/dist/evaluation/comparison/prompt.js +71 -0
  55. package/dist/evaluation/criteria/criteria.cjs +259 -0
  56. package/dist/evaluation/criteria/criteria.d.ts +73 -0
  57. package/dist/evaluation/criteria/criteria.js +253 -0
  58. package/dist/evaluation/criteria/index.cjs +17 -0
  59. package/dist/evaluation/criteria/index.d.ts +1 -0
  60. package/dist/evaluation/criteria/index.js +1 -0
  61. package/dist/evaluation/criteria/prompt.cjs +36 -0
  62. package/dist/evaluation/criteria/prompt.d.ts +12 -0
  63. package/dist/evaluation/criteria/prompt.js +33 -0
  64. package/dist/evaluation/embedding_distance/base.cjs +163 -0
  65. package/dist/evaluation/embedding_distance/base.d.ts +78 -0
  66. package/dist/evaluation/embedding_distance/base.js +156 -0
  67. package/dist/evaluation/embedding_distance/index.cjs +17 -0
  68. package/dist/evaluation/embedding_distance/index.d.ts +1 -0
  69. package/dist/evaluation/embedding_distance/index.js +1 -0
  70. package/dist/evaluation/index.cjs +6 -0
  71. package/dist/evaluation/index.d.ts +6 -0
  72. package/dist/evaluation/index.js +6 -0
  73. package/dist/evaluation/loader.cjs +60 -0
  74. package/dist/evaluation/loader.d.ts +27 -0
  75. package/dist/evaluation/loader.js +56 -0
  76. package/dist/evaluation/types.cjs +2 -0
  77. package/dist/evaluation/types.d.ts +35 -0
  78. package/dist/evaluation/types.js +1 -0
  79. package/dist/experimental/llms/bittensor.cjs +141 -0
  80. package/dist/experimental/llms/bittensor.d.ts +33 -0
  81. package/dist/experimental/llms/bittensor.js +137 -0
  82. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +5 -1
  83. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
  84. package/dist/experimental/multimodal_embeddings/googlevertexai.js +5 -1
  85. package/dist/hub.d.ts +1 -1
  86. package/dist/llms/base.d.ts +1 -1
  87. package/dist/llms/{googlevertexai.js → googlevertexai/common.cjs} +21 -17
  88. package/dist/llms/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -23
  89. package/dist/llms/{googlevertexai.cjs → googlevertexai/common.js} +17 -21
  90. package/dist/llms/googlevertexai/index.cjs +34 -0
  91. package/dist/llms/googlevertexai/index.d.ts +26 -0
  92. package/dist/llms/googlevertexai/index.js +30 -0
  93. package/dist/llms/googlevertexai/web.cjs +31 -0
  94. package/dist/llms/googlevertexai/web.d.ts +24 -0
  95. package/dist/llms/googlevertexai/web.js +27 -0
  96. package/dist/llms/openai-chat.cjs +1 -1
  97. package/dist/llms/openai-chat.js +1 -1
  98. package/dist/llms/openai.cjs +1 -1
  99. package/dist/llms/openai.js +1 -1
  100. package/dist/load/import_constants.cjs +3 -0
  101. package/dist/load/import_constants.js +3 -0
  102. package/dist/load/import_map.cjs +3 -2
  103. package/dist/load/import_map.d.ts +2 -1
  104. package/dist/load/import_map.js +2 -1
  105. package/dist/load/index.cjs +2 -1
  106. package/dist/load/index.js +2 -1
  107. package/dist/load/serializable.cjs +23 -4
  108. package/dist/load/serializable.js +23 -4
  109. package/dist/prompts/base.cjs +2 -2
  110. package/dist/prompts/base.d.ts +1 -1
  111. package/dist/prompts/base.js +1 -1
  112. package/dist/prompts/chat.cjs +2 -2
  113. package/dist/prompts/chat.d.ts +1 -1
  114. package/dist/prompts/chat.js +1 -1
  115. package/dist/retrievers/multi_query.cjs +140 -0
  116. package/dist/retrievers/multi_query.d.ts +33 -0
  117. package/dist/retrievers/multi_query.js +136 -0
  118. package/dist/schema/document.cjs +2 -2
  119. package/dist/schema/document.d.ts +1 -1
  120. package/dist/schema/document.js +1 -1
  121. package/dist/schema/output_parser.cjs +2 -2
  122. package/dist/schema/output_parser.d.ts +2 -1
  123. package/dist/schema/output_parser.js +1 -1
  124. package/dist/schema/retriever.cjs +2 -2
  125. package/dist/schema/retriever.d.ts +2 -1
  126. package/dist/schema/retriever.js +1 -1
  127. package/dist/schema/runnable/config.cjs +8 -0
  128. package/dist/schema/runnable/config.d.ts +3 -0
  129. package/dist/schema/runnable/config.js +4 -0
  130. package/dist/schema/{runnable.cjs → runnable/index.cjs} +290 -101
  131. package/dist/schema/{runnable.d.ts → runnable/index.d.ts} +127 -41
  132. package/dist/schema/{runnable.js → runnable/index.js} +284 -99
  133. package/dist/tools/base.d.ts +1 -1
  134. package/dist/types/googlevertexai-types.d.ts +11 -4
  135. package/dist/util/async_caller.cjs +35 -25
  136. package/dist/util/async_caller.d.ts +8 -0
  137. package/dist/util/async_caller.js +35 -25
  138. package/dist/util/googlevertexai-connection.cjs +14 -15
  139. package/dist/util/googlevertexai-connection.d.ts +7 -7
  140. package/dist/util/googlevertexai-connection.js +14 -15
  141. package/dist/util/googlevertexai-webauth.cjs +56 -0
  142. package/dist/util/googlevertexai-webauth.d.ts +25 -0
  143. package/dist/util/googlevertexai-webauth.js +52 -0
  144. package/dist/vectorstores/googlevertexai.cjs +9 -8
  145. package/dist/vectorstores/googlevertexai.d.ts +8 -7
  146. package/dist/vectorstores/googlevertexai.js +9 -8
  147. package/dist/vectorstores/pinecone.cjs +30 -22
  148. package/dist/vectorstores/pinecone.d.ts +3 -1
  149. package/dist/vectorstores/pinecone.js +30 -22
  150. package/dist/vectorstores/vectara.cjs +20 -23
  151. package/dist/vectorstores/vectara.d.ts +9 -2
  152. package/dist/vectorstores/vectara.js +20 -23
  153. package/experimental/llms/bittensor.cjs +1 -0
  154. package/experimental/llms/bittensor.d.ts +1 -0
  155. package/experimental/llms/bittensor.js +1 -0
  156. package/llms/googlevertexai/web.cjs +1 -0
  157. package/llms/googlevertexai/web.d.ts +1 -0
  158. package/llms/googlevertexai/web.js +1 -0
  159. package/llms/googlevertexai.cjs +1 -1
  160. package/llms/googlevertexai.d.ts +1 -1
  161. package/llms/googlevertexai.js +1 -1
  162. package/package.json +40 -3
  163. package/retrievers/multi_query.cjs +1 -0
  164. package/retrievers/multi_query.d.ts +1 -0
  165. package/retrievers/multi_query.js +1 -0
  166. package/schema/runnable.cjs +1 -1
  167. package/schema/runnable.d.ts +1 -1
  168. package/schema/runnable.js +1 -1
@@ -12,6 +12,32 @@ const STATUS_NO_RETRY = [
12
12
  408,
13
13
  409, // Conflict
14
14
  ];
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ const defaultFailedAttemptHandler = (error) => {
17
+ if (error.message.startsWith("Cancel") ||
18
+ error.message.startsWith("TimeoutError") ||
19
+ error.name === "TimeoutError" ||
20
+ error.message.startsWith("AbortError") ||
21
+ error.name === "AbortError") {
22
+ throw error;
23
+ }
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ if (error?.code === "ECONNABORTED") {
26
+ throw error;
27
+ }
28
+ const status =
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
+ error?.response?.status ?? error?.status;
31
+ if (status && STATUS_NO_RETRY.includes(+status)) {
32
+ throw error;
33
+ }
34
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
+ if (error?.error?.code === "insufficient_quota") {
36
+ const err = new Error(error?.message);
37
+ err.name = "InsufficientQuotaError";
38
+ throw err;
39
+ }
40
+ };
15
41
  /**
16
42
  * A class that can be used to make async calls with concurrency and retry logic.
17
43
  *
@@ -39,6 +65,12 @@ export class AsyncCaller {
39
65
  writable: true,
40
66
  value: void 0
41
67
  });
68
+ Object.defineProperty(this, "onFailedAttempt", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: void 0
73
+ });
42
74
  Object.defineProperty(this, "queue", {
43
75
  enumerable: true,
44
76
  configurable: true,
@@ -47,6 +79,8 @@ export class AsyncCaller {
47
79
  });
48
80
  this.maxConcurrency = params.maxConcurrency ?? Infinity;
49
81
  this.maxRetries = params.maxRetries ?? 6;
82
+ this.onFailedAttempt =
83
+ params.onFailedAttempt ?? defaultFailedAttemptHandler;
50
84
  const PQueue = "default" in PQueueMod ? PQueueMod.default : PQueueMod;
51
85
  this.queue = new PQueue({ concurrency: this.maxConcurrency });
52
86
  }
@@ -61,31 +95,7 @@ export class AsyncCaller {
61
95
  throw new Error(error);
62
96
  }
63
97
  }), {
64
- onFailedAttempt(error) {
65
- if (error.message.startsWith("Cancel") ||
66
- error.message.startsWith("TimeoutError") ||
67
- error.name === "TimeoutError" ||
68
- error.message.startsWith("AbortError") ||
69
- error.name === "AbortError") {
70
- throw error;
71
- }
72
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
- if (error?.code === "ECONNABORTED") {
74
- throw error;
75
- }
76
- const status =
77
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
- error?.response?.status ?? error?.status;
79
- if (status && STATUS_NO_RETRY.includes(+status)) {
80
- throw error;
81
- }
82
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
- if (error?.error?.code === "insufficient_quota") {
84
- const err = new Error(error?.message);
85
- err.name = "InsufficientQuotaError";
86
- throw err;
87
- }
88
- },
98
+ onFailedAttempt: this.onFailedAttempt,
89
99
  retries: this.maxRetries,
90
100
  randomize: true,
91
101
  // If needed we can change some of the defaults here,
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GoogleVertexAILLMConnection = exports.GoogleVertexAIConnection = void 0;
4
- const google_auth_library_1 = require("google-auth-library");
5
4
  class GoogleVertexAIConnection {
6
- constructor(fields, caller) {
5
+ constructor(fields, caller, client) {
7
6
  Object.defineProperty(this, "caller", {
8
7
  enumerable: true,
9
8
  configurable: true,
@@ -28,7 +27,7 @@ class GoogleVertexAIConnection {
28
27
  writable: true,
29
28
  value: "v1"
30
29
  });
31
- Object.defineProperty(this, "auth", {
30
+ Object.defineProperty(this, "client", {
32
31
  enumerable: true,
33
32
  configurable: true,
34
33
  writable: true,
@@ -38,16 +37,12 @@ class GoogleVertexAIConnection {
38
37
  this.endpoint = fields?.endpoint ?? this.endpoint;
39
38
  this.location = fields?.location ?? this.location;
40
39
  this.apiVersion = fields?.apiVersion ?? this.apiVersion;
41
- this.auth = new google_auth_library_1.GoogleAuth({
42
- scopes: "https://www.googleapis.com/auth/cloud-platform",
43
- ...fields?.authOptions,
44
- });
40
+ this.client = client;
45
41
  }
46
42
  buildMethod() {
47
43
  return "POST";
48
44
  }
49
45
  async _request(data, options) {
50
- const client = await this.auth.getClient();
51
46
  const url = await this.buildUrl();
52
47
  const method = this.buildMethod();
53
48
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -58,11 +53,8 @@ class GoogleVertexAIConnection {
58
53
  if (data && method === "POST") {
59
54
  opts.data = data;
60
55
  }
61
- async function _request() {
62
- return client.request(opts);
63
- }
64
56
  try {
65
- const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, _request.bind(client));
57
+ const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, async () => this.client.request(opts));
66
58
  const response = callResponse; // Done for typecast safety, I guess
67
59
  return response;
68
60
  }
@@ -74,18 +66,25 @@ class GoogleVertexAIConnection {
74
66
  }
75
67
  exports.GoogleVertexAIConnection = GoogleVertexAIConnection;
76
68
  class GoogleVertexAILLMConnection extends GoogleVertexAIConnection {
77
- constructor(fields, caller) {
78
- super(fields, caller);
69
+ constructor(fields, caller, client) {
70
+ super(fields, caller, client);
79
71
  Object.defineProperty(this, "model", {
80
72
  enumerable: true,
81
73
  configurable: true,
82
74
  writable: true,
83
75
  value: void 0
84
76
  });
77
+ Object.defineProperty(this, "client", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ this.client = client;
85
84
  this.model = fields?.model ?? this.model;
86
85
  }
87
86
  async buildUrl() {
88
- const projectId = await this.auth.getProjectId();
87
+ const projectId = await this.client.getProjectId();
89
88
  const url = `https://${this.endpoint}/v1/projects/${projectId}/locations/${this.location}/publishers/google/models/${this.model}:predict`;
90
89
  return url;
91
90
  }
@@ -1,21 +1,21 @@
1
- import { GoogleAuth } from "google-auth-library";
2
1
  import { BaseLanguageModelCallOptions } from "../base_language/index.js";
3
2
  import { AsyncCaller, AsyncCallerCallOptions } from "./async_caller.js";
4
- import { GoogleVertexAIBaseLLMInput, GoogleVertexAIBasePrediction, GoogleVertexAIConnectionParams, GoogleVertexAILLMResponse, GoogleVertexAIModelParams, GoogleVertexAIResponse } from "../types/googlevertexai-types.js";
5
- export declare abstract class GoogleVertexAIConnection<CallOptions extends AsyncCallerCallOptions, ResponseType extends GoogleVertexAIResponse> implements GoogleVertexAIConnectionParams {
3
+ import type { GoogleVertexAIBaseLLMInput, GoogleVertexAIBasePrediction, GoogleVertexAIConnectionParams, GoogleVertexAILLMResponse, GoogleVertexAIModelParams, GoogleVertexAIResponse, GoogleVertexAIAbstractedClient } from "../types/googlevertexai-types.js";
4
+ export declare abstract class GoogleVertexAIConnection<CallOptions extends AsyncCallerCallOptions, ResponseType extends GoogleVertexAIResponse, AuthOptions> implements GoogleVertexAIConnectionParams<AuthOptions> {
6
5
  caller: AsyncCaller;
7
6
  endpoint: string;
8
7
  location: string;
9
8
  apiVersion: string;
10
- auth: GoogleAuth;
11
- constructor(fields: GoogleVertexAIConnectionParams | undefined, caller: AsyncCaller);
9
+ client: GoogleVertexAIAbstractedClient;
10
+ constructor(fields: GoogleVertexAIConnectionParams<AuthOptions> | undefined, caller: AsyncCaller, client: GoogleVertexAIAbstractedClient);
12
11
  abstract buildUrl(): Promise<string>;
13
12
  buildMethod(): string;
14
13
  _request(data: unknown | undefined, options: CallOptions): Promise<ResponseType>;
15
14
  }
16
- export declare class GoogleVertexAILLMConnection<CallOptions extends BaseLanguageModelCallOptions, InstanceType, PredictionType extends GoogleVertexAIBasePrediction> extends GoogleVertexAIConnection<CallOptions, PredictionType> implements GoogleVertexAIBaseLLMInput {
15
+ export declare class GoogleVertexAILLMConnection<CallOptions extends BaseLanguageModelCallOptions, InstanceType, PredictionType extends GoogleVertexAIBasePrediction, AuthOptions> extends GoogleVertexAIConnection<CallOptions, PredictionType, AuthOptions> implements GoogleVertexAIBaseLLMInput<AuthOptions> {
17
16
  model: string;
18
- constructor(fields: GoogleVertexAIBaseLLMInput | undefined, caller: AsyncCaller);
17
+ client: GoogleVertexAIAbstractedClient;
18
+ constructor(fields: GoogleVertexAIBaseLLMInput<AuthOptions> | undefined, caller: AsyncCaller, client: GoogleVertexAIAbstractedClient);
19
19
  buildUrl(): Promise<string>;
20
20
  request(instances: InstanceType[], parameters: GoogleVertexAIModelParams, options: CallOptions): Promise<GoogleVertexAILLMResponse<PredictionType>>;
21
21
  }
@@ -1,6 +1,5 @@
1
- import { GoogleAuth } from "google-auth-library";
2
1
  export class GoogleVertexAIConnection {
3
- constructor(fields, caller) {
2
+ constructor(fields, caller, client) {
4
3
  Object.defineProperty(this, "caller", {
5
4
  enumerable: true,
6
5
  configurable: true,
@@ -25,7 +24,7 @@ export class GoogleVertexAIConnection {
25
24
  writable: true,
26
25
  value: "v1"
27
26
  });
28
- Object.defineProperty(this, "auth", {
27
+ Object.defineProperty(this, "client", {
29
28
  enumerable: true,
30
29
  configurable: true,
31
30
  writable: true,
@@ -35,16 +34,12 @@ export class GoogleVertexAIConnection {
35
34
  this.endpoint = fields?.endpoint ?? this.endpoint;
36
35
  this.location = fields?.location ?? this.location;
37
36
  this.apiVersion = fields?.apiVersion ?? this.apiVersion;
38
- this.auth = new GoogleAuth({
39
- scopes: "https://www.googleapis.com/auth/cloud-platform",
40
- ...fields?.authOptions,
41
- });
37
+ this.client = client;
42
38
  }
43
39
  buildMethod() {
44
40
  return "POST";
45
41
  }
46
42
  async _request(data, options) {
47
- const client = await this.auth.getClient();
48
43
  const url = await this.buildUrl();
49
44
  const method = this.buildMethod();
50
45
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -55,11 +50,8 @@ export class GoogleVertexAIConnection {
55
50
  if (data && method === "POST") {
56
51
  opts.data = data;
57
52
  }
58
- async function _request() {
59
- return client.request(opts);
60
- }
61
53
  try {
62
- const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, _request.bind(client));
54
+ const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, async () => this.client.request(opts));
63
55
  const response = callResponse; // Done for typecast safety, I guess
64
56
  return response;
65
57
  }
@@ -70,18 +62,25 @@ export class GoogleVertexAIConnection {
70
62
  }
71
63
  }
72
64
  export class GoogleVertexAILLMConnection extends GoogleVertexAIConnection {
73
- constructor(fields, caller) {
74
- super(fields, caller);
65
+ constructor(fields, caller, client) {
66
+ super(fields, caller, client);
75
67
  Object.defineProperty(this, "model", {
76
68
  enumerable: true,
77
69
  configurable: true,
78
70
  writable: true,
79
71
  value: void 0
80
72
  });
73
+ Object.defineProperty(this, "client", {
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true,
77
+ value: void 0
78
+ });
79
+ this.client = client;
81
80
  this.model = fields?.model ?? this.model;
82
81
  }
83
82
  async buildUrl() {
84
- const projectId = await this.auth.getProjectId();
83
+ const projectId = await this.client.getProjectId();
85
84
  const url = `https://${this.endpoint}/v1/projects/${projectId}/locations/${this.location}/publishers/google/models/${this.model}:predict`;
86
85
  return url;
87
86
  }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebGoogleAuth = void 0;
4
+ const google_1 = require("web-auth-library/google");
5
+ const env_js_1 = require("./env.cjs");
6
+ class WebGoogleAuth {
7
+ constructor(options) {
8
+ Object.defineProperty(this, "options", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: void 0
13
+ });
14
+ const credentials = options?.credentials ??
15
+ (0, env_js_1.getEnvironmentVariable)("GOOGLE_VERTEX_AI_WEB_CREDENTIALS");
16
+ if (credentials === undefined)
17
+ throw new Error(`Credentials not found. Please set the GOOGLE_VERTEX_AI_WEB_CREDENTIALS or pass credentials into "authOptions.credentials".`);
18
+ const scope = options?.scope ?? "https://www.googleapis.com/auth/cloud-platform";
19
+ this.options = { ...options, credentials, scope };
20
+ }
21
+ async getProjectId() {
22
+ const credentials = (0, google_1.getCredentials)(this.options.credentials);
23
+ return credentials.project_id;
24
+ }
25
+ async request(opts) {
26
+ const accessToken = await (0, google_1.getAccessToken)(this.options);
27
+ if (opts.url == null)
28
+ throw new Error("Missing URL");
29
+ const fetchOptions = {
30
+ method: opts.method,
31
+ headers: {
32
+ Authorization: `Bearer ${accessToken}`,
33
+ "Content-Type": "application/json",
34
+ },
35
+ };
36
+ if (opts.data !== undefined) {
37
+ fetchOptions.body = JSON.stringify(opts.data);
38
+ }
39
+ const res = await fetch(opts.url, fetchOptions);
40
+ if (!res.ok) {
41
+ const error = new Error(`Could not get access token for Vertex AI with status code: ${res.status}`);
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ error.response = res;
44
+ throw error;
45
+ }
46
+ return {
47
+ data: await res.json(),
48
+ config: {},
49
+ status: res.status,
50
+ statusText: res.statusText,
51
+ headers: res.headers,
52
+ request: { responseURL: res.url },
53
+ };
54
+ }
55
+ }
56
+ exports.WebGoogleAuth = WebGoogleAuth;
@@ -0,0 +1,25 @@
1
+ import { Credentials } from "web-auth-library/google";
2
+ import type { GoogleVertexAIAbstractedClient } from "../types/googlevertexai-types.js";
3
+ export type WebGoogleAuthOptions = {
4
+ credentials: string | Credentials;
5
+ scope?: string | string[];
6
+ };
7
+ export declare class WebGoogleAuth implements GoogleVertexAIAbstractedClient {
8
+ options: WebGoogleAuthOptions;
9
+ constructor(options?: WebGoogleAuthOptions);
10
+ getProjectId(): Promise<string>;
11
+ request(opts: {
12
+ url?: string;
13
+ method?: string;
14
+ data?: unknown;
15
+ }): Promise<{
16
+ data: any;
17
+ config: {};
18
+ status: number;
19
+ statusText: string;
20
+ headers: Headers;
21
+ request: {
22
+ responseURL: string;
23
+ };
24
+ }>;
25
+ }
@@ -0,0 +1,52 @@
1
+ import { getAccessToken, getCredentials, } from "web-auth-library/google";
2
+ import { getEnvironmentVariable } from "./env.js";
3
+ export class WebGoogleAuth {
4
+ constructor(options) {
5
+ Object.defineProperty(this, "options", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: void 0
10
+ });
11
+ const credentials = options?.credentials ??
12
+ getEnvironmentVariable("GOOGLE_VERTEX_AI_WEB_CREDENTIALS");
13
+ if (credentials === undefined)
14
+ throw new Error(`Credentials not found. Please set the GOOGLE_VERTEX_AI_WEB_CREDENTIALS or pass credentials into "authOptions.credentials".`);
15
+ const scope = options?.scope ?? "https://www.googleapis.com/auth/cloud-platform";
16
+ this.options = { ...options, credentials, scope };
17
+ }
18
+ async getProjectId() {
19
+ const credentials = getCredentials(this.options.credentials);
20
+ return credentials.project_id;
21
+ }
22
+ async request(opts) {
23
+ const accessToken = await getAccessToken(this.options);
24
+ if (opts.url == null)
25
+ throw new Error("Missing URL");
26
+ const fetchOptions = {
27
+ method: opts.method,
28
+ headers: {
29
+ Authorization: `Bearer ${accessToken}`,
30
+ "Content-Type": "application/json",
31
+ },
32
+ };
33
+ if (opts.data !== undefined) {
34
+ fetchOptions.body = JSON.stringify(opts.data);
35
+ }
36
+ const res = await fetch(opts.url, fetchOptions);
37
+ if (!res.ok) {
38
+ const error = new Error(`Could not get access token for Vertex AI with status code: ${res.status}`);
39
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
+ error.response = res;
41
+ throw error;
42
+ }
43
+ return {
44
+ data: await res.json(),
45
+ config: {},
46
+ status: res.status,
47
+ statusText: res.statusText,
48
+ headers: res.headers,
49
+ request: { responseURL: res.url },
50
+ };
51
+ }
52
+ }
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.MatchingEngine = exports.IdDocument = void 0;
30
30
  const uuid = __importStar(require("uuid"));
31
31
  const flat_1 = __importDefault(require("flat"));
32
+ const google_auth_library_1 = require("google-auth-library");
32
33
  const base_js_1 = require("./base.cjs");
33
34
  const document_js_1 = require("../document.cjs");
34
35
  const googlevertexai_connection_js_1 = require("../util/googlevertexai-connection.cjs");
@@ -51,7 +52,7 @@ class IdDocument extends document_js_1.Document {
51
52
  exports.IdDocument = IdDocument;
52
53
  class IndexEndpointConnection extends googlevertexai_connection_js_1.GoogleVertexAIConnection {
53
54
  constructor(fields, caller) {
54
- super(fields, caller);
55
+ super(fields, caller, new google_auth_library_1.GoogleAuth(fields.authOptions));
55
56
  Object.defineProperty(this, "indexEndpoint", {
56
57
  enumerable: true,
57
58
  configurable: true,
@@ -61,7 +62,7 @@ class IndexEndpointConnection extends googlevertexai_connection_js_1.GoogleVerte
61
62
  this.indexEndpoint = fields.indexEndpoint;
62
63
  }
63
64
  async buildUrl() {
64
- const projectId = await this.auth.getProjectId();
65
+ const projectId = await this.client.getProjectId();
65
66
  const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexEndpoints/${this.indexEndpoint}`;
66
67
  return url;
67
68
  }
@@ -74,7 +75,7 @@ class IndexEndpointConnection extends googlevertexai_connection_js_1.GoogleVerte
74
75
  }
75
76
  class RemoveDatapointConnection extends googlevertexai_connection_js_1.GoogleVertexAIConnection {
76
77
  constructor(fields, caller) {
77
- super(fields, caller);
78
+ super(fields, caller, new google_auth_library_1.GoogleAuth(fields.authOptions));
78
79
  Object.defineProperty(this, "index", {
79
80
  enumerable: true,
80
81
  configurable: true,
@@ -84,7 +85,7 @@ class RemoveDatapointConnection extends googlevertexai_connection_js_1.GoogleVer
84
85
  this.index = fields.index;
85
86
  }
86
87
  async buildUrl() {
87
- const projectId = await this.auth.getProjectId();
88
+ const projectId = await this.client.getProjectId();
88
89
  const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexes/${this.index}:removeDatapoints`;
89
90
  return url;
90
91
  }
@@ -100,7 +101,7 @@ class RemoveDatapointConnection extends googlevertexai_connection_js_1.GoogleVer
100
101
  }
101
102
  class UpsertDatapointConnection extends googlevertexai_connection_js_1.GoogleVertexAIConnection {
102
103
  constructor(fields, caller) {
103
- super(fields, caller);
104
+ super(fields, caller, new google_auth_library_1.GoogleAuth(fields.authOptions));
104
105
  Object.defineProperty(this, "index", {
105
106
  enumerable: true,
106
107
  configurable: true,
@@ -110,7 +111,7 @@ class UpsertDatapointConnection extends googlevertexai_connection_js_1.GoogleVer
110
111
  this.index = fields.index;
111
112
  }
112
113
  async buildUrl() {
113
- const projectId = await this.auth.getProjectId();
114
+ const projectId = await this.client.getProjectId();
114
115
  const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexes/${this.index}:upsertDatapoints`;
115
116
  return url;
116
117
  }
@@ -126,7 +127,7 @@ class UpsertDatapointConnection extends googlevertexai_connection_js_1.GoogleVer
126
127
  }
127
128
  class FindNeighborsConnection extends googlevertexai_connection_js_1.GoogleVertexAIConnection {
128
129
  constructor(params, caller) {
129
- super(params, caller);
130
+ super(params, caller, new google_auth_library_1.GoogleAuth(params.authOptions));
130
131
  Object.defineProperty(this, "indexEndpoint", {
131
132
  enumerable: true,
132
133
  configurable: true,
@@ -143,7 +144,7 @@ class FindNeighborsConnection extends googlevertexai_connection_js_1.GoogleVerte
143
144
  this.deployedIndexId = params.deployedIndexId;
144
145
  }
145
146
  async buildUrl() {
146
- const projectId = await this.auth.getProjectId();
147
+ const projectId = await this.client.getProjectId();
147
148
  const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexEndpoints/${this.indexEndpoint}:findNeighbors`;
148
149
  return url;
149
150
  }
@@ -1,3 +1,4 @@
1
+ import { GoogleAuthOptions } from "google-auth-library";
1
2
  import { VectorStore } from "./base.js";
2
3
  import { Embeddings } from "../embeddings/base.js";
3
4
  import { Document, DocumentInput } from "../document.js";
@@ -18,7 +19,7 @@ export declare class IdDocument extends Document implements IdDocumentInput {
18
19
  id?: string;
19
20
  constructor(fields: IdDocumentInput);
20
21
  }
21
- interface IndexEndpointConnectionParams extends GoogleVertexAIConnectionParams {
22
+ interface IndexEndpointConnectionParams extends GoogleVertexAIConnectionParams<GoogleAuthOptions> {
22
23
  indexEndpoint: string;
23
24
  }
24
25
  interface DeployedIndex {
@@ -31,7 +32,7 @@ interface IndexEndpointResponse extends GoogleVertexAIResponse {
31
32
  publicEndpointDomainName: string;
32
33
  };
33
34
  }
34
- declare class IndexEndpointConnection extends GoogleVertexAIConnection<AsyncCallerCallOptions, IndexEndpointResponse> {
35
+ declare class IndexEndpointConnection extends GoogleVertexAIConnection<AsyncCallerCallOptions, IndexEndpointResponse, GoogleAuthOptions> {
35
36
  indexEndpoint: string;
36
37
  constructor(fields: IndexEndpointConnectionParams, caller: AsyncCaller);
37
38
  buildUrl(): Promise<string>;
@@ -45,19 +46,19 @@ declare class IndexEndpointConnection extends GoogleVertexAIConnection<AsyncCall
45
46
  export interface MatchingEngineDeleteParams {
46
47
  ids: string[];
47
48
  }
48
- interface RemoveDatapointParams extends GoogleVertexAIConnectionParams {
49
+ interface RemoveDatapointParams extends GoogleVertexAIConnectionParams<GoogleAuthOptions> {
49
50
  index: string;
50
51
  }
51
52
  interface RemoveDatapointResponse extends GoogleVertexAIResponse {
52
53
  }
53
- declare class RemoveDatapointConnection extends GoogleVertexAIConnection<AsyncCallerCallOptions, RemoveDatapointResponse> {
54
+ declare class RemoveDatapointConnection extends GoogleVertexAIConnection<AsyncCallerCallOptions, RemoveDatapointResponse, GoogleAuthOptions> {
54
55
  index: string;
55
56
  constructor(fields: RemoveDatapointParams, caller: AsyncCaller);
56
57
  buildUrl(): Promise<string>;
57
58
  buildMethod(): string;
58
59
  request(datapointIds: string[], options: AsyncCallerCallOptions): Promise<RemoveDatapointResponse>;
59
60
  }
60
- interface UpsertDatapointParams extends GoogleVertexAIConnectionParams {
61
+ interface UpsertDatapointParams extends GoogleVertexAIConnectionParams<GoogleAuthOptions> {
61
62
  index: string;
62
63
  }
63
64
  export interface Restriction {
@@ -76,7 +77,7 @@ interface IndexDatapoint {
76
77
  }
77
78
  interface UpsertDatapointResponse extends GoogleVertexAIResponse {
78
79
  }
79
- declare class UpsertDatapointConnection extends GoogleVertexAIConnection<AsyncCallerCallOptions, UpsertDatapointResponse> {
80
+ declare class UpsertDatapointConnection extends GoogleVertexAIConnection<AsyncCallerCallOptions, UpsertDatapointResponse, GoogleAuthOptions> {
80
81
  index: string;
81
82
  constructor(fields: UpsertDatapointParams, caller: AsyncCaller);
82
83
  buildUrl(): Promise<string>;
@@ -94,7 +95,7 @@ export interface PublicAPIEndpointInfo {
94
95
  /**
95
96
  * Parameters necessary to configure the Matching Engine.
96
97
  */
97
- export interface MatchingEngineArgs extends GoogleVertexAIConnectionParams, IndexEndpointConnectionParams, UpsertDatapointParams {
98
+ export interface MatchingEngineArgs extends GoogleVertexAIConnectionParams<GoogleAuthOptions>, IndexEndpointConnectionParams, UpsertDatapointParams {
98
99
  docstore: Docstore;
99
100
  callerParams?: AsyncCallerParams;
100
101
  callerOptions?: AsyncCallerCallOptions;
@@ -1,5 +1,6 @@
1
1
  import * as uuid from "uuid";
2
2
  import flatten from "flat";
3
+ import { GoogleAuth } from "google-auth-library";
3
4
  import { VectorStore } from "./base.js";
4
5
  import { Document } from "../document.js";
5
6
  import { GoogleVertexAIConnection } from "../util/googlevertexai-connection.js";
@@ -21,7 +22,7 @@ export class IdDocument extends Document {
21
22
  }
22
23
  class IndexEndpointConnection extends GoogleVertexAIConnection {
23
24
  constructor(fields, caller) {
24
- super(fields, caller);
25
+ super(fields, caller, new GoogleAuth(fields.authOptions));
25
26
  Object.defineProperty(this, "indexEndpoint", {
26
27
  enumerable: true,
27
28
  configurable: true,
@@ -31,7 +32,7 @@ class IndexEndpointConnection extends GoogleVertexAIConnection {
31
32
  this.indexEndpoint = fields.indexEndpoint;
32
33
  }
33
34
  async buildUrl() {
34
- const projectId = await this.auth.getProjectId();
35
+ const projectId = await this.client.getProjectId();
35
36
  const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexEndpoints/${this.indexEndpoint}`;
36
37
  return url;
37
38
  }
@@ -44,7 +45,7 @@ class IndexEndpointConnection extends GoogleVertexAIConnection {
44
45
  }
45
46
  class RemoveDatapointConnection extends GoogleVertexAIConnection {
46
47
  constructor(fields, caller) {
47
- super(fields, caller);
48
+ super(fields, caller, new GoogleAuth(fields.authOptions));
48
49
  Object.defineProperty(this, "index", {
49
50
  enumerable: true,
50
51
  configurable: true,
@@ -54,7 +55,7 @@ class RemoveDatapointConnection extends GoogleVertexAIConnection {
54
55
  this.index = fields.index;
55
56
  }
56
57
  async buildUrl() {
57
- const projectId = await this.auth.getProjectId();
58
+ const projectId = await this.client.getProjectId();
58
59
  const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexes/${this.index}:removeDatapoints`;
59
60
  return url;
60
61
  }
@@ -70,7 +71,7 @@ class RemoveDatapointConnection extends GoogleVertexAIConnection {
70
71
  }
71
72
  class UpsertDatapointConnection extends GoogleVertexAIConnection {
72
73
  constructor(fields, caller) {
73
- super(fields, caller);
74
+ super(fields, caller, new GoogleAuth(fields.authOptions));
74
75
  Object.defineProperty(this, "index", {
75
76
  enumerable: true,
76
77
  configurable: true,
@@ -80,7 +81,7 @@ class UpsertDatapointConnection extends GoogleVertexAIConnection {
80
81
  this.index = fields.index;
81
82
  }
82
83
  async buildUrl() {
83
- const projectId = await this.auth.getProjectId();
84
+ const projectId = await this.client.getProjectId();
84
85
  const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexes/${this.index}:upsertDatapoints`;
85
86
  return url;
86
87
  }
@@ -96,7 +97,7 @@ class UpsertDatapointConnection extends GoogleVertexAIConnection {
96
97
  }
97
98
  class FindNeighborsConnection extends GoogleVertexAIConnection {
98
99
  constructor(params, caller) {
99
- super(params, caller);
100
+ super(params, caller, new GoogleAuth(params.authOptions));
100
101
  Object.defineProperty(this, "indexEndpoint", {
101
102
  enumerable: true,
102
103
  configurable: true,
@@ -113,7 +114,7 @@ class FindNeighborsConnection extends GoogleVertexAIConnection {
113
114
  this.deployedIndexId = params.deployedIndexId;
114
115
  }
115
116
  async buildUrl() {
116
- const projectId = await this.auth.getProjectId();
117
+ const projectId = await this.client.getProjectId();
117
118
  const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/indexEndpoints/${this.indexEndpoint}:findNeighbors`;
118
119
  return url;
119
120
  }