Home / File/ test_utils.py — langchain Source File

test_utils.py — langchain Source File

Architecture documentation for test_utils.py, a python file in the langchain codebase. 4 imports, 0 dependents.

File python LangChainCore ApiManagement 4 imports 1 classes

Entity Profile

Dependency Diagram

graph LR
  0467af3d_8463_8627_a83b_775fc6d09096["test_utils.py"]
  5ae36043_4f11_1bb7_1115_8f9929a82759["math"]
  0467af3d_8463_8627_a83b_775fc6d09096 --> 5ae36043_4f11_1bb7_1115_8f9929a82759
  f69d6389_263d_68a4_7fbf_f14c0602a9ba["pytest"]
  0467af3d_8463_8627_a83b_775fc6d09096 --> f69d6389_263d_68a4_7fbf_f14c0602a9ba
  eea920d0_5f0d_7728_8367_275e1830e552["numpy"]
  0467af3d_8463_8627_a83b_775fc6d09096 --> eea920d0_5f0d_7728_8367_275e1830e552
  ca622562_7d63_1a79_250d_a8aee20b2c43["langchain_core.vectorstores.utils"]
  0467af3d_8463_8627_a83b_775fc6d09096 --> ca622562_7d63_1a79_250d_a8aee20b2c43
  style 0467af3d_8463_8627_a83b_775fc6d09096 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Tests for langchain_core.vectorstores.utils module."""

import math

import pytest

pytest.importorskip("numpy")
import numpy as np

from langchain_core.vectorstores.utils import _cosine_similarity


class TestCosineSimilarity:
    """Tests for _cosine_similarity function."""

    def test_basic_cosine_similarity(self) -> None:
        """Test basic cosine similarity calculation."""
        # Simple orthogonal vectors
        x: list[list[float]] = [[1, 0], [0, 1]]
        y: list[list[float]] = [[1, 0], [0, 1]]
        result = _cosine_similarity(x, y)
        expected = np.array([[1.0, 0.0], [0.0, 1.0]])
        np.testing.assert_array_almost_equal(result, expected)

    def test_identical_vectors(self) -> None:
        """Test cosine similarity of identical vectors."""
        x: list[list[float]] = [[1, 2, 3]]
        y: list[list[float]] = [[1, 2, 3]]
        result = _cosine_similarity(x, y)
        expected = np.array([[1.0]])
        np.testing.assert_array_almost_equal(result, expected)

    def test_opposite_vectors(self) -> None:
        """Test cosine similarity of opposite vectors."""
        x: list[list[float]] = [[1, 2, 3]]
        y: list[list[float]] = [[-1, -2, -3]]
        result = _cosine_similarity(x, y)
        expected = np.array([[-1.0]])
        np.testing.assert_array_almost_equal(result, expected)

    def test_zero_vector(self) -> None:
        """Test cosine similarity with zero vector."""
        x: list[list[float]] = [[0, 0, 0]]
        y: list[list[float]] = [[1, 2, 3]]
        with pytest.raises(ValueError, match="NaN values found"):
            _cosine_similarity(x, y)

    def test_multiple_vectors(self) -> None:
        """Test cosine similarity with multiple vectors."""
        x: list[list[float]] = [[1, 0], [0, 1], [1, 1]]
        y: list[list[float]] = [[1, 0], [0, 1]]
        result = _cosine_similarity(x, y)
        expected = np.array(
            [
                [1.0, 0.0],
                [0.0, 1.0],
                [1 / math.sqrt(2), 1 / math.sqrt(2)],
            ]
        )
        np.testing.assert_array_almost_equal(result, expected)
// ... (105 more lines)

Domain

Subdomains

Dependencies

  • langchain_core.vectorstores.utils
  • math
  • numpy
  • pytest

Frequently Asked Questions

What does test_utils.py do?
test_utils.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, ApiManagement subdomain.
What does test_utils.py depend on?
test_utils.py imports 4 module(s): langchain_core.vectorstores.utils, math, numpy, pytest.
Where is test_utils.py in the architecture?
test_utils.py is located at libs/core/tests/unit_tests/vectorstores/test_utils.py (domain: LangChainCore, subdomain: ApiManagement, directory: libs/core/tests/unit_tests/vectorstores).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free