Prompt engineering text similarity matchers for Jest
npm install @testyourai/jestTest Your AI prompts like a robot not a human! Testing and developing prompts has traditionally been tricky because the output a typical LLM creates is fuzzy and non deterministic but with @testyourai/jest you can manage test regressions using Jest.
``ts`
await expect("What a fine day!").toBeMoreSimilarTo(
"This weather is simply delightful.",
"It is a fine but weathered day bed."
);
This is a set of Jest matchers that use OpenAI embeddings to test for semantic similarities between strings. With these matchers, you can easily test if two strings are similar in meaning or compare the similarity of one string to multiple others.
To use these matchers, simply import the setupMatchers function and pass the output to expect.extend in your setup file or before your tests. Then simply use them with Jest's built-in expect function.
`ts
import {setupMatchers} from "@testyourai/jest";
import {expect} from "@jest/globals";
// Setup your matchers before your tests run
expect.extend(setupMatchers({
openAiApiKey: process.env.OPENAI_API_KEY, // required
model: "text-embedding-ada-002" // optional
}));
`
`ts
describe('String similarity tests', () => {
test('Hello -> Hi', async () => {
await expect('Hello').toBeSimilarTo('Hi');
});
test('Hello -> Yellow', async () => {
await expect('Hello').toBeSimilarTo('Yellow', 0.8);
});
test('Hello is more similar to Hi than Whistle or Lion', async () => {
await expect('Hello').toBeMoreSimilarTo('Hi', ['Whistle', 'Lion']);
});
});
`
You can install this package using your favourite npm client:
| Client | Command |
|---|---|
| npm | npm install --save-dev @testyourai/jest | pnpm add -D @testyourai/jest
| pnpm | | yarn add -D @testyourai/jest
| yarn | |
You must use jest in ESM mode for now. This will change in future releases. To learn how to setup Jest in ECMAScript Module mode have a look here: https://jestjs.io/docs/ecmascript-modules
Checks if the tested string is more similar in meaning to the expected string than it is to any of the other strings provided. The others parameter can be a single string or an array of strings.
#### Example:
`ts
test('Hello is more similar to Hi than Whistle or Lion', async () => {
await expect('Hello').toBeMoreSimilarTo('Hi', ['Whistle', 'Lion']);
});
`
Checks if the tested string is similar in meaning to the expected string. The optional threshold parameter sets the minimum similarity score required for the test to pass (default is 0.85).
#### Example:
`ts`
test('Hello -> Hi', async () => {
await expect('Hello').toBeSimilarTo('Hi');
});
All embedding calls are only made once to save tokens and are cached to a file: /tmp/.tyai-embeddings.json. One day this behaviour will be configurable and optional but that day is not today.
Contributions are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository. If you'd like to contribute code, please fork the repository and submit a pull request.
1. Clone this repository
1. Install dependencies pnpm install.env.example
2. Copy to .env file and supply your OPENAI_API_KEYpnpm test`
3. Run tests by