fake_embeddings.py — langchain Source File
Architecture documentation for fake_embeddings.py, a python file in the langchain codebase. 3 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 15fd92f1_08db_86e1_086a_47f4b74faa93["fake_embeddings.py"] 6d7cdba5_8e52_34b5_6742_57caf6500c80["math"] 15fd92f1_08db_86e1_086a_47f4b74faa93 --> 6d7cdba5_8e52_34b5_6742_57caf6500c80 bc46b61d_cfdf_3f6b_a9dd_ac2a328d84b3["langchain_core.embeddings"] 15fd92f1_08db_86e1_086a_47f4b74faa93 --> bc46b61d_cfdf_3f6b_a9dd_ac2a328d84b3 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] 15fd92f1_08db_86e1_086a_47f4b74faa93 --> 91721f45_4909_e489_8c1f_084f8bd87145 style 15fd92f1_08db_86e1_086a_47f4b74faa93 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Fake Embedding class for testing purposes."""
import math
from langchain_core.embeddings import Embeddings
from typing_extensions import override
fake_texts = ["foo", "bar", "baz"]
class FakeEmbeddings(Embeddings):
"""Fake embeddings functionality for testing."""
@override
def embed_documents(self, texts: list[str]) -> list[list[float]]:
"""Return simple embeddings.
Embeddings encode each text as its index.
Args:
texts: List of text to embed.
Returns:
List of embeddings.
"""
return [[1.0] * 9 + [float(i)] for i in range(len(texts))]
async def aembed_documents(self, texts: list[str]) -> list[list[float]]:
return self.embed_documents(texts)
@override
def embed_query(self, text: str) -> list[float]:
"""Return constant query embeddings.
Embeddings are identical to embed_documents(texts)[0].
Distance to each text will be that text's index,
as it was passed to embed_documents.
Args:
text: Text to embed.
Returns:
Embedding.
"""
return [1.0] * 9 + [0.0]
async def aembed_query(self, text: str) -> list[float]:
return self.embed_query(text)
class ConsistentFakeEmbeddings(FakeEmbeddings):
"""Consistent fake embeddings.
Fake embeddings which remember all the texts seen so far to return consistent
vectors for the same texts.
"""
def __init__(self, dimensionality: int = 10) -> None:
self.known_texts: list[str] = []
self.dimensionality = dimensionality
def embed_documents(self, texts: list[str]) -> list[list[float]]:
"""Return consistent embeddings for each text seen so far."""
out_vectors = []
for text in texts:
if text not in self.known_texts:
self.known_texts.append(text)
vector = [1.0] * (self.dimensionality - 1) + [
float(self.known_texts.index(text)),
]
out_vectors.append(vector)
return out_vectors
@override
def embed_query(self, text: str) -> list[float]:
"""Embed query text.
Return consistent embeddings for the text, if seen before, or a constant
one if the text is unknown.
Args:
text: Text to embed.
Returns:
Embedding.
"""
return self.embed_documents([text])[0]
class AngularTwoDimensionalEmbeddings(Embeddings):
"""From angles (as strings in units of pi) to unit embedding vectors on a circle."""
def embed_documents(self, texts: list[str]) -> list[list[float]]:
"""Make a list of texts into a list of embedding vectors."""
return [self.embed_query(text) for text in texts]
@override
def embed_query(self, text: str) -> list[float]:
"""Embed query text.
Convert input text to a 'vector' (list of floats).
If the text is a number, use it as the angle for the
unit vector in units of pi.
Any other input text becomes the singular result [0, 0] !
Args:
text: Text to embed.
Returns:
Embedding.
"""
try:
angle = float(text)
return [math.cos(angle * math.pi), math.sin(angle * math.pi)]
except ValueError:
# Assume: just test string, no attention is paid to values.
return [0.0, 0.0]
Domain
Subdomains
Dependencies
- langchain_core.embeddings
- math
- typing_extensions
Source
Frequently Asked Questions
What does fake_embeddings.py do?
fake_embeddings.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What does fake_embeddings.py depend on?
fake_embeddings.py imports 3 module(s): langchain_core.embeddings, math, typing_extensions.
Where is fake_embeddings.py in the architecture?
fake_embeddings.py is located at libs/langchain/tests/integration_tests/cache/fake_embeddings.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/tests/integration_tests/cache).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free