12
12
from langchain .prompts .few_shot import FewShotPromptTemplate
13
13
from langchain .prompts .prompt import PromptTemplate
14
14
15
+ URL_BASE = "https://raw.githubusercontent.com/hwchase17/langchain-hub/master/prompts/"
16
+
15
17
16
18
def load_prompt_from_config (config : dict ) -> BasePromptTemplate :
17
19
"""Get the right type from the config and load it accordingly."""
@@ -93,7 +95,16 @@ def _load_prompt(config: dict) -> PromptTemplate:
93
95
return PromptTemplate (** config )
94
96
95
97
96
- def load_prompt (file : Union [str , Path ]) -> BasePromptTemplate :
98
+ def load_prompt (path : Union [str , Path ]) -> BasePromptTemplate :
99
+ """Unified method for loading a prompt from LangChainHub or local fs."""
100
+ if isinstance (path , str ) and path .startswith ("lc://prompts" ):
101
+ path = path .lstrip ("lc://prompts/" )
102
+ return _load_from_hub (path )
103
+ else :
104
+ return _load_prompt_from_file (path )
105
+
106
+
107
+ def _load_prompt_from_file (file : Union [str , Path ]) -> BasePromptTemplate :
97
108
"""Load prompt from file."""
98
109
# Convert file to Path object.
99
110
if isinstance (file , str ):
@@ -125,10 +136,7 @@ def load_prompt(file: Union[str, Path]) -> BasePromptTemplate:
125
136
return load_prompt_from_config (config )
126
137
127
138
128
- URL_BASE = "https://raw.githubusercontent.com/hwchase17/langchain-hub/master/prompts/"
129
-
130
-
131
- def load_from_hub (path : str ) -> BasePromptTemplate :
139
+ def _load_from_hub (path : str ) -> BasePromptTemplate :
132
140
"""Load prompt from hub."""
133
141
suffix = path .split ("." )[- 1 ]
134
142
if suffix not in {"py" , "json" , "yaml" }:
@@ -141,4 +149,4 @@ def load_from_hub(path: str) -> BasePromptTemplate:
141
149
file = tmpdirname + "/prompt." + suffix
142
150
with open (file , "wb" ) as f :
143
151
f .write (r .content )
144
- return load_prompt (file )
152
+ return _load_prompt_from_file (file )
0 commit comments