import * as plugins from './plugins.js'; import * as paths from './paths.js'; import { MultiModalModel } from './abstract.classes.multimodal.js'; export interface IOpenaiProviderOptions { openaiToken: string; } export class OpenAiProvider extends MultiModalModel { private options: IOpenaiProviderOptions; public openAiApiClient: plugins.openai.default; public smartpdfInstance: plugins.smartpdf.SmartPdf; constructor(optionsArg: IOpenaiProviderOptions) { super(); this.options = optionsArg; } public async start() { this.openAiApiClient = new plugins.openai.default({ apiKey: this.options.openaiToken, dangerouslyAllowBrowser: true, }); this.smartpdfInstance = new plugins.smartpdf.SmartPdf(); } public async stop() {} public async chatStream(input: ReadableStream): Promise> { // Create a TextDecoder to handle incoming chunks const decoder = new TextDecoder(); let buffer = ''; let currentMessage: { role: string; content: string; } | null = null; // Create a TransformStream to process the input const transform = new TransformStream({ async transform(chunk, controller) { buffer += decoder.decode(chunk, { stream: true }); // Try to parse complete JSON messages from the buffer while (true) { const newlineIndex = buffer.indexOf('\n'); if (newlineIndex === -1) break; const line = buffer.slice(0, newlineIndex); buffer = buffer.slice(newlineIndex + 1); if (line.trim()) { try { const message = JSON.parse(line); currentMessage = { role: message.role || 'user', content: message.content || '', }; } catch (e) { console.error('Failed to parse message:', e); } } } // If we have a complete message, send it to OpenAI if (currentMessage) { const stream = await this.openAiApiClient.chat.completions.create({ model: 'gpt-4', messages: [{ role: currentMessage.role, content: currentMessage.content }], stream: true, }); // Process each chunk from OpenAI for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content; if (content) { controller.enqueue(content); } } currentMessage = null; } }, flush(controller) { if (buffer) { try { const message = JSON.parse(buffer); controller.enqueue(message.content || ''); } catch (e) { console.error('Failed to parse remaining buffer:', e); } } } }); // Connect the input to our transform stream return input.pipeThrough(transform); } // Implementing the synchronous chat interaction public async chat(optionsArg: { systemMessage: string; userMessage: string; messageHistory: { role: 'assistant' | 'user'; content: string; }[]; }) { const result = await this.openAiApiClient.chat.completions.create({ model: 'gpt-4o', messages: [ { role: 'system', content: optionsArg.systemMessage }, ...optionsArg.messageHistory, { role: 'user', content: optionsArg.userMessage }, ], }); return { role: result.choices[0].message.role as 'assistant', message: result.choices[0].message.content, }; } public async audio(optionsArg: { message: string }): Promise { const done = plugins.smartpromise.defer(); const result = await this.openAiApiClient.audio.speech.create({ model: 'tts-1-hd', input: optionsArg.message, voice: 'nova', response_format: 'mp3', speed: 1, }); const stream = result.body; done.resolve(stream); return done.promise; } public async document(optionsArg: { systemMessage: string; userMessage: string; pdfDocuments: Uint8Array[]; messageHistory: { role: 'assistant' | 'user'; content: any; }[]; }) { let pdfDocumentImageBytesArray: Uint8Array[] = []; for (const pdfDocument of optionsArg.pdfDocuments) { const documentImageArray = await this.smartpdfInstance.convertPDFToPngBytes(pdfDocument); pdfDocumentImageBytesArray = pdfDocumentImageBytesArray.concat(documentImageArray); } console.log(`image smartfile array`); console.log(pdfDocumentImageBytesArray.map((smartfile) => smartfile.length)); const smartfileArray = await plugins.smartarray.map( pdfDocumentImageBytesArray, async (pdfDocumentImageBytes) => { return plugins.smartfile.SmartFile.fromBuffer( 'pdfDocumentImage.jpg', Buffer.from(pdfDocumentImageBytes) ); } ); const result = await this.openAiApiClient.chat.completions.create({ model: 'gpt-4o', // response_format: { type: "json_object" }, // not supported for now messages: [ { role: 'system', content: optionsArg.systemMessage }, ...optionsArg.messageHistory, { role: 'user', content: [ { type: 'text', text: optionsArg.userMessage }, ...(() => { const returnArray = []; for (const imageBytes of pdfDocumentImageBytesArray) { returnArray.push({ type: 'image_url', image_url: { url: 'data:image/png;base64,' + Buffer.from(imageBytes).toString('base64'), }, }); } return returnArray; })(), ], }, ], }); return { message: result.choices[0].message, }; } public async vision(optionsArg: { image: Buffer; prompt: string }): Promise { const result = await this.openAiApiClient.chat.completions.create({ model: 'gpt-4-vision-preview', messages: [ { role: 'user', content: [ { type: 'text', text: optionsArg.prompt }, { type: 'image_url', image_url: { url: `data:image/jpeg;base64,${optionsArg.image.toString('base64')}` } } ] } ], max_tokens: 300 }); return result.choices[0].message.content || ''; } }