Source code for langchain_experimental.cpal.models
from __future__ import annotations  # allows pydantic model to reference itself
import re
from typing import Any, List, Optional, Union
from langchain.graphs.networkx_graph import NetworkxEntityGraph
from langchain_experimental.cpal.constants import Constant
from langchain_experimental.pydantic_v1 import (
    BaseModel,
    Field,
    PrivateAttr,
    root_validator,
    validator,
)
[docs]class NarrativeModel(BaseModel):
    """
    Represent the narrative input as three story elements.
    """
    story_outcome_question: str
    story_hypothetical: str
    story_plot: str  # causal stack of operations
    @validator("*", pre=True)
    def empty_str_to_none(cls, v: str) -> Union[str, None]:
        """Empty strings are not allowed"""
        if v == "":
            return None
        return v
[docs]class EntityModel(BaseModel):
    name: str = Field(description="entity name")
    code: str = Field(description="entity actions")
    value: float = Field(description="entity initial value")
    depends_on: List[str] = Field(default=[], description="ancestor entities")
    # TODO: generalize to multivariate math
    # TODO: acyclic graph
    class Config:
        validate_assignment = True
    @validator("name")
    def lower_case_name(cls, v: str) -> str:
        v = v.lower()
        return v
[docs]class CausalModel(BaseModel):
    attribute: str = Field(description="name of the attribute to be calculated")
    entities: List[EntityModel] = Field(description="entities in the story")