smartai/test/test.ts

85 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-03-30 11:42:44 +00:00
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
2024-04-25 08:49:07 +00:00
import * as qenv from '@push.rocks/qenv';
2024-04-27 10:47:49 +00:00
import * as smartrequest from '@push.rocks/smartrequest';
import * as smartfile from '@push.rocks/smartfile';
2024-03-30 11:42:44 +00:00
2024-04-25 08:49:07 +00:00
const testQenv = new qenv.Qenv('./', './.nogit/');
import * as smartai from '../ts/index.js';
let testSmartai: smartai.SmartAi;
tap.test('should create a smartai instance', async () => {
testSmartai = new smartai.SmartAi({
openaiToken: await testQenv.getEnvVarOnDemand('OPENAI_TOKEN'),
});
2024-04-27 10:47:49 +00:00
await testSmartai.start();
});
tap.test('should create chat response with openai', async () => {
const userMessage = 'How are you?';
const response = await testSmartai.openaiProvider.chat({
systemMessage: 'Hello',
userMessage: userMessage,
messageHistory: [
],
});
console.log(`userMessage: ${userMessage}`);
console.log(response.message.content);
});
tap.test('should document a pdf', async () => {
const pdfUrl = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
const pdfResponse = await smartrequest.getBinary(pdfUrl);
const result = await testSmartai.openaiProvider.document({
systemMessage: 'Classify the document. Only the following answers are allowed: "invoice", "bank account statement", "contract", "other"',
userMessage: "Classify the document.",
messageHistory: [],
pdfDocuments: [pdfResponse.body],
});
console.log(result);
});
tap.test('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; // wether the entity is within EU
};
entityReceiver: {
type: 'official state entity' | 'company' | 'person';
name: string;
address: string;
city: string;
country: string;
EU: boolean; // wether 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);
2024-03-30 11:42:44 +00:00
})
2024-04-27 10:47:49 +00:00
tap.test('should stop the smartai instance', async () => {
await testSmartai.stop();
});
export default tap.start();