mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-16 06:53:16 +00:00
multiple: pydantic 2 compatibility, v0.3 (#26443)
Signed-off-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com> Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com> Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com> Co-authored-by: ZhangShenao <15201440436@163.com> Co-authored-by: Friso H. Kingma <fhkingma@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Morgante Pell <morgantep@google.com>
This commit is contained in:
@@ -5,8 +5,8 @@ from typing import Any, Dict, List, Optional, Sequence, Union
|
||||
|
||||
from langchain_core.callbacks.base import Callbacks
|
||||
from langchain_core.documents import BaseDocumentCompressor, Document
|
||||
from langchain_core.pydantic_v1 import Field, root_validator
|
||||
from langchain_core.utils import get_from_dict_or_env
|
||||
from pydantic import ConfigDict, Field, model_validator
|
||||
|
||||
|
||||
class DashScopeRerank(BaseDocumentCompressor):
|
||||
@@ -25,13 +25,15 @@ class DashScopeRerank(BaseDocumentCompressor):
|
||||
"""DashScope API key. Must be specified directly or via environment variable
|
||||
DASHSCOPE_API_KEY."""
|
||||
|
||||
class Config:
|
||||
allow_population_by_field_name = True
|
||||
arbitrary_types_allowed = True
|
||||
extra = "forbid"
|
||||
model_config = ConfigDict(
|
||||
populate_by_name=True,
|
||||
arbitrary_types_allowed=True,
|
||||
extra="forbid",
|
||||
)
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_environment(cls, values: Dict) -> Any:
|
||||
"""Validate that api key and python package exists in environment."""
|
||||
|
||||
if not values.get("client"):
|
||||
|
@@ -1,10 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Dict, Optional, Sequence
|
||||
from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence
|
||||
|
||||
from langchain_core.callbacks.manager import Callbacks
|
||||
from langchain_core.documents import BaseDocumentCompressor, Document
|
||||
from langchain_core.pydantic_v1 import root_validator
|
||||
from pydantic import ConfigDict, model_validator
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from flashrank import Ranker, RerankRequest
|
||||
@@ -33,12 +33,14 @@ class FlashrankRerank(BaseDocumentCompressor):
|
||||
prefix_metadata: str = ""
|
||||
"""Prefix for flashrank_rerank metadata keys"""
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
extra = "forbid"
|
||||
model_config = ConfigDict(
|
||||
arbitrary_types_allowed=True,
|
||||
extra="forbid",
|
||||
)
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_environment(cls, values: Dict) -> Any:
|
||||
"""Validate that api key and python package exists in environment."""
|
||||
if "client" in values:
|
||||
return values
|
||||
|
@@ -6,8 +6,8 @@ from typing import Any, Dict, List, Optional, Sequence, Union
|
||||
import requests
|
||||
from langchain_core.callbacks import Callbacks
|
||||
from langchain_core.documents import BaseDocumentCompressor, Document
|
||||
from langchain_core.pydantic_v1 import root_validator
|
||||
from langchain_core.utils import get_from_dict_or_env
|
||||
from pydantic import ConfigDict, model_validator
|
||||
|
||||
JINA_API_URL: str = "https://api.jina.ai/v1/rerank"
|
||||
|
||||
@@ -27,12 +27,14 @@ class JinaRerank(BaseDocumentCompressor):
|
||||
user_agent: str = "langchain"
|
||||
"""Identifier for the application making the request."""
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
extra = "forbid"
|
||||
model_config = ConfigDict(
|
||||
arbitrary_types_allowed=True,
|
||||
extra="forbid",
|
||||
)
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_environment(cls, values: Dict) -> Any:
|
||||
"""Validate that api key exists in environment."""
|
||||
jina_api_key = get_from_dict_or_env(values, "jina_api_key", "JINA_API_KEY")
|
||||
user_agent = values.get("user_agent", "langchain")
|
||||
|
@@ -8,7 +8,7 @@ from langchain_core.documents import Document
|
||||
from langchain_core.documents.compressor import (
|
||||
BaseDocumentCompressor,
|
||||
)
|
||||
from langchain_core.pydantic_v1 import root_validator
|
||||
from pydantic import ConfigDict, Field, model_validator
|
||||
|
||||
DEFAULT_LLM_LINGUA_INSTRUCTION = (
|
||||
"Given this documents, please answer the final question"
|
||||
@@ -35,9 +35,9 @@ class LLMLinguaCompressor(BaseDocumentCompressor):
|
||||
"""The target number of compressed tokens"""
|
||||
rank_method: str = "longllmlingua"
|
||||
"""The ranking method to use"""
|
||||
model_config: dict = {}
|
||||
model_configuration: dict = Field(default_factory=dict, alias="model_config")
|
||||
"""Custom configuration for the model"""
|
||||
open_api_config: dict = {}
|
||||
open_api_config: dict = Field(default_factory=dict)
|
||||
"""open_api configuration"""
|
||||
instruction: str = DEFAULT_LLM_LINGUA_INSTRUCTION
|
||||
"""The instruction for the LLM"""
|
||||
@@ -49,11 +49,12 @@ class LLMLinguaCompressor(BaseDocumentCompressor):
|
||||
"dynamic_context_compression_ratio": 0.4,
|
||||
}
|
||||
"""Extra compression arguments"""
|
||||
lingua: Any
|
||||
lingua: Any = None
|
||||
"""The instance of the llm linqua"""
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_environment(cls, values: Dict) -> Any:
|
||||
"""Validate that the python package exists in environment."""
|
||||
try:
|
||||
from llmlingua import PromptCompressor
|
||||
@@ -71,9 +72,12 @@ class LLMLinguaCompressor(BaseDocumentCompressor):
|
||||
)
|
||||
return values
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
extra = "forbid"
|
||||
model_config = ConfigDict(
|
||||
arbitrary_types_allowed=True,
|
||||
extra="forbid",
|
||||
populate_by_name=True,
|
||||
protected_namespaces=(),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _format_context(docs: Sequence[Document]) -> List[str]:
|
||||
|
@@ -5,7 +5,7 @@ import numpy as np
|
||||
from langchain_core.callbacks import Callbacks
|
||||
from langchain_core.documents import Document
|
||||
from langchain_core.documents.compressor import BaseDocumentCompressor
|
||||
from langchain_core.pydantic_v1 import Field
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
class RerankRequest:
|
||||
@@ -21,9 +21,9 @@ class OpenVINOReranker(BaseDocumentCompressor):
|
||||
OpenVINO rerank models.
|
||||
"""
|
||||
|
||||
ov_model: Any
|
||||
ov_model: Any = None
|
||||
"""OpenVINO model object."""
|
||||
tokenizer: Any
|
||||
tokenizer: Any = None
|
||||
"""Tokenizer for embedding model."""
|
||||
model_name_or_path: str
|
||||
"""HuggingFace model id."""
|
||||
|
@@ -7,8 +7,8 @@ from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence
|
||||
from langchain.retrievers.document_compressors.base import BaseDocumentCompressor
|
||||
from langchain_core.callbacks.manager import Callbacks
|
||||
from langchain_core.documents import Document
|
||||
from langchain_core.pydantic_v1 import Field, PrivateAttr, root_validator
|
||||
from langchain_core.utils import get_from_dict_or_env
|
||||
from pydantic import ConfigDict, Field, PrivateAttr, model_validator
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from rank_llm.data import Candidate, Query, Request
|
||||
@@ -36,12 +36,14 @@ class RankLLMRerank(BaseDocumentCompressor):
|
||||
"""OpenAI model name."""
|
||||
_retriever: Any = PrivateAttr()
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
extra = "forbid"
|
||||
model_config = ConfigDict(
|
||||
arbitrary_types_allowed=True,
|
||||
extra="forbid",
|
||||
)
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_environment(cls, values: Dict) -> Any:
|
||||
"""Validate python package exists in environment."""
|
||||
|
||||
if not values.get("client"):
|
||||
|
@@ -5,8 +5,8 @@ from typing import Any, Dict, List, Optional, Sequence, Union
|
||||
|
||||
from langchain_core.callbacks.base import Callbacks
|
||||
from langchain_core.documents import BaseDocumentCompressor, Document
|
||||
from langchain_core.pydantic_v1 import root_validator
|
||||
from langchain_core.utils import get_from_dict_or_env
|
||||
from pydantic import ConfigDict, model_validator
|
||||
|
||||
|
||||
class VolcengineRerank(BaseDocumentCompressor):
|
||||
@@ -32,13 +32,15 @@ class VolcengineRerank(BaseDocumentCompressor):
|
||||
top_n: Optional[int] = 3
|
||||
"""Number of documents to return."""
|
||||
|
||||
class Config:
|
||||
allow_population_by_field_name = True
|
||||
arbitrary_types_allowed = True
|
||||
extra = "forbid"
|
||||
model_config = ConfigDict(
|
||||
populate_by_name=True,
|
||||
arbitrary_types_allowed=True,
|
||||
extra="forbid",
|
||||
)
|
||||
|
||||
@root_validator(pre=True)
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def validate_environment(cls, values: Dict) -> Any:
|
||||
"""Validate that api key and python package exists in environment."""
|
||||
|
||||
if not values.get("client"):
|
||||
|
Reference in New Issue
Block a user