Skip to content

[ACTION] Update Google Gemini actions #16358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions components/google_gemini/actions/common/generate-content.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import app from "../../google_gemini.app.mjs";
import constants from "../../common/constants.mjs";
import { ConfigurationError } from "@pipedream/platform";

export default {
props: {
Expand All @@ -20,6 +21,106 @@ export default {
}),
],
},
text: {
propDefinition: [
app,
"text",
],
},
responseFormat: {
propDefinition: [
app,
"responseFormat",
],
},
history: {
type: "string[]",
label: "Conversation History",
description: "Previous messages in the conversation. Each item must be a valid JSON string with `text` and `role` (either `user` or `model`). Example: `{\"text\": \"Hello\", \"role\": \"user\"}`",
optional: true,
},
safetySettings: {
type: "string[]",
label: "Safety Settings",
description: "Configure content filtering for different harm categories. Each item must be a valid JSON string with `category` (one of: `HARASSMENT`, `HATE_SPEECH`, `SEXUALLY_EXPLICIT`, `DANGEROUS`, `CIVIC`) and `threshold` (one of: `BLOCK_NONE`, `BLOCK_ONLY_HIGH`, `BLOCK_MEDIUM_AND_ABOVE`, `BLOCK_LOW_AND_ABOVE`). Example: `{\"category\": \"HARASSMENT\", \"threshold\": \"BLOCK_MEDIUM_AND_ABOVE\"}`",
optional: true,
},
},
methods: {
formatHistoryToContent(history) {
if (typeof(history) === "string") {
try {
history = JSON.parse(history);
} catch (e) {
throw new ConfigurationError(`Invalid JSON in history: ${history}`);
}
}

if (Array.isArray(history) && !history?.length) {
return [];
}

return history.map((itemStr) => {
let item = itemStr;
if (typeof item === "string") {
try {
item = JSON.parse(itemStr);
} catch (e) {
throw new ConfigurationError(`Invalid JSON in history item: ${itemStr}`);
}
}

if (!item.text || !item.role || ![
"user",
"model",
].includes(item.role)) {
throw new ConfigurationError("Each history item must include `text` and `role` (either `user` or `model`)");
}

return {
parts: [
{
text: item.text,
},
],
role: item.role,
};
});
},
formatSafetySettings(safetySettings) {
if (!safetySettings?.length) {
return undefined;
}

return safetySettings.map((settingStr) => {
let setting = settingStr;
if (typeof setting === "string") {
try {
setting = JSON.parse(settingStr);
} catch (e) {
throw new ConfigurationError(`Invalid JSON in safety setting: ${settingStr}`);
}
}

if (!setting.category || !setting.threshold) {
throw new ConfigurationError("Each safety setting must include 'category' and 'threshold'");
}

const category = constants.HARM_CATEGORIES[setting.category];
if (!category) {
throw new ConfigurationError(`Invalid category '${setting.category}'. Must be one of: ${Object.keys(constants.HARM_CATEGORIES).join(", ")}`);
}

if (!constants.BLOCK_THRESHOLDS[setting.threshold]) {
throw new ConfigurationError(`Invalid threshold '${setting.threshold}'. Must be one of: ${Object.keys(constants.BLOCK_THRESHOLDS).join(", ")}`);
}

return {
category,
threshold: setting.threshold,
};
});
},
},
async additionalProps() {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs";
import mime from "mime";
import { ConfigurationError } from "@pipedream/platform";
import common from "../common/generate-content.mjs";
import utils from "../../common/utils.mjs";
Expand All @@ -8,44 +9,30 @@ export default {
key: "google_gemini-generate-content-from-text-and-image",
name: "Generate Content from Text and Image",
description: "Generates content from both text and image input using the Gemini API. [See the documentation](https://ai.google.dev/tutorials/rest_quickstart#text-and-image_input)",
version: "0.1.2",
version: "0.2.0",
type: "action",
props: {
...common.props,
text: {
mediaPaths: {
propDefinition: [
common.props.app,
"text",
],
},
mimeType: {
propDefinition: [
common.props.app,
"mimeType",
],
},
imagePaths: {
propDefinition: [
common.props.app,
"imagePaths",
],
},
responseFormat: {
propDefinition: [
common.props.app,
"responseFormat",
"mediaPaths",
],
},
},
methods: {
fileToGenerativePart(path, mimeType) {
if (!path) {
...common.methods,
fileToGenerativePart(filePath) {
if (!filePath) {
return;
}

const mimeType = mime.getType(filePath);

return {
inline_data: {
mime_type: mimeType,
data: Buffer.from(fs.readFileSync(path)).toString("base64"),
data: Buffer.from(fs.readFileSync(filePath)).toString("base64"),
},
};
},
Expand All @@ -55,8 +42,9 @@ export default {
app,
model,
text,
imagePaths,
mimeType,
history,
mediaPaths,
safetySettings,
responseFormat,
responseSchema,
maxOutputTokens,
Expand All @@ -66,28 +54,33 @@ export default {
stopSequences,
} = this;

if (!Array.isArray(imagePaths)) {
throw new ConfigurationError("Image paths must be an array.");
if (!Array.isArray(mediaPaths)) {
throw new ConfigurationError("Image/Video paths must be an array.");
}

if (!imagePaths.length) {
throw new ConfigurationError("At least one image path must be provided.");
if (!mediaPaths.length) {
throw new ConfigurationError("At least one media path must be provided.");
}

const contents = [
...this.formatHistoryToContent(history),
{
parts: [
...mediaPaths.map((path) => this.fileToGenerativePart(path)),
{
text,
},
],
role: "user",
},
];

const response = await app.generateContent({
$,
model,
data: {
contents: [
{
parts: [
{
text,
},
...imagePaths.map((path) => this.fileToGenerativePart(path, mimeType)),
],
},
],
contents,
safetySettings: this.formatSafetySettings(safetySettings),
...(
responseFormat || maxOutputTokens || temperature || topP || topK || stopSequences?.length
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,15 @@ export default {
key: "google_gemini-generate-content-from-text",
name: "Generate Content from Text",
description: "Generates content from text input using the Google Gemini API. [See the documentation](https://ai.google.dev/tutorials/rest_quickstart#text-only_input)",
version: "0.1.2",
version: "0.2.0",
type: "action",
props: {
...common.props,
text: {
propDefinition: [
common.props.app,
"text",
],
},
responseFormat: {
propDefinition: [
common.props.app,
"responseFormat",
],
},
},
async run({ $ }) {
const {
app,
model,
text,
history,
safetySettings,
responseFormat,
responseSchema,
maxOutputTokens,
Expand All @@ -37,19 +24,24 @@ export default {
stopSequences,
} = this;

const contents = [
...this.formatHistoryToContent(history),
{
parts: [
{
text,
},
],
role: "user",
},
];

const response = await app.generateContent({
$,
model,
data: {
contents: [
{
parts: [
{
text,
},
],
},
],
contents,
safetySettings: this.formatSafetySettings(safetySettings),
...(
responseFormat || maxOutputTokens || temperature || topP || topK || stopSequences?.length
? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import app from "../../google_gemini.app.mjs";
import constants from "../../common/constants.mjs";

export default {
key: "google_gemini-generate-embeddings",
name: "Generate Embeddings",
description: "Generate embeddings from text input using Google Gemini. [See the documentation](https://ai.google.dev/gemini-api/docs/embeddings)",
version: "0.0.1",
type: "action",
props: {
app,
model: {
reloadProps: false,
propDefinition: [
app,
"model",
() => ({
filter: ({
description,
supportedGenerationMethods,
}) => ![
"discontinued",
"deprecated",
].some((keyword) => description?.includes(keyword))
&& supportedGenerationMethods?.includes(constants.MODEL_METHODS.EMBED_CONTENT),
}),
],
},
text: {
propDefinition: [
app,
"text",
],
description: "The text to generate embeddings for",
},
taskType: {
type: "string",
label: "Task Type",
description: "The type of task for which the embeddings will be used",
options: Object.values(constants.TASK_TYPE),
optional: true,
},
},
async run({ $ }) {
const response = await this.app.embedContent({
model: this.model,
data: {
taskType: this.taskType,
content: {
parts: [
{
text: this.text,
},
],
},
},
});

$.export("$summary", "Successfully generated embeddings");
return response;
},
};
Loading
Loading