Files
promptfoo--promptfoo/site/docs/configuration/expected-outputs/similar.md
T
wehub-resource-sync 0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

3.7 KiB

sidebar_position, description
sidebar_position description
55 Calculate semantic similarity scores between actual and expected outputs using advanced embedding models and multiple distance metrics

Similarity (embeddings)

The similar assertion checks if an embedding of the LLM's output is semantically similar to the expected value, using a configurable similarity or distance metric with a threshold.

By default, embeddings are computed via OpenAI's text-embedding-3-large model.

Example:

assert:
  - type: similar
    value: 'The expected output'
    threshold: 0.8

If you provide an array of values, the test will pass if it is similar to at least one of them:

assert:
  - type: similar
    value:
      - The expected output
      - Expected output
      - file://my_expected_output.txt
    threshold: 0.8

The negated not-similar assertion is the logical inverse: with an array of values it passes only when the output is dissimilar to every value (and fails as soon as it is too similar to any one of them). This is the natural way to assert that an output does not resemble any item in a list of forbidden or canned answers.

Similarity Metrics

You can specify which metric to use by including it in the assertion type. The default is similar (cosine similarity).

Cosine Similarity (default)

Measures the cosine of the angle between two vectors. Range: -1 to 1 (higher is more similar), though text embeddings typically produce values between 0 and 1.

assert:
  # Default - uses cosine similarity
  - type: similar
    value: 'The expected output'
    threshold: 0.8

  # Explicit cosine
  - type: similar:cosine
    value: 'The expected output'
    threshold: 0.8

When to use: Best for semantic similarity where you care about the direction of the embedding vector, not its magnitude. This is the industry standard for embeddings.

Dot Product

Computes the dot product of two vectors. Range: unbounded, but typically 0 to 1 for normalized embeddings (higher is more similar).

assert:
  - type: similar:dot
    value: 'The expected output'
    threshold: 0.8

When to use: Useful when you want to match the metric used in your production vector database (many use dot product for performance). For normalized embeddings, dot product is nearly equivalent to cosine similarity.

Euclidean Distance

Computes the straight-line distance between two vectors. Range: 0 to ∞ (lower is more similar).

assert:
  - type: similar:euclidean
    value: 'The expected output'
    threshold: 0.5 # Maximum distance threshold

When to use: When you care about both the angle and magnitude differences between vectors. Note that the threshold represents the maximum distance (not minimum similarity), so lower values are stricter.

Important: For euclidean distance, the threshold semantics are inverted - it represents the maximum acceptable distance rather than minimum similarity.

Overriding the provider

By default similar will use OpenAI. To specify the model that creates the embeddings, do one of the following:

  1. Use test.options or defaultTest.options to override the provider across the entire test suite. For example:

    defaultTest:
      options:
        provider:
          embedding:
            id: azureopenai:embedding:text-embedding-ada-002
            config:
              apiHost: xxx.openai.azure.com
    tests:
      assert:
        - type: similar
          value: Hello world
    
  2. Set assertion.provider on a per-assertion basis. For example:

    tests:
      assert:
        - type: similar
          value: Hello world
          provider: huggingface:sentence-similarity:sentence-transformers/all-MiniLM-L6-v2