@langchain/google 0.2.11 → 0.2.12
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 +13 -0
- package/dist/chat_models/{api-types.d.ts → api_types/generate_content.d.cts} +199 -199
- package/dist/chat_models/{api-types.d.cts → api_types/generate_content.d.ts} +199 -199
- package/dist/chat_models/base.cjs +53 -397
- package/dist/chat_models/base.cjs.map +1 -1
- package/dist/chat_models/base.d.cts +13 -198
- package/dist/chat_models/base.d.ts +15 -200
- package/dist/chat_models/base.js +55 -397
- package/dist/chat_models/base.js.map +1 -1
- package/dist/chat_models/handlers/base.cjs +63 -0
- package/dist/chat_models/handlers/base.cjs.map +1 -0
- package/dist/chat_models/handlers/base.d.cts +48 -0
- package/dist/chat_models/handlers/base.d.ts +48 -0
- package/dist/chat_models/handlers/base.js +63 -0
- package/dist/chat_models/handlers/base.js.map +1 -0
- package/dist/chat_models/handlers/generate_content.cjs +345 -0
- package/dist/chat_models/handlers/generate_content.cjs.map +1 -0
- package/dist/chat_models/handlers/generate_content.d.cts +34 -0
- package/dist/chat_models/handlers/generate_content.d.ts +34 -0
- package/dist/chat_models/handlers/generate_content.js +345 -0
- package/dist/chat_models/handlers/generate_content.js.map +1 -0
- package/dist/chat_models/handlers/index.cjs +4 -0
- package/dist/chat_models/handlers/index.d.cts +3 -0
- package/dist/chat_models/handlers/index.d.ts +3 -0
- package/dist/chat_models/handlers/index.js +3 -0
- package/dist/chat_models/index.cjs +11 -4
- package/dist/chat_models/index.cjs.map +1 -1
- package/dist/chat_models/index.d.cts +6 -1
- package/dist/chat_models/index.d.ts +6 -1
- package/dist/chat_models/index.js +6 -2
- package/dist/chat_models/index.js.map +1 -1
- package/dist/chat_models/node.cjs +2 -2
- package/dist/chat_models/node.cjs.map +1 -1
- package/dist/chat_models/node.js +2 -2
- package/dist/chat_models/node.js.map +1 -1
- package/dist/chat_models/types.cjs.map +1 -1
- package/dist/chat_models/types.d.cts +281 -94
- package/dist/chat_models/types.d.ts +281 -94
- package/dist/chat_models/types.js.map +1 -1
- package/dist/converters/{messages.cjs → generate_content/messages.cjs} +4 -4
- package/dist/converters/generate_content/messages.cjs.map +1 -0
- package/dist/converters/{messages.js → generate_content/messages.js} +4 -4
- package/dist/converters/generate_content/messages.js.map +1 -0
- package/dist/converters/generate_content/params.cjs +136 -0
- package/dist/converters/generate_content/params.cjs.map +1 -0
- package/dist/converters/generate_content/params.js +132 -0
- package/dist/converters/generate_content/params.js.map +1 -0
- package/dist/{utils → converters/generate_content}/stream_events.cjs +2 -2
- package/dist/converters/generate_content/stream_events.cjs.map +1 -0
- package/dist/{utils → converters/generate_content}/stream_events.js +2 -2
- package/dist/converters/generate_content/stream_events.js.map +1 -0
- package/dist/converters/{tools.cjs → generate_content/tools.cjs} +4 -4
- package/dist/converters/generate_content/tools.cjs.map +1 -0
- package/dist/converters/{tools.js → generate_content/tools.js} +4 -4
- package/dist/converters/generate_content/tools.js.map +1 -0
- package/dist/converters/{params.cjs → shared/params.cjs} +8 -93
- package/dist/converters/shared/params.cjs.map +1 -0
- package/dist/converters/{params.js → shared/params.js} +8 -91
- package/dist/converters/shared/params.js.map +1 -0
- package/dist/index.cjs +6 -0
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +4 -1
- package/dist/utils/metadata.cjs +8 -0
- package/dist/utils/metadata.cjs.map +1 -1
- package/dist/utils/metadata.js +8 -1
- package/dist/utils/metadata.js.map +1 -1
- package/dist/utils/misc.cjs.map +1 -1
- package/dist/utils/misc.d.cts +2 -1
- package/dist/utils/misc.d.ts +2 -1
- package/dist/utils/misc.js.map +1 -1
- package/package.json +4 -4
- 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
package/dist/chat_models/base.js
CHANGED
|
@@ -1,23 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ConfigurationError, MalformedOutputError, NoCandidatesError, PromptBlockedError, RequestError } from "../utils/errors.js";
|
|
3
|
-
import { convertGoogleGeminiStream } from "../utils/stream_events.js";
|
|
1
|
+
import { ConfigurationError, MalformedOutputError } from "../utils/errors.js";
|
|
4
2
|
import PROFILES from "./profiles.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import { convertFieldsToServiceTier, convertFieldsToSpeechConfig, convertFieldsToThinkingConfig, convertParamsToPlatformType } from "../converters/params.js";
|
|
10
|
-
import { subtractUsageMetadata } from "../utils/metadata.js";
|
|
3
|
+
import { usageMetadataToTokenUsage } from "../utils/metadata.js";
|
|
4
|
+
import { convertParamsToPlatformType } from "../converters/shared/params.js";
|
|
5
|
+
import { GenerateContentApiHandler } from "./handlers/generate_content.js";
|
|
6
|
+
import "./handlers/index.js";
|
|
11
7
|
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
12
8
|
import { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
13
9
|
import { AIMessage, AIMessageChunk } from "@langchain/core/messages";
|
|
14
10
|
import { concat } from "@langchain/core/utils/stream";
|
|
15
|
-
import { ChatGenerationChunk } from "@langchain/core/outputs";
|
|
16
|
-
import { EventSourceParserStream } from "eventsource-parser/stream";
|
|
17
11
|
import { RunnableLambda } from "@langchain/core/runnables";
|
|
18
|
-
import { toJsonSchema } from "@langchain/core/utils/json_schema";
|
|
19
|
-
import { isInteropZodSchema } from "@langchain/core/utils/types";
|
|
20
|
-
import { isSerializableSchema } from "@langchain/core/utils/standard_schema";
|
|
21
12
|
import { assembleStructuredOutputPipeline, createContentParser, createFunctionCallingParser } from "@langchain/core/language_models/structured_output";
|
|
22
13
|
import { resolveLangSmithGatewayConfig } from "@langchain/core/utils/gateway";
|
|
23
14
|
//#region src/chat_models/base.ts
|
|
@@ -44,8 +35,9 @@ function hasVertexCredentials(params) {
|
|
|
44
35
|
* @returns a new params object with gateway routing applied when applicable.
|
|
45
36
|
*/
|
|
46
37
|
function applyGeminiGatewayParams(params) {
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
const p = params;
|
|
39
|
+
if (typeof p.endpoint !== "undefined") return { ...params };
|
|
40
|
+
const platform = convertParamsToPlatformType(p) ?? (hasVertexCredentials(p) ? "gcp" : "gai");
|
|
49
41
|
const gatewayConfig = resolveLangSmithGatewayConfig({ providerPath: platform === "gcp" ? "vertex" : "gemini" });
|
|
50
42
|
if (typeof gatewayConfig.baseURL === "undefined") return { ...params };
|
|
51
43
|
const gatewayUrl = new URL(gatewayConfig.baseURL);
|
|
@@ -53,7 +45,7 @@ function applyGeminiGatewayParams(params) {
|
|
|
53
45
|
const endpoint = gatewayUrl.protocol === "https:" ? `${gatewayUrl.host}${path}` : `${gatewayUrl.protocol}//${gatewayUrl.host}${path}`;
|
|
54
46
|
return {
|
|
55
47
|
...params,
|
|
56
|
-
apiKey:
|
|
48
|
+
apiKey: p.apiKey ?? gatewayConfig.apiKey,
|
|
57
49
|
endpoint,
|
|
58
50
|
platformType: platform
|
|
59
51
|
};
|
|
@@ -63,20 +55,6 @@ function getPlatformType(platform, hasApiKey) {
|
|
|
63
55
|
else if (hasApiKey) return "gai";
|
|
64
56
|
else return "gcp";
|
|
65
57
|
}
|
|
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
58
|
var BaseChatGoogle = class extends BaseChatModel {
|
|
81
59
|
params;
|
|
82
60
|
model;
|
|
@@ -92,7 +70,7 @@ var BaseChatGoogle = class extends BaseChatModel {
|
|
|
92
70
|
constructor(params) {
|
|
93
71
|
super(params);
|
|
94
72
|
this.params = params;
|
|
95
|
-
this._addVersion("@langchain/google", "0.2.
|
|
73
|
+
this._addVersion("@langchain/google", "0.2.12");
|
|
96
74
|
if (!params.apiClient) throw new 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
75
|
this.apiClient = params.apiClient;
|
|
98
76
|
this.model = params.model;
|
|
@@ -139,8 +117,37 @@ var BaseChatGoogle = class extends BaseChatModel {
|
|
|
139
117
|
get publisher() {
|
|
140
118
|
return "google";
|
|
141
119
|
}
|
|
142
|
-
|
|
143
|
-
|
|
120
|
+
_apiHandler;
|
|
121
|
+
_handlerParams;
|
|
122
|
+
syncHandlerParams() {
|
|
123
|
+
const nextParams = {
|
|
124
|
+
...this.params,
|
|
125
|
+
model: this.model,
|
|
126
|
+
platform: this.platform,
|
|
127
|
+
endpoint: this.endpoint,
|
|
128
|
+
location: this.location,
|
|
129
|
+
apiVersion: this.apiVersion,
|
|
130
|
+
isVertexExpress: this.isVertexExpress,
|
|
131
|
+
publisher: this.publisher,
|
|
132
|
+
streaming: this.streaming,
|
|
133
|
+
streamUsage: this.streamUsage,
|
|
134
|
+
customHeaders: this._customHeaders,
|
|
135
|
+
apiClient: this.apiClient,
|
|
136
|
+
caller: this.caller
|
|
137
|
+
};
|
|
138
|
+
if (this._handlerParams) return Object.assign(this._handlerParams, nextParams);
|
|
139
|
+
this._handlerParams = nextParams;
|
|
140
|
+
return this._handlerParams;
|
|
141
|
+
}
|
|
142
|
+
get apiHandler() {
|
|
143
|
+
const handlerParams = this.syncHandlerParams();
|
|
144
|
+
if (!this._apiHandler) {
|
|
145
|
+
const handlerConfig = this.params.apiHandler ?? "generateContent";
|
|
146
|
+
if (typeof handlerConfig === "function") this._apiHandler = handlerConfig(handlerParams);
|
|
147
|
+
else if (handlerConfig === "generateContent") this._apiHandler = new GenerateContentApiHandler(handlerParams);
|
|
148
|
+
else throw new ConfigurationError(`Unknown apiHandler: ${handlerConfig}`);
|
|
149
|
+
}
|
|
150
|
+
return this._apiHandler;
|
|
144
151
|
}
|
|
145
152
|
/**
|
|
146
153
|
* Returns the model profile for this instance's model, describing its
|
|
@@ -158,96 +165,24 @@ var BaseChatGoogle = class extends BaseChatModel {
|
|
|
158
165
|
get profile() {
|
|
159
166
|
return PROFILES[this.model] ?? {};
|
|
160
167
|
}
|
|
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
168
|
async buildUrl(urlMethod) {
|
|
183
|
-
|
|
184
|
-
case "gai": return this.buildUrlGemini(urlMethod);
|
|
185
|
-
default: return this.buildUrlVertex(urlMethod);
|
|
186
|
-
}
|
|
169
|
+
return this.apiHandler.buildUrl(urlMethod);
|
|
187
170
|
}
|
|
188
171
|
invocationParams(options) {
|
|
189
|
-
|
|
190
|
-
const mediaResolution = fields.mediaResolution ?? mapDetailToMediaResolution(fields.detail);
|
|
191
|
-
const tools = fields.tools ? convertToolsToGeminiTools(fields.tools) : void 0;
|
|
192
|
-
let toolConfig = convertToolChoiceToGeminiConfig(options.tool_choice, !!(tools && tools.length > 0));
|
|
193
|
-
if (tools && mixesBuiltinAndFunctionTools(tools)) toolConfig = {
|
|
194
|
-
...toolConfig,
|
|
195
|
-
includeServerSideToolInvocations: true
|
|
196
|
-
};
|
|
197
|
-
let responseJsonSchema;
|
|
198
|
-
let responseMimeType;
|
|
199
|
-
if (fields.responseSchema) {
|
|
200
|
-
if (isInteropZodSchema(fields.responseSchema) || isSerializableSchema(fields.responseSchema)) responseJsonSchema = 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: convertFieldsToThinkingConfig(this.model, fields),
|
|
225
|
-
speechConfig: convertFieldsToSpeechConfig(fields),
|
|
226
|
-
...fields.imageConfig ? { imageConfig: fields.imageConfig } : {},
|
|
227
|
-
...mediaResolution ? { mediaResolution } : {}
|
|
228
|
-
},
|
|
229
|
-
...convertFieldsToServiceTier(this.platform, fields)
|
|
230
|
-
};
|
|
172
|
+
return this.apiHandler.invocationParams(options);
|
|
231
173
|
}
|
|
232
174
|
getLsParams(options) {
|
|
233
|
-
const params = this.invocationParams(options);
|
|
234
175
|
return {
|
|
235
176
|
ls_provider: this.platform === "gcp" ? "google_vertexai" : "google_genai",
|
|
236
177
|
ls_model_name: this.model,
|
|
237
178
|
ls_model_type: "chat",
|
|
238
|
-
ls_temperature:
|
|
239
|
-
ls_max_tokens:
|
|
240
|
-
ls_stop: options.
|
|
179
|
+
ls_temperature: options?.temperature ?? this.params.temperature,
|
|
180
|
+
ls_max_tokens: options?.maxOutputTokens ?? this.params.maxOutputTokens,
|
|
181
|
+
ls_stop: options?.stopSequences ?? options?.stop ?? this.params.stopSequences
|
|
241
182
|
};
|
|
242
183
|
}
|
|
243
184
|
getHeaders(options) {
|
|
244
|
-
|
|
245
|
-
return {
|
|
246
|
-
"Content-Type": "application/json",
|
|
247
|
-
...this.platform === "gcp" && typeof fields.serviceTier !== "undefined" && settableServiceTier.includes(fields.serviceTier) ? { "X-Vertex-AI-LLM-Shared-Request-Type": fields.serviceTier } : {},
|
|
248
|
-
...this._customHeaders,
|
|
249
|
-
...options.customHeaders
|
|
250
|
-
};
|
|
185
|
+
return this.apiHandler.getHeaders(options);
|
|
251
186
|
}
|
|
252
187
|
async _generate(messages, options, runManager) {
|
|
253
188
|
if (this.streaming) {
|
|
@@ -255,7 +190,7 @@ var BaseChatGoogle = class extends BaseChatModel {
|
|
|
255
190
|
let finalChunk = null;
|
|
256
191
|
for await (const chunk of stream) finalChunk = !finalChunk ? chunk : concat(finalChunk, chunk);
|
|
257
192
|
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;
|
|
193
|
+
const usageMetadata = (finalChunk?.message)?.usage_metadata;
|
|
259
194
|
return {
|
|
260
195
|
generations: finalChunk ? [finalChunk] : [],
|
|
261
196
|
...usageMetadata ? { llmOutput: {
|
|
@@ -264,233 +199,13 @@ var BaseChatGoogle = class extends BaseChatModel {
|
|
|
264
199
|
} } : {}
|
|
265
200
|
};
|
|
266
201
|
}
|
|
267
|
-
|
|
268
|
-
const headers = this.getHeaders(options);
|
|
269
|
-
const body = {
|
|
270
|
-
...this.invocationParams(options),
|
|
271
|
-
systemInstruction: convertMessagesToGeminiSystemInstruction(messages),
|
|
272
|
-
contents: 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 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 PromptBlockedError.fromPromptFeedback(data.promptFeedback);
|
|
308
|
-
if (!data.candidates || data.candidates.length === 0) throw new NoCandidatesError();
|
|
309
|
-
const candidate = data.candidates[0];
|
|
310
|
-
const message = convertGeminiCandidateToAIMessage(candidate);
|
|
311
|
-
const text = convertAIMessageToText(message);
|
|
312
|
-
const usageMetadata = convertGeminiGenerateContentResponseToUsageMetadata(data);
|
|
313
|
-
message.usage_metadata = usageMetadata;
|
|
314
|
-
const serviceTier = 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
|
-
};
|
|
202
|
+
return this.apiHandler.generate(messages, options, runManager);
|
|
340
203
|
}
|
|
341
|
-
async *_streamChatModelEvents(messages, options,
|
|
342
|
-
|
|
343
|
-
...this.invocationParams(options),
|
|
344
|
-
systemInstruction: convertMessagesToGeminiSystemInstruction(messages),
|
|
345
|
-
contents: 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 RequestError.fromResponse(response);
|
|
356
|
-
if (!response.body) return;
|
|
357
|
-
const eventStream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(new 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* convertGoogleGeminiStream(geminiChunks(eventStream, options.signal), { streamUsage: shouldStreamUsage });
|
|
204
|
+
async *_streamChatModelEvents(messages, options, runManager) {
|
|
205
|
+
yield* this.apiHandler.streamChatModelEvents(messages, options, runManager);
|
|
373
206
|
}
|
|
374
207
|
async *_streamResponseChunks(messages, options, runManager) {
|
|
375
|
-
|
|
376
|
-
const body = {
|
|
377
|
-
...this.invocationParams(options),
|
|
378
|
-
systemInstruction: convertMessagesToGeminiSystemInstruction(messages),
|
|
379
|
-
contents: 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 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 EventSourceParserStream()).pipeThrough(new SafeJsonEventParserStream()).pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
417
|
-
runManager?.handleCustomEvent(`google-chunk-${moduleName}`, { chunk });
|
|
418
|
-
if (chunk === null) {
|
|
419
|
-
controller.enqueue(new ChatGenerationChunk({
|
|
420
|
-
text: "",
|
|
421
|
-
message: new 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 = convertGeminiGenerateContentResponseToUsageMetadata(chunk);
|
|
430
|
-
controller.enqueue(new ChatGenerationChunk({
|
|
431
|
-
text: "",
|
|
432
|
-
message: new AIMessageChunk({
|
|
433
|
-
content: "",
|
|
434
|
-
response_metadata: { model_provider: "google" },
|
|
435
|
-
usage_metadata: subtractUsageMetadata(cumulative, previousUsage)
|
|
436
|
-
})
|
|
437
|
-
}));
|
|
438
|
-
previousUsage = cumulative;
|
|
439
|
-
}
|
|
440
|
-
return;
|
|
441
|
-
}
|
|
442
|
-
const message = convertGeminiCandidateToAIMessage(candidate);
|
|
443
|
-
const text = convertAIMessageToText(message);
|
|
444
|
-
const parts = candidate.content?.parts ?? [];
|
|
445
|
-
const toolCalls = 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 = convertGeminiGenerateContentResponseToUsageMetadata(chunk);
|
|
461
|
-
messageChunkParams.usage_metadata = subtractUsageMetadata(cumulative, previousUsage);
|
|
462
|
-
previousUsage = cumulative;
|
|
463
|
-
}
|
|
464
|
-
const messageChunk = new AIMessageChunk(messageChunkParams);
|
|
465
|
-
controller.enqueue(new 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
|
-
}
|
|
208
|
+
yield* this.apiHandler.streamResponseChunks(messages, options, runManager);
|
|
494
209
|
}
|
|
495
210
|
/**
|
|
496
211
|
* Bind tool-like objects to this chat model.
|
|
@@ -536,71 +251,14 @@ var BaseChatGoogle = class extends BaseChatModel {
|
|
|
536
251
|
return await createContentParser(schema).parse(input.text);
|
|
537
252
|
});
|
|
538
253
|
} else if (method === "functionCalling") {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
if (isInteropZodSchema(schema) || isSerializableSchema(schema)) {
|
|
542
|
-
const jsonSchema = 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 = 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 });
|
|
254
|
+
const { tools, toolChoice, functionName } = this.apiHandler.formatStructuredOutputTool(schema, { name });
|
|
255
|
+
llm = this.bindTools(tools).withConfig({ tool_choice: toolChoice });
|
|
562
256
|
outputParser = createFunctionCallingParser(schema, functionName);
|
|
563
257
|
} else throw new ConfigurationError(`Unrecognized structured output method '${method}'. Expected 'functionCalling' or 'jsonSchema'`);
|
|
564
258
|
return assembleStructuredOutputPipeline(llm, outputParser, includeRaw, includeRaw ? "ChatGoogleStructuredOutputRunnable" : "ChatGoogleStructuredOutput");
|
|
565
259
|
}
|
|
566
260
|
};
|
|
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
261
|
//#endregion
|
|
604
|
-
export { BaseChatGoogle, applyGeminiGatewayParams,
|
|
262
|
+
export { BaseChatGoogle, applyGeminiGatewayParams, getPlatformType };
|
|
605
263
|
|
|
606
264
|
//# sourceMappingURL=base.js.map
|