Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain: make numpy optional #29182

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion libs/langchain/langchain/chains/flare/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
from typing import Any, Dict, List, Optional, Sequence, Tuple

import numpy as np
from langchain_core.callbacks import (
CallbackManagerForChainRun,
)
Expand Down Expand Up @@ -57,6 +56,12 @@ def _low_confidence_spans(
min_token_gap: int,
num_pad_tokens: int,
) -> List[str]:
try:
import numpy as np
except ImportError as e:
raise ImportError(
"Could not import numpy," "please install with `pip install numpy`."
) from e
_low_idx = np.where(np.exp(log_probs) < min_prob)[0]
low_idx = [i for i in _low_idx if re.search(r"\w", tokens[i])]
if len(low_idx) == 0:
Expand Down
7 changes: 6 additions & 1 deletion libs/langchain/langchain/chains/hyde/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from typing import Any, Dict, List, Optional

import numpy as np
from langchain_core.callbacks import CallbackManagerForChainRun
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseLanguageModel
Expand Down Expand Up @@ -54,6 +53,12 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:

def combine_embeddings(self, embeddings: List[List[float]]) -> List[float]:
"""Combine embeddings into final embeddings."""
try:
import numpy as np
except ImportError as e:
raise ImportError(
"Could not import numpy," "please install with `pip install numpy`."
) from e
return list(np.array(embeddings).mean(axis=0))

def embed_query(self, text: str) -> List[float]:
Expand Down
32 changes: 25 additions & 7 deletions libs/langchain/langchain/evaluation/embedding_distance/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from enum import Enum
from typing import Any, Dict, List, Optional

import numpy as np
from langchain_core.callbacks.manager import (
AsyncCallbackManagerForChainRun,
CallbackManagerForChainRun,
Expand All @@ -18,6 +17,17 @@
from langchain.schema import RUN_KEY


def _import_numpy() -> Any:
try:
import numpy as np

return np
except ImportError as e:
raise ImportError(
"Could not import numpy," "please install with `pip install numpy`."
) from e


def _embedding_factory() -> Embeddings:
"""Create an Embeddings object.
Returns:
Expand Down Expand Up @@ -154,7 +164,7 @@ def _get_metric(self, metric: EmbeddingDistance) -> Any:
raise ValueError(f"Invalid metric: {metric}")

@staticmethod
def _cosine_distance(a: np.ndarray, b: np.ndarray) -> np.ndarray:
def _cosine_distance(a: Any, b: Any) -> Any:
"""Compute the cosine distance between two vectors.

Args:
Expand All @@ -175,7 +185,7 @@ def _cosine_distance(a: np.ndarray, b: np.ndarray) -> np.ndarray:
return 1.0 - cosine_similarity(a, b)

@staticmethod
def _euclidean_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
def _euclidean_distance(a: Any, b: Any) -> Any:
"""Compute the Euclidean distance between two vectors.

Args:
Expand All @@ -185,10 +195,11 @@ def _euclidean_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
Returns:
np.floating: The Euclidean distance.
"""
np = _import_numpy()
return np.linalg.norm(a - b)

@staticmethod
def _manhattan_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
def _manhattan_distance(a: Any, b: Any) -> Any:
"""Compute the Manhattan distance between two vectors.

Args:
Expand All @@ -198,10 +209,11 @@ def _manhattan_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
Returns:
np.floating: The Manhattan distance.
"""
np = _import_numpy()
return np.sum(np.abs(a - b))

@staticmethod
def _chebyshev_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
def _chebyshev_distance(a: Any, b: Any) -> Any:
"""Compute the Chebyshev distance between two vectors.

Args:
Expand All @@ -211,10 +223,11 @@ def _chebyshev_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
Returns:
np.floating: The Chebyshev distance.
"""
np = _import_numpy()
return np.max(np.abs(a - b))

@staticmethod
def _hamming_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
def _hamming_distance(a: Any, b: Any) -> Any:
"""Compute the Hamming distance between two vectors.

Args:
Expand All @@ -224,9 +237,10 @@ def _hamming_distance(a: np.ndarray, b: np.ndarray) -> np.floating:
Returns:
np.floating: The Hamming distance.
"""
np = _import_numpy()
return np.mean(a != b)

def _compute_score(self, vectors: np.ndarray) -> float:
def _compute_score(self, vectors: Any) -> float:
"""Compute the score based on the distance metric.

Args:
Expand Down Expand Up @@ -288,6 +302,7 @@ def _call(
Returns:
Dict[str, Any]: The computed score.
"""
np = _import_numpy()
vectors = np.array(
self.embeddings.embed_documents([inputs["prediction"], inputs["reference"]])
)
Expand All @@ -309,6 +324,7 @@ async def _acall(
Returns:
Dict[str, Any]: The computed score.
"""
np = _import_numpy()
embedded = await self.embeddings.aembed_documents(
[inputs["prediction"], inputs["reference"]]
)
Expand Down Expand Up @@ -425,6 +441,7 @@ def _call(
Returns:
Dict[str, Any]: The computed score.
"""
np = _import_numpy()
vectors = np.array(
self.embeddings.embed_documents(
[inputs["prediction"], inputs["prediction_b"]]
Expand All @@ -448,6 +465,7 @@ async def _acall(
Returns:
Dict[str, Any]: The computed score.
"""
np = _import_numpy()
embedded = await self.embeddings.aembed_documents(
[inputs["prediction"], inputs["prediction_b"]]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Callable, Dict, Optional, Sequence

import numpy as np
from langchain_core.callbacks.manager import Callbacks
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
Expand Down Expand Up @@ -69,6 +68,13 @@ def compress_documents(
"To use please install langchain-community "
"with `pip install langchain-community`."
)

try:
import numpy as np
except ImportError as e:
raise ImportError(
"Could not import numpy," "please install with `pip install numpy`."
) from e
stateful_documents = get_stateful_documents(documents)
embedded_documents = _get_embeddings_from_stateful_docs(
self.embeddings, stateful_documents
Expand Down Expand Up @@ -104,6 +110,13 @@ async def acompress_documents(
"To use please install langchain-community "
"with `pip install langchain-community`."
)

try:
import numpy as np
except ImportError as e:
raise ImportError(
"Could not import numpy," "please install with `pip install numpy`."
) from e
stateful_documents = get_stateful_documents(documents)
embedded_documents = await _aget_embeddings_from_stateful_docs(
self.embeddings, stateful_documents
Expand Down
Loading
Loading