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

Function load

libs/partners/prompty/langchain_prompty/utils.py:17–118  ·  view source on GitHub ↗

Load a prompty file and return a Prompty object. Args: prompt_path: The path to the prompty file. configuration: The configuration to use. Defaults to "default". Returns: The Prompty object.

(prompt_path: str, configuration: str = "default")

Source from the content-addressed store, hash-verified

15
16
17def load(prompt_path: str, configuration: str = "default") -> Prompty:
18 """Load a prompty file and return a Prompty object.
19
20 Args:
21 prompt_path: The path to the prompty file.
22 configuration: The configuration to use. Defaults to "default".
23
24 Returns:
25 The Prompty object.
26 """
27 file_path = Path(prompt_path)
28 if not file_path.is_absolute():
29 # get caller's path (take into account trace frame)
30 caller = Path(traceback.extract_stack()[-3].filename)
31 file_path = Path(caller.parent / file_path).resolve().absolute()
32
33 # load dictionary from prompty file
34 matter = Frontmatter.read_file(file_path.__fspath__())
35 attributes = matter["attributes"]
36 content = matter["body"]
37
38 # normalize attribute dictionary resolve keys and files
39 attributes = Prompty.normalize(attributes, file_path.parent)
40
41 # load global configuration
42 if "model" not in attributes:
43 attributes["model"] = {}
44
45 # pull model settings out of attributes
46 try:
47 model = ModelSettings(**attributes.pop("model"))
48 except Exception as e:
49 raise ValueError(f"Error in model settings: {e}")
50
51 # pull template settings
52 try:
53 if "template" in attributes:
54 t = attributes.pop("template")
55 if isinstance(t, dict):
56 template = TemplateSettings(**t)
57 # has to be a string denoting the type
58 else:
59 template = TemplateSettings(type=t, parser="prompty")
60 else:
61 template = TemplateSettings(type="mustache", parser="prompty")
62 except Exception as e:
63 raise ValueError(f"Error in template loader: {e}")
64
65 # formalize inputs and outputs
66 if "inputs" in attributes:
67 try:
68 inputs = {
69 k: PropertySettings(**v) for (k, v) in attributes.pop("inputs").items()
70 }
71 except Exception as e:
72 raise ValueError(f"Error in inputs: {e}")
73 else:
74 inputs = {}

Callers 1

executeFunction · 0.70

Calls 8

ModelSettingsClass · 0.85
TemplateSettingsClass · 0.85
PropertySettingsClass · 0.85
param_hoistingFunction · 0.85
PromptyClass · 0.85
popMethod · 0.80
read_fileMethod · 0.45
normalizeMethod · 0.45

Tested by

no test coverage detected