Working with images and multimodal inputs
import { Sudo } from "sudo-ai";
const sudo = new Sudo({
serverURL: "https://sudoapp.dev/api",
apiKey: process.env.SUDO_API_KEY ?? "",
});
async function analyzeImageFromUrl() {
try {
const response = await sudo.router.create({
model: "gpt-4o", // Vision-capable model
messages: [
{
role: "user",
content: [
{
type: "text",
text: "What do you see in this image?"
},
{
type: "image_url",
imageUrl: {
url: "https://example.com/image.jpg"
}
}
]
}
]
});
console.log(response.choices[0].message.content);
} catch (error) {
console.error("Image analysis error:", error);
}
}
analyzeImageFromUrl();
import { Sudo } from "sudo-ai";
import * as fs from 'fs';
import * as path from 'path';
const sudo = new Sudo({
serverURL: "https://sudoapp.dev/api",
apiKey: process.env.SUDO_API_KEY ?? "",
});
function encodeImageToBase64(imagePath: string): string {
try {
const imageBuffer = fs.readFileSync(imagePath);
return imageBuffer.toString('base64');
} catch (error) {
throw new Error(`Failed to read image file: ${error}`);
}
}
async function analyzeLocalImage(imagePath: string, prompt: string = "Describe this image"): Promise<string | null> {
try {
// Encode image to base64
const base64Image = encodeImageToBase64(imagePath);
// Determine image type from file extension
const ext = path.extname(imagePath).toLowerCase();
const mimeType = ext === '.png' ? 'image/png' :
ext === '.webp' ? 'image/webp' :
'image/jpeg';
const response = await sudo.router.create({
model: "gpt-4o",
messages: [
{
role: "user",
content: [
{
type: "text",
text: prompt
},
{
type: "image_url",
imageUrl: {
url: `data:${mimeType};base64,${base64Image}`
}
}
]
}
]
});
return response.choices[0].message.content;
} catch (error) {
console.error("Local image analysis error:", error);
return null;
}
}
// Usage
async function example() {
const description = await analyzeLocalImage("path/to/your/image.jpg", "What objects can you identify in this image?");
if (description) {
console.log(description);
}
}
example();
import { Sudo } from "sudo-ai";
import * as fs from 'fs';
import * as path from 'path';
const sudo = new Sudo({
serverURL: "https://sudoapp.dev/api",
apiKey: process.env.SUDO_API_KEY ?? "",
});
function encodeImage(imagePath: string): string {
const imageBuffer = fs.readFileSync(imagePath);
return imageBuffer.toString('base64');
}
function getMimeType(imagePath: string): string {
const ext = path.extname(imagePath).toLowerCase();
switch (ext) {
case '.png': return 'image/png';
case '.webp': return 'image/webp';
case '.gif': return 'image/gif';
default: return 'image/jpeg';
}
}
async function compareImages(imagePaths: string[], prompt: string = "Compare these images"): Promise<string | null> {
try {
// Build content with text and multiple images
const content: any[] = [{ type: "text", text: prompt }];
for (const imagePath of imagePaths) {
const base64Image = encodeImage(imagePath);
const mimeType = getMimeType(imagePath);
content.push({
type: "image_url",
imageUrl: {
url: `data:${mimeType};base64,${base64Image}`
}
});
}
const response = await sudo.router.create({
model: "gpt-4o",
messages: [{ role: "user", content: content }]
});
return response.choices[0].message.content;
} catch (error) {
console.error("Image comparison error:", error);
return null;
}
}
// Compare two images
async function imageComparisonExample() {
const comparison = await compareImages(
["image1.jpg", "image2.jpg"],
"What are the main differences between these two images?"
);
if (comparison) {
console.log(comparison);
}
}
imageComparisonExample();