MCPcopy
hub / github.com/langchain-ai/langchain / normalize

Method normalize

libs/partners/prompty/langchain_prompty/core.py:112–154  ·  view source on GitHub ↗
(attribute: Any, parent: Path, env_error: bool = True)

Source from the content-addressed store, hash-verified

110
111 @staticmethod
112 def normalize(attribute: Any, parent: Path, env_error: bool = True) -> Any:
113 if isinstance(attribute, str):
114 attribute = attribute.strip()
115 if attribute.startswith("${") and attribute.endswith("}"):
116 variable = attribute[2:-1].split(":")
117 if variable[0] in os.environ.keys():
118 return os.environ[variable[0]]
119 else:
120 if len(variable) > 1:
121 return variable[1]
122 else:
123 if env_error:
124 raise ValueError(
125 f"Variable {variable[0]} not found in environment"
126 )
127 else:
128 return ""
129 elif (
130 attribute.startswith("file:")
131 and Path(parent / attribute.split(":")[1]).exists()
132 ):
133 with open(parent / attribute.split(":")[1], "r") as f:
134 items = json.load(f)
135 if isinstance(items, list):
136 return [Prompty.normalize(value, parent) for value in items]
137 elif isinstance(items, dict):
138 return {
139 key: Prompty.normalize(value, parent)
140 for key, value in items.items()
141 }
142 else:
143 return items
144 else:
145 return attribute
146 elif isinstance(attribute, list):
147 return [Prompty.normalize(value, parent) for value in attribute]
148 elif isinstance(attribute, dict):
149 return {
150 key: Prompty.normalize(value, parent)
151 for key, value in attribute.items()
152 }
153 else:
154 return attribute
155
156
157def param_hoisting(

Callers 1

loadFunction · 0.45

Calls 3

keysMethod · 0.80
existsMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected