@langchain/google-common 0.0.5 → 0.0.7

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.
@@ -11,6 +11,7 @@ function copyAIModelParamsInto(params, options, target) {
11
11
  const model = options?.model ?? params?.model ?? target.model;
12
12
  ret.modelName =
13
13
  model ?? options?.modelName ?? params?.modelName ?? target.modelName;
14
+ ret.model = model;
14
15
  ret.temperature =
15
16
  options?.temperature ?? params?.temperature ?? target.temperature;
16
17
  ret.maxOutputTokens =
@@ -7,6 +7,7 @@ export function copyAIModelParamsInto(params, options, target) {
7
7
  const model = options?.model ?? params?.model ?? target.model;
8
8
  ret.modelName =
9
9
  model ?? options?.modelName ?? params?.modelName ?? target.modelName;
10
+ ret.model = model;
10
11
  ret.temperature =
11
12
  options?.temperature ?? params?.temperature ?? target.temperature;
12
13
  ret.maxOutputTokens =
@@ -5,6 +5,15 @@ const uuid_1 = require("uuid");
5
5
  const messages_1 = require("@langchain/core/messages");
6
6
  const outputs_1 = require("@langchain/core/outputs");
7
7
  const safety_js_1 = require("./safety.cjs");
8
+ const extractMimeType = (str) => {
9
+ if (str.startsWith("data:")) {
10
+ return {
11
+ mimeType: str.split(":")[1].split(";")[0],
12
+ data: str.split(",")[1],
13
+ };
14
+ }
15
+ return null;
16
+ };
8
17
  function messageContentText(content) {
9
18
  if (content?.text && content?.text.length > 0) {
10
19
  return {
@@ -22,12 +31,10 @@ function messageContentImageUrl(content) {
22
31
  if (!url) {
23
32
  throw new Error("Missing Image URL");
24
33
  }
25
- if (url.startsWith("data:")) {
34
+ const mineTypeAndData = extractMimeType(url);
35
+ if (mineTypeAndData) {
26
36
  return {
27
- inlineData: {
28
- mimeType: url.split(":")[1].split(";")[0],
29
- data: url.split(",")[1],
30
- },
37
+ inlineData: mineTypeAndData,
31
38
  };
32
39
  }
33
40
  else {
@@ -40,6 +47,27 @@ function messageContentImageUrl(content) {
40
47
  };
41
48
  }
42
49
  }
50
+ function messageContentMedia(
51
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
+ content) {
53
+ if ("mimeType" in content && "data" in content) {
54
+ return {
55
+ inlineData: {
56
+ mimeType: content.mimeType,
57
+ data: content.data,
58
+ },
59
+ };
60
+ }
61
+ else if ("mimeType" in content && "fileUri" in content) {
62
+ return {
63
+ fileData: {
64
+ mimeType: content.mimeType,
65
+ fileUri: content.fileUri,
66
+ },
67
+ };
68
+ }
69
+ throw new Error("Invalid media content");
70
+ }
43
71
  function messageContentToParts(content) {
44
72
  // Convert a string to a text type MessageContent if needed
45
73
  const messageContent = typeof content === "string"
@@ -65,6 +93,8 @@ function messageContentToParts(content) {
65
93
  return messageContentImageUrl(content);
66
94
  }
67
95
  break;
96
+ case "media":
97
+ return messageContentMedia(content);
68
98
  default:
69
99
  throw new Error(`Unsupported type received while converting message to message parts`);
70
100
  }
@@ -149,7 +179,6 @@ function toolMessageToContent(message, prevMessage) {
149
179
  const responseName = ((0, messages_1.isAIMessage)(prevMessage) && !!prevMessage.tool_calls?.length
150
180
  ? prevMessage.tool_calls[0].name
151
181
  : prevMessage.name) ?? message.tool_call_id;
152
- console.log(contentStr);
153
182
  try {
154
183
  const content = JSON.parse(contentStr);
155
184
  return [
@@ -2,6 +2,15 @@ import { v4 as uuidv4 } from "uuid";
2
2
  import { AIMessage, AIMessageChunk, isAIMessage, } from "@langchain/core/messages";
3
3
  import { ChatGenerationChunk, } from "@langchain/core/outputs";
4
4
  import { GoogleAISafetyError } from "./safety.js";
5
+ const extractMimeType = (str) => {
6
+ if (str.startsWith("data:")) {
7
+ return {
8
+ mimeType: str.split(":")[1].split(";")[0],
9
+ data: str.split(",")[1],
10
+ };
11
+ }
12
+ return null;
13
+ };
5
14
  function messageContentText(content) {
6
15
  if (content?.text && content?.text.length > 0) {
7
16
  return {
@@ -19,12 +28,10 @@ function messageContentImageUrl(content) {
19
28
  if (!url) {
20
29
  throw new Error("Missing Image URL");
21
30
  }
22
- if (url.startsWith("data:")) {
31
+ const mineTypeAndData = extractMimeType(url);
32
+ if (mineTypeAndData) {
23
33
  return {
24
- inlineData: {
25
- mimeType: url.split(":")[1].split(";")[0],
26
- data: url.split(",")[1],
27
- },
34
+ inlineData: mineTypeAndData,
28
35
  };
29
36
  }
30
37
  else {
@@ -37,6 +44,27 @@ function messageContentImageUrl(content) {
37
44
  };
38
45
  }
39
46
  }
47
+ function messageContentMedia(
48
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
+ content) {
50
+ if ("mimeType" in content && "data" in content) {
51
+ return {
52
+ inlineData: {
53
+ mimeType: content.mimeType,
54
+ data: content.data,
55
+ },
56
+ };
57
+ }
58
+ else if ("mimeType" in content && "fileUri" in content) {
59
+ return {
60
+ fileData: {
61
+ mimeType: content.mimeType,
62
+ fileUri: content.fileUri,
63
+ },
64
+ };
65
+ }
66
+ throw new Error("Invalid media content");
67
+ }
40
68
  export function messageContentToParts(content) {
41
69
  // Convert a string to a text type MessageContent if needed
42
70
  const messageContent = typeof content === "string"
@@ -62,6 +90,8 @@ export function messageContentToParts(content) {
62
90
  return messageContentImageUrl(content);
63
91
  }
64
92
  break;
93
+ case "media":
94
+ return messageContentMedia(content);
65
95
  default:
66
96
  throw new Error(`Unsupported type received while converting message to message parts`);
67
97
  }
@@ -145,7 +175,6 @@ function toolMessageToContent(message, prevMessage) {
145
175
  const responseName = (isAIMessage(prevMessage) && !!prevMessage.tool_calls?.length
146
176
  ? prevMessage.tool_calls[0].name
147
177
  : prevMessage.name) ?? message.tool_call_id;
148
- console.log(contentStr);
149
178
  try {
150
179
  const content = JSON.parse(contentStr);
151
180
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-common",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Core types and classes for Google services.",
5
5
  "type": "module",
6
6
  "engines": {