langchain
langchain copied to clipboard
Error try_load_from_hub could not find file
I'm on Windows 10 and one of the examples from the Documentation Load a prompt template from LangChainHub has the following code:
from langchain.prompts import load_prompt
prompt = load_prompt("lc://prompts/conversation/prompt.json")
prompt.format(history="", input="What is 1 + 1?")
Which in Windows produces the following error:
(venv) PS D:\Usuarios\Dev01\Documents\GitHub\almohada\chat> python prompts.py
Traceback (most recent call last):
File "D:\Usuarios\Dev01\Documents\GitHub\almohada\chat\prompts.py", line 58, in <module>
prompt = load_prompt("lc://prompts/conversation/prompt.json")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Usuarios\Dev01\Documents\GitHub\almohada\chat\venv\Lib\site-packages\langchain\prompts\loading.py", line 120, in load_prompt
if hub_result := try_load_from_hub(
^^^^^^^^^^^^^^^^^^
File "D:\Usuarios\Dev01\Documents\GitHub\almohada\chat\venv\Lib\site-packages\langchain\utilities\loading.py", line 44, in try_load_from_hub
raise ValueError(f"Could not find file at {full_url}")
ValueError: Could not find file at https://raw.githubusercontent.com/hwchase17/langchain-hub/master/prompts\conversation\prompt.json
This is caused by line 41 in utilities/loading.py
full_url = urljoin(URL_BASE.format(ref=ref), str(remote_path))
I have not tested this on another OS but in my mind the fix it's the following:
def try_load_from_hub(
path: Union[str, Path],
loader: Callable[[str], T],
valid_prefix: str,
valid_suffixes: Set[str],
**kwargs: Any,
) -> Optional[T]:
"""Load configuration from hub. Returns None if path is not a hub path."""
if not isinstance(path, str) or not (match := HUB_PATH_RE.match(path)):
return None
ref, remote_path_str = match.groups()
ref = ref[1:] if ref else DEFAULT_REF
remote_path = Path(remote_path_str)
if remote_path.parts[0] != valid_prefix:
return None
if remote_path.suffix[1:] not in valid_suffixes:
raise ValueError("Unsupported file type.")
full_url = urljoin(URL_BASE.format(ref=ref), remote_path_str) # here, instead of stringifying the remote_path just use the one extracted in the line above
r = requests.get(full_url, timeout=5)
if r.status_code != 200:
raise ValueError(f"Could not find file at {full_url}")
with tempfile.TemporaryDirectory() as tmpdirname:
file = Path(tmpdirname) / remote_path.name
with open(file, "wb") as f:
f.write(r.content)
return loader(str(file), **kwargs)
I'd feedback on the solution. Since I can't test on all possible environments I'm not 100% sure about it