@langchain/google-common 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/dist/chat_models.cjs +3 -1
- package/dist/chat_models.js +4 -2
- package/dist/types-anthropic.d.ts +1 -0
- package/dist/utils/anthropic.cjs +14 -4
- package/dist/utils/anthropic.js +14 -4
- package/dist/utils/zod_to_gemini_parameters.cjs +38 -26
- package/dist/utils/zod_to_gemini_parameters.d.ts +1 -0
- package/dist/utils/zod_to_gemini_parameters.js +36 -25
- package/package.json +1 -1
package/dist/chat_models.cjs
CHANGED
|
@@ -409,10 +409,12 @@ class ChatGoogleBase extends chat_models_1.BaseChatModel {
|
|
|
409
409
|
functionName = schema.name;
|
|
410
410
|
}
|
|
411
411
|
else {
|
|
412
|
+
// We are providing the schema for *just* the parameters, probably
|
|
413
|
+
const parameters = (0, zod_to_gemini_parameters_js_1.removeAdditionalProperties)(schema);
|
|
412
414
|
geminiFunctionDefinition = {
|
|
413
415
|
name: functionName,
|
|
414
416
|
description: schema.description ?? "",
|
|
415
|
-
parameters
|
|
417
|
+
parameters,
|
|
416
418
|
};
|
|
417
419
|
}
|
|
418
420
|
tools = [
|
package/dist/chat_models.js
CHANGED
|
@@ -11,7 +11,7 @@ import { AbstractGoogleLLMConnection } from "./connection.js";
|
|
|
11
11
|
import { DefaultGeminiSafetyHandler, getGeminiAPI } from "./utils/gemini.js";
|
|
12
12
|
import { ApiKeyGoogleAuth } from "./auth.js";
|
|
13
13
|
import { ensureParams } from "./utils/failed_handler.js";
|
|
14
|
-
import { schemaToGeminiParameters } from "./utils/zod_to_gemini_parameters.js";
|
|
14
|
+
import { removeAdditionalProperties, schemaToGeminiParameters, } from "./utils/zod_to_gemini_parameters.js";
|
|
15
15
|
export class ChatConnection extends AbstractGoogleLLMConnection {
|
|
16
16
|
constructor(fields, caller, client, streaming) {
|
|
17
17
|
super(fields, caller, client, streaming);
|
|
@@ -405,10 +405,12 @@ export class ChatGoogleBase extends BaseChatModel {
|
|
|
405
405
|
functionName = schema.name;
|
|
406
406
|
}
|
|
407
407
|
else {
|
|
408
|
+
// We are providing the schema for *just* the parameters, probably
|
|
409
|
+
const parameters = removeAdditionalProperties(schema);
|
|
408
410
|
geminiFunctionDefinition = {
|
|
409
411
|
name: functionName,
|
|
410
412
|
description: schema.description ?? "",
|
|
411
|
-
parameters
|
|
413
|
+
parameters,
|
|
412
414
|
};
|
|
413
415
|
}
|
|
414
416
|
tools = [
|
|
@@ -154,6 +154,7 @@ export interface AnthropicUsage {
|
|
|
154
154
|
output_tokens: number;
|
|
155
155
|
cache_creation_input_tokens: number | null;
|
|
156
156
|
cache_creation_output_tokens: number | null;
|
|
157
|
+
cache_read_input_tokens: number | null;
|
|
157
158
|
}
|
|
158
159
|
export type AnthropicResponseData = AnthropicResponseMessage | AnthropicStreamBaseEvent;
|
|
159
160
|
export interface AnthropicResponseMessage {
|
package/dist/utils/anthropic.cjs
CHANGED
|
@@ -116,13 +116,23 @@ function getAnthropicAPI(config) {
|
|
|
116
116
|
};
|
|
117
117
|
return newAIMessageChunk(ret);
|
|
118
118
|
}
|
|
119
|
-
function
|
|
119
|
+
function messageToUsageMetadata(message) {
|
|
120
120
|
const usage = message?.usage;
|
|
121
|
+
const inputTokens = usage?.input_tokens ?? 0;
|
|
122
|
+
const outputTokens = usage?.output_tokens ?? 0;
|
|
121
123
|
const usageMetadata = {
|
|
122
|
-
input_tokens:
|
|
123
|
-
output_tokens:
|
|
124
|
-
total_tokens:
|
|
124
|
+
input_tokens: inputTokens,
|
|
125
|
+
output_tokens: outputTokens,
|
|
126
|
+
total_tokens: inputTokens + outputTokens,
|
|
127
|
+
input_token_details: {
|
|
128
|
+
cache_read: usage?.cache_read_input_tokens ?? 0,
|
|
129
|
+
cache_creation: usage?.cache_creation_input_tokens ?? 0,
|
|
130
|
+
},
|
|
125
131
|
};
|
|
132
|
+
return usageMetadata;
|
|
133
|
+
}
|
|
134
|
+
function messageToGenerationInfo(message) {
|
|
135
|
+
const usageMetadata = messageToUsageMetadata(message);
|
|
126
136
|
return {
|
|
127
137
|
usage_metadata: usageMetadata,
|
|
128
138
|
finish_reason: message.stop_reason,
|
package/dist/utils/anthropic.js
CHANGED
|
@@ -113,13 +113,23 @@ export function getAnthropicAPI(config) {
|
|
|
113
113
|
};
|
|
114
114
|
return newAIMessageChunk(ret);
|
|
115
115
|
}
|
|
116
|
-
function
|
|
116
|
+
function messageToUsageMetadata(message) {
|
|
117
117
|
const usage = message?.usage;
|
|
118
|
+
const inputTokens = usage?.input_tokens ?? 0;
|
|
119
|
+
const outputTokens = usage?.output_tokens ?? 0;
|
|
118
120
|
const usageMetadata = {
|
|
119
|
-
input_tokens:
|
|
120
|
-
output_tokens:
|
|
121
|
-
total_tokens:
|
|
121
|
+
input_tokens: inputTokens,
|
|
122
|
+
output_tokens: outputTokens,
|
|
123
|
+
total_tokens: inputTokens + outputTokens,
|
|
124
|
+
input_token_details: {
|
|
125
|
+
cache_read: usage?.cache_read_input_tokens ?? 0,
|
|
126
|
+
cache_creation: usage?.cache_creation_input_tokens ?? 0,
|
|
127
|
+
},
|
|
122
128
|
};
|
|
129
|
+
return usageMetadata;
|
|
130
|
+
}
|
|
131
|
+
function messageToGenerationInfo(message) {
|
|
132
|
+
const usageMetadata = messageToUsageMetadata(message);
|
|
123
133
|
return {
|
|
124
134
|
usage_metadata: usageMetadata,
|
|
125
135
|
finish_reason: message.stop_reason,
|
|
@@ -1,9 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.jsonSchemaToGeminiParameters = exports.schemaToGeminiParameters = exports.removeAdditionalProperties = void 0;
|
|
4
|
+
exports.jsonSchemaToGeminiParameters = exports.schemaToGeminiParameters = exports.removeAdditionalProperties = exports.adjustObjectType = void 0;
|
|
5
5
|
const types_1 = require("@langchain/core/utils/types");
|
|
6
6
|
const json_schema_1 = require("@langchain/core/utils/json_schema");
|
|
7
|
+
/* eslint-disable no-param-reassign */
|
|
8
|
+
function adjustObjectType(
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
obj
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
+
) {
|
|
13
|
+
if (!Array.isArray(obj.type)) {
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
const len = obj.type.length;
|
|
17
|
+
const nullIndex = obj.type.indexOf("null");
|
|
18
|
+
if (len === 2 && nullIndex >= 0) {
|
|
19
|
+
// There are only two values set for the type, and one of them is "null".
|
|
20
|
+
// Set the type to the other one and set nullable to true.
|
|
21
|
+
const typeIndex = nullIndex === 0 ? 1 : 0;
|
|
22
|
+
obj.type = obj.type[typeIndex];
|
|
23
|
+
obj.nullable = true;
|
|
24
|
+
}
|
|
25
|
+
else if (len === 1 && nullIndex === 0) {
|
|
26
|
+
// This is nullable only without a type, which doesn't
|
|
27
|
+
// make sense for Gemini
|
|
28
|
+
throw new Error("zod_to_gemini_parameters: Gemini cannot handle null type");
|
|
29
|
+
}
|
|
30
|
+
else if (len === 1) {
|
|
31
|
+
// Although an array, it has only one value.
|
|
32
|
+
// So set it to the string to match what Gemini expects.
|
|
33
|
+
obj.type = obj?.type[0];
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// Anything else could be a union type, so reject it.
|
|
37
|
+
throw new Error("zod_to_gemini_parameters: Gemini cannot handle union types");
|
|
38
|
+
}
|
|
39
|
+
return obj;
|
|
40
|
+
}
|
|
41
|
+
exports.adjustObjectType = adjustObjectType;
|
|
42
|
+
/* eslint-enable no-param-reassign */
|
|
7
43
|
function removeAdditionalProperties(
|
|
8
44
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
45
|
obj) {
|
|
@@ -12,31 +48,7 @@ obj) {
|
|
|
12
48
|
if ("additionalProperties" in newObj) {
|
|
13
49
|
delete newObj.additionalProperties;
|
|
14
50
|
}
|
|
15
|
-
|
|
16
|
-
const len = obj.type.length;
|
|
17
|
-
const nullIndex = obj.type.indexOf("null");
|
|
18
|
-
if (len === 2 && nullIndex >= 0) {
|
|
19
|
-
// There are only two values set for the type, and one of them is "null".
|
|
20
|
-
// Set the type to the other one and set nullable to true.
|
|
21
|
-
const typeIndex = nullIndex === 0 ? 1 : 0;
|
|
22
|
-
newObj.type = obj.type[typeIndex];
|
|
23
|
-
newObj.nullable = true;
|
|
24
|
-
}
|
|
25
|
-
else if (len === 1 && nullIndex === 0) {
|
|
26
|
-
// This is nullable only without a type, which doesn't
|
|
27
|
-
// make sense for Gemini
|
|
28
|
-
throw new Error("zod_to_gemini_parameters: Gemini cannot handle null type");
|
|
29
|
-
}
|
|
30
|
-
else if (len === 1) {
|
|
31
|
-
// Although an array, it has only one value.
|
|
32
|
-
// So set it to the string to match what Gemini expects.
|
|
33
|
-
newObj.type = obj?.type[0];
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
// Anything else could be a union type, so reject it.
|
|
37
|
-
throw new Error("zod_to_gemini_parameters: Gemini cannot handle union types");
|
|
38
|
-
}
|
|
39
|
-
}
|
|
51
|
+
adjustObjectType(newObj);
|
|
40
52
|
for (const key in newObj) {
|
|
41
53
|
if (key in newObj) {
|
|
42
54
|
if (Array.isArray(newObj[key])) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { InteropZodType } from "@langchain/core/utils/types";
|
|
2
2
|
import { type JsonSchema7Type } from "@langchain/core/utils/json_schema";
|
|
3
3
|
import { GeminiFunctionSchema, GeminiJsonSchema } from "../types.js";
|
|
4
|
+
export declare function adjustObjectType(obj: Record<string, any>): Record<string, any>;
|
|
4
5
|
export declare function removeAdditionalProperties(obj: Record<string, any>): GeminiJsonSchema;
|
|
5
6
|
export declare function schemaToGeminiParameters<RunOutput extends Record<string, any> = Record<string, any>>(schema: InteropZodType<RunOutput> | JsonSchema7Type): GeminiFunctionSchema;
|
|
6
7
|
export declare function jsonSchemaToGeminiParameters(schema: Record<string, any>): GeminiFunctionSchema;
|
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
import { isInteropZodSchema, } from "@langchain/core/utils/types";
|
|
3
3
|
import { toJsonSchema, } from "@langchain/core/utils/json_schema";
|
|
4
|
+
/* eslint-disable no-param-reassign */
|
|
5
|
+
export function adjustObjectType(
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
obj
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
) {
|
|
10
|
+
if (!Array.isArray(obj.type)) {
|
|
11
|
+
return obj;
|
|
12
|
+
}
|
|
13
|
+
const len = obj.type.length;
|
|
14
|
+
const nullIndex = obj.type.indexOf("null");
|
|
15
|
+
if (len === 2 && nullIndex >= 0) {
|
|
16
|
+
// There are only two values set for the type, and one of them is "null".
|
|
17
|
+
// Set the type to the other one and set nullable to true.
|
|
18
|
+
const typeIndex = nullIndex === 0 ? 1 : 0;
|
|
19
|
+
obj.type = obj.type[typeIndex];
|
|
20
|
+
obj.nullable = true;
|
|
21
|
+
}
|
|
22
|
+
else if (len === 1 && nullIndex === 0) {
|
|
23
|
+
// This is nullable only without a type, which doesn't
|
|
24
|
+
// make sense for Gemini
|
|
25
|
+
throw new Error("zod_to_gemini_parameters: Gemini cannot handle null type");
|
|
26
|
+
}
|
|
27
|
+
else if (len === 1) {
|
|
28
|
+
// Although an array, it has only one value.
|
|
29
|
+
// So set it to the string to match what Gemini expects.
|
|
30
|
+
obj.type = obj?.type[0];
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// Anything else could be a union type, so reject it.
|
|
34
|
+
throw new Error("zod_to_gemini_parameters: Gemini cannot handle union types");
|
|
35
|
+
}
|
|
36
|
+
return obj;
|
|
37
|
+
}
|
|
38
|
+
/* eslint-enable no-param-reassign */
|
|
4
39
|
export function removeAdditionalProperties(
|
|
5
40
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
41
|
obj) {
|
|
@@ -9,31 +44,7 @@ obj) {
|
|
|
9
44
|
if ("additionalProperties" in newObj) {
|
|
10
45
|
delete newObj.additionalProperties;
|
|
11
46
|
}
|
|
12
|
-
|
|
13
|
-
const len = obj.type.length;
|
|
14
|
-
const nullIndex = obj.type.indexOf("null");
|
|
15
|
-
if (len === 2 && nullIndex >= 0) {
|
|
16
|
-
// There are only two values set for the type, and one of them is "null".
|
|
17
|
-
// Set the type to the other one and set nullable to true.
|
|
18
|
-
const typeIndex = nullIndex === 0 ? 1 : 0;
|
|
19
|
-
newObj.type = obj.type[typeIndex];
|
|
20
|
-
newObj.nullable = true;
|
|
21
|
-
}
|
|
22
|
-
else if (len === 1 && nullIndex === 0) {
|
|
23
|
-
// This is nullable only without a type, which doesn't
|
|
24
|
-
// make sense for Gemini
|
|
25
|
-
throw new Error("zod_to_gemini_parameters: Gemini cannot handle null type");
|
|
26
|
-
}
|
|
27
|
-
else if (len === 1) {
|
|
28
|
-
// Although an array, it has only one value.
|
|
29
|
-
// So set it to the string to match what Gemini expects.
|
|
30
|
-
newObj.type = obj?.type[0];
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
// Anything else could be a union type, so reject it.
|
|
34
|
-
throw new Error("zod_to_gemini_parameters: Gemini cannot handle union types");
|
|
35
|
-
}
|
|
36
|
-
}
|
|
47
|
+
adjustObjectType(newObj);
|
|
37
48
|
for (const key in newObj) {
|
|
38
49
|
if (key in newObj) {
|
|
39
50
|
if (Array.isArray(newObj[key])) {
|