@langchain/google 0.2.11 → 0.2.13
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.
- package/CHANGELOG.md +29 -0
- package/dist/chat_models/{api-types.d.ts → api_types/generate_content.d.cts} +198 -200
- package/dist/chat_models/{api-types.d.cts → api_types/generate_content.d.ts} +198 -200
- package/dist/chat_models/api_types/interactions.d.cts +1423 -0
- package/dist/chat_models/api_types/interactions.d.ts +1423 -0
- package/dist/chat_models/base.cjs +60 -403
- package/dist/chat_models/base.cjs.map +1 -1
- package/dist/chat_models/base.d.cts +13 -200
- package/dist/chat_models/base.d.ts +15 -202
- package/dist/chat_models/base.js +61 -402
- package/dist/chat_models/base.js.map +1 -1
- package/dist/chat_models/handlers/base.cjs +62 -0
- package/dist/chat_models/handlers/base.cjs.map +1 -0
- package/dist/chat_models/handlers/base.d.cts +46 -0
- package/dist/chat_models/handlers/base.d.ts +46 -0
- package/dist/chat_models/handlers/base.js +62 -0
- package/dist/chat_models/handlers/base.js.map +1 -0
- package/dist/chat_models/handlers/generate_content.cjs +344 -0
- package/dist/chat_models/handlers/generate_content.cjs.map +1 -0
- package/dist/chat_models/handlers/generate_content.d.cts +32 -0
- package/dist/chat_models/handlers/generate_content.d.ts +32 -0
- package/dist/chat_models/handlers/generate_content.js +344 -0
- package/dist/chat_models/handlers/generate_content.js.map +1 -0
- package/dist/chat_models/handlers/index.cjs +6 -0
- package/dist/chat_models/handlers/index.d.cts +4 -0
- package/dist/chat_models/handlers/index.d.ts +4 -0
- package/dist/chat_models/handlers/index.js +4 -0
- package/dist/chat_models/handlers/interactions.cjs +303 -0
- package/dist/chat_models/handlers/interactions.cjs.map +1 -0
- package/dist/chat_models/handlers/interactions.d.cts +33 -0
- package/dist/chat_models/handlers/interactions.d.ts +33 -0
- package/dist/chat_models/handlers/interactions.js +303 -0
- package/dist/chat_models/handlers/interactions.js.map +1 -0
- package/dist/chat_models/index.cjs +14 -6
- package/dist/chat_models/index.cjs.map +1 -1
- package/dist/chat_models/index.d.cts +7 -3
- package/dist/chat_models/index.d.ts +7 -3
- package/dist/chat_models/index.js +8 -4
- package/dist/chat_models/index.js.map +1 -1
- package/dist/chat_models/node.cjs +4 -5
- package/dist/chat_models/node.cjs.map +1 -1
- package/dist/chat_models/node.d.cts +1 -3
- package/dist/chat_models/node.d.ts +1 -3
- package/dist/chat_models/node.js +3 -4
- package/dist/chat_models/node.js.map +1 -1
- package/dist/chat_models/profiles.cjs +1 -2
- package/dist/chat_models/profiles.js +1 -2
- package/dist/chat_models/types.cjs +1 -2
- package/dist/chat_models/types.cjs.map +1 -1
- package/dist/chat_models/types.d.cts +776 -88
- package/dist/chat_models/types.d.ts +776 -88
- package/dist/chat_models/types.js +1 -2
- package/dist/chat_models/types.js.map +1 -1
- package/dist/clients/index.cjs +3 -4
- package/dist/clients/index.d.cts +0 -2
- package/dist/clients/index.d.ts +0 -2
- package/dist/clients/index.js +3 -4
- package/dist/clients/node.cjs +4 -5
- package/dist/clients/node.d.cts +0 -2
- package/dist/clients/node.d.ts +0 -2
- package/dist/clients/node.js +4 -5
- package/dist/const.cjs +1 -2
- package/dist/const.js +1 -2
- package/dist/converters/{messages.cjs → generate_content/messages.cjs} +4 -5
- package/dist/converters/generate_content/messages.cjs.map +1 -0
- package/dist/converters/{messages.js → generate_content/messages.js} +4 -5
- package/dist/converters/generate_content/messages.js.map +1 -0
- package/dist/converters/generate_content/params.cjs +135 -0
- package/dist/converters/generate_content/params.cjs.map +1 -0
- package/dist/converters/generate_content/params.js +131 -0
- package/dist/converters/generate_content/params.js.map +1 -0
- package/dist/{utils → converters/generate_content}/stream_events.cjs +2 -3
- package/dist/converters/generate_content/stream_events.cjs.map +1 -0
- package/dist/{utils → converters/generate_content}/stream_events.js +2 -3
- package/dist/converters/generate_content/stream_events.js.map +1 -0
- package/dist/converters/{tools.cjs → generate_content/tools.cjs} +4 -5
- package/dist/converters/generate_content/tools.cjs.map +1 -0
- package/dist/converters/{tools.js → generate_content/tools.js} +4 -5
- package/dist/converters/generate_content/tools.js.map +1 -0
- package/dist/converters/interactions/messages.cjs +766 -0
- package/dist/converters/interactions/messages.cjs.map +1 -0
- package/dist/converters/interactions/messages.js +759 -0
- package/dist/converters/interactions/messages.js.map +1 -0
- package/dist/converters/interactions/params.cjs +244 -0
- package/dist/converters/interactions/params.cjs.map +1 -0
- package/dist/converters/interactions/params.js +237 -0
- package/dist/converters/interactions/params.js.map +1 -0
- package/dist/converters/interactions/stream_events.cjs +769 -0
- package/dist/converters/interactions/stream_events.cjs.map +1 -0
- package/dist/converters/interactions/stream_events.js +768 -0
- package/dist/converters/interactions/stream_events.js.map +1 -0
- package/dist/converters/interactions/tools.cjs +229 -0
- package/dist/converters/interactions/tools.cjs.map +1 -0
- package/dist/converters/interactions/tools.js +228 -0
- package/dist/converters/interactions/tools.js.map +1 -0
- package/dist/converters/{params.cjs → shared/params.cjs} +8 -94
- package/dist/converters/shared/params.cjs.map +1 -0
- package/dist/converters/{params.js → shared/params.js} +8 -92
- package/dist/converters/shared/params.js.map +1 -0
- package/dist/index.cjs +8 -0
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +5 -1
- package/dist/node.cjs +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.js +1 -1
- package/dist/utils/errors.cjs +1 -2
- package/dist/utils/errors.js +1 -2
- package/dist/utils/gcp-auth.cjs +3 -4
- package/dist/utils/gcp-auth.d.cts +0 -2
- package/dist/utils/gcp-auth.d.ts +0 -2
- package/dist/utils/gcp-auth.js +3 -4
- package/dist/utils/handler.cjs +1 -2
- package/dist/utils/handler.d.cts +0 -2
- package/dist/utils/handler.d.ts +0 -2
- package/dist/utils/handler.js +1 -2
- package/dist/utils/metadata.cjs +9 -2
- package/dist/utils/metadata.cjs.map +1 -1
- package/dist/utils/metadata.js +9 -3
- package/dist/utils/metadata.js.map +1 -1
- package/dist/utils/misc.cjs +1 -2
- package/dist/utils/misc.cjs.map +1 -1
- package/dist/utils/misc.d.cts +2 -3
- package/dist/utils/misc.d.ts +2 -3
- package/dist/utils/misc.js +1 -2
- package/dist/utils/misc.js.map +1 -1
- package/dist/utils/stream.cjs +1 -2
- package/dist/utils/stream.js +1 -2
- package/package.json +12 -10
- package/dist/converters/messages.cjs.map +0 -1
- package/dist/converters/messages.js.map +0 -1
- package/dist/converters/params.cjs.map +0 -1
- package/dist/converters/params.js.map +0 -1
- package/dist/converters/tools.cjs.map +0 -1
- package/dist/converters/tools.js.map +0 -1
- package/dist/utils/stream_events.cjs.map +0 -1
- package/dist/utils/stream_events.js.map +0 -1
|
@@ -1,26 +1,18 @@
|
|
|
1
|
-
const require_misc = require("../utils/misc.cjs");
|
|
2
|
-
const require_errors = require("../utils/errors.cjs");
|
|
3
|
-
const require_stream_events = require("../utils/stream_events.cjs");
|
|
4
1
|
const require_profiles = require("./profiles.cjs");
|
|
5
|
-
const
|
|
6
|
-
const require_stream = require("../utils/stream.cjs");
|
|
7
|
-
const require_messages = require("../converters/messages.cjs");
|
|
8
|
-
const require_tools = require("../converters/tools.cjs");
|
|
9
|
-
const require_params = require("../converters/params.cjs");
|
|
2
|
+
const require_errors = require("../utils/errors.cjs");
|
|
10
3
|
const require_metadata = require("../utils/metadata.cjs");
|
|
4
|
+
require("./handlers/index.cjs");
|
|
5
|
+
const require_generate_content = require("./handlers/generate_content.cjs");
|
|
6
|
+
const require_interactions = require("./handlers/interactions.cjs");
|
|
7
|
+
const require_params = require("../converters/shared/params.cjs");
|
|
11
8
|
let _langchain_core_utils_env = require("@langchain/core/utils/env");
|
|
12
9
|
let _langchain_core_language_models_chat_models = require("@langchain/core/language_models/chat_models");
|
|
13
10
|
let _langchain_core_messages = require("@langchain/core/messages");
|
|
14
11
|
let _langchain_core_utils_stream = require("@langchain/core/utils/stream");
|
|
15
|
-
let _langchain_core_outputs = require("@langchain/core/outputs");
|
|
16
|
-
let eventsource_parser_stream = require("eventsource-parser/stream");
|
|
17
12
|
let _langchain_core_runnables = require("@langchain/core/runnables");
|
|
18
|
-
let _langchain_core_utils_json_schema = require("@langchain/core/utils/json_schema");
|
|
19
|
-
let _langchain_core_utils_types = require("@langchain/core/utils/types");
|
|
20
|
-
let _langchain_core_utils_standard_schema = require("@langchain/core/utils/standard_schema");
|
|
21
13
|
let _langchain_core_language_models_structured_output = require("@langchain/core/language_models/structured_output");
|
|
22
14
|
let _langchain_core_utils_gateway = require("@langchain/core/utils/gateway");
|
|
23
|
-
|
|
15
|
+
// src/chat_models/base.ts
|
|
24
16
|
function hasVertexCredentials(params) {
|
|
25
17
|
return typeof params.credentials !== "undefined" || typeof params.googleAuthOptions !== "undefined" || typeof (0, _langchain_core_utils_env.getEnvironmentVariable)("GOOGLE_CLOUD_CREDENTIALS") !== "undefined";
|
|
26
18
|
}
|
|
@@ -28,11 +20,11 @@ function hasVertexCredentials(params) {
|
|
|
28
20
|
* Resolves LangSmith gateway routing for Google chat model params.
|
|
29
21
|
*
|
|
30
22
|
* The LangSmith gateway exposes direct paths for the Gemini Developer API and
|
|
31
|
-
* Vertex AI. An explicit `endpoint`
|
|
23
|
+
* Vertex AI. An explicit `endpoint` or explicit credentials (`apiKey`,
|
|
24
|
+
* `credentials`, `googleAuthOptions`) suppress gateway routing.
|
|
32
25
|
*
|
|
33
26
|
* When `LANGSMITH_GATEWAY` is set, this rewrites `endpoint` to the gateway
|
|
34
|
-
* host and
|
|
35
|
-
* then rides as the `x-goog-api-key` header via the ApiClient). The default
|
|
27
|
+
* host and uses the gateway key as the API key. The default
|
|
36
28
|
* `https` gateway host is written scheme-less (the URL builders prepend
|
|
37
29
|
* `https://`); a non-`https` custom gateway keeps its scheme so it is not
|
|
38
30
|
* forced to TLS.
|
|
@@ -44,8 +36,9 @@ function hasVertexCredentials(params) {
|
|
|
44
36
|
* @returns a new params object with gateway routing applied when applicable.
|
|
45
37
|
*/
|
|
46
38
|
function applyGeminiGatewayParams(params) {
|
|
47
|
-
|
|
48
|
-
|
|
39
|
+
const p = params;
|
|
40
|
+
if (typeof p.endpoint !== "undefined" || typeof p.apiKey !== "undefined" || typeof p.credentials !== "undefined" || typeof p.googleAuthOptions !== "undefined") return { ...params };
|
|
41
|
+
const platform = require_params.convertParamsToPlatformType(p) ?? (hasVertexCredentials(p) ? "gcp" : "gai");
|
|
49
42
|
const gatewayConfig = (0, _langchain_core_utils_gateway.resolveLangSmithGatewayConfig)({ providerPath: platform === "gcp" ? "vertex" : "gemini" });
|
|
50
43
|
if (typeof gatewayConfig.baseURL === "undefined") return { ...params };
|
|
51
44
|
const gatewayUrl = new URL(gatewayConfig.baseURL);
|
|
@@ -53,7 +46,7 @@ function applyGeminiGatewayParams(params) {
|
|
|
53
46
|
const endpoint = gatewayUrl.protocol === "https:" ? `${gatewayUrl.host}${path}` : `${gatewayUrl.protocol}//${gatewayUrl.host}${path}`;
|
|
54
47
|
return {
|
|
55
48
|
...params,
|
|
56
|
-
apiKey:
|
|
49
|
+
apiKey: gatewayConfig.apiKey,
|
|
57
50
|
endpoint,
|
|
58
51
|
platformType: platform
|
|
59
52
|
};
|
|
@@ -63,20 +56,6 @@ function getPlatformType(platform, hasApiKey) {
|
|
|
63
56
|
else if (hasApiKey) return "gai";
|
|
64
57
|
else return "gcp";
|
|
65
58
|
}
|
|
66
|
-
function usageMetadataToTokenUsage(usageMetadata) {
|
|
67
|
-
return {
|
|
68
|
-
promptTokens: usageMetadata.input_tokens,
|
|
69
|
-
completionTokens: usageMetadata.output_tokens,
|
|
70
|
-
totalTokens: usageMetadata.total_tokens
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
function mapDetailToMediaResolution(detail) {
|
|
74
|
-
switch (detail) {
|
|
75
|
-
case "low": return "MEDIA_RESOLUTION_LOW";
|
|
76
|
-
case "high": return "MEDIA_RESOLUTION_HIGH";
|
|
77
|
-
default: return;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
59
|
var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.BaseChatModel {
|
|
81
60
|
params;
|
|
82
61
|
model;
|
|
@@ -92,7 +71,7 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
|
|
|
92
71
|
constructor(params) {
|
|
93
72
|
super(params);
|
|
94
73
|
this.params = params;
|
|
95
|
-
this._addVersion("@langchain/google", "0.2.
|
|
74
|
+
this._addVersion("@langchain/google", "0.2.13");
|
|
96
75
|
if (!params.apiClient) throw new require_errors.ConfigurationError("BaseChatGoogle requires an apiClient. This should be provided automatically by ChatGoogle constructors. If you're extending BaseChatGoogle directly, please provide an apiClient instance.");
|
|
97
76
|
this.apiClient = params.apiClient;
|
|
98
77
|
this.model = params.model;
|
|
@@ -139,8 +118,38 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
|
|
|
139
118
|
get publisher() {
|
|
140
119
|
return "google";
|
|
141
120
|
}
|
|
142
|
-
|
|
143
|
-
|
|
121
|
+
_apiHandler;
|
|
122
|
+
_handlerParams;
|
|
123
|
+
syncHandlerParams() {
|
|
124
|
+
const nextParams = {
|
|
125
|
+
...this.params,
|
|
126
|
+
model: this.model,
|
|
127
|
+
platform: this.platform,
|
|
128
|
+
endpoint: this.endpoint,
|
|
129
|
+
location: this.location,
|
|
130
|
+
apiVersion: this.apiVersion,
|
|
131
|
+
isVertexExpress: this.isVertexExpress,
|
|
132
|
+
publisher: this.publisher,
|
|
133
|
+
streaming: this.streaming,
|
|
134
|
+
streamUsage: this.streamUsage,
|
|
135
|
+
customHeaders: this._customHeaders,
|
|
136
|
+
apiClient: this.apiClient,
|
|
137
|
+
caller: this.caller
|
|
138
|
+
};
|
|
139
|
+
if (this._handlerParams) return Object.assign(this._handlerParams, nextParams);
|
|
140
|
+
this._handlerParams = nextParams;
|
|
141
|
+
return this._handlerParams;
|
|
142
|
+
}
|
|
143
|
+
get apiHandler() {
|
|
144
|
+
const handlerParams = this.syncHandlerParams();
|
|
145
|
+
if (!this._apiHandler) {
|
|
146
|
+
const handlerConfig = this.params.apiHandler ?? "generateContent";
|
|
147
|
+
if (typeof handlerConfig === "function") this._apiHandler = handlerConfig(handlerParams);
|
|
148
|
+
else if (handlerConfig === "generateContent") this._apiHandler = new require_generate_content.GenerateContentApiHandler(handlerParams);
|
|
149
|
+
else if (handlerConfig === "interactions") this._apiHandler = new require_interactions.InteractionsApiHandler(handlerParams);
|
|
150
|
+
else throw new require_errors.ConfigurationError(`Unknown apiHandler: ${handlerConfig}`);
|
|
151
|
+
}
|
|
152
|
+
return this._apiHandler;
|
|
144
153
|
}
|
|
145
154
|
/**
|
|
146
155
|
* Returns the model profile for this instance's model, describing its
|
|
@@ -158,96 +167,24 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
|
|
|
158
167
|
get profile() {
|
|
159
168
|
return require_profiles.default[this.model] ?? {};
|
|
160
169
|
}
|
|
161
|
-
async buildUrlGemini(urlMethod) {
|
|
162
|
-
const url = this.endpoint.includes("://") ? new URL(this.endpoint) : new URL(`https://${this.endpoint}`);
|
|
163
|
-
const [method, search] = (urlMethod ?? this.urlMethod).split("?", 2);
|
|
164
|
-
url.pathname = `${url.pathname.replace(/\/+$/, "")}/${this.apiVersion}/models/${this.model}:${method}`;
|
|
165
|
-
if (search) url.search = search;
|
|
166
|
-
return url.toString();
|
|
167
|
-
}
|
|
168
|
-
get endpointUrl() {
|
|
169
|
-
return this.endpoint.includes("://") ? this.endpoint : `https://${this.endpoint}`;
|
|
170
|
-
}
|
|
171
|
-
async buildUrlVertexExpress(urlMethod) {
|
|
172
|
-
return `${this.endpointUrl}/${this.apiVersion}/publishers/${this.publisher}/models/${this.model}:${urlMethod ?? this.urlMethod}`;
|
|
173
|
-
}
|
|
174
|
-
async buildUrlVertexLocation(urlMethod) {
|
|
175
|
-
const projectId = await this.apiClient.getProjectId();
|
|
176
|
-
return `${this.endpointUrl}/${this.apiVersion}/projects/${projectId}/locations/${this.location}/publishers/${this.publisher}/models/${this.model}:${urlMethod ?? this.urlMethod}`;
|
|
177
|
-
}
|
|
178
|
-
async buildUrlVertex(urlMethod) {
|
|
179
|
-
if (this.isVertexExpress) return this.buildUrlVertexExpress(urlMethod);
|
|
180
|
-
else return this.buildUrlVertexLocation(urlMethod);
|
|
181
|
-
}
|
|
182
170
|
async buildUrl(urlMethod) {
|
|
183
|
-
|
|
184
|
-
case "gai": return this.buildUrlGemini(urlMethod);
|
|
185
|
-
default: return this.buildUrlVertex(urlMethod);
|
|
186
|
-
}
|
|
171
|
+
return this.apiHandler.buildUrl(urlMethod);
|
|
187
172
|
}
|
|
188
173
|
invocationParams(options) {
|
|
189
|
-
|
|
190
|
-
const mediaResolution = fields.mediaResolution ?? mapDetailToMediaResolution(fields.detail);
|
|
191
|
-
const tools = fields.tools ? require_tools.convertToolsToGeminiTools(fields.tools) : void 0;
|
|
192
|
-
let toolConfig = require_tools.convertToolChoiceToGeminiConfig(options.tool_choice, !!(tools && tools.length > 0));
|
|
193
|
-
if (tools && require_tools.mixesBuiltinAndFunctionTools(tools)) toolConfig = {
|
|
194
|
-
...toolConfig,
|
|
195
|
-
includeServerSideToolInvocations: true
|
|
196
|
-
};
|
|
197
|
-
let responseJsonSchema;
|
|
198
|
-
let responseMimeType;
|
|
199
|
-
if (fields.responseSchema) {
|
|
200
|
-
if ((0, _langchain_core_utils_types.isInteropZodSchema)(fields.responseSchema) || (0, _langchain_core_utils_standard_schema.isSerializableSchema)(fields.responseSchema)) responseJsonSchema = (0, _langchain_core_utils_json_schema.toJsonSchema)(fields.responseSchema);
|
|
201
|
-
else responseJsonSchema = fields.responseSchema;
|
|
202
|
-
responseMimeType = "application/json";
|
|
203
|
-
}
|
|
204
|
-
return {
|
|
205
|
-
...tools && tools.length > 0 ? { tools } : {},
|
|
206
|
-
...toolConfig ? { toolConfig } : {},
|
|
207
|
-
safetySettings: fields.safetySettings,
|
|
208
|
-
generationConfig: {
|
|
209
|
-
temperature: fields.temperature,
|
|
210
|
-
topP: fields.topP,
|
|
211
|
-
topK: fields.topK,
|
|
212
|
-
maxOutputTokens: fields.maxOutputTokens,
|
|
213
|
-
presencePenalty: fields.presencePenalty,
|
|
214
|
-
frequencyPenalty: fields.frequencyPenalty,
|
|
215
|
-
stopSequences: fields.stopSequences,
|
|
216
|
-
...responseMimeType ? { responseMimeType } : {},
|
|
217
|
-
...responseJsonSchema ? { responseJsonSchema } : {},
|
|
218
|
-
...fields.responseModalities && fields.responseModalities.length > 0 ? { responseModalities: fields.responseModalities } : {},
|
|
219
|
-
candidateCount: 1,
|
|
220
|
-
seed: fields.seed,
|
|
221
|
-
responseLogprobs: fields.responseLogprobs,
|
|
222
|
-
logprobs: fields.logprobs,
|
|
223
|
-
...fields.enableEnhancedCivicAnswers !== void 0 ? { enableEnhancedCivicAnswers: fields.enableEnhancedCivicAnswers } : {},
|
|
224
|
-
thinkingConfig: require_params.convertFieldsToThinkingConfig(this.model, fields),
|
|
225
|
-
speechConfig: require_params.convertFieldsToSpeechConfig(fields),
|
|
226
|
-
...fields.imageConfig ? { imageConfig: fields.imageConfig } : {},
|
|
227
|
-
...mediaResolution ? { mediaResolution } : {}
|
|
228
|
-
},
|
|
229
|
-
...require_params.convertFieldsToServiceTier(this.platform, fields)
|
|
230
|
-
};
|
|
174
|
+
return this.apiHandler.invocationParams(options);
|
|
231
175
|
}
|
|
232
176
|
getLsParams(options) {
|
|
233
|
-
const params = this.invocationParams(options);
|
|
234
177
|
return {
|
|
235
178
|
ls_provider: this.platform === "gcp" ? "google_vertexai" : "google_genai",
|
|
236
179
|
ls_model_name: this.model,
|
|
237
180
|
ls_model_type: "chat",
|
|
238
|
-
ls_temperature:
|
|
239
|
-
ls_max_tokens:
|
|
240
|
-
ls_stop: options.
|
|
181
|
+
ls_temperature: options?.temperature ?? this.params.temperature,
|
|
182
|
+
ls_max_tokens: options?.maxOutputTokens ?? this.params.maxOutputTokens,
|
|
183
|
+
ls_stop: options?.stopSequences ?? options?.stop ?? this.params.stopSequences
|
|
241
184
|
};
|
|
242
185
|
}
|
|
243
186
|
getHeaders(options) {
|
|
244
|
-
|
|
245
|
-
return {
|
|
246
|
-
"Content-Type": "application/json",
|
|
247
|
-
...this.platform === "gcp" && typeof fields.serviceTier !== "undefined" && require_types.settableServiceTier.includes(fields.serviceTier) ? { "X-Vertex-AI-LLM-Shared-Request-Type": fields.serviceTier } : {},
|
|
248
|
-
...this._customHeaders,
|
|
249
|
-
...options.customHeaders
|
|
250
|
-
};
|
|
187
|
+
return this.apiHandler.getHeaders(options);
|
|
251
188
|
}
|
|
252
189
|
async _generate(messages, options, runManager) {
|
|
253
190
|
if (this.streaming) {
|
|
@@ -255,242 +192,22 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
|
|
|
255
192
|
let finalChunk = null;
|
|
256
193
|
for await (const chunk of stream) finalChunk = !finalChunk ? chunk : (0, _langchain_core_utils_stream.concat)(finalChunk, chunk);
|
|
257
194
|
if (typeof finalChunk?.message?.content === "string" && typeof (finalChunk?.message?.additional_kwargs?.originalTextContentBlock)?.text === "string") finalChunk.message.additional_kwargs.originalTextContentBlock.text = finalChunk.message.content;
|
|
258
|
-
const usageMetadata = finalChunk?.message?.usage_metadata;
|
|
195
|
+
const usageMetadata = (finalChunk?.message)?.usage_metadata;
|
|
259
196
|
return {
|
|
260
197
|
generations: finalChunk ? [finalChunk] : [],
|
|
261
198
|
...usageMetadata ? { llmOutput: {
|
|
262
|
-
tokenUsage: usageMetadataToTokenUsage(usageMetadata),
|
|
199
|
+
tokenUsage: require_metadata.usageMetadataToTokenUsage(usageMetadata),
|
|
263
200
|
usageMetadata
|
|
264
201
|
} } : {}
|
|
265
202
|
};
|
|
266
203
|
}
|
|
267
|
-
|
|
268
|
-
const headers = this.getHeaders(options);
|
|
269
|
-
const body = {
|
|
270
|
-
...this.invocationParams(options),
|
|
271
|
-
systemInstruction: require_messages.convertMessagesToGeminiSystemInstruction(messages),
|
|
272
|
-
contents: require_messages.convertMessagesToGeminiContents(messages)
|
|
273
|
-
};
|
|
274
|
-
const moduleName = this.constructor.name;
|
|
275
|
-
await runManager?.handleCustomEvent(`google-request-${moduleName}`, {
|
|
276
|
-
url,
|
|
277
|
-
headers,
|
|
278
|
-
body
|
|
279
|
-
});
|
|
280
|
-
let response;
|
|
281
|
-
try {
|
|
282
|
-
response = await this.caller.callWithOptions({
|
|
283
|
-
signal: options.signal,
|
|
284
|
-
maxRetries: options.maxRetries
|
|
285
|
-
}, async () => {
|
|
286
|
-
const nextResponse = await this.apiClient.fetch(new Request(url, {
|
|
287
|
-
method: "POST",
|
|
288
|
-
headers,
|
|
289
|
-
body: JSON.stringify(body),
|
|
290
|
-
signal: options.signal
|
|
291
|
-
}));
|
|
292
|
-
if (!nextResponse.ok) throw await require_errors.RequestError.fromResponse(nextResponse);
|
|
293
|
-
return nextResponse;
|
|
294
|
-
});
|
|
295
|
-
} catch (error) {
|
|
296
|
-
await runManager?.handleCustomEvent(`google-response-${moduleName}`, { error });
|
|
297
|
-
throw error;
|
|
298
|
-
}
|
|
299
|
-
const data = await response.json();
|
|
300
|
-
await runManager?.handleCustomEvent(`google-response-${moduleName}`, {
|
|
301
|
-
data,
|
|
302
|
-
url: response.url,
|
|
303
|
-
headers: Array.from(response.headers.entries()),
|
|
304
|
-
status: response.status,
|
|
305
|
-
statusText: response.statusText
|
|
306
|
-
});
|
|
307
|
-
if (data.promptFeedback?.blockReason) throw require_errors.PromptBlockedError.fromPromptFeedback(data.promptFeedback);
|
|
308
|
-
if (!data.candidates || data.candidates.length === 0) throw new require_errors.NoCandidatesError();
|
|
309
|
-
const candidate = data.candidates[0];
|
|
310
|
-
const message = require_messages.convertGeminiCandidateToAIMessage(candidate);
|
|
311
|
-
const text = require_messages.convertAIMessageToText(message);
|
|
312
|
-
const usageMetadata = require_messages.convertGeminiGenerateContentResponseToUsageMetadata(data);
|
|
313
|
-
message.usage_metadata = usageMetadata;
|
|
314
|
-
const serviceTier = require_misc.iife(() => {
|
|
315
|
-
const trafficType = data.usageMetadata?.trafficType;
|
|
316
|
-
const serviceTierHeader = response.headers.get("x-gemini-service-tier");
|
|
317
|
-
if (trafficType?.startsWith("ON_DEMAND_")) return trafficType?.substring(10).toLowerCase();
|
|
318
|
-
return serviceTierHeader || "standard";
|
|
319
|
-
});
|
|
320
|
-
return {
|
|
321
|
-
generations: [{
|
|
322
|
-
text,
|
|
323
|
-
message,
|
|
324
|
-
generationInfo: {
|
|
325
|
-
finishReason: candidate.finishReason,
|
|
326
|
-
finishMessage: candidate.finishMessage,
|
|
327
|
-
safetyRatings: candidate.safetyRatings,
|
|
328
|
-
citationMetadata: candidate.citationMetadata,
|
|
329
|
-
tokenCount: candidate.tokenCount
|
|
330
|
-
}
|
|
331
|
-
}],
|
|
332
|
-
llmOutput: {
|
|
333
|
-
tokenUsage: usageMetadataToTokenUsage(usageMetadata),
|
|
334
|
-
model: data.modelVersion,
|
|
335
|
-
responseId: data.responseId,
|
|
336
|
-
usageMetadata,
|
|
337
|
-
serviceTier
|
|
338
|
-
}
|
|
339
|
-
};
|
|
204
|
+
return this.apiHandler.generate(messages, options, runManager);
|
|
340
205
|
}
|
|
341
|
-
async *_streamChatModelEvents(messages, options,
|
|
342
|
-
|
|
343
|
-
...this.invocationParams(options),
|
|
344
|
-
systemInstruction: require_messages.convertMessagesToGeminiSystemInstruction(messages),
|
|
345
|
-
contents: require_messages.convertMessagesToGeminiContents(messages)
|
|
346
|
-
};
|
|
347
|
-
const url = await this.buildUrl("streamGenerateContent?alt=sse");
|
|
348
|
-
const headers = this.getHeaders(options);
|
|
349
|
-
const response = await this.apiClient.fetch(new Request(url, {
|
|
350
|
-
method: "POST",
|
|
351
|
-
headers,
|
|
352
|
-
body: JSON.stringify(body),
|
|
353
|
-
signal: options.signal
|
|
354
|
-
}));
|
|
355
|
-
if (!response.ok) throw await require_errors.RequestError.fromResponse(response);
|
|
356
|
-
if (!response.body) return;
|
|
357
|
-
const eventStream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new eventsource_parser_stream.EventSourceParserStream()).pipeThrough(new require_stream.SafeJsonEventParserStream());
|
|
358
|
-
const shouldStreamUsage = this.streamUsage !== false && options.streamUsage !== false;
|
|
359
|
-
async function* geminiChunks(stream, signal) {
|
|
360
|
-
const reader = stream.getReader();
|
|
361
|
-
try {
|
|
362
|
-
while (true) {
|
|
363
|
-
if (signal?.aborted) return;
|
|
364
|
-
const { done, value } = await reader.read();
|
|
365
|
-
if (done) break;
|
|
366
|
-
if (value !== null) yield value;
|
|
367
|
-
}
|
|
368
|
-
} finally {
|
|
369
|
-
reader.releaseLock();
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
yield* require_stream_events.convertGoogleGeminiStream(geminiChunks(eventStream, options.signal), { streamUsage: shouldStreamUsage });
|
|
206
|
+
async *_streamChatModelEvents(messages, options, runManager) {
|
|
207
|
+
yield* this.apiHandler.streamChatModelEvents(messages, options, runManager);
|
|
373
208
|
}
|
|
374
209
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
375
|
-
|
|
376
|
-
const body = {
|
|
377
|
-
...this.invocationParams(options),
|
|
378
|
-
systemInstruction: require_messages.convertMessagesToGeminiSystemInstruction(messages),
|
|
379
|
-
contents: require_messages.convertMessagesToGeminiContents(messages)
|
|
380
|
-
};
|
|
381
|
-
const url = await this.buildUrl("streamGenerateContent?alt=sse");
|
|
382
|
-
const headers = this.getHeaders(options);
|
|
383
|
-
const moduleName = this.constructor.name;
|
|
384
|
-
await runManager?.handleCustomEvent(`google-request-${moduleName}`, {
|
|
385
|
-
url,
|
|
386
|
-
headers,
|
|
387
|
-
body
|
|
388
|
-
});
|
|
389
|
-
let response;
|
|
390
|
-
try {
|
|
391
|
-
response = await this.caller.callWithOptions({
|
|
392
|
-
signal: options.signal,
|
|
393
|
-
maxRetries: options.maxRetries
|
|
394
|
-
}, async () => {
|
|
395
|
-
const nextResponse = await this.apiClient.fetch(new Request(url, {
|
|
396
|
-
method: "POST",
|
|
397
|
-
headers,
|
|
398
|
-
body: JSON.stringify(body),
|
|
399
|
-
signal: options.signal
|
|
400
|
-
}));
|
|
401
|
-
if (!nextResponse.ok) throw await require_errors.RequestError.fromResponse(nextResponse);
|
|
402
|
-
return nextResponse;
|
|
403
|
-
});
|
|
404
|
-
} catch (error) {
|
|
405
|
-
await runManager?.handleCustomEvent(`google-response-${moduleName}`, { error });
|
|
406
|
-
throw error;
|
|
407
|
-
}
|
|
408
|
-
await runManager?.handleCustomEvent(`google-response-${moduleName}`, {
|
|
409
|
-
url: response.url,
|
|
410
|
-
headers: response.headers,
|
|
411
|
-
status: response.status,
|
|
412
|
-
statusText: response.statusText
|
|
413
|
-
});
|
|
414
|
-
if (response.body) {
|
|
415
|
-
let previousUsage;
|
|
416
|
-
const reader = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new eventsource_parser_stream.EventSourceParserStream()).pipeThrough(new require_stream.SafeJsonEventParserStream()).pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
417
|
-
runManager?.handleCustomEvent(`google-chunk-${moduleName}`, { chunk });
|
|
418
|
-
if (chunk === null) {
|
|
419
|
-
controller.enqueue(new _langchain_core_outputs.ChatGenerationChunk({
|
|
420
|
-
text: "",
|
|
421
|
-
message: new _langchain_core_messages.AIMessageChunk({ content: "" }),
|
|
422
|
-
generationInfo: { finishReason: "stop" }
|
|
423
|
-
}));
|
|
424
|
-
return;
|
|
425
|
-
}
|
|
426
|
-
const candidate = chunk.candidates?.[0];
|
|
427
|
-
if (!candidate) {
|
|
428
|
-
if (chunk.usageMetadata && streamUsage) {
|
|
429
|
-
const cumulative = require_messages.convertGeminiGenerateContentResponseToUsageMetadata(chunk);
|
|
430
|
-
controller.enqueue(new _langchain_core_outputs.ChatGenerationChunk({
|
|
431
|
-
text: "",
|
|
432
|
-
message: new _langchain_core_messages.AIMessageChunk({
|
|
433
|
-
content: "",
|
|
434
|
-
response_metadata: { model_provider: "google" },
|
|
435
|
-
usage_metadata: require_metadata.subtractUsageMetadata(cumulative, previousUsage)
|
|
436
|
-
})
|
|
437
|
-
}));
|
|
438
|
-
previousUsage = cumulative;
|
|
439
|
-
}
|
|
440
|
-
return;
|
|
441
|
-
}
|
|
442
|
-
const message = require_messages.convertGeminiCandidateToAIMessage(candidate);
|
|
443
|
-
const text = require_messages.convertAIMessageToText(message);
|
|
444
|
-
const parts = candidate.content?.parts ?? [];
|
|
445
|
-
const toolCalls = require_messages.convertGeminiPartsToToolCalls(parts);
|
|
446
|
-
if (parts.length > 0 || candidate.finishReason) {
|
|
447
|
-
const messageChunkParams = {
|
|
448
|
-
content: message.content,
|
|
449
|
-
tool_calls: toolCalls,
|
|
450
|
-
response_metadata: { model_provider: "google" },
|
|
451
|
-
additional_kwargs: {
|
|
452
|
-
...message.additional_kwargs.originalTextContentBlock ? { originalTextContentBlock: message.additional_kwargs.originalTextContentBlock } : {},
|
|
453
|
-
...candidate.finishReason ? {
|
|
454
|
-
finishReason: candidate.finishReason,
|
|
455
|
-
finishMessage: candidate.finishMessage
|
|
456
|
-
} : {}
|
|
457
|
-
}
|
|
458
|
-
};
|
|
459
|
-
if (chunk?.usageMetadata && streamUsage) {
|
|
460
|
-
const cumulative = require_messages.convertGeminiGenerateContentResponseToUsageMetadata(chunk);
|
|
461
|
-
messageChunkParams.usage_metadata = require_metadata.subtractUsageMetadata(cumulative, previousUsage);
|
|
462
|
-
previousUsage = cumulative;
|
|
463
|
-
}
|
|
464
|
-
const messageChunk = new _langchain_core_messages.AIMessageChunk(messageChunkParams);
|
|
465
|
-
controller.enqueue(new _langchain_core_outputs.ChatGenerationChunk({
|
|
466
|
-
text,
|
|
467
|
-
message: messageChunk,
|
|
468
|
-
generationInfo: {
|
|
469
|
-
...candidate.finishReason && {
|
|
470
|
-
finishReason: candidate.finishReason,
|
|
471
|
-
finishMessage: candidate.finishMessage
|
|
472
|
-
},
|
|
473
|
-
...candidate.safetyRatings && { safetyRatings: candidate.safetyRatings },
|
|
474
|
-
...candidate.citationMetadata && { citationMetadata: candidate.citationMetadata },
|
|
475
|
-
...candidate.groundingMetadata && {
|
|
476
|
-
groundingMetadata: candidate.groundingMetadata,
|
|
477
|
-
groundingSupport: candidate.groundingMetadata.groundingSupports?.filter((s) => (s?.segment?.partIndex ?? 0) === 0)
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}));
|
|
481
|
-
}
|
|
482
|
-
} })).getReader();
|
|
483
|
-
try {
|
|
484
|
-
while (true) {
|
|
485
|
-
const { done, value } = await reader.read();
|
|
486
|
-
if (done) break;
|
|
487
|
-
yield value;
|
|
488
|
-
await runManager?.handleLLMNewToken(value.text ?? "", void 0, void 0, void 0, void 0, { chunk: value });
|
|
489
|
-
}
|
|
490
|
-
} finally {
|
|
491
|
-
reader.releaseLock();
|
|
492
|
-
}
|
|
493
|
-
}
|
|
210
|
+
yield* this.apiHandler.streamResponseChunks(messages, options, runManager);
|
|
494
211
|
}
|
|
495
212
|
/**
|
|
496
213
|
* Bind tool-like objects to this chat model.
|
|
@@ -536,75 +253,15 @@ var BaseChatGoogle = class extends _langchain_core_language_models_chat_models.B
|
|
|
536
253
|
return await (0, _langchain_core_language_models_structured_output.createContentParser)(schema).parse(input.text);
|
|
537
254
|
});
|
|
538
255
|
} else if (method === "functionCalling") {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
if ((0, _langchain_core_utils_types.isInteropZodSchema)(schema) || (0, _langchain_core_utils_standard_schema.isSerializableSchema)(schema)) {
|
|
542
|
-
const jsonSchema = require_tools.schemaToGeminiParameters(schema);
|
|
543
|
-
const description = typeof jsonSchema.description === "string" ? jsonSchema.description : "A function available to call.";
|
|
544
|
-
geminiFunctionDeclaration = {
|
|
545
|
-
name: functionName,
|
|
546
|
-
description,
|
|
547
|
-
parameters: jsonSchema
|
|
548
|
-
};
|
|
549
|
-
} else if (typeof schema.name === "string" && typeof schema.parameters === "object" && schema.parameters != null) {
|
|
550
|
-
geminiFunctionDeclaration = schema;
|
|
551
|
-
functionName = schema.name;
|
|
552
|
-
} else {
|
|
553
|
-
const parameters = require_tools.schemaToGeminiParameters(schema);
|
|
554
|
-
geminiFunctionDeclaration = {
|
|
555
|
-
name: functionName,
|
|
556
|
-
description: schema.description ?? "",
|
|
557
|
-
parameters
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
const tools = [{ functionDeclarations: [geminiFunctionDeclaration] }];
|
|
561
|
-
llm = this.bindTools(tools).withConfig({ tool_choice: functionName });
|
|
256
|
+
const { tools, toolChoice, functionName } = this.apiHandler.formatStructuredOutputTool(schema, { name });
|
|
257
|
+
llm = this.bindTools(tools).withConfig({ tool_choice: toolChoice });
|
|
562
258
|
outputParser = (0, _langchain_core_language_models_structured_output.createFunctionCallingParser)(schema, functionName);
|
|
563
259
|
} else throw new require_errors.ConfigurationError(`Unrecognized structured output method '${method}'. Expected 'functionCalling' or 'jsonSchema'`);
|
|
564
260
|
return (0, _langchain_core_language_models_structured_output.assembleStructuredOutputPipeline)(llm, outputParser, includeRaw, includeRaw ? "ChatGoogleStructuredOutputRunnable" : "ChatGoogleStructuredOutput");
|
|
565
261
|
}
|
|
566
262
|
};
|
|
567
|
-
function combineGoogleChatModelFields(a, b, ...rest) {
|
|
568
|
-
const combined = {
|
|
569
|
-
temperature: b.temperature ?? a.temperature,
|
|
570
|
-
topP: b.topP ?? a.topP,
|
|
571
|
-
topK: b.topK ?? a.topK,
|
|
572
|
-
maxOutputTokens: b.maxOutputTokens ?? a.maxOutputTokens,
|
|
573
|
-
presencePenalty: b.presencePenalty ?? a.presencePenalty,
|
|
574
|
-
frequencyPenalty: b.frequencyPenalty ?? a.frequencyPenalty,
|
|
575
|
-
stopSequences: b.stopSequences ?? a.stopSequences,
|
|
576
|
-
seed: b.seed ?? a.seed,
|
|
577
|
-
responseLogprobs: b.responseLogprobs ?? a.responseLogprobs,
|
|
578
|
-
logprobs: b.logprobs ?? a.logprobs,
|
|
579
|
-
safetySettings: b.safetySettings ?? a.safetySettings,
|
|
580
|
-
responseSchema: b.responseSchema ?? a.responseSchema,
|
|
581
|
-
tools: b.tools ?? a.tools,
|
|
582
|
-
responseModalities: b.responseModalities ?? a.responseModalities,
|
|
583
|
-
enableEnhancedCivicAnswers: b.enableEnhancedCivicAnswers ?? a.enableEnhancedCivicAnswers,
|
|
584
|
-
speechConfig: b.speechConfig ?? a.speechConfig,
|
|
585
|
-
imageConfig: b.imageConfig ?? a.imageConfig,
|
|
586
|
-
mediaResolution: b.mediaResolution ?? a.mediaResolution,
|
|
587
|
-
detail: b.detail ?? a.detail,
|
|
588
|
-
maxReasoningTokens: b.maxReasoningTokens ?? a.maxReasoningTokens,
|
|
589
|
-
thinkingBudget: b.thinkingBudget ?? a.thinkingBudget,
|
|
590
|
-
reasoningEffort: b.reasoningEffort ?? a.reasoningEffort,
|
|
591
|
-
thinkingLevel: b.thinkingLevel ?? a.thinkingLevel,
|
|
592
|
-
serviceTier: b.serviceTier ?? a.serviceTier
|
|
593
|
-
};
|
|
594
|
-
if (rest.length > 0) return combineGoogleChatModelFields(combined, rest[0], ...rest.slice(1));
|
|
595
|
-
return combined;
|
|
596
|
-
}
|
|
597
|
-
function getGoogleChatModelParams(modelOrParams, paramsArg) {
|
|
598
|
-
return {
|
|
599
|
-
model: typeof modelOrParams === "string" ? modelOrParams : modelOrParams.model,
|
|
600
|
-
...typeof modelOrParams === "string" ? paramsArg : modelOrParams
|
|
601
|
-
};
|
|
602
|
-
}
|
|
603
|
-
//#endregion
|
|
604
263
|
exports.BaseChatGoogle = BaseChatGoogle;
|
|
605
264
|
exports.applyGeminiGatewayParams = applyGeminiGatewayParams;
|
|
606
|
-
exports.combineGoogleChatModelFields = combineGoogleChatModelFields;
|
|
607
|
-
exports.getGoogleChatModelParams = getGoogleChatModelParams;
|
|
608
265
|
exports.getPlatformType = getPlatformType;
|
|
609
266
|
|
|
610
267
|
//# sourceMappingURL=base.cjs.map
|