mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-19 21:33:51 +00:00
community[patch]: Update root_validators embeddings: llamacpp, jina, dashscope, mosaicml, huggingface_hub, Toolkits: Connery, ChatModels: PAI_EAS, (#22828)
This PR updates root validators for: * Embeddings: llamacpp, jina, dashscope, mosaicml, huggingface_hub * Toolkits: Connery * ChatModels: PAI_EAS Following this issue: https://github.com/langchain-ai/langchain/issues/22819
This commit is contained in:
parent
32ba8cfab0
commit
265e650e64
@ -19,7 +19,7 @@ class ConneryToolkit(BaseToolkit):
|
|||||||
"""
|
"""
|
||||||
return self.tools
|
return self.tools
|
||||||
|
|
||||||
@root_validator()
|
@root_validator(pre=True)
|
||||||
def validate_attributes(cls, values: dict) -> dict:
|
def validate_attributes(cls, values: dict) -> dict:
|
||||||
"""
|
"""
|
||||||
Validate the attributes of the ConneryToolkit class.
|
Validate the attributes of the ConneryToolkit class.
|
||||||
|
@ -67,7 +67,7 @@ class PaiEasChatEndpoint(BaseChatModel):
|
|||||||
|
|
||||||
timeout: Optional[int] = 5000
|
timeout: Optional[int] = 5000
|
||||||
|
|
||||||
@root_validator()
|
@root_validator(pre=True)
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
"""Validate that api key and python package exists in environment."""
|
"""Validate that api key and python package exists in environment."""
|
||||||
values["eas_service_url"] = get_from_dict_or_env(
|
values["eas_service_url"] = get_from_dict_or_env(
|
||||||
|
@ -110,7 +110,7 @@ class DashScopeEmbeddings(BaseModel, Embeddings):
|
|||||||
|
|
||||||
extra = Extra.forbid
|
extra = Extra.forbid
|
||||||
|
|
||||||
@root_validator()
|
@root_validator(pre=True)
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
import dashscope
|
import dashscope
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from langchain_core._api import deprecated
|
from langchain_core._api import deprecated
|
||||||
from langchain_core.embeddings import Embeddings
|
from langchain_core.embeddings import Embeddings
|
||||||
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
|
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
|
||||||
|
from langchain_core.utils import get_from_dict_or_env
|
||||||
|
|
||||||
DEFAULT_MODEL = "sentence-transformers/all-mpnet-base-v2"
|
DEFAULT_MODEL = "sentence-transformers/all-mpnet-base-v2"
|
||||||
VALID_TASKS = ("feature-extraction",)
|
VALID_TASKS = ("feature-extraction",)
|
||||||
@ -52,19 +52,19 @@ class HuggingFaceHubEmbeddings(BaseModel, Embeddings):
|
|||||||
|
|
||||||
extra = Extra.forbid
|
extra = Extra.forbid
|
||||||
|
|
||||||
@root_validator()
|
@root_validator(pre=True)
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
"""Validate that api key and python package exists in environment."""
|
"""Validate that api key and python package exists in environment."""
|
||||||
huggingfacehub_api_token = values["huggingfacehub_api_token"] or os.getenv(
|
huggingfacehub_api_token = get_from_dict_or_env(
|
||||||
"HUGGINGFACEHUB_API_TOKEN"
|
values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN"
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from huggingface_hub import AsyncInferenceClient, InferenceClient
|
from huggingface_hub import AsyncInferenceClient, InferenceClient
|
||||||
|
|
||||||
if values["model"]:
|
if values.get("model"):
|
||||||
values["repo_id"] = values["model"]
|
values["repo_id"] = values["model"]
|
||||||
elif values["repo_id"]:
|
elif values.get("repo_id"):
|
||||||
values["model"] = values["repo_id"]
|
values["model"] = values["repo_id"]
|
||||||
else:
|
else:
|
||||||
values["model"] = DEFAULT_MODEL
|
values["model"] = DEFAULT_MODEL
|
||||||
@ -80,11 +80,6 @@ class HuggingFaceHubEmbeddings(BaseModel, Embeddings):
|
|||||||
token=huggingfacehub_api_token,
|
token=huggingfacehub_api_token,
|
||||||
)
|
)
|
||||||
|
|
||||||
if values["task"] not in VALID_TASKS:
|
|
||||||
raise ValueError(
|
|
||||||
f"Got invalid task {values['task']}, "
|
|
||||||
f"currently only {VALID_TASKS} are supported"
|
|
||||||
)
|
|
||||||
values["client"] = client
|
values["client"] = client
|
||||||
values["async_client"] = async_client
|
values["async_client"] = async_client
|
||||||
|
|
||||||
@ -95,6 +90,16 @@ class HuggingFaceHubEmbeddings(BaseModel, Embeddings):
|
|||||||
)
|
)
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
@root_validator(pre=False, skip_on_failure=True)
|
||||||
|
def post_init(cls, values: Dict) -> Dict:
|
||||||
|
"""Post init validation for the class."""
|
||||||
|
if values["task"] not in VALID_TASKS:
|
||||||
|
raise ValueError(
|
||||||
|
f"Got invalid task {values['task']}, "
|
||||||
|
f"currently only {VALID_TASKS} are supported"
|
||||||
|
)
|
||||||
|
return values
|
||||||
|
|
||||||
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||||
"""Call out to HuggingFaceHub's embedding endpoint for embedding search docs.
|
"""Call out to HuggingFaceHub's embedding endpoint for embedding search docs.
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ class JinaEmbeddings(BaseModel, Embeddings):
|
|||||||
model_name: str = "jina-embeddings-v2-base-en"
|
model_name: str = "jina-embeddings-v2-base-en"
|
||||||
jina_api_key: Optional[SecretStr] = None
|
jina_api_key: Optional[SecretStr] = None
|
||||||
|
|
||||||
@root_validator()
|
@root_validator(pre=True)
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
"""Validate that auth token exists in environment."""
|
"""Validate that auth token exists in environment."""
|
||||||
try:
|
try:
|
||||||
|
@ -62,7 +62,7 @@ class LlamaCppEmbeddings(BaseModel, Embeddings):
|
|||||||
|
|
||||||
extra = Extra.forbid
|
extra = Extra.forbid
|
||||||
|
|
||||||
@root_validator()
|
@root_validator(pre=False, skip_on_failure=True)
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
"""Validate that llama-cpp-python library is installed."""
|
"""Validate that llama-cpp-python library is installed."""
|
||||||
model_path = values["model_path"]
|
model_path = values["model_path"]
|
||||||
|
@ -46,7 +46,7 @@ class MosaicMLInstructorEmbeddings(BaseModel, Embeddings):
|
|||||||
|
|
||||||
extra = Extra.forbid
|
extra = Extra.forbid
|
||||||
|
|
||||||
@root_validator()
|
@root_validator(pre=True)
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
def validate_environment(cls, values: Dict) -> Dict:
|
||||||
"""Validate that api key and python package exists in environment."""
|
"""Validate that api key and python package exists in environment."""
|
||||||
mosaicml_api_token = get_from_dict_or_env(
|
mosaicml_api_token = get_from_dict_or_env(
|
||||||
|
Loading…
Reference in New Issue
Block a user