@langchain/google-common 0.0.0

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.
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractGoogleLLMConnection = exports.GoogleAIConnection = exports.GoogleHostConnection = exports.GoogleConnection = void 0;
4
+ const env_1 = require("@langchain/core/utils/env");
5
+ class GoogleConnection {
6
+ constructor(caller, client, streaming) {
7
+ Object.defineProperty(this, "caller", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "client", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "streaming", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.caller = caller;
26
+ this.client = client;
27
+ this.streaming = streaming ?? false;
28
+ }
29
+ async _clientInfoHeaders() {
30
+ const clientLibraryVersion = await this._clientLibraryVersion();
31
+ return {
32
+ "User-Agent": clientLibraryVersion,
33
+ };
34
+ }
35
+ async _clientLibraryVersion() {
36
+ const env = await (0, env_1.getRuntimeEnvironment)();
37
+ const langchain = env?.library ?? "langchain-js";
38
+ const langchainVersion = env?.libraryVersion ?? "0";
39
+ const moduleName = await this._moduleName();
40
+ let ret = `${langchain}/${langchainVersion}`;
41
+ if (moduleName && moduleName.length) {
42
+ ret = `${ret}-${moduleName}`;
43
+ }
44
+ return ret;
45
+ }
46
+ async _moduleName() {
47
+ return this.constructor.name;
48
+ }
49
+ async _request(data, options) {
50
+ const url = await this.buildUrl();
51
+ const method = this.buildMethod();
52
+ const infoHeaders = (await this._clientInfoHeaders()) ?? {};
53
+ const headers = {
54
+ ...infoHeaders,
55
+ };
56
+ const opts = {
57
+ url,
58
+ method,
59
+ headers,
60
+ };
61
+ if (data && method === "POST") {
62
+ opts.data = data;
63
+ }
64
+ if (this.streaming) {
65
+ opts.responseType = "stream";
66
+ }
67
+ else {
68
+ opts.responseType = "json";
69
+ }
70
+ const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, async () => this.client.request(opts));
71
+ const response = callResponse; // Done for typecast safety, I guess
72
+ return response;
73
+ }
74
+ }
75
+ exports.GoogleConnection = GoogleConnection;
76
+ class GoogleHostConnection extends GoogleConnection {
77
+ constructor(fields, caller, client, streaming) {
78
+ super(caller, client, streaming);
79
+ // This does not default to a value intentionally.
80
+ // Use the "platform" getter if you need this.
81
+ Object.defineProperty(this, "platformType", {
82
+ enumerable: true,
83
+ configurable: true,
84
+ writable: true,
85
+ value: void 0
86
+ });
87
+ Object.defineProperty(this, "endpoint", {
88
+ enumerable: true,
89
+ configurable: true,
90
+ writable: true,
91
+ value: "us-central1-aiplatform.googleapis.com"
92
+ });
93
+ Object.defineProperty(this, "location", {
94
+ enumerable: true,
95
+ configurable: true,
96
+ writable: true,
97
+ value: "us-central1"
98
+ });
99
+ Object.defineProperty(this, "apiVersion", {
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true,
103
+ value: "v1"
104
+ });
105
+ this.caller = caller;
106
+ this.platformType = fields?.platformType;
107
+ this.endpoint = fields?.endpoint ?? this.endpoint;
108
+ this.location = fields?.location ?? this.location;
109
+ this.apiVersion = fields?.apiVersion ?? this.apiVersion;
110
+ this.client = client;
111
+ }
112
+ get platform() {
113
+ return this.platformType ?? this.computedPlatformType;
114
+ }
115
+ get computedPlatformType() {
116
+ return "gcp";
117
+ }
118
+ buildMethod() {
119
+ return "POST";
120
+ }
121
+ }
122
+ exports.GoogleHostConnection = GoogleHostConnection;
123
+ class GoogleAIConnection extends GoogleHostConnection {
124
+ constructor(fields, caller, client, streaming) {
125
+ super(fields, caller, client, streaming);
126
+ Object.defineProperty(this, "model", {
127
+ enumerable: true,
128
+ configurable: true,
129
+ writable: true,
130
+ value: void 0
131
+ });
132
+ Object.defineProperty(this, "client", {
133
+ enumerable: true,
134
+ configurable: true,
135
+ writable: true,
136
+ value: void 0
137
+ });
138
+ this.client = client;
139
+ this.model = fields?.model ?? this.model;
140
+ }
141
+ get modelFamily() {
142
+ if (this.model.startsWith("gemini")) {
143
+ return "gemini";
144
+ }
145
+ else {
146
+ return null;
147
+ }
148
+ }
149
+ get computedPlatformType() {
150
+ if (this.client.clientType === "apiKey") {
151
+ return "gai";
152
+ }
153
+ else {
154
+ return "gcp";
155
+ }
156
+ }
157
+ async buildUrlGenerativeLanguage() {
158
+ const method = await this.buildUrlMethod();
159
+ const url = `https://generativelanguage.googleapis.com/${this.apiVersion}/models/${this.model}:${method}`;
160
+ return url;
161
+ }
162
+ async buildUrlVertex() {
163
+ const projectId = await this.client.getProjectId();
164
+ const method = await this.buildUrlMethod();
165
+ const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/publishers/google/models/${this.model}:${method}`;
166
+ return url;
167
+ }
168
+ async buildUrl() {
169
+ switch (this.platform) {
170
+ case "gai":
171
+ return this.buildUrlGenerativeLanguage();
172
+ default:
173
+ return this.buildUrlVertex();
174
+ }
175
+ }
176
+ async request(input, parameters, options) {
177
+ const data = this.formatData(input, parameters);
178
+ const response = await this._request(data, options);
179
+ return response;
180
+ }
181
+ }
182
+ exports.GoogleAIConnection = GoogleAIConnection;
183
+ class AbstractGoogleLLMConnection extends GoogleAIConnection {
184
+ async buildUrlMethodGemini() {
185
+ // Vertex AI only handles streamedGenerateContent
186
+ return "streamGenerateContent";
187
+ }
188
+ async buildUrlMethod() {
189
+ switch (this.modelFamily) {
190
+ case "gemini":
191
+ return this.buildUrlMethodGemini();
192
+ default:
193
+ throw new Error(`Unknown model family: ${this.modelFamily}`);
194
+ }
195
+ }
196
+ formatGenerationConfig(_input, parameters) {
197
+ return {
198
+ temperature: parameters.temperature,
199
+ topK: parameters.topK,
200
+ topP: parameters.topP,
201
+ maxOutputTokens: parameters.maxOutputTokens,
202
+ stopSequences: parameters.stopSequences,
203
+ };
204
+ }
205
+ formatSafetySettings(_input, parameters) {
206
+ return parameters.safetySettings ?? [];
207
+ }
208
+ formatData(input, parameters) {
209
+ /*
210
+ const parts = messageContentToParts(input);
211
+ const contents: GeminiContent[] = [
212
+ {
213
+ role: "user", // Required by Vertex AI
214
+ parts,
215
+ }
216
+ ]
217
+ */
218
+ const contents = this.formatContents(input, parameters);
219
+ const generationConfig = this.formatGenerationConfig(input, parameters);
220
+ const safetySettings = this.formatSafetySettings(input, parameters);
221
+ const ret = {
222
+ contents,
223
+ generationConfig,
224
+ };
225
+ if (safetySettings && safetySettings.length) {
226
+ ret.safetySettings = safetySettings;
227
+ }
228
+ return ret;
229
+ }
230
+ }
231
+ exports.AbstractGoogleLLMConnection = AbstractGoogleLLMConnection;
@@ -0,0 +1,47 @@
1
+ import { BaseLanguageModelCallOptions } from "@langchain/core/language_models/base";
2
+ import { AsyncCaller, AsyncCallerCallOptions } from "@langchain/core/utils/async_caller";
3
+ import type { GoogleAIBaseLLMInput, GoogleAIModelParams, GoogleConnectionParams, GoogleLLMModelFamily, GooglePlatformType, GoogleResponse, GoogleLLMResponse, GeminiContent, GeminiGenerationConfig, GeminiRequest, GeminiSafetySetting } from "./types.js";
4
+ import { GoogleAbstractedClient, GoogleAbstractedClientOpsMethod } from "./auth.js";
5
+ export declare abstract class GoogleConnection<CallOptions extends AsyncCallerCallOptions, ResponseType extends GoogleResponse> {
6
+ caller: AsyncCaller;
7
+ client: GoogleAbstractedClient;
8
+ streaming: boolean;
9
+ constructor(caller: AsyncCaller, client: GoogleAbstractedClient, streaming?: boolean);
10
+ abstract buildUrl(): Promise<string>;
11
+ abstract buildMethod(): GoogleAbstractedClientOpsMethod;
12
+ _clientInfoHeaders(): Promise<Record<string, string>>;
13
+ _clientLibraryVersion(): Promise<string>;
14
+ _moduleName(): Promise<string>;
15
+ _request(data: unknown | undefined, options: CallOptions): Promise<ResponseType>;
16
+ }
17
+ export declare abstract class GoogleHostConnection<CallOptions extends AsyncCallerCallOptions, ResponseType extends GoogleResponse, AuthOptions> extends GoogleConnection<CallOptions, ResponseType> implements GoogleConnectionParams<AuthOptions> {
18
+ platformType: GooglePlatformType | undefined;
19
+ endpoint: string;
20
+ location: string;
21
+ apiVersion: string;
22
+ constructor(fields: GoogleConnectionParams<AuthOptions> | undefined, caller: AsyncCaller, client: GoogleAbstractedClient, streaming?: boolean);
23
+ get platform(): GooglePlatformType;
24
+ get computedPlatformType(): GooglePlatformType;
25
+ buildMethod(): GoogleAbstractedClientOpsMethod;
26
+ }
27
+ export declare abstract class GoogleAIConnection<CallOptions extends BaseLanguageModelCallOptions, MessageType, AuthOptions> extends GoogleHostConnection<CallOptions, GoogleLLMResponse, AuthOptions> implements GoogleAIBaseLLMInput<AuthOptions> {
28
+ model: string;
29
+ client: GoogleAbstractedClient;
30
+ constructor(fields: GoogleAIBaseLLMInput<AuthOptions> | undefined, caller: AsyncCaller, client: GoogleAbstractedClient, streaming?: boolean);
31
+ get modelFamily(): GoogleLLMModelFamily;
32
+ get computedPlatformType(): GooglePlatformType;
33
+ abstract buildUrlMethod(): Promise<string>;
34
+ buildUrlGenerativeLanguage(): Promise<string>;
35
+ buildUrlVertex(): Promise<string>;
36
+ buildUrl(): Promise<string>;
37
+ abstract formatData(input: MessageType, parameters: GoogleAIModelParams): unknown;
38
+ request(input: MessageType, parameters: GoogleAIModelParams, options: CallOptions): Promise<GoogleLLMResponse>;
39
+ }
40
+ export declare abstract class AbstractGoogleLLMConnection<MessageType, AuthOptions> extends GoogleAIConnection<BaseLanguageModelCallOptions, MessageType, AuthOptions> {
41
+ buildUrlMethodGemini(): Promise<string>;
42
+ buildUrlMethod(): Promise<string>;
43
+ abstract formatContents(input: MessageType, parameters: GoogleAIModelParams): GeminiContent[];
44
+ formatGenerationConfig(_input: MessageType, parameters: GoogleAIModelParams): GeminiGenerationConfig;
45
+ formatSafetySettings(_input: MessageType, parameters: GoogleAIModelParams): GeminiSafetySetting[];
46
+ formatData(input: MessageType, parameters: GoogleAIModelParams): GeminiRequest;
47
+ }
@@ -0,0 +1,224 @@
1
+ import { getRuntimeEnvironment } from "@langchain/core/utils/env";
2
+ export class GoogleConnection {
3
+ constructor(caller, client, streaming) {
4
+ Object.defineProperty(this, "caller", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: void 0
9
+ });
10
+ Object.defineProperty(this, "client", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "streaming", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ this.caller = caller;
23
+ this.client = client;
24
+ this.streaming = streaming ?? false;
25
+ }
26
+ async _clientInfoHeaders() {
27
+ const clientLibraryVersion = await this._clientLibraryVersion();
28
+ return {
29
+ "User-Agent": clientLibraryVersion,
30
+ };
31
+ }
32
+ async _clientLibraryVersion() {
33
+ const env = await getRuntimeEnvironment();
34
+ const langchain = env?.library ?? "langchain-js";
35
+ const langchainVersion = env?.libraryVersion ?? "0";
36
+ const moduleName = await this._moduleName();
37
+ let ret = `${langchain}/${langchainVersion}`;
38
+ if (moduleName && moduleName.length) {
39
+ ret = `${ret}-${moduleName}`;
40
+ }
41
+ return ret;
42
+ }
43
+ async _moduleName() {
44
+ return this.constructor.name;
45
+ }
46
+ async _request(data, options) {
47
+ const url = await this.buildUrl();
48
+ const method = this.buildMethod();
49
+ const infoHeaders = (await this._clientInfoHeaders()) ?? {};
50
+ const headers = {
51
+ ...infoHeaders,
52
+ };
53
+ const opts = {
54
+ url,
55
+ method,
56
+ headers,
57
+ };
58
+ if (data && method === "POST") {
59
+ opts.data = data;
60
+ }
61
+ if (this.streaming) {
62
+ opts.responseType = "stream";
63
+ }
64
+ else {
65
+ opts.responseType = "json";
66
+ }
67
+ const callResponse = await this.caller.callWithOptions({ signal: options?.signal }, async () => this.client.request(opts));
68
+ const response = callResponse; // Done for typecast safety, I guess
69
+ return response;
70
+ }
71
+ }
72
+ export class GoogleHostConnection extends GoogleConnection {
73
+ constructor(fields, caller, client, streaming) {
74
+ super(caller, client, streaming);
75
+ // This does not default to a value intentionally.
76
+ // Use the "platform" getter if you need this.
77
+ Object.defineProperty(this, "platformType", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "endpoint", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: "us-central1-aiplatform.googleapis.com"
88
+ });
89
+ Object.defineProperty(this, "location", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: "us-central1"
94
+ });
95
+ Object.defineProperty(this, "apiVersion", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: "v1"
100
+ });
101
+ this.caller = caller;
102
+ this.platformType = fields?.platformType;
103
+ this.endpoint = fields?.endpoint ?? this.endpoint;
104
+ this.location = fields?.location ?? this.location;
105
+ this.apiVersion = fields?.apiVersion ?? this.apiVersion;
106
+ this.client = client;
107
+ }
108
+ get platform() {
109
+ return this.platformType ?? this.computedPlatformType;
110
+ }
111
+ get computedPlatformType() {
112
+ return "gcp";
113
+ }
114
+ buildMethod() {
115
+ return "POST";
116
+ }
117
+ }
118
+ export class GoogleAIConnection extends GoogleHostConnection {
119
+ constructor(fields, caller, client, streaming) {
120
+ super(fields, caller, client, streaming);
121
+ Object.defineProperty(this, "model", {
122
+ enumerable: true,
123
+ configurable: true,
124
+ writable: true,
125
+ value: void 0
126
+ });
127
+ Object.defineProperty(this, "client", {
128
+ enumerable: true,
129
+ configurable: true,
130
+ writable: true,
131
+ value: void 0
132
+ });
133
+ this.client = client;
134
+ this.model = fields?.model ?? this.model;
135
+ }
136
+ get modelFamily() {
137
+ if (this.model.startsWith("gemini")) {
138
+ return "gemini";
139
+ }
140
+ else {
141
+ return null;
142
+ }
143
+ }
144
+ get computedPlatformType() {
145
+ if (this.client.clientType === "apiKey") {
146
+ return "gai";
147
+ }
148
+ else {
149
+ return "gcp";
150
+ }
151
+ }
152
+ async buildUrlGenerativeLanguage() {
153
+ const method = await this.buildUrlMethod();
154
+ const url = `https://generativelanguage.googleapis.com/${this.apiVersion}/models/${this.model}:${method}`;
155
+ return url;
156
+ }
157
+ async buildUrlVertex() {
158
+ const projectId = await this.client.getProjectId();
159
+ const method = await this.buildUrlMethod();
160
+ const url = `https://${this.endpoint}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/publishers/google/models/${this.model}:${method}`;
161
+ return url;
162
+ }
163
+ async buildUrl() {
164
+ switch (this.platform) {
165
+ case "gai":
166
+ return this.buildUrlGenerativeLanguage();
167
+ default:
168
+ return this.buildUrlVertex();
169
+ }
170
+ }
171
+ async request(input, parameters, options) {
172
+ const data = this.formatData(input, parameters);
173
+ const response = await this._request(data, options);
174
+ return response;
175
+ }
176
+ }
177
+ export class AbstractGoogleLLMConnection extends GoogleAIConnection {
178
+ async buildUrlMethodGemini() {
179
+ // Vertex AI only handles streamedGenerateContent
180
+ return "streamGenerateContent";
181
+ }
182
+ async buildUrlMethod() {
183
+ switch (this.modelFamily) {
184
+ case "gemini":
185
+ return this.buildUrlMethodGemini();
186
+ default:
187
+ throw new Error(`Unknown model family: ${this.modelFamily}`);
188
+ }
189
+ }
190
+ formatGenerationConfig(_input, parameters) {
191
+ return {
192
+ temperature: parameters.temperature,
193
+ topK: parameters.topK,
194
+ topP: parameters.topP,
195
+ maxOutputTokens: parameters.maxOutputTokens,
196
+ stopSequences: parameters.stopSequences,
197
+ };
198
+ }
199
+ formatSafetySettings(_input, parameters) {
200
+ return parameters.safetySettings ?? [];
201
+ }
202
+ formatData(input, parameters) {
203
+ /*
204
+ const parts = messageContentToParts(input);
205
+ const contents: GeminiContent[] = [
206
+ {
207
+ role: "user", // Required by Vertex AI
208
+ parts,
209
+ }
210
+ ]
211
+ */
212
+ const contents = this.formatContents(input, parameters);
213
+ const generationConfig = this.formatGenerationConfig(input, parameters);
214
+ const safetySettings = this.formatSafetySettings(input, parameters);
215
+ const ret = {
216
+ contents,
217
+ generationConfig,
218
+ };
219
+ if (safetySettings && safetySettings.length) {
220
+ ret.safetySettings = safetySettings;
221
+ }
222
+ return ret;
223
+ }
224
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./chat_models.cjs"), exports);
18
+ __exportStar(require("./llms.cjs"), exports);
19
+ __exportStar(require("./auth.cjs"), exports);
20
+ __exportStar(require("./connection.cjs"), exports);
21
+ __exportStar(require("./types.cjs"), exports);
22
+ __exportStar(require("./utils/stream.cjs"), exports);
23
+ __exportStar(require("./utils/common.cjs"), exports);
@@ -0,0 +1,7 @@
1
+ export * from "./chat_models.js";
2
+ export * from "./llms.js";
3
+ export * from "./auth.js";
4
+ export * from "./connection.js";
5
+ export * from "./types.js";
6
+ export * from "./utils/stream.js";
7
+ export * from "./utils/common.js";
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export * from "./chat_models.js";
2
+ export * from "./llms.js";
3
+ export * from "./auth.js";
4
+ export * from "./connection.js";
5
+ export * from "./types.js";
6
+ export * from "./utils/stream.js";
7
+ export * from "./utils/common.js";