mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-23 19:39:58 +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:
@@ -7,6 +7,7 @@ from __future__ import annotations
|
||||
import json
|
||||
from typing import Any, ClassVar, Dict, List, Optional, Type
|
||||
|
||||
import pydantic
|
||||
from langchain.base_language import BaseLanguageModel
|
||||
from langchain.chains.base import Chain
|
||||
from langchain.chains.llm import LLMChain
|
||||
@@ -14,7 +15,6 @@ from langchain.output_parsers import PydanticOutputParser
|
||||
from langchain_core.callbacks.manager import CallbackManagerForChainRun
|
||||
from langchain_core.prompts.prompt import PromptTemplate
|
||||
|
||||
from langchain_experimental import pydantic_v1 as pydantic
|
||||
from langchain_experimental.cpal.constants import Constant
|
||||
from langchain_experimental.cpal.models import (
|
||||
CausalModel,
|
||||
|
@@ -4,16 +4,17 @@ import re
|
||||
from typing import Any, List, Optional, Union
|
||||
|
||||
from langchain_community.graphs.networkx_graph import NetworkxEntityGraph
|
||||
|
||||
from langchain_experimental.cpal.constants import Constant
|
||||
from langchain_experimental.pydantic_v1 import (
|
||||
from pydantic import (
|
||||
BaseModel,
|
||||
ConfigDict,
|
||||
Field,
|
||||
PrivateAttr,
|
||||
root_validator,
|
||||
validator,
|
||||
field_validator,
|
||||
model_validator,
|
||||
)
|
||||
|
||||
from langchain_experimental.cpal.constants import Constant
|
||||
|
||||
|
||||
class NarrativeModel(BaseModel):
|
||||
"""
|
||||
@@ -24,7 +25,7 @@ class NarrativeModel(BaseModel):
|
||||
story_hypothetical: str
|
||||
story_plot: str # causal stack of operations
|
||||
|
||||
@validator("*", pre=True)
|
||||
@field_validator("*", mode="before")
|
||||
def empty_str_to_none(cls, v: str) -> Union[str, None]:
|
||||
"""Empty strings are not allowed"""
|
||||
if v == "":
|
||||
@@ -43,10 +44,11 @@ class EntityModel(BaseModel):
|
||||
# TODO: generalize to multivariate math
|
||||
# TODO: acyclic graph
|
||||
|
||||
class Config:
|
||||
validate_assignment = True
|
||||
model_config = ConfigDict(
|
||||
validate_assignment=True,
|
||||
)
|
||||
|
||||
@validator("name")
|
||||
@field_validator("name")
|
||||
def lower_case_name(cls, v: str) -> str:
|
||||
v = v.lower()
|
||||
return v
|
||||
@@ -73,7 +75,7 @@ class EntitySettingModel(BaseModel):
|
||||
attribute: str = Field(description="name of the attribute to be calculated")
|
||||
value: float = Field(description="entity's attribute value (calculated)")
|
||||
|
||||
@validator("name")
|
||||
@field_validator("name")
|
||||
def lower_case_transform(cls, v: str) -> str:
|
||||
v = v.lower()
|
||||
return v
|
||||
@@ -107,7 +109,7 @@ class InterventionModel(BaseModel):
|
||||
entity_settings: List[EntitySettingModel]
|
||||
system_settings: Optional[List[SystemSettingModel]] = None
|
||||
|
||||
@validator("system_settings")
|
||||
@field_validator("system_settings")
|
||||
def lower_case_name(cls, v: str) -> Union[str, None]:
|
||||
if v is not None:
|
||||
raise NotImplementedError("system_setting is not implemented yet")
|
||||
@@ -152,10 +154,9 @@ class StoryModel(BaseModel):
|
||||
# TODO: when langchain adopts pydantic.v2 replace w/ `__post_init__`
|
||||
# misses hints github.com/pydantic/pydantic/issues/1729#issuecomment-1300576214
|
||||
|
||||
# TODO: move away from `root_validator` since it is deprecated in pydantic v2
|
||||
# and causes mypy type-checking failures (hence the `type: ignore`)
|
||||
@root_validator # type: ignore[call-overload]
|
||||
def check_intervention_is_valid(cls, values: dict) -> dict:
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def check_intervention_is_valid(cls, values: dict) -> Any:
|
||||
valid_names = [e.name for e in values["causal_operations"].entities]
|
||||
for setting in values["intervention"].entity_settings:
|
||||
if setting.name not in valid_names:
|
||||
|
Reference in New Issue
Block a user