@langchain/google-genai 0.0.19 → 0.0.20
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/utils/common.cjs +68 -68
- package/dist/utils/common.d.ts +1 -1
- package/dist/utils/common.js +68 -68
- package/package.json +1 -1
package/dist/utils/common.cjs
CHANGED
|
@@ -53,84 +53,84 @@ function messageContentMedia(content) {
|
|
|
53
53
|
}
|
|
54
54
|
throw new Error("Invalid media content");
|
|
55
55
|
}
|
|
56
|
-
function convertMessageContentToParts(message, isMultimodalModel
|
|
57
|
-
if (typeof message.content === "string") {
|
|
56
|
+
function convertMessageContentToParts(message, isMultimodalModel) {
|
|
57
|
+
if (typeof message.content === "string" && message.content !== "") {
|
|
58
58
|
return [{ text: message.content }];
|
|
59
59
|
}
|
|
60
|
-
let
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
let functionCalls = [];
|
|
61
|
+
let functionResponses = [];
|
|
62
|
+
let messageParts = [];
|
|
63
|
+
if ("tool_calls" in message &&
|
|
64
|
+
Array.isArray(message.tool_calls) &&
|
|
65
|
+
message.tool_calls.length > 0) {
|
|
66
|
+
functionCalls = message.tool_calls.map((tc) => ({
|
|
67
|
+
functionCall: {
|
|
68
|
+
name: tc.name,
|
|
69
|
+
args: tc.args,
|
|
70
|
+
},
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
else if (message._getType() === "tool" && message.name && message.content) {
|
|
74
|
+
functionResponses = [
|
|
75
|
+
{
|
|
64
76
|
functionResponse: {
|
|
65
77
|
name: message.name,
|
|
66
78
|
response: message.content,
|
|
67
79
|
},
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
else {
|
|
71
|
-
throw new Error("ChatGoogleGenerativeAI requires tool messages to contain the tool name, and a string content.");
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if ("tool_calls" in message) {
|
|
75
|
-
const castMessage = message;
|
|
76
|
-
if (castMessage.tool_calls && castMessage.tool_calls.length > 0) {
|
|
77
|
-
functionCallParts = castMessage.tool_calls.map((tc) => ({
|
|
78
|
-
functionCall: {
|
|
79
|
-
name: tc.name,
|
|
80
|
-
args: tc.args,
|
|
81
|
-
},
|
|
82
|
-
}));
|
|
83
|
-
}
|
|
80
|
+
},
|
|
81
|
+
];
|
|
84
82
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (c.type === "image_url") {
|
|
92
|
-
if (!isMultimodalModel) {
|
|
93
|
-
throw new Error(`This model does not support images`);
|
|
94
|
-
}
|
|
95
|
-
let source;
|
|
96
|
-
if (typeof c.image_url === "string") {
|
|
97
|
-
source = c.image_url;
|
|
83
|
+
else if (Array.isArray(message.content)) {
|
|
84
|
+
messageParts = message.content.map((c) => {
|
|
85
|
+
if (c.type === "text") {
|
|
86
|
+
return {
|
|
87
|
+
text: c.text,
|
|
88
|
+
};
|
|
98
89
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
90
|
+
if (c.type === "image_url") {
|
|
91
|
+
if (!isMultimodalModel) {
|
|
92
|
+
throw new Error(`This model does not support images`);
|
|
93
|
+
}
|
|
94
|
+
let source;
|
|
95
|
+
if (typeof c.image_url === "string") {
|
|
96
|
+
source = c.image_url;
|
|
97
|
+
}
|
|
98
|
+
else if (typeof c.image_url === "object" && "url" in c.image_url) {
|
|
99
|
+
source = c.image_url.url;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
throw new Error("Please provide image as base64 encoded data URL");
|
|
103
|
+
}
|
|
104
|
+
const [dm, data] = source.split(",");
|
|
105
|
+
if (!dm.startsWith("data:")) {
|
|
106
|
+
throw new Error("Please provide image as base64 encoded data URL");
|
|
107
|
+
}
|
|
108
|
+
const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
|
|
109
|
+
if (encoding !== "base64") {
|
|
110
|
+
throw new Error("Please provide image as base64 encoded data URL");
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
inlineData: {
|
|
114
|
+
data,
|
|
115
|
+
mimeType,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
104
118
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
throw new Error("Please provide image as base64 encoded data URL");
|
|
119
|
+
else if (c.type === "media") {
|
|
120
|
+
return messageContentMedia(c);
|
|
108
121
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
122
|
+
else if (c.type === "tool_use") {
|
|
123
|
+
return {
|
|
124
|
+
functionCall: {
|
|
125
|
+
name: c.name,
|
|
126
|
+
args: c.input,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
112
129
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
else if (c.type === "media") {
|
|
121
|
-
return messageContentMedia(c);
|
|
122
|
-
}
|
|
123
|
-
else if (c.type === "tool_use") {
|
|
124
|
-
return {
|
|
125
|
-
functionCall: {
|
|
126
|
-
name: c.name,
|
|
127
|
-
args: c.input,
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
throw new Error(`Unknown content type ${c.type}`);
|
|
132
|
-
});
|
|
133
|
-
return [...messageContentParts, ...functionCallParts];
|
|
130
|
+
throw new Error(`Unknown content type ${c.type}`);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return [...messageParts, ...functionCalls, ...functionResponses];
|
|
134
134
|
}
|
|
135
135
|
exports.convertMessageContentToParts = convertMessageContentToParts;
|
|
136
136
|
function convertBaseMessagesToContent(messages, isMultimodalModel) {
|
|
@@ -149,7 +149,7 @@ function convertBaseMessagesToContent(messages, isMultimodalModel) {
|
|
|
149
149
|
prevContent.role === role) {
|
|
150
150
|
throw new Error("Google Generative AI requires alternate messages between authors");
|
|
151
151
|
}
|
|
152
|
-
const parts = convertMessageContentToParts(message, isMultimodalModel
|
|
152
|
+
const parts = convertMessageContentToParts(message, isMultimodalModel);
|
|
153
153
|
if (acc.mergeWithPreviousContent) {
|
|
154
154
|
const prevContent = acc.content[acc.content.length - 1];
|
|
155
155
|
if (!prevContent) {
|
package/dist/utils/common.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare function getMessageAuthor(message: BaseMessage): string;
|
|
|
10
10
|
* @returns The message type mapped to a Google Generative AI chat author.
|
|
11
11
|
*/
|
|
12
12
|
export declare function convertAuthorToRole(author: string): (typeof POSSIBLE_ROLES)[number];
|
|
13
|
-
export declare function convertMessageContentToParts(message: BaseMessage, isMultimodalModel: boolean
|
|
13
|
+
export declare function convertMessageContentToParts(message: BaseMessage, isMultimodalModel: boolean): Part[];
|
|
14
14
|
export declare function convertBaseMessagesToContent(messages: BaseMessage[], isMultimodalModel: boolean): Content[];
|
|
15
15
|
export declare function mapGenerateContentResultToChatResult(response: EnhancedGenerateContentResponse, extra?: {
|
|
16
16
|
usageMetadata: UsageMetadata | undefined;
|
package/dist/utils/common.js
CHANGED
|
@@ -48,84 +48,84 @@ function messageContentMedia(content) {
|
|
|
48
48
|
}
|
|
49
49
|
throw new Error("Invalid media content");
|
|
50
50
|
}
|
|
51
|
-
export function convertMessageContentToParts(message, isMultimodalModel
|
|
52
|
-
if (typeof message.content === "string") {
|
|
51
|
+
export function convertMessageContentToParts(message, isMultimodalModel) {
|
|
52
|
+
if (typeof message.content === "string" && message.content !== "") {
|
|
53
53
|
return [{ text: message.content }];
|
|
54
54
|
}
|
|
55
|
-
let
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
let functionCalls = [];
|
|
56
|
+
let functionResponses = [];
|
|
57
|
+
let messageParts = [];
|
|
58
|
+
if ("tool_calls" in message &&
|
|
59
|
+
Array.isArray(message.tool_calls) &&
|
|
60
|
+
message.tool_calls.length > 0) {
|
|
61
|
+
functionCalls = message.tool_calls.map((tc) => ({
|
|
62
|
+
functionCall: {
|
|
63
|
+
name: tc.name,
|
|
64
|
+
args: tc.args,
|
|
65
|
+
},
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
else if (message._getType() === "tool" && message.name && message.content) {
|
|
69
|
+
functionResponses = [
|
|
70
|
+
{
|
|
59
71
|
functionResponse: {
|
|
60
72
|
name: message.name,
|
|
61
73
|
response: message.content,
|
|
62
74
|
},
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
else {
|
|
66
|
-
throw new Error("ChatGoogleGenerativeAI requires tool messages to contain the tool name, and a string content.");
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if ("tool_calls" in message) {
|
|
70
|
-
const castMessage = message;
|
|
71
|
-
if (castMessage.tool_calls && castMessage.tool_calls.length > 0) {
|
|
72
|
-
functionCallParts = castMessage.tool_calls.map((tc) => ({
|
|
73
|
-
functionCall: {
|
|
74
|
-
name: tc.name,
|
|
75
|
-
args: tc.args,
|
|
76
|
-
},
|
|
77
|
-
}));
|
|
78
|
-
}
|
|
75
|
+
},
|
|
76
|
+
];
|
|
79
77
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (c.type === "image_url") {
|
|
87
|
-
if (!isMultimodalModel) {
|
|
88
|
-
throw new Error(`This model does not support images`);
|
|
89
|
-
}
|
|
90
|
-
let source;
|
|
91
|
-
if (typeof c.image_url === "string") {
|
|
92
|
-
source = c.image_url;
|
|
78
|
+
else if (Array.isArray(message.content)) {
|
|
79
|
+
messageParts = message.content.map((c) => {
|
|
80
|
+
if (c.type === "text") {
|
|
81
|
+
return {
|
|
82
|
+
text: c.text,
|
|
83
|
+
};
|
|
93
84
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
85
|
+
if (c.type === "image_url") {
|
|
86
|
+
if (!isMultimodalModel) {
|
|
87
|
+
throw new Error(`This model does not support images`);
|
|
88
|
+
}
|
|
89
|
+
let source;
|
|
90
|
+
if (typeof c.image_url === "string") {
|
|
91
|
+
source = c.image_url;
|
|
92
|
+
}
|
|
93
|
+
else if (typeof c.image_url === "object" && "url" in c.image_url) {
|
|
94
|
+
source = c.image_url.url;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
throw new Error("Please provide image as base64 encoded data URL");
|
|
98
|
+
}
|
|
99
|
+
const [dm, data] = source.split(",");
|
|
100
|
+
if (!dm.startsWith("data:")) {
|
|
101
|
+
throw new Error("Please provide image as base64 encoded data URL");
|
|
102
|
+
}
|
|
103
|
+
const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
|
|
104
|
+
if (encoding !== "base64") {
|
|
105
|
+
throw new Error("Please provide image as base64 encoded data URL");
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
inlineData: {
|
|
109
|
+
data,
|
|
110
|
+
mimeType,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
99
113
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
throw new Error("Please provide image as base64 encoded data URL");
|
|
114
|
+
else if (c.type === "media") {
|
|
115
|
+
return messageContentMedia(c);
|
|
103
116
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
117
|
+
else if (c.type === "tool_use") {
|
|
118
|
+
return {
|
|
119
|
+
functionCall: {
|
|
120
|
+
name: c.name,
|
|
121
|
+
args: c.input,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
107
124
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
},
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
else if (c.type === "media") {
|
|
116
|
-
return messageContentMedia(c);
|
|
117
|
-
}
|
|
118
|
-
else if (c.type === "tool_use") {
|
|
119
|
-
return {
|
|
120
|
-
functionCall: {
|
|
121
|
-
name: c.name,
|
|
122
|
-
args: c.input,
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
throw new Error(`Unknown content type ${c.type}`);
|
|
127
|
-
});
|
|
128
|
-
return [...messageContentParts, ...functionCallParts];
|
|
125
|
+
throw new Error(`Unknown content type ${c.type}`);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return [...messageParts, ...functionCalls, ...functionResponses];
|
|
129
129
|
}
|
|
130
130
|
export function convertBaseMessagesToContent(messages, isMultimodalModel) {
|
|
131
131
|
return messages.reduce((acc, message, index) => {
|
|
@@ -143,7 +143,7 @@ export function convertBaseMessagesToContent(messages, isMultimodalModel) {
|
|
|
143
143
|
prevContent.role === role) {
|
|
144
144
|
throw new Error("Google Generative AI requires alternate messages between authors");
|
|
145
145
|
}
|
|
146
|
-
const parts = convertMessageContentToParts(message, isMultimodalModel
|
|
146
|
+
const parts = convertMessageContentToParts(message, isMultimodalModel);
|
|
147
147
|
if (acc.mergeWithPreviousContent) {
|
|
148
148
|
const prevContent = acc.content[acc.content.length - 1];
|
|
149
149
|
if (!prevContent) {
|