2023-02-03 06:05:47 +00:00
|
|
|
"""Fake Embedding class for testing purposes."""
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from langchain.embeddings.base import Embeddings
|
|
|
|
|
|
|
|
fake_texts = ["foo", "bar", "baz"]
|
|
|
|
|
|
|
|
|
|
|
|
class FakeEmbeddings(Embeddings):
|
|
|
|
"""Fake embeddings functionality for testing."""
|
|
|
|
|
|
|
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
2023-03-23 02:40:10 +00:00
|
|
|
"""Return simple embeddings.
|
|
|
|
Embeddings encode each text as its index."""
|
2023-02-17 23:18:09 +00:00
|
|
|
return [[float(1.0)] * 9 + [float(i)] for i in range(len(texts))]
|
2023-02-03 06:05:47 +00:00
|
|
|
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
2023-03-23 02:40:10 +00:00
|
|
|
"""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."""
|
2023-02-17 23:18:09 +00:00
|
|
|
return [float(1.0)] * 9 + [float(0.0)]
|
2023-05-30 22:33:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConsistentFakeEmbeddings(FakeEmbeddings):
|
|
|
|
"""Fake embeddings which remember all the texts seen so far to return consistent
|
|
|
|
vectors for the same texts."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.known_texts: List[str] = []
|
|
|
|
|
|
|
|
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 = [float(1.0)] * 9 + [float(self.known_texts.index(text))]
|
|
|
|
out_vectors.append(vector)
|
|
|
|
return out_vectors
|
|
|
|
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
|
|
|
"""Return consistent embeddings for the text, if seen before, or a constant
|
|
|
|
one if the text is unknown."""
|
|
|
|
if text not in self.known_texts:
|
|
|
|
return [float(1.0)] * 9 + [float(0.0)]
|
|
|
|
return [float(1.0)] * 9 + [float(self.known_texts.index(text))]
|