-
Notifications
You must be signed in to change notification settings - Fork 490
/
Image.js
45 lines (43 loc) · 1.48 KB
/
Image.js
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
import fetch from "node-fetch";
export default {
rules: [
`You are an AI that's good at describing images.`,
`First check if my message includes the word "image", "photo", "picture", or "drawing"`,
`If it does include one of those words then at the very end of your reply you should include an image description enclosed in double curly brackets.`,
`If it does not include one of those words then don't add an image description.`,
],
parse: async (reply) => {
// Match anything between {{ }}
const regex = /\{\{([^\]]+?)\}\}/g;
const matches = reply.match(regex);
if (matches?.length) {
for (const match of matches) {
// Get image description between curly brackets
const imageDescription = match.replace(regex, "$1").trim();
// Search for image on Lexica
const image = await fetch(
`https://lexica.art/api/v1/search?q=${encodeURIComponent(
imageDescription
)}`,
{
method: "GET",
}
)
.then((response) => response.json())
.then((response) => {
if (response?.images) {
return `https://image.lexica.art/md/${response?.images[0]?.id}`;
}
})
.catch((error) => {
// Ignore error
});
// Replace description with image URL
reply = image
? reply.replace(`\{\{${imageDescription}\}\}`, image)
: reply;
}
}
return reply;
},
};