PairwiseEmbeddingDistanceEvalChain Class — langchain Architecture
Architecture documentation for the PairwiseEmbeddingDistanceEvalChain class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD a279fd93_b575_438d_176b_7abfe52f575d["PairwiseEmbeddingDistanceEvalChain"] dc2d52f5_736c_0ec2_ad57_0e2fdaa94e04["_EmbeddingDistanceChainMixin"] a279fd93_b575_438d_176b_7abfe52f575d -->|extends| dc2d52f5_736c_0ec2_ad57_0e2fdaa94e04 910b9203_afa5_b8ca_1a1e_3933f70c340f["PairwiseStringEvaluator"] a279fd93_b575_438d_176b_7abfe52f575d -->|extends| 910b9203_afa5_b8ca_1a1e_3933f70c340f 8775603f_4af4_2e2d_459a_8e3ad12aaec4["base.py"] a279fd93_b575_438d_176b_7abfe52f575d -->|defined in| 8775603f_4af4_2e2d_459a_8e3ad12aaec4 b2d77581_9b1a_5afb_5480_df373f3f59f5["input_keys()"] a279fd93_b575_438d_176b_7abfe52f575d -->|method| b2d77581_9b1a_5afb_5480_df373f3f59f5 c04b8f1e_44b2_2e07_36b3_37e8dd7226cb["evaluation_name()"] a279fd93_b575_438d_176b_7abfe52f575d -->|method| c04b8f1e_44b2_2e07_36b3_37e8dd7226cb 55bba962_c474_7d72_fad4_2419ad83c3a7["_call()"] a279fd93_b575_438d_176b_7abfe52f575d -->|method| 55bba962_c474_7d72_fad4_2419ad83c3a7 5cbc8187_38d5_da15_88a2_96912b92daeb["_acall()"] a279fd93_b575_438d_176b_7abfe52f575d -->|method| 5cbc8187_38d5_da15_88a2_96912b92daeb 06371274_4c90_26da_3a86_0757828f2835["_evaluate_string_pairs()"] a279fd93_b575_438d_176b_7abfe52f575d -->|method| 06371274_4c90_26da_3a86_0757828f2835 410d183f_2cb6_1006_4210_ea1c8753bacf["_aevaluate_string_pairs()"] a279fd93_b575_438d_176b_7abfe52f575d -->|method| 410d183f_2cb6_1006_4210_ea1c8753bacf
Relationship Graph
Source Code
libs/langchain/langchain_classic/evaluation/embedding_distance/base.py lines 506–657
class PairwiseEmbeddingDistanceEvalChain(
_EmbeddingDistanceChainMixin,
PairwiseStringEvaluator,
):
"""Use embedding distances to score semantic difference between two predictions.
Examples:
>>> chain = PairwiseEmbeddingDistanceEvalChain()
>>> result = chain.evaluate_string_pairs(prediction="Hello", prediction_b="Hi")
>>> print(result)
{'score': 0.5}
"""
@property
def input_keys(self) -> list[str]:
"""Return the input keys of the chain.
Returns:
The input keys.
"""
return ["prediction", "prediction_b"]
@property
def evaluation_name(self) -> str:
"""Return the evaluation name."""
return f"pairwise_embedding_{self.distance_metric.value}_distance"
@override
def _call(
self,
inputs: dict[str, Any],
run_manager: CallbackManagerForChainRun | None = None,
) -> dict[str, Any]:
"""Compute the score for two predictions.
Args:
inputs: The input data.
run_manager: The callback manager.
Returns:
The computed score.
"""
vectors = self.embeddings.embed_documents(
[
inputs["prediction"],
inputs["prediction_b"],
],
)
if _check_numpy():
np = _import_numpy()
vectors = np.array(vectors)
score = self._compute_score(vectors)
return {"score": score}
@override
async def _acall(
self,
inputs: dict[str, Any],
run_manager: AsyncCallbackManagerForChainRun | None = None,
) -> dict[str, Any]:
"""Asynchronously compute the score for two predictions.
Args:
inputs: The input data.
run_manager: The callback manager.
Returns:
The computed score.
"""
vectors = await self.embeddings.aembed_documents(
[
inputs["prediction"],
inputs["prediction_b"],
],
)
if _check_numpy():
np = _import_numpy()
vectors = np.array(vectors)
score = self._compute_score(vectors)
return {"score": score}
Source
Frequently Asked Questions
What is the PairwiseEmbeddingDistanceEvalChain class?
PairwiseEmbeddingDistanceEvalChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/evaluation/embedding_distance/base.py.
Where is PairwiseEmbeddingDistanceEvalChain defined?
PairwiseEmbeddingDistanceEvalChain is defined in libs/langchain/langchain_classic/evaluation/embedding_distance/base.py at line 506.
What does PairwiseEmbeddingDistanceEvalChain extend?
PairwiseEmbeddingDistanceEvalChain extends _EmbeddingDistanceChainMixin, PairwiseStringEvaluator.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free