Home / File/ utils.py — langchain Source File

utils.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  2e7e9cfc_77cb_4736_6653_c9ca97216858["utils.py"]
  2a7f66a7_8738_3d47_375b_70fcaa6ac169["logging"]
  2e7e9cfc_77cb_4736_6653_c9ca97216858 --> 2a7f66a7_8738_3d47_375b_70fcaa6ac169
  0c635125_6987_b8b3_7ff7_d60249aecde7["warnings"]
  2e7e9cfc_77cb_4736_6653_c9ca97216858 --> 0c635125_6987_b8b3_7ff7_d60249aecde7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  2e7e9cfc_77cb_4736_6653_c9ca97216858 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  cd17727f_b882_7f06_aadc_71fbf75bebb0["numpy"]
  2e7e9cfc_77cb_4736_6653_c9ca97216858 --> cd17727f_b882_7f06_aadc_71fbf75bebb0
  e42fcce0_5a28_05e0_a83d_9af129fce0a3["simsimd"]
  2e7e9cfc_77cb_4736_6653_c9ca97216858 --> e42fcce0_5a28_05e0_a83d_9af129fce0a3
  style 2e7e9cfc_77cb_4736_6653_c9ca97216858 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Internal utilities for the in memory implementation of `VectorStore`.

!!! warning

    These are part of a private API, and users should not use them directly as they can
    change without notice.
"""

from __future__ import annotations

import logging
import warnings
from typing import TYPE_CHECKING, cast

try:
    import numpy as np

    _HAS_NUMPY = True
except ImportError:
    _HAS_NUMPY = False

try:
    import simsimd as simd  # type: ignore[import-not-found]

    _HAS_SIMSIMD = True
except ImportError:
    _HAS_SIMSIMD = False

if TYPE_CHECKING:
    Matrix = list[list[float]] | list[np.ndarray] | np.ndarray

logger = logging.getLogger(__name__)


def _cosine_similarity(x: Matrix, y: Matrix) -> np.ndarray:
    """Row-wise cosine similarity between two equal-width matrices.

    Args:
        x: A matrix of shape `(n, m)`.
        y: A matrix of shape `(k, m)`.

    Returns:
        A matrix of shape `(n, k)` where each element `(i, j)` is the cosine similarity
            between the `i`th row of `x` and the `j`th row of `y`.

    Raises:
        ValueError: If the number of columns in `x` and `y` are not the same.
        ImportError: If numpy is not installed.
    """
    if not _HAS_NUMPY:
        msg = (
            "cosine_similarity requires numpy to be installed. "
            "Please install numpy with `pip install numpy`."
        )
        raise ImportError(msg)

    if len(x) == 0 or len(y) == 0:
        return np.array([[]])

    x = np.array(x)
// ... (98 more lines)

Subdomains

Dependencies

  • logging
  • numpy
  • simsimd
  • typing
  • warnings

Frequently Asked Questions

What does utils.py do?
utils.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in utils.py?
utils.py defines 7 function(s): Matrix, _HAS_NUMPY, _HAS_SIMSIMD, _cosine_similarity, maximal_marginal_relevance, numpy, simsimd.
What does utils.py depend on?
utils.py imports 5 module(s): logging, numpy, simsimd, typing, warnings.
Where is utils.py in the architecture?
utils.py is located at libs/core/langchain_core/vectorstores/utils.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/core/langchain_core/vectorstores).

Analyze Your Own Codebase

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

Try Supermodel Free