|
| 1 | +/** |
| 2 | + * Example: OpenRouter FileParserPlugin - PDF URL |
| 3 | + * |
| 4 | + * This example demonstrates sending PDFs via publicly accessible URLs. |
| 5 | + * This is more efficient than base64 encoding as you don't need to download |
| 6 | + * and encode the file. |
| 7 | + * |
| 8 | + * Key Points: |
| 9 | + * - Send PDFs directly via URL without downloading |
| 10 | + * - Works with all PDF processing engines |
| 11 | + * - Reduces payload size compared to base64 |
| 12 | + * - Ideal for publicly accessible documents |
| 13 | + * |
| 14 | + * To run: bun run typescript/fetch/src/plugin-file-parser/file-parser-pdf-url.ts |
| 15 | + */ |
| 16 | + |
| 17 | +import type { ChatCompletionResponse } from '@openrouter-examples/shared/types'; |
| 18 | + |
| 19 | +const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Example using the Bitcoin whitepaper (publicly accessible PDF) |
| 23 | + */ |
| 24 | +async function examplePDFFromURL() { |
| 25 | + console.log('╔════════════════════════════════════════════════════════════════════════════╗'); |
| 26 | + console.log('║ OpenRouter FileParserPlugin - PDF URL Example ║'); |
| 27 | + console.log('╚════════════════════════════════════════════════════════════════════════════╝'); |
| 28 | + console.log(); |
| 29 | + console.log('Sending PDF via public URL (Bitcoin whitepaper)'); |
| 30 | + console.log('URL: https://bitcoin.org/bitcoin.pdf'); |
| 31 | + console.log(); |
| 32 | + |
| 33 | + if (!process.env.OPENROUTER_API_KEY) { |
| 34 | + throw new Error('OPENROUTER_API_KEY environment variable is not set'); |
| 35 | + } |
| 36 | + |
| 37 | + const response = await fetch(OPENROUTER_API_URL, { |
| 38 | + method: 'POST', |
| 39 | + headers: { |
| 40 | + Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`, |
| 41 | + 'Content-Type': 'application/json', |
| 42 | + 'HTTP-Referer': 'https://github.com/openrouter/examples', |
| 43 | + 'X-Title': 'FileParser - PDF URL Example', |
| 44 | + }, |
| 45 | + body: JSON.stringify({ |
| 46 | + model: 'openai/gpt-4o-mini', |
| 47 | + messages: [ |
| 48 | + { |
| 49 | + role: 'user', |
| 50 | + content: [ |
| 51 | + { |
| 52 | + type: 'file', |
| 53 | + file: { |
| 54 | + filename: 'bitcoin.pdf', |
| 55 | + // Send PDF via public URL - no need to download or encode |
| 56 | + file_data: 'https://bitcoin.org/bitcoin.pdf', |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + type: 'text', |
| 61 | + text: 'What are the main points of this document? Provide a brief 2-3 sentence summary.', |
| 62 | + }, |
| 63 | + ], |
| 64 | + }, |
| 65 | + ], |
| 66 | + // Configure PDF processing engine |
| 67 | + plugins: [ |
| 68 | + { |
| 69 | + id: 'file-parser', |
| 70 | + pdf: { |
| 71 | + engine: 'mistral-ocr', // or 'pdf-text' for free tier |
| 72 | + }, |
| 73 | + }, |
| 74 | + ], |
| 75 | + max_tokens: 500, |
| 76 | + }), |
| 77 | + }); |
| 78 | + |
| 79 | + if (!response.ok) { |
| 80 | + const errorText = await response.text(); |
| 81 | + throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`); |
| 82 | + } |
| 83 | + |
| 84 | + const data = (await response.json()) as ChatCompletionResponse; |
| 85 | + |
| 86 | + console.log('✅ Request successful!'); |
| 87 | + console.log('\nModel:', data.model); |
| 88 | + console.log('Provider:', data.provider); |
| 89 | + console.log('\nSummary:'); |
| 90 | + console.log(data.choices[0].message.content); |
| 91 | + console.log('\nToken usage:'); |
| 92 | + console.log(`- Prompt tokens: ${data.usage.prompt_tokens}`); |
| 93 | + console.log(`- Completion tokens: ${data.usage.completion_tokens}`); |
| 94 | + console.log(`- Total tokens: ${data.usage.total_tokens}`); |
| 95 | + |
| 96 | + return data; |
| 97 | +} |
| 98 | + |
| 99 | +async function main() { |
| 100 | + try { |
| 101 | + await examplePDFFromURL(); |
| 102 | + } catch (error) { |
| 103 | + console.error('\n❌ Error:', error instanceof Error ? error.message : String(error)); |
| 104 | + process.exit(1); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +main(); |
0 commit comments