mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-27 14:26:48 +00:00
This PR upgrades langchain-community to pydantic 2.
* Most of this PR was auto-generated using code mods with gritql
(https://github.com/eyurtsev/migrate-pydantic/tree/main)
* Subsequently, some code was fixed manually due to accommodate
differences between pydantic 1 and 2
Breaking Changes:
- Use TEXTEMBED_API_KEY and TEXTEMBEB_API_URL for env variables for text
embed integrations:
cbea780492
Other changes:
- Added pydantic_settings as a required dependency for community. This
may be removed if we have enough time to convert the dependency into an
optional one.
---------
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
import hashlib
|
|
from typing import List
|
|
|
|
import numpy as np
|
|
from langchain_core.embeddings import Embeddings
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class FakeEmbeddings(Embeddings, BaseModel):
|
|
"""Fake embedding model."""
|
|
|
|
size: int
|
|
"""The size of the embedding vector."""
|
|
|
|
def _get_embedding(self) -> List[float]:
|
|
return list(np.random.normal(size=self.size))
|
|
|
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
|
return [self._get_embedding() for _ in texts]
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
|
return self._get_embedding()
|
|
|
|
|
|
class DeterministicFakeEmbedding(Embeddings, BaseModel):
|
|
"""
|
|
Fake embedding model that always returns
|
|
the same embedding vector for the same text.
|
|
"""
|
|
|
|
size: int
|
|
"""The size of the embedding vector."""
|
|
|
|
def _get_embedding(self, seed: int) -> List[float]:
|
|
# set the seed for the random generator
|
|
np.random.seed(seed)
|
|
return list(np.random.normal(size=self.size))
|
|
|
|
def _get_seed(self, text: str) -> int:
|
|
"""
|
|
Get a seed for the random generator, using the hash of the text.
|
|
"""
|
|
return int(hashlib.sha256(text.encode("utf-8")).hexdigest(), 16) % 10**8
|
|
|
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
|
return [self._get_embedding(seed=self._get_seed(_)) for _ in texts]
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
|
return self._get_embedding(seed=self._get_seed(text))
|