forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescribe-image.ts
143 lines (135 loc) · 4.45 KB
/
describe-image.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {
Action,
IAgentRuntime,
Memory,
State,
HandlerCallback,
composeContext,
generateObject,
ActionExample,
ModelClass,
elizaLogger,
ServiceType,
IImageDescriptionService,
} from "@elizaos/core";
import { getFileLocationTemplate } from "../templates";
import { FileLocationResultSchema, isFileLocationResult } from "../types";
export const describeImage: Action = {
name: "DESCRIBE_IMAGE",
similes: ["DESCRIBE_PICTURE", "EXPLAIN_PICTURE", "EXPLAIN_IMAGE"],
validate: async (_runtime: IAgentRuntime, _message: Memory) => {
return true;
},
description: "Describe an image",
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
_options: { [key: string]: unknown },
callback?: HandlerCallback
): Promise<boolean> => {
// Create context with attachments and URL
const getFileLocationContext = composeContext({
state,
template: getFileLocationTemplate,
});
const fileLocationResultObject = await generateObject({
runtime,
context: getFileLocationContext,
modelClass: ModelClass.SMALL,
schema: FileLocationResultSchema,
stop: ["\n"],
});
if (
!isFileLocationResult(
fileLocationResultObject?.object ?? fileLocationResultObject
)
) {
elizaLogger.error("Failed to generate file location");
return false;
}
let fileLocation = (fileLocationResultObject?.object as any)
?.fileLocation;
fileLocation ??= fileLocationResultObject;
const { description } = await runtime
.getService<IImageDescriptionService>(ServiceType.IMAGE_DESCRIPTION)
.describeImage(fileLocation);
runtime.messageManager.createMemory({
userId: message.agentId,
agentId: message.agentId,
roomId: message.roomId,
content: {
text: description,
},
});
callback({
text: description,
});
return true;
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Can you describe this image for me?",
},
},
{
user: "{{user2}}",
content: {
text: "Let me analyze this image for you...",
action: "DESCRIBE_IMAGE",
},
},
{
user: "{{user2}}",
content: {
text: "I see an orange tabby cat sitting on a windowsill. The cat appears to be relaxed and looking out the window at birds flying by. The lighting suggests it's a sunny afternoon.",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "What's in this picture?",
},
},
{
user: "{{user2}}",
content: {
text: "I'll take a look at that image...",
action: "DESCRIBE_IMAGE",
},
},
{
user: "{{user2}}",
content: {
text: "The image shows a modern kitchen with stainless steel appliances. There's a large island counter in the center with marble countertops. The cabinets are white with sleek handles, and there's pendant lighting hanging above the island.",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Could you tell me what this image depicts?",
},
},
{
user: "{{user2}}",
content: {
text: "I'll describe this image for you...",
action: "DESCRIBE_IMAGE",
},
},
{
user: "{{user2}}",
content: {
text: "This is a scenic mountain landscape at sunset. The peaks are snow-capped and reflected in a calm lake below. The sky is painted in vibrant oranges and purples, with a few wispy clouds catching the last rays of sunlight.",
},
},
],
] as ActionExample[][],
} as Action;