Source code for langchain_community.embeddings.jina
from typing import Any, Dict, List, Optional
import requests
from langchain_core.embeddings import Embeddings
from langchain_core.pydantic_v1 import BaseModel, root_validator
from langchain_core.utils import get_from_dict_or_env
JINA_API_URL: str = "https://api.jina.ai/v1/embeddings"
[docs]class JinaEmbeddings(BaseModel, Embeddings):
    """Jina embedding models."""
    session: Any  #: :meta private:
    model_name: str = "jina-embeddings-v2-base-en"
    jina_api_key: Optional[str] = None
    @root_validator()
    def validate_environment(cls, values: Dict) -> Dict:
        """Validate that auth token exists in environment."""
        try:
            jina_api_key = get_from_dict_or_env(values, "jina_api_key", "JINA_API_KEY")
        except ValueError as original_exc:
            try:
                jina_api_key = get_from_dict_or_env(
                    values, "jina_auth_token", "JINA_AUTH_TOKEN"
                )
            except ValueError:
                raise original_exc
        session = requests.Session()
        session.headers.update(
            {
                "Authorization": f"Bearer {jina_api_key}",
                "Accept-Encoding": "identity",
                "Content-type": "application/json",
            }
        )
        values["session"] = session
        return values
    def _embed(self, texts: List[str]) -> List[List[float]]:
        # Call Jina AI Embedding API
        resp = self.session.post(  # type: ignore
            JINA_API_URL, json={"input": texts, "model": self.model_name}
        ).json()