Rewrite frontend as single self-contained HTML file — all CSS/JS inline, no external files to fail loading
This commit is contained in:
182
dist/providers/vision/openAIVisionProvider.js
vendored
Normal file
182
dist/providers/vision/openAIVisionProvider.js
vendored
Normal file
@@ -0,0 +1,182 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.OpenAIVisionProvider = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const openai_1 = require("openai");
|
||||
/**
|
||||
* OpenAI Vision Provider Implementation
|
||||
*/
|
||||
class OpenAIVisionProvider {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
this.openai = new openai_1.OpenAI({
|
||||
apiKey: config.apiKey,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Describe a single image
|
||||
* @param imagePath - Path to the image file
|
||||
* @param prompt - Prompt for the AI
|
||||
* @returns Description and usage stats
|
||||
*/
|
||||
async describeImage(imagePath, prompt) {
|
||||
try {
|
||||
const imageData = fs_1.default.readFileSync(imagePath);
|
||||
const base64Image = imageData.toString('base64');
|
||||
const response = await this.openai.chat.completions.create({
|
||||
model: this.config.model,
|
||||
temperature: 0.1,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: prompt },
|
||||
{
|
||||
type: "image_url",
|
||||
image_url: {
|
||||
url: `data:image/jpeg;base64,${base64Image}`
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
max_completion_tokens: this.config.maxTokens || 300
|
||||
});
|
||||
return {
|
||||
description: response.choices[0].message.content?.trim() || "No description generated.",
|
||||
usage: {
|
||||
inputTokens: response.usage?.prompt_tokens || 0,
|
||||
outputTokens: response.usage?.completion_tokens || 0,
|
||||
totalTokens: response.usage?.total_tokens || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error describing image:", error);
|
||||
return {
|
||||
description: "Unable to describe this image.",
|
||||
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
|
||||
};
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Compare two images and describe the differences
|
||||
* @param image1Path - Path to the first image
|
||||
* @param image2Path - Path to the second image
|
||||
* @param prompt - Prompt for the AI
|
||||
* @returns Description and usage stats
|
||||
*/
|
||||
async compareImages(image1Path, image2Path, prompt) {
|
||||
try {
|
||||
const image1Data = fs_1.default.readFileSync(image1Path);
|
||||
const image2Data = fs_1.default.readFileSync(image2Path);
|
||||
const base64Image1 = image1Data.toString('base64');
|
||||
const base64Image2 = image2Data.toString('base64');
|
||||
const response = await this.openai.chat.completions.create({
|
||||
model: this.config.model,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: prompt },
|
||||
{
|
||||
type: "image_url",
|
||||
image_url: {
|
||||
url: `data:image/jpeg;base64,${base64Image1}`
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "image_url",
|
||||
image_url: {
|
||||
url: `data:image/jpeg;base64,${base64Image2}`
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
max_completion_tokens: this.config.maxTokens || 300
|
||||
});
|
||||
return {
|
||||
description: response.choices[0].message.content?.trim() || "No description generated.",
|
||||
usage: {
|
||||
inputTokens: response.usage?.prompt_tokens || 0,
|
||||
outputTokens: response.usage?.completion_tokens || 0,
|
||||
totalTokens: response.usage?.total_tokens || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error comparing images:", error);
|
||||
return {
|
||||
description: "Unable to describe the differences between these images.",
|
||||
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
|
||||
};
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Describe a batch of images
|
||||
* @param imagePaths - Array of paths to the images
|
||||
* @param lastBatchContext - Context from the previous batch
|
||||
* @param prompt - Prompt for the AI
|
||||
* @returns Description and usage stats
|
||||
*/
|
||||
async describeBatch(imagePaths, lastBatchContext, prompt) {
|
||||
try {
|
||||
// Convert images to base64
|
||||
const imagesBase64 = imagePaths.map(fp => {
|
||||
const imageData = fs_1.default.readFileSync(fp);
|
||||
return imageData.toString('base64');
|
||||
});
|
||||
// Build the messages array for the chat completion
|
||||
const messages = [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: prompt }
|
||||
]
|
||||
}
|
||||
];
|
||||
// If we have some text context from the last batch, inject that as well
|
||||
if (lastBatchContext && lastBatchContext.lastDescription) {
|
||||
messages.unshift({
|
||||
role: "system",
|
||||
content: `Previous batch summary: ${lastBatchContext.lastDescription}`
|
||||
});
|
||||
}
|
||||
// Append each image in the new batch
|
||||
imagesBase64.forEach(base64 => {
|
||||
messages[messages.length - 1].content.push({
|
||||
type: "image_url",
|
||||
image_url: {
|
||||
url: `data:image/jpeg;base64,${base64}`
|
||||
}
|
||||
});
|
||||
});
|
||||
const response = await this.openai.chat.completions.create({
|
||||
model: this.config.model,
|
||||
messages,
|
||||
max_completion_tokens: this.config.maxTokens || 300
|
||||
});
|
||||
return {
|
||||
description: response.choices[0].message.content?.trim() || "No description generated.",
|
||||
usage: {
|
||||
inputTokens: response.usage?.prompt_tokens || 0,
|
||||
outputTokens: response.usage?.completion_tokens || 0,
|
||||
totalTokens: response.usage?.total_tokens || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
console.error("Error describing batch of images:", error);
|
||||
return {
|
||||
description: "Unable to describe this batch of images.",
|
||||
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 }
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.OpenAIVisionProvider = OpenAIVisionProvider;
|
||||
//# sourceMappingURL=openAIVisionProvider.js.map
|
||||
Reference in New Issue
Block a user