@langchain/google-common 0.0.23 → 0.0.24

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.
@@ -28,7 +28,7 @@ export declare abstract class GoogleHostConnection<CallOptions extends AsyncCall
28
28
  get computedPlatformType(): GooglePlatformType;
29
29
  buildMethod(): GoogleAbstractedClientOpsMethod;
30
30
  }
31
- export declare abstract class GoogleAIConnection<CallOptions extends BaseLanguageModelCallOptions, MessageType, AuthOptions> extends GoogleHostConnection<CallOptions, GoogleLLMResponse, AuthOptions> implements GoogleAIBaseLLMInput<AuthOptions> {
31
+ export declare abstract class GoogleAIConnection<CallOptions extends AsyncCallerCallOptions, InputType, AuthOptions, ResponseType extends GoogleResponse> extends GoogleHostConnection<CallOptions, ResponseType, AuthOptions> implements GoogleAIBaseLLMInput<AuthOptions> {
32
32
  model: string;
33
33
  modelName: string;
34
34
  client: GoogleAbstractedClient;
@@ -39,10 +39,10 @@ export declare abstract class GoogleAIConnection<CallOptions extends BaseLanguag
39
39
  buildUrlGenerativeLanguage(): Promise<string>;
40
40
  buildUrlVertex(): Promise<string>;
41
41
  buildUrl(): Promise<string>;
42
- abstract formatData(input: MessageType, parameters: GoogleAIModelRequestParams): unknown;
43
- request(input: MessageType, parameters: GoogleAIModelRequestParams, options: CallOptions): Promise<GoogleLLMResponse>;
42
+ abstract formatData(input: InputType, parameters: GoogleAIModelRequestParams): unknown;
43
+ request(input: InputType, parameters: GoogleAIModelRequestParams, options: CallOptions): Promise<ResponseType>;
44
44
  }
45
- export declare abstract class AbstractGoogleLLMConnection<MessageType, AuthOptions> extends GoogleAIConnection<BaseLanguageModelCallOptions, MessageType, AuthOptions> {
45
+ export declare abstract class AbstractGoogleLLMConnection<MessageType, AuthOptions> extends GoogleAIConnection<BaseLanguageModelCallOptions, MessageType, AuthOptions, GoogleLLMResponse> {
46
46
  buildUrlMethodGemini(): Promise<string>;
47
47
  buildUrlMethod(): Promise<string>;
48
48
  abstract formatContents(input: MessageType, parameters: GoogleAIModelRequestParams): GeminiContent[];
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseGoogleEmbeddings = void 0;
4
+ const embeddings_1 = require("@langchain/core/embeddings");
5
+ const chunk_array_1 = require("@langchain/core/utils/chunk_array");
6
+ const env_1 = require("@langchain/core/utils/env");
7
+ const connection_js_1 = require("./connection.cjs");
8
+ const auth_js_1 = require("./auth.cjs");
9
+ class EmbeddingsConnection extends connection_js_1.GoogleAIConnection {
10
+ constructor(fields, caller, client, streaming) {
11
+ super(fields, caller, client, streaming);
12
+ Object.defineProperty(this, "convertSystemMessageToHumanContent", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ }
19
+ async buildUrlMethod() {
20
+ return "predict";
21
+ }
22
+ formatData(input, parameters) {
23
+ return {
24
+ instances: input,
25
+ parameters,
26
+ };
27
+ }
28
+ }
29
+ /**
30
+ * Enables calls to the Google Cloud's Vertex AI API to access
31
+ * the embeddings generated by Large Language Models.
32
+ *
33
+ * To use, you will need to have one of the following authentication
34
+ * methods in place:
35
+ * - You are logged into an account permitted to the Google Cloud project
36
+ * using Vertex AI.
37
+ * - You are running this on a machine using a service account permitted to
38
+ * the Google Cloud project using Vertex AI.
39
+ * - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
40
+ * path of a credentials file for a service account permitted to the
41
+ * Google Cloud project using Vertex AI.
42
+ * @example
43
+ * ```typescript
44
+ * const model = new GoogleEmbeddings();
45
+ * const res = await model.embedQuery(
46
+ * "What would be a good company name for a company that makes colorful socks?"
47
+ * );
48
+ * console.log({ res });
49
+ * ```
50
+ */
51
+ class BaseGoogleEmbeddings extends embeddings_1.Embeddings {
52
+ constructor(fields) {
53
+ super(fields);
54
+ Object.defineProperty(this, "model", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ Object.defineProperty(this, "connection", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
66
+ this.model = fields.model;
67
+ this.connection = new EmbeddingsConnection({ ...fields, ...this }, this.caller, this.buildClient(fields), false);
68
+ }
69
+ buildApiKeyClient(apiKey) {
70
+ return new auth_js_1.ApiKeyGoogleAuth(apiKey);
71
+ }
72
+ buildApiKey(fields) {
73
+ return fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("GOOGLE_API_KEY");
74
+ }
75
+ buildClient(fields) {
76
+ const apiKey = this.buildApiKey(fields);
77
+ if (apiKey) {
78
+ return this.buildApiKeyClient(apiKey);
79
+ }
80
+ else {
81
+ return this.buildAbstractedClient(fields);
82
+ }
83
+ }
84
+ /**
85
+ * Takes an array of documents as input and returns a promise that
86
+ * resolves to a 2D array of embeddings for each document. It splits the
87
+ * documents into chunks and makes requests to the Google Vertex AI API to
88
+ * generate embeddings.
89
+ * @param documents An array of documents to be embedded.
90
+ * @returns A promise that resolves to a 2D array of embeddings for each document.
91
+ */
92
+ async embedDocuments(documents) {
93
+ const instanceChunks = (0, chunk_array_1.chunkArray)(documents.map((document) => ({
94
+ content: document,
95
+ })), 5); // Vertex AI accepts max 5 instances per prediction
96
+ const parameters = {};
97
+ const options = {};
98
+ const responses = await Promise.all(instanceChunks.map((instances) => this.connection.request(instances, parameters, options)));
99
+ const result = responses
100
+ ?.map((response) => response?.data?.predictions?.map((result) => result.embeddings.values) ?? [])
101
+ .flat() ?? [];
102
+ return result;
103
+ }
104
+ /**
105
+ * Takes a document as input and returns a promise that resolves to an
106
+ * embedding for the document. It calls the embedDocuments method with the
107
+ * document as the input.
108
+ * @param document A document to be embedded.
109
+ * @returns A promise that resolves to an embedding for the document.
110
+ */
111
+ async embedQuery(document) {
112
+ const data = await this.embedDocuments([document]);
113
+ return data[0];
114
+ }
115
+ }
116
+ exports.BaseGoogleEmbeddings = BaseGoogleEmbeddings;
@@ -0,0 +1,91 @@
1
+ import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings";
2
+ import { AsyncCallerCallOptions } from "@langchain/core/utils/async_caller";
3
+ import { GoogleAbstractedClient } from "./auth.js";
4
+ import { GoogleConnectionParams, GoogleResponse } from "./types.js";
5
+ /**
6
+ * Defines the parameters required to initialize a
7
+ * GoogleEmbeddings instance. It extends EmbeddingsParams and
8
+ * GoogleConnectionParams.
9
+ */
10
+ export interface BaseGoogleEmbeddingsParams<AuthOptions> extends EmbeddingsParams, GoogleConnectionParams<AuthOptions> {
11
+ model: string;
12
+ }
13
+ /**
14
+ * Defines additional options specific to the
15
+ * GoogleEmbeddingsInstance. It extends AsyncCallerCallOptions.
16
+ */
17
+ export interface BaseGoogleEmbeddingsOptions extends AsyncCallerCallOptions {
18
+ }
19
+ /**
20
+ * Represents an instance for generating embeddings using the Google
21
+ * Vertex AI API. It contains the content to be embedded.
22
+ */
23
+ export interface GoogleEmbeddingsInstance {
24
+ content: string;
25
+ }
26
+ /**
27
+ * Defines the structure of the embeddings results returned by the Google
28
+ * Vertex AI API. It extends GoogleBasePrediction and contains the
29
+ * embeddings and their statistics.
30
+ */
31
+ export interface GoogleEmbeddingsResponse extends GoogleResponse {
32
+ data: {
33
+ predictions: {
34
+ embeddings: {
35
+ statistics: {
36
+ token_count: number;
37
+ truncated: boolean;
38
+ };
39
+ values: number[];
40
+ };
41
+ }[];
42
+ };
43
+ }
44
+ /**
45
+ * Enables calls to the Google Cloud's Vertex AI API to access
46
+ * the embeddings generated by Large Language Models.
47
+ *
48
+ * To use, you will need to have one of the following authentication
49
+ * methods in place:
50
+ * - You are logged into an account permitted to the Google Cloud project
51
+ * using Vertex AI.
52
+ * - You are running this on a machine using a service account permitted to
53
+ * the Google Cloud project using Vertex AI.
54
+ * - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
55
+ * path of a credentials file for a service account permitted to the
56
+ * Google Cloud project using Vertex AI.
57
+ * @example
58
+ * ```typescript
59
+ * const model = new GoogleEmbeddings();
60
+ * const res = await model.embedQuery(
61
+ * "What would be a good company name for a company that makes colorful socks?"
62
+ * );
63
+ * console.log({ res });
64
+ * ```
65
+ */
66
+ export declare abstract class BaseGoogleEmbeddings<AuthOptions> extends Embeddings implements BaseGoogleEmbeddingsParams<AuthOptions> {
67
+ model: string;
68
+ private connection;
69
+ constructor(fields: BaseGoogleEmbeddingsParams<AuthOptions>);
70
+ abstract buildAbstractedClient(fields?: GoogleConnectionParams<AuthOptions>): GoogleAbstractedClient;
71
+ buildApiKeyClient(apiKey: string): GoogleAbstractedClient;
72
+ buildApiKey(fields?: GoogleConnectionParams<AuthOptions>): string | undefined;
73
+ buildClient(fields?: GoogleConnectionParams<AuthOptions>): GoogleAbstractedClient;
74
+ /**
75
+ * Takes an array of documents as input and returns a promise that
76
+ * resolves to a 2D array of embeddings for each document. It splits the
77
+ * documents into chunks and makes requests to the Google Vertex AI API to
78
+ * generate embeddings.
79
+ * @param documents An array of documents to be embedded.
80
+ * @returns A promise that resolves to a 2D array of embeddings for each document.
81
+ */
82
+ embedDocuments(documents: string[]): Promise<number[][]>;
83
+ /**
84
+ * Takes a document as input and returns a promise that resolves to an
85
+ * embedding for the document. It calls the embedDocuments method with the
86
+ * document as the input.
87
+ * @param document A document to be embedded.
88
+ * @returns A promise that resolves to an embedding for the document.
89
+ */
90
+ embedQuery(document: string): Promise<number[]>;
91
+ }
@@ -0,0 +1,112 @@
1
+ import { Embeddings } from "@langchain/core/embeddings";
2
+ import { chunkArray } from "@langchain/core/utils/chunk_array";
3
+ import { getEnvironmentVariable } from "@langchain/core/utils/env";
4
+ import { GoogleAIConnection } from "./connection.js";
5
+ import { ApiKeyGoogleAuth } from "./auth.js";
6
+ class EmbeddingsConnection extends GoogleAIConnection {
7
+ constructor(fields, caller, client, streaming) {
8
+ super(fields, caller, client, streaming);
9
+ Object.defineProperty(this, "convertSystemMessageToHumanContent", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: void 0
14
+ });
15
+ }
16
+ async buildUrlMethod() {
17
+ return "predict";
18
+ }
19
+ formatData(input, parameters) {
20
+ return {
21
+ instances: input,
22
+ parameters,
23
+ };
24
+ }
25
+ }
26
+ /**
27
+ * Enables calls to the Google Cloud's Vertex AI API to access
28
+ * the embeddings generated by Large Language Models.
29
+ *
30
+ * To use, you will need to have one of the following authentication
31
+ * methods in place:
32
+ * - You are logged into an account permitted to the Google Cloud project
33
+ * using Vertex AI.
34
+ * - You are running this on a machine using a service account permitted to
35
+ * the Google Cloud project using Vertex AI.
36
+ * - The `GOOGLE_APPLICATION_CREDENTIALS` environment variable is set to the
37
+ * path of a credentials file for a service account permitted to the
38
+ * Google Cloud project using Vertex AI.
39
+ * @example
40
+ * ```typescript
41
+ * const model = new GoogleEmbeddings();
42
+ * const res = await model.embedQuery(
43
+ * "What would be a good company name for a company that makes colorful socks?"
44
+ * );
45
+ * console.log({ res });
46
+ * ```
47
+ */
48
+ export class BaseGoogleEmbeddings extends Embeddings {
49
+ constructor(fields) {
50
+ super(fields);
51
+ Object.defineProperty(this, "model", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "connection", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ this.model = fields.model;
64
+ this.connection = new EmbeddingsConnection({ ...fields, ...this }, this.caller, this.buildClient(fields), false);
65
+ }
66
+ buildApiKeyClient(apiKey) {
67
+ return new ApiKeyGoogleAuth(apiKey);
68
+ }
69
+ buildApiKey(fields) {
70
+ return fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
71
+ }
72
+ buildClient(fields) {
73
+ const apiKey = this.buildApiKey(fields);
74
+ if (apiKey) {
75
+ return this.buildApiKeyClient(apiKey);
76
+ }
77
+ else {
78
+ return this.buildAbstractedClient(fields);
79
+ }
80
+ }
81
+ /**
82
+ * Takes an array of documents as input and returns a promise that
83
+ * resolves to a 2D array of embeddings for each document. It splits the
84
+ * documents into chunks and makes requests to the Google Vertex AI API to
85
+ * generate embeddings.
86
+ * @param documents An array of documents to be embedded.
87
+ * @returns A promise that resolves to a 2D array of embeddings for each document.
88
+ */
89
+ async embedDocuments(documents) {
90
+ const instanceChunks = chunkArray(documents.map((document) => ({
91
+ content: document,
92
+ })), 5); // Vertex AI accepts max 5 instances per prediction
93
+ const parameters = {};
94
+ const options = {};
95
+ const responses = await Promise.all(instanceChunks.map((instances) => this.connection.request(instances, parameters, options)));
96
+ const result = responses
97
+ ?.map((response) => response?.data?.predictions?.map((result) => result.embeddings.values) ?? [])
98
+ .flat() ?? [];
99
+ return result;
100
+ }
101
+ /**
102
+ * Takes a document as input and returns a promise that resolves to an
103
+ * embedding for the document. It calls the embedDocuments method with the
104
+ * document as the input.
105
+ * @param document A document to be embedded.
106
+ * @returns A promise that resolves to an embedding for the document.
107
+ */
108
+ async embedQuery(document) {
109
+ const data = await this.embedDocuments([document]);
110
+ return data[0];
111
+ }
112
+ }
package/dist/index.cjs CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./chat_models.cjs"), exports);
18
18
  __exportStar(require("./llms.cjs"), exports);
19
+ __exportStar(require("./embeddings.cjs"), exports);
19
20
  __exportStar(require("./auth.cjs"), exports);
20
21
  __exportStar(require("./connection.cjs"), exports);
21
22
  __exportStar(require("./types.cjs"), exports);
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./chat_models.js";
2
2
  export * from "./llms.js";
3
+ export * from "./embeddings.js";
3
4
  export * from "./auth.js";
4
5
  export * from "./connection.js";
5
6
  export * from "./types.js";
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./chat_models.js";
2
2
  export * from "./llms.js";
3
+ export * from "./embeddings.js";
3
4
  export * from "./auth.js";
4
5
  export * from "./connection.js";
5
6
  export * from "./types.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-common",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "description": "Core types and classes for Google services.",
5
5
  "type": "module",
6
6
  "engines": {