Day 4 builds a document processor that reads files and uses Claude to analyze, summarize, and extract information from them.
npm install multer pdf-parseimport Anthropic from '@anthropic-ai/sdk';
import fs from 'fs/promises';
import path from 'path';
import * as dotenv from 'dotenv';
dotenv.config();
const client = new Anthropic();
async function analyzeDocument(filePath, instruction) {
const ext = path.extname(filePath).toLowerCase();
let content;
if (ext === '.txt' || ext === '.md' || ext === '.csv') {
content = await fs.readFile(filePath, 'utf-8');
} else {
throw new Error(`Unsupported file type: ${ext}`);
}
// Truncate if too long (Claude handles up to 200K tokens)
const maxChars = 180000;
if (content.length > maxChars) {
content = content.slice(0, maxChars) + '
[Content truncated...]';
}
const response = await client.messages.create({
model: 'claude-opus-4-5',
max_tokens: 2048,
messages: [{
role: 'user',
content: `Here is the document content:
${content}
---
${instruction}`
}]
});
return response.content[0].text;
}
// Example usage
const summary = await analyzeDocument('./report.txt',
'Summarize this document in 5 bullet points.');
console.log(summary);
const extraction = await analyzeDocument('./data.csv',
'List all unique values in the "category" column.');import multer from 'multer';
const upload = multer({ dest: 'uploads/', limits: { fileSize: 10 * 1024 * 1024 } });
app.post('/analyze', upload.single('document'), async (req, res) => {
if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
const { instruction = 'Summarize this document.' } = req.body;
try {
const content = await fs.readFile(req.file.path, 'utf-8');
const response = await client.messages.create({
model: 'claude-opus-4-5',
max_tokens: 2048,
messages: [{
role: 'user',
content: `Document:
${content}
Task: ${instruction}`
}]
});
// Clean up temp file
await fs.unlink(req.file.path);
res.json({ result: response.content[0].text });
} catch (error) {
res.status(500).json({ error: error.message });
}
});