77 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { expect, tap } from '@push.rocks/tapbundle';
 | 
						|
import * as qenv from '@push.rocks/qenv';
 | 
						|
import * as smartrequest from '@push.rocks/smartrequest';
 | 
						|
import * as smartfile from '@push.rocks/smartfile';
 | 
						|
 | 
						|
const testQenv = new qenv.Qenv('./', './.nogit/');
 | 
						|
 | 
						|
import * as smartai from '../ts/index.js';
 | 
						|
 | 
						|
let testSmartai: smartai.SmartAi;
 | 
						|
 | 
						|
tap.test('OpenAI Document: should create a smartai instance with OpenAI provider', async () => {
 | 
						|
  testSmartai = new smartai.SmartAi({
 | 
						|
    openaiToken: await testQenv.getEnvVarOnDemand('OPENAI_TOKEN'),
 | 
						|
  });
 | 
						|
  await testSmartai.start();
 | 
						|
});
 | 
						|
 | 
						|
tap.test('OpenAI Document: should document a pdf', async () => {
 | 
						|
  const pdfUrl = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
 | 
						|
  const pdfResponse = await smartrequest.SmartRequest.create()
 | 
						|
    .url(pdfUrl)
 | 
						|
    .get();
 | 
						|
  const result = await testSmartai.openaiProvider.document({
 | 
						|
    systemMessage: 'Classify the document. Only the following answers are allowed: "invoice", "bank account statement", "contract", "other". The answer should only contain the keyword for machine use.',
 | 
						|
    userMessage: "Classify the document.",
 | 
						|
    messageHistory: [],
 | 
						|
    pdfDocuments: [Buffer.from(await pdfResponse.arrayBuffer())],
 | 
						|
  });
 | 
						|
  console.log(result);
 | 
						|
  expect(result.message).toBeTruthy();
 | 
						|
});
 | 
						|
 | 
						|
tap.test('OpenAI Document: should recognize companies in a pdf', async () => {
 | 
						|
  const pdfBuffer = await smartfile.fs.toBuffer('./.nogit/demo_without_textlayer.pdf');
 | 
						|
  const result = await testSmartai.openaiProvider.document({
 | 
						|
    systemMessage: `
 | 
						|
      summarize the document.
 | 
						|
 | 
						|
      answer in JSON format, adhering to the following schema:
 | 
						|
      \`\`\`typescript
 | 
						|
        type TAnswer = {
 | 
						|
          entitySender: {
 | 
						|
            type: 'official state entity' | 'company' | 'person';
 | 
						|
            name: string;
 | 
						|
            address: string;
 | 
						|
            city: string;
 | 
						|
            country: string;
 | 
						|
            EU: boolean; // whether the entity is within EU
 | 
						|
          };
 | 
						|
          entityReceiver: {
 | 
						|
            type: 'official state entity' | 'company' | 'person';
 | 
						|
            name: string;
 | 
						|
            address: string;
 | 
						|
            city: string;
 | 
						|
            country: string;
 | 
						|
            EU: boolean; // whether the entity is within EU
 | 
						|
          };
 | 
						|
          date: string; // the date of the document as YYYY-MM-DD
 | 
						|
          title: string; // a short title, suitable for a filename
 | 
						|
        }
 | 
						|
      \`\`\`
 | 
						|
    `,
 | 
						|
    userMessage: "Classify the document.",
 | 
						|
    messageHistory: [],
 | 
						|
    pdfDocuments: [pdfBuffer],
 | 
						|
  });
 | 
						|
  console.log(result);
 | 
						|
  expect(result.message).toBeTruthy();
 | 
						|
});
 | 
						|
 | 
						|
tap.test('OpenAI Document: should stop the smartai instance', async () => {
 | 
						|
  await testSmartai.stop();
 | 
						|
});
 | 
						|
 | 
						|
export default tap.start();
 |