Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
synw committed Oct 27, 2023
1 parent ca65848 commit 020c535
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
20 changes: 20 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from '@jest/types';
// Sync object
const config: Config.InitialOptions = {
verbose: true,
preset: 'ts-jest/presets/default-esm', // or other ESM presets
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
};
export default config;
42 changes: 42 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { templates, PromptTemplate } from "../src/main";

describe('templates', () => {
it('base', async () => {
const tpl = new PromptTemplate(templates.alpaca);
expect(tpl.name).toBe("Alpaca");
expect(tpl.user).toBe("### Instruction:\n{prompt}");
expect(tpl.assistant).toBe("### Response:");
});
it('system', async () => {
const tpl = new PromptTemplate(templates.alpaca);
expect(tpl.system?.schema).toBe("{system}")
const sysMsg = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
expect(tpl.system?.message).toBe(sysMsg);
tpl.afterSystem("AFTER");
expect(tpl.render()).toContain(sysMsg + "AFTER");
tpl.replaceSystem("NEW SYS");
const txt = tpl.render()
expect(txt).not.toContain(sysMsg);
expect(txt.startsWith("NEW SYS")).toBeTruthy()
});
it('shots', async () => {
const tpl = new PromptTemplate(templates.human_response);
tpl.addShot("2+2", "4");
const txt = `### HUMAN:
2+2
### RESPONSE:
4
### HUMAN:
{prompt}
### RESPONSE:
`
expect(tpl.render()).toBe(txt);
const ntpl = tpl.cloneTo("mistral");
const newtxt = `<s>[INST] 2+2 [/INST]4
[INST] {prompt} [/INST]`;
expect(ntpl.render()).toBe(newtxt)
});
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"dom"
],
"types": [
"node"
"node",
"jest"
],
"baseUrl": ".",
"paths": {
Expand Down

0 comments on commit 020c535

Please sign in to comment.