@langchain/google-common 0.0.23 → 0.0.25

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.
@@ -98,7 +98,7 @@ class ChatConnection extends connection_js_1.AbstractGoogleLLMConnection {
98
98
  }
99
99
  }
100
100
  /**
101
- * Integration with a chat model.
101
+ * Integration with a Google chat model.
102
102
  */
103
103
  class ChatGoogleBase extends chat_models_1.BaseChatModel {
104
104
  // Used for tracing, replace with the same name as your class
@@ -25,7 +25,7 @@ declare class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<Ba
25
25
  export interface ChatGoogleBaseInput<AuthOptions> extends BaseChatModelParams, GoogleConnectionParams<AuthOptions>, GoogleAIModelParams, GoogleAISafetyParams, Pick<GoogleAIBaseLanguageModelCallOptions, "streamUsage"> {
26
26
  }
27
27
  /**
28
- * Integration with a chat model.
28
+ * Integration with a Google chat model.
29
29
  */
30
30
  export declare abstract class ChatGoogleBase<AuthOptions> extends BaseChatModel<GoogleAIBaseLanguageModelCallOptions, AIMessageChunk> implements ChatGoogleBaseInput<AuthOptions> {
31
31
  static lc_name(): string;
@@ -95,7 +95,7 @@ class ChatConnection extends AbstractGoogleLLMConnection {
95
95
  }
96
96
  }
97
97
  /**
98
- * Integration with a chat model.
98
+ * Integration with a Google chat model.
99
99
  */
100
100
  export class ChatGoogleBase extends BaseChatModel {
101
101
  // Used for tracing, replace with the same name as your class
@@ -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,98 @@
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 Google APIs for generating
31
+ * text embeddings.
32
+ */
33
+ class BaseGoogleEmbeddings extends embeddings_1.Embeddings {
34
+ constructor(fields) {
35
+ super(fields);
36
+ Object.defineProperty(this, "model", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "connection", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ this.model = fields.model;
49
+ this.connection = new EmbeddingsConnection({ ...fields, ...this }, this.caller, this.buildClient(fields), false);
50
+ }
51
+ buildApiKeyClient(apiKey) {
52
+ return new auth_js_1.ApiKeyGoogleAuth(apiKey);
53
+ }
54
+ buildApiKey(fields) {
55
+ return fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("GOOGLE_API_KEY");
56
+ }
57
+ buildClient(fields) {
58
+ const apiKey = this.buildApiKey(fields);
59
+ if (apiKey) {
60
+ return this.buildApiKeyClient(apiKey);
61
+ }
62
+ else {
63
+ return this.buildAbstractedClient(fields);
64
+ }
65
+ }
66
+ /**
67
+ * Takes an array of documents as input and returns a promise that
68
+ * resolves to a 2D array of embeddings for each document. It splits the
69
+ * documents into chunks and makes requests to the Google Vertex AI API to
70
+ * generate embeddings.
71
+ * @param documents An array of documents to be embedded.
72
+ * @returns A promise that resolves to a 2D array of embeddings for each document.
73
+ */
74
+ async embedDocuments(documents) {
75
+ const instanceChunks = (0, chunk_array_1.chunkArray)(documents.map((document) => ({
76
+ content: document,
77
+ })), 5); // Vertex AI accepts max 5 instances per prediction
78
+ const parameters = {};
79
+ const options = {};
80
+ const responses = await Promise.all(instanceChunks.map((instances) => this.connection.request(instances, parameters, options)));
81
+ const result = responses
82
+ ?.map((response) => response?.data?.predictions?.map((result) => result.embeddings.values) ?? [])
83
+ .flat() ?? [];
84
+ return result;
85
+ }
86
+ /**
87
+ * Takes a document as input and returns a promise that resolves to an
88
+ * embedding for the document. It calls the embedDocuments method with the
89
+ * document as the input.
90
+ * @param document A document to be embedded.
91
+ * @returns A promise that resolves to an embedding for the document.
92
+ */
93
+ async embedQuery(document) {
94
+ const data = await this.embedDocuments([document]);
95
+ return data[0];
96
+ }
97
+ }
98
+ exports.BaseGoogleEmbeddings = BaseGoogleEmbeddings;
@@ -0,0 +1,73 @@
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 Google APIs for generating
46
+ * text embeddings.
47
+ */
48
+ export declare abstract class BaseGoogleEmbeddings<AuthOptions> extends Embeddings implements BaseGoogleEmbeddingsParams<AuthOptions> {
49
+ model: string;
50
+ private connection;
51
+ constructor(fields: BaseGoogleEmbeddingsParams<AuthOptions>);
52
+ abstract buildAbstractedClient(fields?: GoogleConnectionParams<AuthOptions>): GoogleAbstractedClient;
53
+ buildApiKeyClient(apiKey: string): GoogleAbstractedClient;
54
+ buildApiKey(fields?: GoogleConnectionParams<AuthOptions>): string | undefined;
55
+ buildClient(fields?: GoogleConnectionParams<AuthOptions>): GoogleAbstractedClient;
56
+ /**
57
+ * Takes an array of documents as input and returns a promise that
58
+ * resolves to a 2D array of embeddings for each document. It splits the
59
+ * documents into chunks and makes requests to the Google Vertex AI API to
60
+ * generate embeddings.
61
+ * @param documents An array of documents to be embedded.
62
+ * @returns A promise that resolves to a 2D array of embeddings for each document.
63
+ */
64
+ embedDocuments(documents: string[]): Promise<number[][]>;
65
+ /**
66
+ * Takes a document as input and returns a promise that resolves to an
67
+ * embedding for the document. It calls the embedDocuments method with the
68
+ * document as the input.
69
+ * @param document A document to be embedded.
70
+ * @returns A promise that resolves to an embedding for the document.
71
+ */
72
+ embedQuery(document: string): Promise<number[]>;
73
+ }
@@ -0,0 +1,94 @@
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 Google APIs for generating
28
+ * text embeddings.
29
+ */
30
+ export class BaseGoogleEmbeddings extends Embeddings {
31
+ constructor(fields) {
32
+ super(fields);
33
+ Object.defineProperty(this, "model", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "connection", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ this.model = fields.model;
46
+ this.connection = new EmbeddingsConnection({ ...fields, ...this }, this.caller, this.buildClient(fields), false);
47
+ }
48
+ buildApiKeyClient(apiKey) {
49
+ return new ApiKeyGoogleAuth(apiKey);
50
+ }
51
+ buildApiKey(fields) {
52
+ return fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
53
+ }
54
+ buildClient(fields) {
55
+ const apiKey = this.buildApiKey(fields);
56
+ if (apiKey) {
57
+ return this.buildApiKeyClient(apiKey);
58
+ }
59
+ else {
60
+ return this.buildAbstractedClient(fields);
61
+ }
62
+ }
63
+ /**
64
+ * Takes an array of documents as input and returns a promise that
65
+ * resolves to a 2D array of embeddings for each document. It splits the
66
+ * documents into chunks and makes requests to the Google Vertex AI API to
67
+ * generate embeddings.
68
+ * @param documents An array of documents to be embedded.
69
+ * @returns A promise that resolves to a 2D array of embeddings for each document.
70
+ */
71
+ async embedDocuments(documents) {
72
+ const instanceChunks = chunkArray(documents.map((document) => ({
73
+ content: document,
74
+ })), 5); // Vertex AI accepts max 5 instances per prediction
75
+ const parameters = {};
76
+ const options = {};
77
+ const responses = await Promise.all(instanceChunks.map((instances) => this.connection.request(instances, parameters, options)));
78
+ const result = responses
79
+ ?.map((response) => response?.data?.predictions?.map((result) => result.embeddings.values) ?? [])
80
+ .flat() ?? [];
81
+ return result;
82
+ }
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
+ async embedQuery(document) {
91
+ const data = await this.embedDocuments([document]);
92
+ return data[0];
93
+ }
94
+ }
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.25",
4
4
  "description": "Core types and classes for Google services.",
5
5
  "type": "module",
6
6
  "engines": {