BREAKING CHANGE(vercel-ai-sdk): migrate to Vercel AI SDK v6 and introduce provider registry (getModel) returning LanguageModelV3
This commit is contained in:
61
ts_document/index.ts
Normal file
61
ts_document/index.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import type { LanguageModelV3 } from '@ai-sdk/provider';
|
||||
import type { ModelMessage } from 'ai';
|
||||
|
||||
let smartpdfInstance: InstanceType<typeof plugins.smartpdf.SmartPdf> | null = null;
|
||||
|
||||
async function ensureSmartpdf(): Promise<InstanceType<typeof plugins.smartpdf.SmartPdf>> {
|
||||
if (!smartpdfInstance) {
|
||||
smartpdfInstance = new plugins.smartpdf.SmartPdf();
|
||||
await smartpdfInstance.start();
|
||||
}
|
||||
return smartpdfInstance;
|
||||
}
|
||||
|
||||
export interface IDocumentOptions {
|
||||
model: LanguageModelV3;
|
||||
systemMessage?: string;
|
||||
userMessage: string;
|
||||
pdfDocuments: Uint8Array[];
|
||||
messageHistory?: ModelMessage[];
|
||||
}
|
||||
|
||||
export async function analyzeDocuments(options: IDocumentOptions): Promise<string> {
|
||||
const pdf = await ensureSmartpdf();
|
||||
|
||||
const imagePages: Uint8Array[] = [];
|
||||
for (const doc of options.pdfDocuments) {
|
||||
const pages = await pdf.convertPDFToPngBytes(doc);
|
||||
imagePages.push(...pages);
|
||||
}
|
||||
|
||||
// Filter out empty buffers
|
||||
const validPages = imagePages.filter(page => page && page.length > 0);
|
||||
|
||||
const result = await plugins.generateText({
|
||||
model: options.model,
|
||||
system: options.systemMessage,
|
||||
messages: [
|
||||
...(options.messageHistory ?? []),
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: options.userMessage },
|
||||
...validPages.map(page => ({
|
||||
type: 'image' as const,
|
||||
image: page,
|
||||
mimeType: 'image/png' as const,
|
||||
})),
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
return result.text;
|
||||
}
|
||||
|
||||
export async function stopSmartpdf(): Promise<void> {
|
||||
if (smartpdfInstance) {
|
||||
await smartpdfInstance.stop();
|
||||
smartpdfInstance = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user