-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathgenerate-content-from-text-and-image.mjs
106 lines (97 loc) · 2.53 KB
/
generate-content-from-text-and-image.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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";
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.2.0",
type: "action",
props: {
...common.props,
mediaPaths: {
propDefinition: [
common.props.app,
"mediaPaths",
],
},
},
methods: {
...common.methods,
fileToGenerativePart(filePath) {
if (!filePath) {
return;
}
const mimeType = mime.getType(filePath);
return {
inline_data: {
mime_type: mimeType,
data: Buffer.from(fs.readFileSync(filePath)).toString("base64"),
},
};
},
},
async run({ $ }) {
const {
app,
model,
text,
history,
mediaPaths,
safetySettings,
responseFormat,
responseSchema,
maxOutputTokens,
temperature,
topP,
topK,
stopSequences,
} = this;
if (!Array.isArray(mediaPaths)) {
throw new ConfigurationError("Image/Video paths must be an array.");
}
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,
safetySettings: this.formatSafetySettings(safetySettings),
...(
responseFormat || maxOutputTokens || temperature || topP || topK || stopSequences?.length
? {
generationConfig: {
responseMimeType: "application/json",
responseSchema: utils.parse(responseSchema),
maxOutputTokens,
temperature,
topP,
topK,
stopSequences,
},
}
: {}
),
},
});
$.export("$summary", "Successfully generated content from text and image.");
return response;
},
};