Skip to content
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

[FEATURE] Google Gemini - Add support for structure JSON output #14896

Merged
merged 1 commit into from
Dec 11, 2024
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
92 changes: 92 additions & 0 deletions components/google_gemini/actions/common/generate-content.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import app from "../../google_gemini.app.mjs";
import constants from "../../common/constants.mjs";

export default {
props: {
app,
model: {
propDefinition: [
app,
"model",
() => ({
filter: ({
description,
supportedGenerationMethods,
}) => ![
"discontinued",
"deprecated",
].some((keyword) => description.includes(keyword))
&& supportedGenerationMethods?.includes(constants.MODEL_METHODS.GENERATE_CONTENT),
}),
],
},
},
async additionalProps() {
const {
model,
responseFormat,
} = this;

const {
outputTokenLimit,
temperature,
topP,
topK,
maxTemperature,
} = await this.app.getModel({
model,
});

return {
...(responseFormat && {
responseSchema: {
type: "string",
label: "Response Schema",
description: "Define the structure of the JSON response. Must be a valid JSON schema object. Leave empty to let Gemini determine the structure.",
optional: true,
},
}),
...(outputTokenLimit && {
maxOutputTokens: {
type: "integer",
label: "Max Output Tokens",
description: `The maximum number of tokens to generate in the response. Eg. \`${outputTokenLimit}\`.`,
optional: true,
max: outputTokenLimit,
},
}),
...(temperature && {
temperature: {
type: "string",
label: "Temperature",
description: `Controls the randomness of the generated text. Lower values make the text more deterministic, while higher values make it more random. Eg. \`${temperature}\`.${maxTemperature
? ` Where max temperature is \`${maxTemperature}\`.`
: ""}`,
optional: true,
},
}),
...(topP && {
topP: {
type: "string",
label: "Top P",
description: `Controls the diversity of the generated text. Lower values make the text more deterministic, while higher values make it more random. Eg. \`${topP}\`.`,
optional: true,
},
}),
...(topK && {
topK: {
type: "integer",
label: "Top K",
description: `Controls the diversity of the generated text. Lower values make the text more deterministic, while higher values make it more random. Eg. \`${topK}\`.`,
optional: true,
},
}),
stopSequences: {
type: "string[]",
label: "Stop Sequences",
description: "The set of character sequences (up to 5) that will stop output generation. If specified, the API will stop at the first appearance of a `stop_sequence`. The stop sequence will not be included as part of the response.",
optional: true,
},
};
},
};
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
import fs from "fs";
import { ConfigurationError } from "@pipedream/platform";
import app from "../../google_gemini.app.mjs";
import common from "../common/generate-content.mjs";
import utils from "../../common/utils.mjs";

export default {
...common,
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.0",
version: "0.1.1",
type: "action",
props: {
app,
model: {
propDefinition: [
app,
"model",
],
},
...common.props,
text: {
propDefinition: [
app,
common.props.app,
"text",
],
},
mimeType: {
propDefinition: [
app,
common.props.app,
"mimeType",
],
},
imagePaths: {
propDefinition: [
app,
common.props.app,
"imagePaths",
],
},
responseFormat: {
propDefinition: [
common.props.app,
"responseFormat",
],
},
jcortes marked this conversation as resolved.
Show resolved Hide resolved
},
methods: {
fileToGenerativePart(path, mimeType) {
Expand All @@ -55,6 +57,13 @@ export default {
text,
imagePaths,
mimeType,
responseFormat,
responseSchema,
maxOutputTokens,
temperature,
topP,
topK,
stopSequences,
jcortes marked this conversation as resolved.
Show resolved Hide resolved
} = this;

if (!Array.isArray(imagePaths)) {
Expand All @@ -79,6 +88,21 @@ export default {
],
},
],
...(
responseFormat || maxOutputTokens || temperature || topP || topK || stopSequences?.length
? {
generationConfig: {
responseMimeType: "application/json",
responseSchema: utils.parse(responseSchema),
maxOutputTokens,
temperature,
topP,
topK,
stopSequences,
},
}
: {}
),
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import app from "../../google_gemini.app.mjs";
import common from "../common/generate-content.mjs";
import utils from "../../common/utils.mjs";

export default {
...common,
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.0",
version: "0.1.1",
type: "action",
props: {
app,
model: {
...common.props,
text: {
propDefinition: [
app,
"model",
common.props.app,
"text",
],
},
text: {
responseFormat: {
propDefinition: [
app,
"text",
common.props.app,
"responseFormat",
],
},
},
jcortes marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -26,6 +28,13 @@ export default {
app,
model,
text,
responseFormat,
responseSchema,
maxOutputTokens,
temperature,
topP,
topK,
stopSequences,
} = this;

const response = await app.generateContent({
Expand All @@ -41,6 +50,21 @@ export default {
],
},
],
...(
responseFormat || maxOutputTokens || temperature || topP || topK || stopSequences?.length
? {
generationConfig: {
responseMimeType: "application/json",
responseSchema: utils.parse(responseSchema),
maxOutputTokens,
jcortes marked this conversation as resolved.
Show resolved Hide resolved
temperature,
topP,
topK,
stopSequences,
},
}
: {}
),
},
});

Expand Down
7 changes: 6 additions & 1 deletion components/google_gemini/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const BASE_URL = "https://generativelanguage.googleapis.com";
const VERSION_PATH = "/v1";
const VERSION_PATH = "/v1beta";

const MODEL_METHODS = {
GENERATE_CONTENT: "generateContent",
};

export default {
BASE_URL,
VERSION_PATH,
MODEL_METHODS,
};
24 changes: 24 additions & 0 deletions components/google_gemini/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ConfigurationError } from "@pipedream/platform";

function emptyStrToUndefined(value) {
const trimmed = typeof(value) === "string" && value.trim();
return trimmed === ""
? undefined
: value;
}
jcortes marked this conversation as resolved.
Show resolved Hide resolved

function parse(value) {
const valueToParse = emptyStrToUndefined(value);
if (typeof(valueToParse) === "object" || valueToParse === undefined) {
return valueToParse;
}
try {
return JSON.parse(valueToParse);
} catch (e) {
throw new ConfigurationError("Make sure the custom expression contains a valid object");
}
}

export default {
parse,
};
41 changes: 32 additions & 9 deletions components/google_gemini/google_gemini.app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export default {
type: "string",
label: "Model",
description: "The model to use for content generation",
async options({ prevContext: { pageToken } }) {
reloadProps: true,
async options({
prevContext: { pageToken },
filter = (model) => model,
}) {
if (pageToken === null) {
return [];
}
Expand All @@ -46,13 +50,16 @@ export default {
pageToken,
},
});
const options = models.map(({
name: value,
displayName: label,
}) => ({
label,
value,
}));

const options = models
.filter(filter)
.map(({
name: value,
displayName: label,
}) => ({
label,
value,
}));

return {
options,
Expand All @@ -62,6 +69,14 @@ export default {
};
},
},
responseFormat: {
type: "boolean",
label: "JSON Output",
description: "Enable to receive responses in structured JSON format instead of plain text. Useful for automated processing, data extraction, or when you need to parse the response programmatically. You can optionally define a specific schema for the response structure.",
optional: true,
default: false,
reloadProps: true,
},
},
methods: {
getUrl(path) {
Expand Down Expand Up @@ -99,7 +114,7 @@ export default {
? model
: `models/${model}`;
return this.post({
path: `/${pathPrefix}:generateContent`,
path: `/${pathPrefix}:${constants.MODEL_METHODS.GENERATE_CONTENT}`,
...args,
});
},
Expand All @@ -109,5 +124,13 @@ export default {
...args,
});
},
getModel({
model, ...args
} = {}) {
return this.makeRequest({
path: `/${model}`,
...args,
});
},
},
};
4 changes: 2 additions & 2 deletions components/google_gemini/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/google_gemini",
"version": "0.2.0",
"version": "0.2.1",
"description": "Pipedream Google Gemini Components",
"main": "google_gemini.app.mjs",
"keywords": [
Expand All @@ -13,6 +13,6 @@
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.0"
"@pipedream/platform": "^3.0.3"
}
}
Loading
Loading