langchain 0.0.147 → 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 (110) 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/chains/constitutional_ai/constitutional_principle.cjs +272 -1
  8. package/dist/chains/constitutional_ai/constitutional_principle.js +272 -1
  9. package/dist/chat_models/{googlevertexai.cjs → googlevertexai/common.cjs} +14 -26
  10. package/dist/chat_models/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -22
  11. package/dist/chat_models/{googlevertexai.js → googlevertexai/common.js} +12 -24
  12. package/dist/chat_models/googlevertexai/index.cjs +36 -0
  13. package/dist/chat_models/googlevertexai/index.d.ts +21 -0
  14. package/dist/chat_models/googlevertexai/index.js +31 -0
  15. package/dist/chat_models/googlevertexai/web.cjs +33 -0
  16. package/dist/chat_models/googlevertexai/web.d.ts +19 -0
  17. package/dist/chat_models/googlevertexai/web.js +28 -0
  18. package/dist/document_loaders/web/notionapi.cjs +93 -70
  19. package/dist/document_loaders/web/notionapi.d.ts +33 -1
  20. package/dist/document_loaders/web/notionapi.js +89 -71
  21. package/dist/embeddings/googlevertexai.cjs +5 -1
  22. package/dist/embeddings/googlevertexai.d.ts +2 -1
  23. package/dist/embeddings/googlevertexai.js +5 -1
  24. package/dist/evaluation/agents/index.cjs +17 -0
  25. package/dist/evaluation/agents/index.d.ts +1 -0
  26. package/dist/evaluation/agents/index.js +1 -0
  27. package/dist/evaluation/agents/prompt.cjs +132 -0
  28. package/dist/evaluation/agents/prompt.d.ts +6 -0
  29. package/dist/evaluation/agents/prompt.js +129 -0
  30. package/dist/evaluation/agents/trajectory.cjs +189 -0
  31. package/dist/evaluation/agents/trajectory.d.ts +54 -0
  32. package/dist/evaluation/agents/trajectory.js +184 -0
  33. package/dist/evaluation/base.cjs +274 -0
  34. package/dist/evaluation/base.d.ts +232 -0
  35. package/dist/evaluation/base.js +263 -0
  36. package/dist/evaluation/comparison/index.cjs +17 -0
  37. package/dist/evaluation/comparison/index.d.ts +1 -0
  38. package/dist/evaluation/comparison/index.js +1 -0
  39. package/dist/evaluation/comparison/pairwise.cjs +244 -0
  40. package/dist/evaluation/comparison/pairwise.d.ts +50 -0
  41. package/dist/evaluation/comparison/pairwise.js +238 -0
  42. package/dist/evaluation/comparison/prompt.cjs +74 -0
  43. package/dist/evaluation/comparison/prompt.d.ts +21 -0
  44. package/dist/evaluation/comparison/prompt.js +71 -0
  45. package/dist/evaluation/criteria/criteria.cjs +259 -0
  46. package/dist/evaluation/criteria/criteria.d.ts +73 -0
  47. package/dist/evaluation/criteria/criteria.js +253 -0
  48. package/dist/evaluation/criteria/index.cjs +17 -0
  49. package/dist/evaluation/criteria/index.d.ts +1 -0
  50. package/dist/evaluation/criteria/index.js +1 -0
  51. package/dist/evaluation/criteria/prompt.cjs +36 -0
  52. package/dist/evaluation/criteria/prompt.d.ts +12 -0
  53. package/dist/evaluation/criteria/prompt.js +33 -0
  54. package/dist/evaluation/embedding_distance/base.cjs +163 -0
  55. package/dist/evaluation/embedding_distance/base.d.ts +78 -0
  56. package/dist/evaluation/embedding_distance/base.js +156 -0
  57. package/dist/evaluation/embedding_distance/index.cjs +17 -0
  58. package/dist/evaluation/embedding_distance/index.d.ts +1 -0
  59. package/dist/evaluation/embedding_distance/index.js +1 -0
  60. package/dist/evaluation/index.cjs +6 -0
  61. package/dist/evaluation/index.d.ts +6 -0
  62. package/dist/evaluation/index.js +6 -0
  63. package/dist/evaluation/loader.cjs +60 -0
  64. package/dist/evaluation/loader.d.ts +27 -0
  65. package/dist/evaluation/loader.js +56 -0
  66. package/dist/evaluation/types.cjs +2 -0
  67. package/dist/evaluation/types.d.ts +35 -0
  68. package/dist/evaluation/types.js +1 -0
  69. package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +5 -1
  70. package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
  71. package/dist/experimental/multimodal_embeddings/googlevertexai.js +5 -1
  72. package/dist/llms/{googlevertexai.js → googlevertexai/common.cjs} +21 -17
  73. package/dist/llms/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -23
  74. package/dist/llms/{googlevertexai.cjs → googlevertexai/common.js} +17 -21
  75. package/dist/llms/googlevertexai/index.cjs +34 -0
  76. package/dist/llms/googlevertexai/index.d.ts +26 -0
  77. package/dist/llms/googlevertexai/index.js +30 -0
  78. package/dist/llms/googlevertexai/web.cjs +31 -0
  79. package/dist/llms/googlevertexai/web.d.ts +24 -0
  80. package/dist/llms/googlevertexai/web.js +27 -0
  81. package/dist/load/import_constants.cjs +2 -0
  82. package/dist/load/import_constants.js +2 -0
  83. package/dist/load/import_map.cjs +2 -1
  84. package/dist/load/import_map.d.ts +1 -0
  85. package/dist/load/import_map.js +1 -0
  86. package/dist/load/serializable.cjs +23 -4
  87. package/dist/load/serializable.js +23 -4
  88. package/dist/retrievers/multi_query.cjs +140 -0
  89. package/dist/retrievers/multi_query.d.ts +33 -0
  90. package/dist/retrievers/multi_query.js +136 -0
  91. package/dist/types/googlevertexai-types.d.ts +11 -4
  92. package/dist/util/googlevertexai-connection.cjs +14 -15
  93. package/dist/util/googlevertexai-connection.d.ts +7 -7
  94. package/dist/util/googlevertexai-connection.js +14 -15
  95. package/dist/util/googlevertexai-webauth.cjs +56 -0
  96. package/dist/util/googlevertexai-webauth.d.ts +25 -0
  97. package/dist/util/googlevertexai-webauth.js +52 -0
  98. package/dist/vectorstores/googlevertexai.cjs +9 -8
  99. package/dist/vectorstores/googlevertexai.d.ts +8 -7
  100. package/dist/vectorstores/googlevertexai.js +9 -8
  101. package/llms/googlevertexai/web.cjs +1 -0
  102. package/llms/googlevertexai/web.d.ts +1 -0
  103. package/llms/googlevertexai/web.js +1 -0
  104. package/llms/googlevertexai.cjs +1 -1
  105. package/llms/googlevertexai.d.ts +1 -1
  106. package/llms/googlevertexai.js +1 -1
  107. package/package.json +32 -3
  108. package/retrievers/multi_query.cjs +1 -0
  109. package/retrievers/multi_query.d.ts +1 -0
  110. package/retrievers/multi_query.js +1 -0
@@ -134,11 +134,30 @@ export class Serializable {
134
134
  }
135
135
  // include all secrets used, even if not in kwargs,
136
136
  // will be replaced with sentinel value in replaceSecrets
137
- for (const key in secrets) {
138
- if (key in this && this[key] !== undefined) {
139
- kwargs[key] = this[key] || kwargs[key];
137
+ Object.keys(secrets).forEach((keyPath) => {
138
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, @typescript-eslint/no-explicit-any
139
+ let read = this;
140
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
141
+ let write = kwargs;
142
+ const [last, ...partsReverse] = keyPath.split(".").reverse();
143
+ for (const key of partsReverse.reverse()) {
144
+ if (!(key in read) || read[key] === undefined)
145
+ return;
146
+ if (!(key in write) || write[key] === undefined) {
147
+ if (typeof read[key] === "object" && read[key] != null) {
148
+ write[key] = {};
149
+ }
150
+ else if (Array.isArray(read[key])) {
151
+ write[key] = [];
152
+ }
153
+ }
154
+ read = read[key];
155
+ write = write[key];
140
156
  }
141
- }
157
+ if (last in read && read[last] !== undefined) {
158
+ write[last] = write[last] || read[last];
159
+ }
160
+ });
142
161
  return {
143
162
  lc: 1,
144
163
  type: "constructor",
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MultiQueryRetriever = void 0;
4
+ const llm_chain_js_1 = require("../chains/llm_chain.cjs");
5
+ const prompt_js_1 = require("../prompts/prompt.cjs");
6
+ const output_parser_js_1 = require("../schema/output_parser.cjs");
7
+ const retriever_js_1 = require("../schema/retriever.cjs");
8
+ class LineListOutputParser extends output_parser_js_1.BaseOutputParser {
9
+ constructor() {
10
+ super(...arguments);
11
+ Object.defineProperty(this, "lc_namespace", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: ["langchain", "retrievers", "multiquery"]
16
+ });
17
+ }
18
+ static lc_name() {
19
+ return "LineListOutputParser";
20
+ }
21
+ async parse(text) {
22
+ const startKeyIndex = text.indexOf("<questions>");
23
+ const endKeyIndex = text.indexOf("</questions>");
24
+ const questionsStartIndex = startKeyIndex === -1 ? 0 : startKeyIndex + "<questions>".length;
25
+ const questionsEndIndex = endKeyIndex === -1 ? text.length : endKeyIndex;
26
+ const lines = text
27
+ .slice(questionsStartIndex, questionsEndIndex)
28
+ .trim()
29
+ .split("\n")
30
+ .filter((line) => line.trim() !== "");
31
+ return { lines };
32
+ }
33
+ getFormatInstructions() {
34
+ throw new Error("Not implemented.");
35
+ }
36
+ }
37
+ // Create template
38
+ const DEFAULT_QUERY_PROMPT = /* #__PURE__ */ new prompt_js_1.PromptTemplate({
39
+ inputVariables: ["question", "queryCount"],
40
+ template: `You are an AI language model assistant. Your task is
41
+ to generate {queryCount} different versions of the given user
42
+ question to retrieve relevant documents from a vector database.
43
+ By generating multiple perspectives on the user question,
44
+ your goal is to help the user overcome some of the limitations
45
+ of distance-based similarity search.
46
+
47
+ Provide these alternative questions separated by newlines between XML tags. For example:
48
+
49
+ <questions>
50
+ Question 1
51
+ Question 2
52
+ Question 3
53
+ </questions>
54
+
55
+ Original question: {question}`,
56
+ });
57
+ // Export class
58
+ class MultiQueryRetriever extends retriever_js_1.BaseRetriever {
59
+ static lc_name() {
60
+ return "MultiQueryRetriever";
61
+ }
62
+ constructor(fields) {
63
+ super(fields);
64
+ Object.defineProperty(this, "lc_namespace", {
65
+ enumerable: true,
66
+ configurable: true,
67
+ writable: true,
68
+ value: ["langchain", "retrievers", "multiquery"]
69
+ });
70
+ Object.defineProperty(this, "retriever", {
71
+ enumerable: true,
72
+ configurable: true,
73
+ writable: true,
74
+ value: void 0
75
+ });
76
+ Object.defineProperty(this, "llmChain", {
77
+ enumerable: true,
78
+ configurable: true,
79
+ writable: true,
80
+ value: void 0
81
+ });
82
+ Object.defineProperty(this, "queryCount", {
83
+ enumerable: true,
84
+ configurable: true,
85
+ writable: true,
86
+ value: 3
87
+ });
88
+ Object.defineProperty(this, "parserKey", {
89
+ enumerable: true,
90
+ configurable: true,
91
+ writable: true,
92
+ value: "lines"
93
+ });
94
+ this.retriever = fields.retriever;
95
+ this.llmChain = fields.llmChain;
96
+ this.queryCount = fields.queryCount ?? this.queryCount;
97
+ this.parserKey = fields.parserKey ?? this.parserKey;
98
+ }
99
+ static fromLLM(fields) {
100
+ const { retriever, llm, prompt = DEFAULT_QUERY_PROMPT, queryCount, parserKey, ...rest } = fields;
101
+ const outputParser = new LineListOutputParser();
102
+ const llmChain = new llm_chain_js_1.LLMChain({ llm, prompt, outputParser });
103
+ return new this({ retriever, llmChain, queryCount, parserKey, ...rest });
104
+ }
105
+ // Generate the different queries for each retrieval, using our llmChain
106
+ async _generateQueries(question, runManager) {
107
+ const response = await this.llmChain.call({ question, queryCount: this.queryCount }, runManager?.getChild());
108
+ const lines = response.text[this.parserKey] || [];
109
+ if (this.verbose) {
110
+ console.log(`Generated queries: ${lines}`);
111
+ }
112
+ return lines;
113
+ }
114
+ // Retrieve documents using the original retriever
115
+ async _retrieveDocuments(queries, runManager) {
116
+ const documents = [];
117
+ for (const query of queries) {
118
+ const docs = await this.retriever.getRelevantDocuments(query, runManager?.getChild());
119
+ documents.push(...docs);
120
+ }
121
+ return documents;
122
+ }
123
+ // Deduplicate the documents that were returned in multiple retrievals
124
+ _uniqueUnion(documents) {
125
+ const uniqueDocumentsDict = {};
126
+ for (const doc of documents) {
127
+ const key = `${doc.pageContent}:${JSON.stringify(Object.entries(doc.metadata).sort())}`;
128
+ uniqueDocumentsDict[key] = doc;
129
+ }
130
+ const uniqueDocuments = Object.values(uniqueDocumentsDict);
131
+ return uniqueDocuments;
132
+ }
133
+ async _getRelevantDocuments(question, runManager) {
134
+ const queries = await this._generateQueries(question, runManager);
135
+ const documents = await this._retrieveDocuments(queries, runManager);
136
+ const uniqueDocuments = this._uniqueUnion(documents);
137
+ return uniqueDocuments;
138
+ }
139
+ }
140
+ exports.MultiQueryRetriever = MultiQueryRetriever;
@@ -0,0 +1,33 @@
1
+ import { LLMChain } from "../chains/llm_chain.js";
2
+ import { Document } from "../document.js";
3
+ import { BaseRetriever, BaseRetrieverInput } from "../schema/retriever.js";
4
+ import { CallbackManagerForRetrieverRun } from "../callbacks/index.js";
5
+ import { BaseLanguageModel } from "../base_language/index.js";
6
+ import { BasePromptTemplate } from "../prompts/base.js";
7
+ interface LineList {
8
+ lines: string[];
9
+ }
10
+ export interface MultiQueryRetrieverInput extends BaseRetrieverInput {
11
+ retriever: BaseRetriever;
12
+ llmChain: LLMChain<LineList>;
13
+ queryCount?: number;
14
+ parserKey?: string;
15
+ }
16
+ export declare class MultiQueryRetriever extends BaseRetriever {
17
+ static lc_name(): string;
18
+ lc_namespace: string[];
19
+ private retriever;
20
+ private llmChain;
21
+ private queryCount;
22
+ private parserKey;
23
+ constructor(fields: MultiQueryRetrieverInput);
24
+ static fromLLM(fields: Omit<MultiQueryRetrieverInput, "llmChain"> & {
25
+ llm: BaseLanguageModel;
26
+ prompt?: BasePromptTemplate;
27
+ }): MultiQueryRetriever;
28
+ private _generateQueries;
29
+ private _retrieveDocuments;
30
+ private _uniqueUnion;
31
+ _getRelevantDocuments(question: string, runManager?: CallbackManagerForRetrieverRun): Promise<Document[]>;
32
+ }
33
+ export {};
@@ -0,0 +1,136 @@
1
+ import { LLMChain } from "../chains/llm_chain.js";
2
+ import { PromptTemplate } from "../prompts/prompt.js";
3
+ import { BaseOutputParser } from "../schema/output_parser.js";
4
+ import { BaseRetriever } from "../schema/retriever.js";
5
+ class LineListOutputParser extends BaseOutputParser {
6
+ constructor() {
7
+ super(...arguments);
8
+ Object.defineProperty(this, "lc_namespace", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: ["langchain", "retrievers", "multiquery"]
13
+ });
14
+ }
15
+ static lc_name() {
16
+ return "LineListOutputParser";
17
+ }
18
+ async parse(text) {
19
+ const startKeyIndex = text.indexOf("<questions>");
20
+ const endKeyIndex = text.indexOf("</questions>");
21
+ const questionsStartIndex = startKeyIndex === -1 ? 0 : startKeyIndex + "<questions>".length;
22
+ const questionsEndIndex = endKeyIndex === -1 ? text.length : endKeyIndex;
23
+ const lines = text
24
+ .slice(questionsStartIndex, questionsEndIndex)
25
+ .trim()
26
+ .split("\n")
27
+ .filter((line) => line.trim() !== "");
28
+ return { lines };
29
+ }
30
+ getFormatInstructions() {
31
+ throw new Error("Not implemented.");
32
+ }
33
+ }
34
+ // Create template
35
+ const DEFAULT_QUERY_PROMPT = /* #__PURE__ */ new PromptTemplate({
36
+ inputVariables: ["question", "queryCount"],
37
+ template: `You are an AI language model assistant. Your task is
38
+ to generate {queryCount} different versions of the given user
39
+ question to retrieve relevant documents from a vector database.
40
+ By generating multiple perspectives on the user question,
41
+ your goal is to help the user overcome some of the limitations
42
+ of distance-based similarity search.
43
+
44
+ Provide these alternative questions separated by newlines between XML tags. For example:
45
+
46
+ <questions>
47
+ Question 1
48
+ Question 2
49
+ Question 3
50
+ </questions>
51
+
52
+ Original question: {question}`,
53
+ });
54
+ // Export class
55
+ export class MultiQueryRetriever extends BaseRetriever {
56
+ static lc_name() {
57
+ return "MultiQueryRetriever";
58
+ }
59
+ constructor(fields) {
60
+ super(fields);
61
+ Object.defineProperty(this, "lc_namespace", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: ["langchain", "retrievers", "multiquery"]
66
+ });
67
+ Object.defineProperty(this, "retriever", {
68
+ enumerable: true,
69
+ configurable: true,
70
+ writable: true,
71
+ value: void 0
72
+ });
73
+ Object.defineProperty(this, "llmChain", {
74
+ enumerable: true,
75
+ configurable: true,
76
+ writable: true,
77
+ value: void 0
78
+ });
79
+ Object.defineProperty(this, "queryCount", {
80
+ enumerable: true,
81
+ configurable: true,
82
+ writable: true,
83
+ value: 3
84
+ });
85
+ Object.defineProperty(this, "parserKey", {
86
+ enumerable: true,
87
+ configurable: true,
88
+ writable: true,
89
+ value: "lines"
90
+ });
91
+ this.retriever = fields.retriever;
92
+ this.llmChain = fields.llmChain;
93
+ this.queryCount = fields.queryCount ?? this.queryCount;
94
+ this.parserKey = fields.parserKey ?? this.parserKey;
95
+ }
96
+ static fromLLM(fields) {
97
+ const { retriever, llm, prompt = DEFAULT_QUERY_PROMPT, queryCount, parserKey, ...rest } = fields;
98
+ const outputParser = new LineListOutputParser();
99
+ const llmChain = new LLMChain({ llm, prompt, outputParser });
100
+ return new this({ retriever, llmChain, queryCount, parserKey, ...rest });
101
+ }
102
+ // Generate the different queries for each retrieval, using our llmChain
103
+ async _generateQueries(question, runManager) {
104
+ const response = await this.llmChain.call({ question, queryCount: this.queryCount }, runManager?.getChild());
105
+ const lines = response.text[this.parserKey] || [];
106
+ if (this.verbose) {
107
+ console.log(`Generated queries: ${lines}`);
108
+ }
109
+ return lines;
110
+ }
111
+ // Retrieve documents using the original retriever
112
+ async _retrieveDocuments(queries, runManager) {
113
+ const documents = [];
114
+ for (const query of queries) {
115
+ const docs = await this.retriever.getRelevantDocuments(query, runManager?.getChild());
116
+ documents.push(...docs);
117
+ }
118
+ return documents;
119
+ }
120
+ // Deduplicate the documents that were returned in multiple retrievals
121
+ _uniqueUnion(documents) {
122
+ const uniqueDocumentsDict = {};
123
+ for (const doc of documents) {
124
+ const key = `${doc.pageContent}:${JSON.stringify(Object.entries(doc.metadata).sort())}`;
125
+ uniqueDocumentsDict[key] = doc;
126
+ }
127
+ const uniqueDocuments = Object.values(uniqueDocumentsDict);
128
+ return uniqueDocuments;
129
+ }
130
+ async _getRelevantDocuments(question, runManager) {
131
+ const queries = await this._generateQueries(question, runManager);
132
+ const documents = await this._retrieveDocuments(queries, runManager);
133
+ const uniqueDocuments = this._uniqueUnion(documents);
134
+ return uniqueDocuments;
135
+ }
136
+ }
@@ -1,9 +1,8 @@
1
- import { GoogleAuthOptions } from "google-auth-library";
2
1
  import { BaseLLMParams } from "../llms/base.js";
3
- export interface GoogleVertexAIConnectionParams {
2
+ export interface GoogleVertexAIConnectionParams<AuthOptions> {
4
3
  /** Hostname for the API call */
5
4
  endpoint?: string;
6
- authOptions?: GoogleAuthOptions;
5
+ authOptions?: AuthOptions;
7
6
  /** Region where the LLM is stored */
8
7
  location?: string;
9
8
  /** The version of the API functions. Part of the path. */
@@ -39,7 +38,7 @@ export interface GoogleVertexAIModelParams {
39
38
  */
40
39
  topK?: number;
41
40
  }
42
- export interface GoogleVertexAIBaseLLMInput extends BaseLLMParams, GoogleVertexAIConnectionParams, GoogleVertexAIModelParams {
41
+ export interface GoogleVertexAIBaseLLMInput<AuthOptions> extends BaseLLMParams, GoogleVertexAIConnectionParams<AuthOptions>, GoogleVertexAIModelParams {
43
42
  }
44
43
  export interface GoogleVertexAIResponse {
45
44
  data: any;
@@ -52,3 +51,11 @@ export interface GoogleVertexAILLMResponse<PredictionType extends GoogleVertexAI
52
51
  predictions: PredictionType[];
53
52
  };
54
53
  }
54
+ export interface GoogleVertexAIAbstractedClient {
55
+ request: (opts: {
56
+ url?: string;
57
+ method?: "GET" | "POST";
58
+ data?: unknown;
59
+ }) => unknown;
60
+ getProjectId: () => Promise<string>;
61
+ }
@@ -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
+ }