@langchain/google-common 0.0.1 → 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/llms.d.ts CHANGED
@@ -18,7 +18,7 @@ export declare abstract class GoogleBaseLLM<AuthOptions> extends LLM<BaseLanguag
18
18
  static lc_name(): string;
19
19
  originalFields?: GoogleBaseLLMInput<AuthOptions>;
20
20
  lc_serializable: boolean;
21
- model: string;
21
+ modelName: string;
22
22
  temperature: number;
23
23
  maxOutputTokens: number;
24
24
  topP: number;
package/dist/llms.js CHANGED
@@ -50,7 +50,7 @@ export class GoogleBaseLLM extends LLM {
50
50
  writable: true,
51
51
  value: true
52
52
  });
53
- Object.defineProperty(this, "model", {
53
+ Object.defineProperty(this, "modelName", {
54
54
  enumerable: true,
55
55
  configurable: true,
56
56
  writable: true,
@@ -153,7 +153,7 @@ export class GoogleBaseLLM extends LLM {
153
153
  * in order to handle public APi calls to `generate()`.
154
154
  */
155
155
  async _call(prompt, options) {
156
- const parameters = copyAIModelParams(this);
156
+ const parameters = copyAIModelParams(this, options);
157
157
  const result = await this.connection.request(prompt, parameters, options);
158
158
  const ret = safeResponseToString(result, this.safetyHandler);
159
159
  return ret;
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,8 +71,16 @@ export interface GoogleAIModelParams {
67
71
  stopSequences?: string[];
68
72
  safetySettings?: GoogleAISafetySetting[];
69
73
  }
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
+ }
70
80
  export interface GoogleAIBaseLLMInput<AuthOptions> extends BaseLLMParams, GoogleConnectionParams<AuthOptions>, GoogleAIModelParams, GoogleAISafetyParams {
71
81
  }
82
+ export interface GoogleAIBaseLanguageModelCallOptions extends BaseLanguageModelCallOptions, GoogleAIModelRequestParams, GoogleAISafetyParams {
83
+ }
72
84
  /**
73
85
  * Input to LLM class.
74
86
  */
@@ -113,13 +125,30 @@ export interface GeminiSafetyRating {
113
125
  category: string;
114
126
  probability: string;
115
127
  }
116
- export type GeminiRole = "user" | "model";
128
+ export type GeminiRole = "user" | "model" | "function";
117
129
  export interface GeminiContent {
118
130
  parts: GeminiPart[];
119
131
  role: GeminiRole;
120
132
  }
121
133
  export interface GeminiTool {
122
- }
134
+ functionDeclarations?: GeminiFunctionDeclaration[];
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";
123
152
  export interface GeminiGenerationConfig {
124
153
  stopSequences?: string[];
125
154
  candidateCount?: number;
@@ -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
  }
@@ -1,13 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MessageGeminiSafetyHandler = exports.DefaultGeminiSafetyHandler = exports.isModelGemini = exports.validateGeminiParams = exports.safeResponseToChatResult = exports.responseToChatResult = exports.safeResponseToBaseMessage = exports.responseToBaseMessage = exports.responseToMessageContent = exports.responseToChatGenerations = exports.partToChatGeneration = exports.partToMessage = exports.chunkToString = exports.safeResponseToChatGeneration = exports.responseToChatGeneration = exports.safeResponseToGeneration = exports.responseToGeneration = exports.safeResponseToString = exports.responseToString = exports.partToText = exports.responseToParts = exports.responseToGenerateContentResponseData = exports.partsToMessageContent = exports.baseMessageToContent = exports.messageContentToParts = void 0;
3
+ exports.MessageGeminiSafetyHandler = exports.DefaultGeminiSafetyHandler = exports.isModelGemini = exports.validateGeminiParams = exports.safeResponseToChatResult = exports.responseToChatResult = exports.safeResponseToBaseMessage = exports.responseToBaseMessage = exports.partsToBaseMessageFields = exports.responseToBaseMessageFields = exports.responseToChatGenerations = exports.partToChatGeneration = exports.partToMessage = exports.chunkToString = exports.safeResponseToChatGeneration = exports.responseToChatGeneration = exports.safeResponseToGeneration = exports.responseToGeneration = exports.safeResponseToString = exports.responseToString = exports.partToText = exports.responseToParts = exports.responseToGenerateContentResponseData = exports.toolsRawToTools = exports.partsToToolsRaw = exports.partsToMessageContent = exports.baseMessageToContent = exports.messageContentToParts = void 0;
4
4
  const messages_1 = require("@langchain/core/messages");
5
5
  const outputs_1 = require("@langchain/core/outputs");
6
6
  const safety_js_1 = require("./safety.cjs");
7
7
  function messageContentText(content) {
8
- return {
9
- text: content.text,
10
- };
8
+ if (content?.text && content?.text.length > 0) {
9
+ return {
10
+ text: content.text,
11
+ };
12
+ }
13
+ else {
14
+ return null;
15
+ }
11
16
  }
12
17
  function messageContentImageUrl(content) {
13
18
  const url = typeof content.image_url === "string"
@@ -45,23 +50,61 @@ function messageContentToParts(content) {
45
50
  ]
46
51
  : content;
47
52
  // eslint-disable-next-line array-callback-return
48
- const parts = messageContent.map((content) => {
49
- // eslint-disable-next-line default-case
53
+ const parts = messageContent
54
+ .map((content) => {
50
55
  switch (content.type) {
51
56
  case "text":
52
57
  return messageContentText(content);
53
58
  case "image_url":
54
59
  return messageContentImageUrl(content);
60
+ default:
61
+ throw new Error(`Unsupported type received while converting message to message parts`);
55
62
  }
56
- });
63
+ })
64
+ .reduce((acc, val) => {
65
+ if (val) {
66
+ return [...acc, val];
67
+ }
68
+ else {
69
+ return acc;
70
+ }
71
+ }, []);
57
72
  return parts;
58
73
  }
59
74
  exports.messageContentToParts = messageContentToParts;
75
+ function messageToolCallsToParts(toolCalls) {
76
+ if (!toolCalls || toolCalls.length === 0) {
77
+ return [];
78
+ }
79
+ return toolCalls.map((tool) => {
80
+ let args = {};
81
+ if (tool?.function?.arguments) {
82
+ const argStr = tool.function.arguments;
83
+ args = JSON.parse(argStr);
84
+ }
85
+ return {
86
+ functionCall: {
87
+ name: tool.function.name,
88
+ args,
89
+ },
90
+ };
91
+ });
92
+ }
93
+ function messageKwargsToParts(kwargs) {
94
+ const ret = [];
95
+ if (kwargs?.tool_calls) {
96
+ ret.push(...messageToolCallsToParts(kwargs.tool_calls));
97
+ }
98
+ return ret;
99
+ }
60
100
  function roleMessageToContent(role, message) {
101
+ const contentParts = messageContentToParts(message.content);
102
+ const toolParts = messageKwargsToParts(message.additional_kwargs);
103
+ const parts = [...contentParts, ...toolParts];
61
104
  return [
62
105
  {
63
106
  role,
64
- parts: messageContentToParts(message.content),
107
+ parts,
65
108
  },
66
109
  ];
67
110
  }
@@ -71,6 +114,32 @@ function systemMessageToContent(message) {
71
114
  ...roleMessageToContent("model", new messages_1.AIMessage("Ok")),
72
115
  ];
73
116
  }
117
+ function toolMessageToContent(message) {
118
+ const contentStr = typeof message.content === "string"
119
+ ? message.content
120
+ : message.content.reduce((acc, content) => {
121
+ if (content.type === "text") {
122
+ return acc + content.text;
123
+ }
124
+ else {
125
+ return acc;
126
+ }
127
+ }, "");
128
+ const content = JSON.parse(contentStr);
129
+ return [
130
+ {
131
+ role: "function",
132
+ parts: [
133
+ {
134
+ functionResponse: {
135
+ name: message.tool_call_id,
136
+ response: content,
137
+ },
138
+ },
139
+ ],
140
+ },
141
+ ];
142
+ }
74
143
  function baseMessageToContent(message) {
75
144
  const type = message._getType();
76
145
  switch (type) {
@@ -80,6 +149,8 @@ function baseMessageToContent(message) {
80
149
  return roleMessageToContent("user", message);
81
150
  case "ai":
82
151
  return roleMessageToContent("model", message);
152
+ case "tool":
153
+ return toolMessageToContent(message);
83
154
  default:
84
155
  console.log(`Unsupported message type: ${type}`);
85
156
  return [];
@@ -131,6 +202,51 @@ function partsToMessageContent(parts) {
131
202
  }, []);
132
203
  }
133
204
  exports.partsToMessageContent = partsToMessageContent;
205
+ function toolRawToTool(raw) {
206
+ return {
207
+ id: raw.id,
208
+ type: raw.type,
209
+ function: {
210
+ name: raw.function.name,
211
+ arguments: JSON.stringify(raw.function.arguments),
212
+ },
213
+ };
214
+ }
215
+ function functionCallPartToToolRaw(part) {
216
+ return {
217
+ id: part?.functionCall?.name ?? "",
218
+ type: "function",
219
+ function: {
220
+ name: part.functionCall.name,
221
+ arguments: part.functionCall.args ?? {},
222
+ },
223
+ };
224
+ }
225
+ function partsToToolsRaw(parts) {
226
+ return parts
227
+ .map((part) => {
228
+ if (part === undefined || part === null) {
229
+ return null;
230
+ }
231
+ else if ("functionCall" in part) {
232
+ return functionCallPartToToolRaw(part);
233
+ }
234
+ else {
235
+ return null;
236
+ }
237
+ })
238
+ .reduce((acc, content) => {
239
+ if (content) {
240
+ acc.push(content);
241
+ }
242
+ return acc;
243
+ }, []);
244
+ }
245
+ exports.partsToToolsRaw = partsToToolsRaw;
246
+ function toolsRawToTools(raws) {
247
+ return raws.map((raw) => toolRawToTool(raw));
248
+ }
249
+ exports.toolsRawToTools = toolsRawToTools;
134
250
  function responseToGenerateContentResponseData(response) {
135
251
  if ("nextChunk" in response.data) {
136
252
  throw new Error("Cannot convert Stream to GenerateContentResponseData");
@@ -231,8 +347,8 @@ function chunkToString(chunk) {
231
347
  }
232
348
  exports.chunkToString = chunkToString;
233
349
  function partToMessage(part) {
234
- const content = partsToMessageContent([part]);
235
- return new messages_1.AIMessageChunk({ content });
350
+ const fields = partsToBaseMessageFields([part]);
351
+ return new messages_1.AIMessageChunk(fields);
236
352
  }
237
353
  exports.partToMessage = partToMessage;
238
354
  function partToChatGeneration(part) {
@@ -250,15 +366,28 @@ function responseToChatGenerations(response) {
250
366
  return ret;
251
367
  }
252
368
  exports.responseToChatGenerations = responseToChatGenerations;
253
- function responseToMessageContent(response) {
369
+ function responseToBaseMessageFields(response) {
254
370
  const parts = responseToParts(response);
255
- return partsToMessageContent(parts);
371
+ return partsToBaseMessageFields(parts);
256
372
  }
257
- exports.responseToMessageContent = responseToMessageContent;
373
+ exports.responseToBaseMessageFields = responseToBaseMessageFields;
374
+ function partsToBaseMessageFields(parts) {
375
+ const fields = {
376
+ content: partsToMessageContent(parts),
377
+ };
378
+ const rawTools = partsToToolsRaw(parts);
379
+ if (rawTools.length > 0) {
380
+ const tools = toolsRawToTools(rawTools);
381
+ fields.additional_kwargs = {
382
+ tool_calls: tools,
383
+ };
384
+ }
385
+ return fields;
386
+ }
387
+ exports.partsToBaseMessageFields = partsToBaseMessageFields;
258
388
  function responseToBaseMessage(response) {
259
- return new messages_1.AIMessage({
260
- content: responseToMessageContent(response),
261
- });
389
+ const fields = responseToBaseMessageFields(response);
390
+ return new messages_1.AIMessage(fields);
262
391
  }
263
392
  exports.responseToBaseMessage = responseToBaseMessage;
264
393
  function safeResponseToBaseMessage(response, safetyHandler) {
@@ -1,9 +1,29 @@
1
- import { BaseMessage, BaseMessageChunk, MessageContent } from "@langchain/core/messages";
1
+ import { BaseMessage, BaseMessageChunk, BaseMessageFields, MessageContent } from "@langchain/core/messages";
2
2
  import { ChatGeneration, ChatGenerationChunk, ChatResult, Generation } from "@langchain/core/outputs";
3
3
  import type { GoogleLLMResponse, GoogleAIModelParams, GeminiPart, GeminiContent, GenerateContentResponseData, GoogleAISafetyHandler } from "../types.js";
4
4
  export declare function messageContentToParts(content: MessageContent): GeminiPart[];
5
5
  export declare function baseMessageToContent(message: BaseMessage): GeminiContent[];
6
6
  export declare function partsToMessageContent(parts: GeminiPart[]): MessageContent;
7
+ interface FunctionCall {
8
+ name: string;
9
+ arguments: string;
10
+ }
11
+ interface ToolCall {
12
+ id: string;
13
+ type: "function";
14
+ function: FunctionCall;
15
+ }
16
+ interface FunctionCallRaw {
17
+ name: string;
18
+ arguments: object;
19
+ }
20
+ interface ToolCallRaw {
21
+ id: string;
22
+ type: "function";
23
+ function: FunctionCallRaw;
24
+ }
25
+ export declare function partsToToolsRaw(parts: GeminiPart[]): ToolCallRaw[];
26
+ export declare function toolsRawToTools(raws: ToolCallRaw[]): ToolCall[];
7
27
  export declare function responseToGenerateContentResponseData(response: GoogleLLMResponse): GenerateContentResponseData;
8
28
  export declare function responseToParts(response: GoogleLLMResponse): GeminiPart[];
9
29
  export declare function partToText(part: GeminiPart): string;
@@ -17,7 +37,8 @@ export declare function chunkToString(chunk: BaseMessageChunk): string;
17
37
  export declare function partToMessage(part: GeminiPart): BaseMessageChunk;
18
38
  export declare function partToChatGeneration(part: GeminiPart): ChatGeneration;
19
39
  export declare function responseToChatGenerations(response: GoogleLLMResponse): ChatGeneration[];
20
- export declare function responseToMessageContent(response: GoogleLLMResponse): MessageContent;
40
+ export declare function responseToBaseMessageFields(response: GoogleLLMResponse): BaseMessageFields;
41
+ export declare function partsToBaseMessageFields(parts: GeminiPart[]): BaseMessageFields;
21
42
  export declare function responseToBaseMessage(response: GoogleLLMResponse): BaseMessage;
22
43
  export declare function safeResponseToBaseMessage(response: GoogleLLMResponse, safetyHandler: GoogleAISafetyHandler): BaseMessage;
23
44
  export declare function responseToChatResult(response: GoogleLLMResponse): ChatResult;
@@ -46,3 +67,4 @@ export declare class MessageGeminiSafetyHandler extends DefaultGeminiSafetyHandl
46
67
  setMessage(data: GenerateContentResponseData): GenerateContentResponseData;
47
68
  handleData(response: GoogleLLMResponse, data: GenerateContentResponseData): GenerateContentResponseData;
48
69
  }
70
+ export {};