@langchain/google-common 0.0.0 → 0.0.2
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/README.md +7 -2
- package/dist/chat_models.cjs +115 -7
- package/dist/chat_models.d.ts +16 -6
- package/dist/chat_models.js +116 -8
- package/dist/connection.cjs +48 -4
- package/dist/connection.d.ts +14 -7
- package/dist/connection.js +48 -4
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/llms.cjs +91 -24
- package/dist/llms.d.ts +21 -12
- package/dist/llms.js +93 -26
- package/dist/types.d.ts +66 -11
- package/dist/utils/common.cjs +19 -12
- package/dist/utils/common.d.ts +3 -3
- package/dist/utils/common.js +19 -12
- package/dist/utils/failed_handler.cjs +37 -0
- package/dist/utils/failed_handler.d.ts +3 -0
- package/dist/utils/failed_handler.js +32 -0
- package/dist/utils/gemini.cjs +321 -25
- package/dist/utils/gemini.d.ts +53 -3
- package/dist/utils/gemini.js +308 -23
- package/dist/utils/index.cjs +23 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/safety.cjs +23 -0
- package/dist/utils/safety.d.ts +6 -0
- package/dist/utils/safety.js +19 -0
- package/dist/utils/zod_to_gemini_parameters.cjs +15 -0
- package/dist/utils/zod_to_gemini_parameters.d.ts +3 -0
- package/dist/utils/zod_to_gemini_parameters.js +11 -0
- package/index.d.cts +1 -0
- package/package.json +42 -7
- package/types.cjs +1 -0
- package/types.d.cts +1 -0
- package/types.d.ts +1 -0
- package/types.js +1 -0
- package/utils.cjs +1 -0
- package/utils.d.cts +1 -0
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { BaseLLMParams } from "@langchain/core/language_models/llms";
|
|
2
|
+
import { BaseLanguageModelCallOptions } from "@langchain/core/language_models/base";
|
|
3
|
+
import { StructuredToolInterface } from "@langchain/core/tools";
|
|
2
4
|
import type { JsonStream } from "./utils/stream.js";
|
|
3
5
|
/**
|
|
4
6
|
* Parameters needed to setup the client connection.
|
|
@@ -36,8 +38,10 @@ export interface GoogleAISafetySetting {
|
|
|
36
38
|
threshold: string;
|
|
37
39
|
}
|
|
38
40
|
export interface GoogleAIModelParams {
|
|
39
|
-
/**
|
|
41
|
+
/** @deprecated Prefer `modelName` */
|
|
40
42
|
model?: string;
|
|
43
|
+
/** Model to use */
|
|
44
|
+
modelName?: string;
|
|
41
45
|
/** Sampling temperature to use */
|
|
42
46
|
temperature?: number;
|
|
43
47
|
/**
|
|
@@ -67,7 +71,20 @@ export interface GoogleAIModelParams {
|
|
|
67
71
|
stopSequences?: string[];
|
|
68
72
|
safetySettings?: GoogleAISafetySetting[];
|
|
69
73
|
}
|
|
70
|
-
|
|
74
|
+
/**
|
|
75
|
+
* The params which can be passed to the API at request time.
|
|
76
|
+
*/
|
|
77
|
+
export interface GoogleAIModelRequestParams extends GoogleAIModelParams {
|
|
78
|
+
tools?: StructuredToolInterface[] | GeminiTool[];
|
|
79
|
+
}
|
|
80
|
+
export interface GoogleAIBaseLLMInput<AuthOptions> extends BaseLLMParams, GoogleConnectionParams<AuthOptions>, GoogleAIModelParams, GoogleAISafetyParams {
|
|
81
|
+
}
|
|
82
|
+
export interface GoogleAIBaseLanguageModelCallOptions extends BaseLanguageModelCallOptions, GoogleAIModelRequestParams, GoogleAISafetyParams {
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Input to LLM class.
|
|
86
|
+
*/
|
|
87
|
+
export interface GoogleBaseLLMInput<AuthOptions> extends GoogleAIBaseLLMInput<AuthOptions> {
|
|
71
88
|
}
|
|
72
89
|
export interface GoogleResponse {
|
|
73
90
|
data: any;
|
|
@@ -76,20 +93,28 @@ export interface GeminiPartText {
|
|
|
76
93
|
text: string;
|
|
77
94
|
}
|
|
78
95
|
export interface GeminiPartInlineData {
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
inlineData: {
|
|
97
|
+
mimeType: string;
|
|
98
|
+
data: string;
|
|
99
|
+
};
|
|
81
100
|
}
|
|
82
101
|
export interface GeminiPartFileData {
|
|
83
|
-
|
|
84
|
-
|
|
102
|
+
fileData: {
|
|
103
|
+
mimeType: string;
|
|
104
|
+
fileUri: string;
|
|
105
|
+
};
|
|
85
106
|
}
|
|
86
107
|
export interface GeminiPartFunctionCall {
|
|
87
|
-
|
|
88
|
-
|
|
108
|
+
functionCall: {
|
|
109
|
+
name: string;
|
|
110
|
+
args?: object;
|
|
111
|
+
};
|
|
89
112
|
}
|
|
90
113
|
export interface GeminiPartFunctionResponse {
|
|
91
|
-
|
|
92
|
-
|
|
114
|
+
functionResponse: {
|
|
115
|
+
name: string;
|
|
116
|
+
response: object;
|
|
117
|
+
};
|
|
93
118
|
}
|
|
94
119
|
export type GeminiPart = GeminiPartText | GeminiPartInlineData | GeminiPartFileData | GeminiPartFunctionCall | GeminiPartFunctionResponse;
|
|
95
120
|
export interface GeminiSafetySetting {
|
|
@@ -100,13 +125,30 @@ export interface GeminiSafetyRating {
|
|
|
100
125
|
category: string;
|
|
101
126
|
probability: string;
|
|
102
127
|
}
|
|
103
|
-
export type GeminiRole = "user" | "model";
|
|
128
|
+
export type GeminiRole = "user" | "model" | "function";
|
|
104
129
|
export interface GeminiContent {
|
|
105
130
|
parts: GeminiPart[];
|
|
106
131
|
role: GeminiRole;
|
|
107
132
|
}
|
|
108
133
|
export interface GeminiTool {
|
|
134
|
+
functionDeclarations?: GeminiFunctionDeclaration[];
|
|
109
135
|
}
|
|
136
|
+
export interface GeminiFunctionDeclaration {
|
|
137
|
+
name: string;
|
|
138
|
+
description: string;
|
|
139
|
+
parameters?: GeminiFunctionSchema;
|
|
140
|
+
}
|
|
141
|
+
export interface GeminiFunctionSchema {
|
|
142
|
+
type: GeminiFunctionSchemaType;
|
|
143
|
+
format?: string;
|
|
144
|
+
description?: string;
|
|
145
|
+
nullable?: boolean;
|
|
146
|
+
enum?: string[];
|
|
147
|
+
properties?: Record<string, GeminiFunctionSchema>;
|
|
148
|
+
required?: string[];
|
|
149
|
+
items?: GeminiFunctionSchema;
|
|
150
|
+
}
|
|
151
|
+
export type GeminiFunctionSchemaType = "string" | "number" | "integer" | "boolean" | "array" | "object";
|
|
110
152
|
export interface GeminiGenerationConfig {
|
|
111
153
|
stopSequences?: string[];
|
|
112
154
|
candidateCount?: number;
|
|
@@ -132,6 +174,7 @@ interface GeminiResponseCandidate {
|
|
|
132
174
|
safetyRatings: GeminiSafetyRating[];
|
|
133
175
|
}
|
|
134
176
|
interface GeminiResponsePromptFeedback {
|
|
177
|
+
blockReason?: string;
|
|
135
178
|
safetyRatings: GeminiSafetyRating[];
|
|
136
179
|
}
|
|
137
180
|
export interface GenerateContentResponseData {
|
|
@@ -143,4 +186,16 @@ export type GoogleLLMResponseData = JsonStream | GenerateContentResponseData | G
|
|
|
143
186
|
export interface GoogleLLMResponse extends GoogleResponse {
|
|
144
187
|
data: GoogleLLMResponseData;
|
|
145
188
|
}
|
|
189
|
+
export interface GoogleAISafetyHandler {
|
|
190
|
+
/**
|
|
191
|
+
* A function that will take a response and return the, possibly modified,
|
|
192
|
+
* response or throw an exception if there are safety issues.
|
|
193
|
+
*
|
|
194
|
+
* @throws GoogleAISafetyError
|
|
195
|
+
*/
|
|
196
|
+
handle(response: GoogleLLMResponse): GoogleLLMResponse;
|
|
197
|
+
}
|
|
198
|
+
export interface GoogleAISafetyParams {
|
|
199
|
+
safetyHandler?: GoogleAISafetyHandler;
|
|
200
|
+
}
|
|
146
201
|
export {};
|
package/dist/utils/common.cjs
CHANGED
|
@@ -2,19 +2,26 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.copyAndValidateModelParamsInto = exports.validateModelParams = exports.modelToFamily = exports.copyAIModelParamsInto = exports.copyAIModelParams = void 0;
|
|
4
4
|
const gemini_js_1 = require("./gemini.cjs");
|
|
5
|
-
function copyAIModelParams(params) {
|
|
6
|
-
return copyAIModelParamsInto(params, {});
|
|
5
|
+
function copyAIModelParams(params, options) {
|
|
6
|
+
return copyAIModelParamsInto(params, options, {});
|
|
7
7
|
}
|
|
8
8
|
exports.copyAIModelParams = copyAIModelParams;
|
|
9
|
-
function copyAIModelParamsInto(params, target) {
|
|
9
|
+
function copyAIModelParamsInto(params, options, target) {
|
|
10
10
|
const ret = target || {};
|
|
11
|
-
ret.
|
|
12
|
-
ret.temperature =
|
|
13
|
-
|
|
14
|
-
ret.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
ret.modelName = options?.modelName ?? params?.modelName ?? target.modelName;
|
|
12
|
+
ret.temperature =
|
|
13
|
+
options?.temperature ?? params?.temperature ?? target.temperature;
|
|
14
|
+
ret.maxOutputTokens =
|
|
15
|
+
options?.maxOutputTokens ??
|
|
16
|
+
params?.maxOutputTokens ??
|
|
17
|
+
target.maxOutputTokens;
|
|
18
|
+
ret.topP = options?.topP ?? params?.topP ?? target.topP;
|
|
19
|
+
ret.topK = options?.topK ?? params?.topK ?? target.topK;
|
|
20
|
+
ret.stopSequences =
|
|
21
|
+
options?.stopSequences ?? params?.stopSequences ?? target.stopSequences;
|
|
22
|
+
ret.safetySettings =
|
|
23
|
+
options?.safetySettings ?? params?.safetySettings ?? target.safetySettings;
|
|
24
|
+
ret.tools = options?.tools;
|
|
18
25
|
return ret;
|
|
19
26
|
}
|
|
20
27
|
exports.copyAIModelParamsInto = copyAIModelParamsInto;
|
|
@@ -32,7 +39,7 @@ function modelToFamily(modelName) {
|
|
|
32
39
|
exports.modelToFamily = modelToFamily;
|
|
33
40
|
function validateModelParams(params) {
|
|
34
41
|
const testParams = params ?? {};
|
|
35
|
-
switch (modelToFamily(testParams.
|
|
42
|
+
switch (modelToFamily(testParams.modelName)) {
|
|
36
43
|
case "gemini":
|
|
37
44
|
return (0, gemini_js_1.validateGeminiParams)(testParams);
|
|
38
45
|
default:
|
|
@@ -41,7 +48,7 @@ function validateModelParams(params) {
|
|
|
41
48
|
}
|
|
42
49
|
exports.validateModelParams = validateModelParams;
|
|
43
50
|
function copyAndValidateModelParamsInto(params, target) {
|
|
44
|
-
copyAIModelParamsInto(params, target);
|
|
51
|
+
copyAIModelParamsInto(params, undefined, target);
|
|
45
52
|
validateModelParams(target);
|
|
46
53
|
return target;
|
|
47
54
|
}
|
package/dist/utils/common.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { GoogleAIModelParams, GoogleLLMModelFamily } from "../types.js";
|
|
2
|
-
export declare function copyAIModelParams(params: GoogleAIModelParams | undefined):
|
|
3
|
-
export declare function copyAIModelParamsInto(params: GoogleAIModelParams | undefined, target: GoogleAIModelParams):
|
|
1
|
+
import type { GoogleAIBaseLanguageModelCallOptions, GoogleAIModelParams, GoogleAIModelRequestParams, GoogleLLMModelFamily } from "../types.js";
|
|
2
|
+
export declare function copyAIModelParams(params: GoogleAIModelParams | undefined, options: GoogleAIBaseLanguageModelCallOptions | undefined): GoogleAIModelRequestParams;
|
|
3
|
+
export declare function copyAIModelParamsInto(params: GoogleAIModelParams | undefined, options: GoogleAIBaseLanguageModelCallOptions | undefined, target: GoogleAIModelParams): GoogleAIModelRequestParams;
|
|
4
4
|
export declare function modelToFamily(modelName: string | undefined): GoogleLLMModelFamily;
|
|
5
5
|
export declare function validateModelParams(params: GoogleAIModelParams | undefined): void;
|
|
6
6
|
export declare function copyAndValidateModelParamsInto(params: GoogleAIModelParams | undefined, target: GoogleAIModelParams): GoogleAIModelParams;
|
package/dist/utils/common.js
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
import { isModelGemini, validateGeminiParams } from "./gemini.js";
|
|
2
|
-
export function copyAIModelParams(params) {
|
|
3
|
-
return copyAIModelParamsInto(params, {});
|
|
2
|
+
export function copyAIModelParams(params, options) {
|
|
3
|
+
return copyAIModelParamsInto(params, options, {});
|
|
4
4
|
}
|
|
5
|
-
export function copyAIModelParamsInto(params, target) {
|
|
5
|
+
export function copyAIModelParamsInto(params, options, target) {
|
|
6
6
|
const ret = target || {};
|
|
7
|
-
ret.
|
|
8
|
-
ret.temperature =
|
|
9
|
-
|
|
10
|
-
ret.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
ret.modelName = options?.modelName ?? params?.modelName ?? target.modelName;
|
|
8
|
+
ret.temperature =
|
|
9
|
+
options?.temperature ?? params?.temperature ?? target.temperature;
|
|
10
|
+
ret.maxOutputTokens =
|
|
11
|
+
options?.maxOutputTokens ??
|
|
12
|
+
params?.maxOutputTokens ??
|
|
13
|
+
target.maxOutputTokens;
|
|
14
|
+
ret.topP = options?.topP ?? params?.topP ?? target.topP;
|
|
15
|
+
ret.topK = options?.topK ?? params?.topK ?? target.topK;
|
|
16
|
+
ret.stopSequences =
|
|
17
|
+
options?.stopSequences ?? params?.stopSequences ?? target.stopSequences;
|
|
18
|
+
ret.safetySettings =
|
|
19
|
+
options?.safetySettings ?? params?.safetySettings ?? target.safetySettings;
|
|
20
|
+
ret.tools = options?.tools;
|
|
14
21
|
return ret;
|
|
15
22
|
}
|
|
16
23
|
export function modelToFamily(modelName) {
|
|
@@ -26,7 +33,7 @@ export function modelToFamily(modelName) {
|
|
|
26
33
|
}
|
|
27
34
|
export function validateModelParams(params) {
|
|
28
35
|
const testParams = params ?? {};
|
|
29
|
-
switch (modelToFamily(testParams.
|
|
36
|
+
switch (modelToFamily(testParams.modelName)) {
|
|
30
37
|
case "gemini":
|
|
31
38
|
return validateGeminiParams(testParams);
|
|
32
39
|
default:
|
|
@@ -34,7 +41,7 @@ export function validateModelParams(params) {
|
|
|
34
41
|
}
|
|
35
42
|
}
|
|
36
43
|
export function copyAndValidateModelParamsInto(params, target) {
|
|
37
|
-
copyAIModelParamsInto(params, target);
|
|
44
|
+
copyAIModelParamsInto(params, undefined, target);
|
|
38
45
|
validateModelParams(target);
|
|
39
46
|
return target;
|
|
40
47
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureParams = exports.failedAttemptHandler = void 0;
|
|
4
|
+
const STATUS_NO_RETRY = [
|
|
5
|
+
400,
|
|
6
|
+
401,
|
|
7
|
+
402,
|
|
8
|
+
403,
|
|
9
|
+
404,
|
|
10
|
+
405,
|
|
11
|
+
406,
|
|
12
|
+
407,
|
|
13
|
+
408,
|
|
14
|
+
409, // Conflict
|
|
15
|
+
];
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
function failedAttemptHandler(error) {
|
|
18
|
+
const status = error?.response?.status ?? 0;
|
|
19
|
+
if (status === 0) {
|
|
20
|
+
// What is this?
|
|
21
|
+
console.error("failedAttemptHandler", error);
|
|
22
|
+
}
|
|
23
|
+
// What errors shouldn't be retried?
|
|
24
|
+
if (STATUS_NO_RETRY.includes(+status)) {
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
exports.failedAttemptHandler = failedAttemptHandler;
|
|
30
|
+
function ensureParams(params) {
|
|
31
|
+
const base = params ?? {};
|
|
32
|
+
return {
|
|
33
|
+
onFailedAttempt: failedAttemptHandler,
|
|
34
|
+
...base,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
exports.ensureParams = ensureParams;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const STATUS_NO_RETRY = [
|
|
2
|
+
400,
|
|
3
|
+
401,
|
|
4
|
+
402,
|
|
5
|
+
403,
|
|
6
|
+
404,
|
|
7
|
+
405,
|
|
8
|
+
406,
|
|
9
|
+
407,
|
|
10
|
+
408,
|
|
11
|
+
409, // Conflict
|
|
12
|
+
];
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
export function failedAttemptHandler(error) {
|
|
15
|
+
const status = error?.response?.status ?? 0;
|
|
16
|
+
if (status === 0) {
|
|
17
|
+
// What is this?
|
|
18
|
+
console.error("failedAttemptHandler", error);
|
|
19
|
+
}
|
|
20
|
+
// What errors shouldn't be retried?
|
|
21
|
+
if (STATUS_NO_RETRY.includes(+status)) {
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
export function ensureParams(params) {
|
|
27
|
+
const base = params ?? {};
|
|
28
|
+
return {
|
|
29
|
+
onFailedAttempt: failedAttemptHandler,
|
|
30
|
+
...base,
|
|
31
|
+
};
|
|
32
|
+
}
|