@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/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
- /** Model to use */
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
- export interface GoogleAIBaseLLMInput<AuthOptions> extends BaseLLMParams, GoogleConnectionParams<AuthOptions>, GoogleAIModelParams {
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
- mimeType: string;
80
- data: string;
96
+ inlineData: {
97
+ mimeType: string;
98
+ data: string;
99
+ };
81
100
  }
82
101
  export interface GeminiPartFileData {
83
- mimeType: string;
84
- fileUri: string;
102
+ fileData: {
103
+ mimeType: string;
104
+ fileUri: string;
105
+ };
85
106
  }
86
107
  export interface GeminiPartFunctionCall {
87
- name: string;
88
- args?: object;
108
+ functionCall: {
109
+ name: string;
110
+ args?: object;
111
+ };
89
112
  }
90
113
  export interface GeminiPartFunctionResponse {
91
- name: string;
92
- response: object;
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 {};
@@ -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.model = params?.model ?? target.model;
12
- ret.temperature = params?.temperature ?? target.temperature;
13
- ret.maxOutputTokens = params?.maxOutputTokens ?? target.maxOutputTokens;
14
- ret.topP = params?.topP ?? target.topP;
15
- ret.topK = params?.topK ?? target.topK;
16
- ret.stopSequences = params?.stopSequences ?? target.stopSequences;
17
- ret.safetySettings = params?.safetySettings ?? target.safetySettings;
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.model)) {
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
  }
@@ -1,6 +1,6 @@
1
- import type { GoogleAIModelParams, GoogleLLMModelFamily } from "../types.js";
2
- export declare function copyAIModelParams(params: GoogleAIModelParams | undefined): GoogleAIModelParams;
3
- export declare function copyAIModelParamsInto(params: GoogleAIModelParams | undefined, target: GoogleAIModelParams): 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;
@@ -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.model = params?.model ?? target.model;
8
- ret.temperature = params?.temperature ?? target.temperature;
9
- ret.maxOutputTokens = params?.maxOutputTokens ?? target.maxOutputTokens;
10
- ret.topP = params?.topP ?? target.topP;
11
- ret.topK = params?.topK ?? target.topK;
12
- ret.stopSequences = params?.stopSequences ?? target.stopSequences;
13
- ret.safetySettings = params?.safetySettings ?? target.safetySettings;
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.model)) {
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,3 @@
1
+ import { AsyncCallerParams } from "@langchain/core/utils/async_caller";
2
+ export declare function failedAttemptHandler(error: any): void;
3
+ export declare function ensureParams(params?: AsyncCallerParams): AsyncCallerParams;
@@ -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
+ }