mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-19 21:33:31 -04:00
1c51d3db0f
Created fix for 5475 Currently in PGvector, we do not have any function that returns the instance of an existing store. The from_documents always adds embeddings and then returns the store. This fix is to add a function that will return the instance of an existing store Also changed the jupyter example for PGVector to show the example of using the function <!-- Remove if not applicable --> Fixes # 5475 #### Before submitting <!-- If you're adding a new integration, please include: 1. a test for the integration - favor unit tests that does not rely on network access. 2. an example notebook showing its use See contribution guidelines for more information on how to write tests, lint etc: https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md --> #### Who can review? @dev2049 @hwchase17 Tag maintainers/contributors who might be interested: <!-- For a quicker response, figure out the right person to tag with @ @hwchase17 - project lead Tracing / Callbacks - @agola11 Async - @agola11 DataLoaders - @eyurtsev Models - @hwchase17 - @agola11 Agents / Tools / Toolkits - @vowelparrot VectorStores / Retrievers / Memory - @dev2049 --> --------- Co-authored-by: rajib76 <rajib76@yahoo.com> Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
84 lines
2.5 KiB
Python
84 lines
2.5 KiB
Python
"""Wrapper around Cohere embedding models."""
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from pydantic import BaseModel, Extra, root_validator
|
|
|
|
from langchain.embeddings.base import Embeddings
|
|
from langchain.utils import get_from_dict_or_env
|
|
|
|
|
|
class CohereEmbeddings(BaseModel, Embeddings):
|
|
"""Wrapper around Cohere embedding models.
|
|
|
|
To use, you should have the ``cohere`` python package installed, and the
|
|
environment variable ``COHERE_API_KEY`` set with your API key or pass it
|
|
as a named parameter to the constructor.
|
|
|
|
Example:
|
|
.. code-block:: python
|
|
|
|
from langchain.embeddings import CohereEmbeddings
|
|
cohere = CohereEmbeddings(
|
|
model="embed-english-light-v2.0", cohere_api_key="my-api-key"
|
|
)
|
|
"""
|
|
|
|
client: Any #: :meta private:
|
|
model: str = "embed-english-v2.0"
|
|
"""Model name to use."""
|
|
|
|
truncate: Optional[str] = None
|
|
"""Truncate embeddings that are too long from start or end ("NONE"|"START"|"END")"""
|
|
|
|
cohere_api_key: Optional[str] = None
|
|
|
|
class Config:
|
|
"""Configuration for this pydantic object."""
|
|
|
|
extra = Extra.forbid
|
|
|
|
@root_validator()
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
"""Validate that api key and python package exists in environment."""
|
|
cohere_api_key = get_from_dict_or_env(
|
|
values, "cohere_api_key", "COHERE_API_KEY"
|
|
)
|
|
try:
|
|
import cohere
|
|
|
|
values["client"] = cohere.Client(cohere_api_key)
|
|
except ImportError:
|
|
raise ValueError(
|
|
"Could not import cohere python package. "
|
|
"Please install it with `pip install cohere`."
|
|
)
|
|
return values
|
|
|
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
|
"""Call out to Cohere's embedding endpoint.
|
|
|
|
Args:
|
|
texts: The list of texts to embed.
|
|
|
|
Returns:
|
|
List of embeddings, one for each text.
|
|
"""
|
|
embeddings = self.client.embed(
|
|
model=self.model, texts=texts, truncate=self.truncate
|
|
).embeddings
|
|
return [list(map(float, e)) for e in embeddings]
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
|
"""Call out to Cohere's embedding endpoint.
|
|
|
|
Args:
|
|
text: The text to embed.
|
|
|
|
Returns:
|
|
Embeddings for the text.
|
|
"""
|
|
embedding = self.client.embed(
|
|
model=self.model, texts=[text], truncate=self.truncate
|
|
).embeddings[0]
|
|
return list(map(float, embedding))
|