npm package: @flowprompter/jest
Description: Prompt engineering toolkit for Jest
Latest version: 0.0.10

<p align="center">
  <a href="https://flowprompter.ai">
    <picture>
      <source media="(prefers-color-scheme: light)" srcset="assets/FLOGO.svg">
      <img src="assets/FLOGO.svg" height="128">
    </picture>
    <h1 align="center">FlowPrompter.ai</h1>
  </a>
</p>

<p align="center">
  <a aria-label="NPM version" href="https://www.npmjs.com/package/@flowprompter/jest">
    <img alt="" src="https://img.shields.io/npm/v/@flowprompter/jest?style=for-the-badge&labelColor=000000">
  </a>
  <a arial-label="Tests Passing" href="https://img.shields.io/github/actions/workflow/status/ryardley/flowprompter/main.yml">
    <img alt="" src="https://img.shields.io/github/actions/workflow/status/ryardley/flowprompter/main.yml?style=for-the-badge&labelColor=000000">
  </a>
  <a aria-label="License" href="https://github.com/ryardley/flowprompter-jest/blob/main/license.md">
    <img alt="" src="https://img.shields.io/npm/l/@flowprompter/jest?style=for-the-badge&labelColor=000000">
  </a>
  <a aria-label="Join the community on GitHub" href="https://github.com/ryardley/flowprompter-jest/discussions">
    <img alt="" src="https://img.shields.io/badge/Join%20the%20community-black.svg?style=for-the-badge&labelColor=000000&logoWidth=20">
  </a>
  
</p>

## About Flow Prompter

Testing and developing prompts has traditionally been tricky because the output a typical LLM creates is fuzzy and non deterministic but with Floe Prompter you can simply manage test regressions using [Jest](https://jestjs.io) without worrying about output variants that throw your tests off. This works because Flow Prompter delivers a set of Jest matchers that use 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 your prompt outputs.

```ts
await expect("What a fine day!").toBeMoreSimilarTo(
  "This weather is simply delightful.",
  "It is a fine but weathered day bed."
);
```

## Getting Started

You can install this package using your favourite npm client:


| Client | Command |
|---|---|
| npm | `npm install --save-dev @flowprompter/jest` | 
| pnpm | `pnpm add -D @flowprompter/jest` | 
| yarn | `yarn add -D @flowprompter/jest` | 

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 "@flowprompter/jest"; 

// Setup your matchers before your tests run
expect.extend(setupMatchers({
  openAiApiKey: 'sk-abc123def456...987xyz'
}));
```

### Usage

```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']);
  });
});
```

## API Reference

### `toBeMoreSimilarTo(expected: string, others: string[] | string)`

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']);
});

```

### `toBeSimilarTo(expected: string, threshold?: number)`

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');
});
```

## Caching

All embedding calls are only made once to save tokens and are cached to a file: `~/.config/flowprompter/.fpjembeddings`. On windows this file will be something like `C:\Users\{username}\AppData\Local\flowprompter\.fpjembeddings`.

To configure the file location use the `embeddingCachePath` option: 

```ts
import {setupMatchers} from "@flowprompter/jest"; 

// Setup your matchers before your tests run
expect.extend(setupMatchers({
  // other config...
  embeddingCachePath: "/path/to/my/embeddings/cache"
}));
```

## Contributing

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`
2. Copy `.env.example` to `.env` file and supply your `OPENAI_API_KEY`
3. Run tests by `pnpm test`

## Authors

- Rudi Yardley ([@ryardley](https://twitter.com/rudiyardley))
- Dan Mechea ([@dmechea](https://twitter.com/dmechea))