mirror of
https://github.com/hwchase17/langchain
synced 2024-11-02 09:40:22 +00:00
aa19ca9723
<!-- Thank you for contributing to LangChain! Please title your PR "<package>: <description>", where <package> is whichever of langchain, community, core, experimental, etc. is being modified. Replace this entire comment with: - **Description:** a description of the change, - **Issue:** the issue # it fixes if applicable, - **Dependencies:** any dependencies required for this change, - **Twitter handle:** we announce bigger features on Twitter. If your PR gets announced, and you'd like a mention, we'll gladly shout you out! Please make sure your PR is passing linting and testing before submitting. Run `make format`, `make lint` and `make test` from the root of the package you've modified to check this locally. See contribution guidelines for more information on how to write/run tests, lint, etc: https://python.langchain.com/docs/contributing/ If you're adding a new integration, please include: 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. If no one reviews your PR within a few days, please @-mention one of @baskaryan, @eyurtsev, @hwchase17. -->
76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
from typing import Any, Dict, List, Optional
|
|
|
|
import requests
|
|
from langchain_core.embeddings import Embeddings
|
|
from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator
|
|
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
|
|
|
|
JINA_API_URL: str = "https://api.jina.ai/v1/embeddings"
|
|
|
|
|
|
class JinaEmbeddings(BaseModel, Embeddings):
|
|
"""Jina embedding models."""
|
|
|
|
session: Any #: :meta private:
|
|
model_name: str = "jina-embeddings-v2-base-en"
|
|
jina_api_key: Optional[SecretStr] = None
|
|
|
|
@root_validator()
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
"""Validate that auth token exists in environment."""
|
|
try:
|
|
jina_api_key = convert_to_secret_str(
|
|
get_from_dict_or_env(values, "jina_api_key", "JINA_API_KEY")
|
|
)
|
|
except ValueError as original_exc:
|
|
try:
|
|
jina_api_key = convert_to_secret_str(
|
|
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.get_secret_value()}",
|
|
"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()
|
|
if "data" not in resp:
|
|
raise RuntimeError(resp["detail"])
|
|
|
|
embeddings = resp["data"]
|
|
|
|
# Sort resulting embeddings by index
|
|
sorted_embeddings = sorted(embeddings, key=lambda e: e["index"]) # type: ignore
|
|
|
|
# Return just the embeddings
|
|
return [result["embedding"] for result in sorted_embeddings]
|
|
|
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
|
"""Call out to Jina's embedding endpoint.
|
|
Args:
|
|
texts: The list of texts to embed.
|
|
Returns:
|
|
List of embeddings, one for each text.
|
|
"""
|
|
return self._embed(texts)
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
|
"""Call out to Jina's embedding endpoint.
|
|
Args:
|
|
text: The text to embed.
|
|
Returns:
|
|
Embeddings for the text.
|
|
"""
|
|
return self._embed([text])[0]
|