-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvision-example.ts
More file actions
167 lines (135 loc) · 4.69 KB
/
Copy pathvision-example.ts
File metadata and controls
167 lines (135 loc) · 4.69 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { initAIHooks, wrap, type MultimodalInput } from 'npm-ai-hooks';
import * as fs from 'fs';
import * as path from 'path';
// Initialize with your API key
initAIHooks({
providers: [
{
provider: 'openai',
key: process.env.OPENAI_KEY || 'your-openai-key'
}
]
});
// Example 1: Image Analysis
async function analyzeImage(imagePath: string) {
// Read image and convert to base64
const imageBuffer = fs.readFileSync(imagePath);
const base64Image = `data:image/jpeg;base64,${imageBuffer.toString('base64')}`;
// Create a multimodal input function
const analyzeImageFn = wrap((input: MultimodalInput) => input, {
provider: 'openai',
model: 'gpt-4o', // GPT-4 with vision
customPrompt: 'Describe what you see in this image in detail.'
});
const result = await analyzeImageFn({
text: 'Please analyze this image',
image: base64Image
});
console.log('Image Analysis:', result.output);
}
// Example 2: OCR (Text Extraction from Image)
async function extractTextFromImage(imagePath: string) {
const imageBuffer = fs.readFileSync(imagePath);
const base64Image = `data:image/png;base64,${imageBuffer.toString('base64')}`;
const ocrFn = wrap((input: MultimodalInput) => input, {
provider: 'openai',
model: 'gpt-4o',
customPrompt: 'Extract all text from this image. Return only the text content.'
});
const result = await ocrFn({
text: 'Extract text from this document',
image: base64Image
});
console.log('Extracted Text:', result.output);
}
// Example 3: Image + Code Review
async function reviewCodeScreenshot(imagePath: string) {
const imageBuffer = fs.readFileSync(imagePath);
const base64Image = `data:image/jpeg;base64,${imageBuffer.toString('base64')}`;
const reviewFn = wrap((input: MultimodalInput) => input, {
provider: 'openai',
model: 'gpt-4o',
task: 'codeReview'
});
const result = await reviewFn({
text: 'Review the code in this screenshot',
image: base64Image
});
console.log('Code Review:', result.output);
}
// Example 4: File Content Analysis
async function analyzeDocument(filePath: string) {
const fileBuffer = fs.readFileSync(filePath);
const base64File = fileBuffer.toString('base64');
const fileName = path.basename(filePath);
const mimeType = fileName.endsWith('.pdf') ? 'application/pdf' : 'text/plain';
const analyzeFn = wrap((input: MultimodalInput) => input, {
provider: 'openai',
model: 'gpt-4o',
task: 'summarize'
});
const result = await analyzeFn({
text: 'Summarize this document',
file: {
name: fileName,
data: `data:${mimeType};base64,${base64File}`,
type: mimeType
}
});
console.log('Document Summary:', result.output);
}
// Example 5: Multiple Images Comparison (Sequential)
async function compareImages(image1Path: string, image2Path: string) {
const img1Buffer = fs.readFileSync(image1Path);
const img2Buffer = fs.readFileSync(image2Path);
const base64Image1 = `data:image/jpeg;base64,${img1Buffer.toString('base64')}`;
const base64Image2 = `data:image/jpeg;base64,${img2Buffer.toString('base64')}`;
const compareFn = wrap((input: MultimodalInput) => input, {
provider: 'openai',
model: 'gpt-4o',
customPrompt: 'Compare these two images and describe the differences.'
});
// Note: This sends one image at a time. For actual multi-image support,
// you'd need to extend the library or use provider-specific features
console.log('Analyzing first image...');
const result1 = await compareFn({
text: 'First image',
image: base64Image1
});
console.log('Analyzing second image...');
const result2 = await compareFn({
text: 'Second image',
image: base64Image2
});
console.log('Image 1 Analysis:', result1.output);
console.log('Image 2 Analysis:', result2.output);
}
// Run examples
async function main() {
try {
console.log('=== Image Analysis Example ===');
// await analyzeImage('./test-image.jpg');
console.log('\n=== OCR Example ===');
// await extractTextFromImage('./document.png');
console.log('\n=== Code Review from Screenshot ===');
// await reviewCodeScreenshot('./code-screenshot.png');
console.log('\n=== Document Analysis Example ===');
// await analyzeDocument('./document.pdf');
console.log('\n=== Image Comparison Example ===');
// await compareImages('./image1.jpg', './image2.jpg');
console.log('\nAll examples completed!');
console.log('\nNote: Uncomment the function calls and provide actual image paths to run.');
} catch (error) {
console.error('Error:', error);
}
}
// Uncomment to run
// main();
export {
analyzeImage,
extractTextFromImage,
reviewCodeScreenshot,
analyzeDocument,
compareImages,
main
};