Skip to content

Commit

Permalink
Update OpenAI tests style and structure
Browse files Browse the repository at this point in the history
PR-URL: #5
  • Loading branch information
timursevimli authored May 17, 2024
2 parents 7263792 + 4e57930 commit 2f29e9d
Show file tree
Hide file tree
Showing 10 changed files with 477 additions and 403 deletions.
403 changes: 0 additions & 403 deletions test/openai.js

This file was deleted.

40 changes: 40 additions & 0 deletions test/openai/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const { beforeEach, it, describe } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');

const { openai } = require('../../lib');

const { Chat } = openai;
const API_KEY = process.env.OPENAI_API_KEY;

const FILES = path.join(__dirname, '../../files');
const OPEN_AI_FILES = path.join(FILES, 'openai');
const AUDIOS = path.join(OPEN_AI_FILES, 'audios');

describe('Chat Operations', () => {
let chat = null;
beforeEach(() => {
chat = new Chat({ apiKey: API_KEY });
});

it('Chat message', async () => {
const res = await chat.message({ text: 'Hello' });

assert.strictEqual(typeof res, 'string');
});

it('Chat voice message', async () => {
const res = await chat.voiceMessage({
inputFilePath: AUDIOS + '/test-speech-input-en.mp3',
outputFilePath: AUDIOS + '/my_test-speech-output-en.mp3',
returnIntermediateResult: false,
});

assert.ok('inputText' in res);
assert.strictEqual(res.inputText, 'Hello there.');
assert.ok('outputText' in res);
assert.ok('outputFilePath' in res);
});
});
88 changes: 88 additions & 0 deletions test/openai/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict';

const { beforeEach, it, describe } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');

const { openai } = require('../../lib');
const utils = require('../../lib/openai/utils');
const { tested } = require('./tested.js');

const FILES = path.join(__dirname, '../../files');
const OPEN_AI_FILES = path.join(FILES, 'openai');
const FINE_TUNE = path.join(OPEN_AI_FILES, 'fine-tune');
const ASSISTANTS = path.join(OPEN_AI_FILES, 'assistants');

const { Chat } = openai;
const API_KEY = process.env.OPENAI_API_KEY;

describe('File Operations', () => {
let files;
beforeEach(() => {
const chat = new Chat({ apiKey: API_KEY });
files = utils.files(chat.openai);
});

it('Count file tokens', async () => {
const res = await files.countFileTokens({
pathToFile: FINE_TUNE + '/test-fine-tune-24.jsonl',
purpose: 'fine-tune',
});

assert.strictEqual(res, 1889);
});

it('Create fine-tune file', async () => {
const res = await files.create({
pathToFile: FINE_TUNE + '/test-fine-tune-24.jsonl',
purpose: 'fine-tune',
});

assert.ok('id' in res);
assert.strictEqual(res.status, 'processed');

tested.fineTune.file.id = res.id;
});

it('Create assistant file', async () => {
const res = await files.create({
pathToFile: ASSISTANTS + '/test.csv',
purpose: 'assistants',
});

assert.ok('id' in res);
assert.strictEqual(res.status, 'processed');

tested.assistant.file.id = res.id;
});

it('List files', async () => {
const res = await files.list();

assert.ok(Array.isArray(res));
});

it('Retrieve file', async () => {
const file_id = tested.fineTune.file.id;
const res = await files.retrieve({ file_id });

assert.ok('id' in res);
assert.strictEqual(res.id, file_id);
});

it('Retrieve file content', async () => {
const file_id = tested.fineTune.file.id;
const res = await files.content({ file_id });

assert.strictEqual(typeof res, 'string');
});

it('Delete file', async () => {
const file_id = tested.fineTune.file.id;
const res = await files.del({ file_id });

assert.ok('id' in res);
assert.strictEqual(res.id, file_id);
assert.strictEqual(res.deleted, true);
});
});
62 changes: 62 additions & 0 deletions test/openai/finetune.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

const { beforeEach, it, describe } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');

const { openai } = require('../../lib');
const utils = require('../../lib/openai/utils');
const { tested } = require('./tested.js');

const FILES = path.join(__dirname, '../../files');
const OPEN_AI_FILES = path.join(FILES, 'openai');
const FINE_TUNE = path.join(OPEN_AI_FILES, 'fine-tune');

const { Chat } = openai;
const API_KEY = process.env.OPENAI_API_KEY;

describe('Fine Tune Operations', () => {
let fineTune = null;
beforeEach(() => {
const chat = new Chat({ apiKey: API_KEY });
fineTune = utils.fineTune(chat.openai);
});

it('Create Fine Tune job', async () => {
const res = await fineTune.create({
pathToFile: FINE_TUNE + '/test-fine-tune-24.jsonl',
});

assert.ok('id' in res);
assert.deepEqual(res.error, {});

tested.fineTune.id = res.id;
tested.fineTune.file.id = res.training_file;
});

it('Create Fine Tune from training file', async () => {
const file_id = tested.fineTune.file.id;
const res = await fineTune.create({
training_file: file_id,
});

assert.ok('id' in res);
assert.deepEqual(res.error, {});

tested.fineTune.id = res.id;
});

it('List Fine Tune jobs', async () => {
const res = await fineTune.list();

assert.ok(Array.isArray(res));
});

it('Retrieve Fine Tune job', async () => {
const ftJobId = tested.fineTune.id;
const res = await fineTune.retrieve({ id: ftJobId });

assert.ok('id' in res);
assert.strictEqual(res.id, ftJobId);
});
});
60 changes: 60 additions & 0 deletions test/openai/images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

const { beforeEach, it, describe } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');

const { openai } = require('../../lib');
const utils = require('../../lib/openai/utils');

const FILES = path.join(__dirname, '../../files');
const OPEN_AI_FILES = path.join(FILES, 'openai');
const IMAGES = path.join(OPEN_AI_FILES, 'images');

const { Chat } = openai;
const API_KEY = process.env.OPENAI_API_KEY;

describe('Image Operations', () => {
let images = null;
beforeEach(() => {
const chat = new Chat({ apiKey: API_KEY });
images = utils.images(chat.openai);
});

it('Image Generation', async () => {
const saveAs = IMAGES + '/my_test-image-create-result.jpg';

const res = await images.create({
text: 'a white siamese cat',
saveAs,
});

assert.ok('url' in res);
assert.ok('local' in res);
});

it('Image Edit', async () => {
const saveAs = IMAGES + '/my_test-edit-image-result.jpg';

const res = await images.edit({
text: 'A futuristic landscape behind a foregraund emoticon',
pathToFile: IMAGES + '/test-edit-image.png',
pathToMask: IMAGES + '/test-edit-image.png',
saveAs,
size: '256x256',
});

assert.ok('url' in res);
assert.ok('local' in res);
});

it('Image Variation', async () => {
const pathToFile = IMAGES + '/test-edit-image.png';
const saveAs = IMAGES + '/my_test-image-variation-result.jpg';

const res = await images.variation({ pathToFile, saveAs });

assert.ok('url' in res);
assert.ok('local' in res);
});
});
61 changes: 61 additions & 0 deletions test/openai/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const { beforeEach, it, describe } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');

const { openai } = require('../../lib');
const utils = require('../../lib/openai/utils');

const FILES = path.join(__dirname, '../../files');
const OPEN_AI_FILES = path.join(FILES, 'openai');
const TOOLS = path.join(OPEN_AI_FILES, 'tools');
const TEST_LIBRARY = require(TOOLS + '/test-library.js');

const { Chat } = openai;
const API_KEY = process.env.OPENAI_API_KEY;

describe('Chat Operations', () => {
let language = null;
beforeEach(() => {
const chat = new Chat({ apiKey: API_KEY });
language = utils.language(chat.openai);
});

it('Text completion', async () => {
const res = await language.generate({ text: 'hello' });

assert.ok('message' in res);
assert.ok('messages' in res);
assert.ok('usage' in res);
});

it('Text completion with function call', async () => {
const res = await language.generate({
text: 'What is the weather like in San Francisco, Tokyo and Paris?',
tools: TEST_LIBRARY.tools,
});

assert.ok('message' in res);
assert.ok('messages' in res);
assert.ok('usage' in res);
});

it('Text Embedding', async () => {
const res = await language.embedding({ text: 'hello' });

assert.ok('embedding' in res);
assert.ok('usage' in res);
});

// WARN: 400 Invalid value for 'model' = text-moderation-007
it.skip('Text Classification', async () => {
const res = await language.classification({
text: 'I will kill you boatman',
});

assert.strictEqual(res.flagged, true);
assert.strictEqual(res.categories.violence, true);
assert.ok('violence' in res.category_scores);
});
});
45 changes: 45 additions & 0 deletions test/openai/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const { beforeEach, it, describe } = require('node:test');
const assert = require('node:assert');

const { openai } = require('../../lib');
const utils = require('../../lib/openai/utils');
const { tested } = require('./tested.js');

const { Chat } = openai;
const API_KEY = process.env.OPENAI_API_KEY;

const modelId = 'gpt-3.5-turbo-16k';

describe('Model Operations', () => {
let models = null;
beforeEach(() => {
const chat = new Chat({ apiKey: API_KEY });
models = utils.models(chat.openai);
});

it('List models', async () => {
const res = await models.list();

assert.ok(Array.isArray(res));

const custom_model = res.find((model) => model.id.startsWith('ft'));
tested.model.id = custom_model.id;
});

it('Retrieve model', async () => {
const res = await models.retrieve({ model_id: modelId });

assert.ok('id' in res);
assert.strictEqual(res.id, modelId);
});

// WARN: 403 You have insufficient permissions for this operation
it.skip('Delete model', async () => {
const res = await models.del({ model_id: modelId });

assert.ok('deleted' in res);
assert.ok(res.deleted);
});
});
31 changes: 31 additions & 0 deletions test/openai/recognition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const { beforeEach, it, describe } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');

const { openai } = require('../../lib');
const utils = require('../../lib/openai/utils');

const FILES = path.join(__dirname, '../../files');
const OPEN_AI_FILES = path.join(FILES, 'openai');
const IMAGES = path.join(OPEN_AI_FILES, 'images');

const { Chat } = openai;
const API_KEY = process.env.OPENAI_API_KEY;

describe('Recognition Operations', () => {
let recognition = null;
beforeEach(() => {
const chat = new Chat({ apiKey: API_KEY });
recognition = utils.recognition(chat.openai);
});

it('Image Recognition', async () => {
const pathToFile = IMAGES + '/test-image.jpg';
const res = await recognition.image({ pathToFile });

assert.ok('message' in res);
assert.ok('usage' in res);
});
});
Loading

0 comments on commit 2f29e9d

Please sign in to comment.