@zodic/shared 0.0.363 → 0.0.364
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,41 +6,79 @@ import { AppContext } from '../base';
|
|
|
6
6
|
export class ImageDescriberService {
|
|
7
7
|
constructor(@inject(AppContext) private context: AppContext) {}
|
|
8
8
|
|
|
9
|
+
// Helper function to add a delay
|
|
10
|
+
private delay(ms: number): Promise<void> {
|
|
11
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
12
|
+
}
|
|
13
|
+
|
|
9
14
|
async describeImage({ imageUrl }: { imageUrl: string }): Promise<string> {
|
|
10
15
|
console.log('ImageDescriber imageUrl ->', imageUrl);
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
|
|
17
|
+
// Define the models to try in sequence
|
|
18
|
+
const models = [
|
|
19
|
+
'meta-llama/Llama-Vision-Free',
|
|
20
|
+
'meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo',
|
|
21
|
+
'meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo',
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
// Delay between retries (in milliseconds)
|
|
25
|
+
const RETRY_DELAY_MS = 1000;
|
|
26
|
+
|
|
27
|
+
let lastError: Error | null = null;
|
|
28
|
+
|
|
29
|
+
// Attempt to describe the image with each model in sequence
|
|
30
|
+
for (let i = 0; i < models.length; i++) {
|
|
31
|
+
const model = models[i];
|
|
32
|
+
console.log(`Attempting to describe image with model: ${model} (Attempt ${i + 1}/${models.length})`);
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
const response = await this.context.api().callTogether.single(
|
|
36
|
+
[
|
|
37
|
+
{
|
|
38
|
+
role: 'user',
|
|
39
|
+
content: [
|
|
40
|
+
{
|
|
41
|
+
type: 'text',
|
|
42
|
+
text: 'Describe the following traits of the person in the picture:\n- Hair color, size and style\n- Skin color\n\nReturn the result in the following format:\n\nHair: hair description\nSkin: skin description',
|
|
25
43
|
},
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
44
|
+
{
|
|
45
|
+
type: 'image_url',
|
|
46
|
+
image_url: {
|
|
47
|
+
url: imageUrl,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
{
|
|
54
|
+
model,
|
|
55
|
+
temperature: 0.3,
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
if (!response) {
|
|
60
|
+
throw new Error(`No response received from model ${model}`);
|
|
33
61
|
}
|
|
34
|
-
);
|
|
35
62
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
63
|
+
console.log(`Successfully described image with model: ${model}`);
|
|
64
|
+
return response;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
lastError = error as Error;
|
|
67
|
+
console.error(`Error describing image with model ${model}:`, error);
|
|
39
68
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
69
|
+
// If this is not the last model, wait before retrying
|
|
70
|
+
if (i < models.length - 1) {
|
|
71
|
+
console.log(`Waiting ${RETRY_DELAY_MS}ms before retrying with the next model...`);
|
|
72
|
+
await this.delay(RETRY_DELAY_MS); // Add delay before the next attempt
|
|
73
|
+
console.log(`Retrying with the next model...`);
|
|
74
|
+
} else {
|
|
75
|
+
// If this is the last model, throw the final error
|
|
76
|
+
console.error('All models failed to describe the image.');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
44
79
|
}
|
|
80
|
+
|
|
81
|
+
// If we reach here, all retries failed; throw the last error
|
|
82
|
+
throw lastError || new Error('Failed to generate image description after all retries');
|
|
45
83
|
}
|
|
46
|
-
}
|
|
84
|
+
}
|