[docs]classCohereEmbeddings(BaseModel,Embeddings):""" Implements the Embeddings interface with Cohere's text representation language models. Find out more about us at https://cohere.com and https://huggingface.co/CohereForAI This implementation uses the Embed API - see https://docs.cohere.com/reference/embed To use this you'll need to a Cohere API key - either pass it to cohere_api_key parameter or set the COHERE_API_KEY environment variable. API keys are available on https://cohere.com - it's free to sign up and trial API keys work with this implementation. Basic Example: .. code-block:: python cohere_embeddings = CohereEmbeddings(model="embed-english-light-v3.0") text = "This is a test document." query_result = cohere_embeddings.embed_query(text) print(query_result) doc_result = cohere_embeddings.embed_documents([text]) print(doc_result) """client:Any#: :meta private:"""Cohere client."""async_client:Any#: :meta private:"""Cohere async client."""model:Optional[str]=None"""Model name to use. It is mandatory to specify the model name."""truncate:Optional[str]=None"""Truncate embeddings that are too long from start or end ("NONE"|"START"|"END")"""cohere_api_key:Optional[SecretStr]=Field(default_factory=secret_from_env("COHERE_API_KEY",default=None))embedding_types:Sequence[str]=["float"]"Specifies the types of embeddings you want to get back"max_retries:int=3"""Maximum number of retries to make when generating."""request_timeout:Optional[float]=None"""Timeout in seconds for the Cohere API request."""user_agent:str="langchain:partner""""Identifier for the application making the request."""base_url:Optional[str]=None"""Override the default Cohere API URL."""model_config=ConfigDict(arbitrary_types_allowed=True,extra="forbid",protected_namespaces=(),)@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:Dict)->Any:"""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")ifisinstance(cohere_api_key,SecretStr):cohere_api_key=cohere_api_key.get_secret_value()request_timeout=values.get("request_timeout")client_name=values.get("user_agent","langchain:partner")values["client"]=cohere.Client(cohere_api_key,timeout=request_timeout,client_name=client_name,base_url=values.get("base_url"),)values["async_client"]=cohere.AsyncClient(cohere_api_key,timeout=request_timeout,client_name=client_name,base_url=values.get("base_url"),)returnvalues@model_validator(mode="after")defvalidate_model_specified(self)->Self:# type: ignore[valid-type]"""Validate that model is specified."""ifnotself.model:raiseValueError("Did not find `model`! Please "" pass `model` as a named parameter."" Please check out"" https://docs.cohere.com/reference/embed"" for available models.")returnself
[docs]defembed_with_retry(self,**kwargs:Any)->Any:"""Use tenacity to retry the embed call."""retry_decorator=_create_retry_decorator(self.max_retries)@retry_decoratordef_embed_with_retry(**kwargs:Any)->Any:returnself.client.embed(**kwargs)return_embed_with_retry(**kwargs)
[docs]defaembed_with_retry(self,**kwargs:Any)->Any:"""Use tenacity to retry the embed call."""retry_decorator=_create_retry_decorator(self.max_retries)@retry_decoratorasyncdef_embed_with_retry(**kwargs:Any)->Any:returnawaitself.async_client.embed(**kwargs)return_embed_with_retry(**kwargs)
[docs]defembed_documents(self,texts:List[str])->List[List[float]]:"""Embed a list of document texts. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """returnself.embed(texts,input_type="search_document")
[docs]asyncdefaembed_documents(self,texts:List[str])->List[List[float]]:"""Async call out to Cohere's embedding endpoint. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """returnawaitself.aembed(texts,input_type="search_document")
[docs]defembed_query(self,text:str)->List[float]:"""Call out to Cohere's embedding endpoint. Args: text: The text to embed. Returns: Embeddings for the text. """returnself.embed([text],input_type="search_query")[0]
[docs]asyncdefaembed_query(self,text:str)->List[float]:"""Async call out to Cohere's embedding endpoint. Args: text: The text to embed. Returns: Embeddings for the text. """return(awaitself.aembed([text],input_type="search_query"))[0]