From b48865bf94d4d738504bcd10accae0fb238b280d Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Fri, 8 Mar 2024 18:40:49 -0800 Subject: [PATCH] langchain[patch]: attach hub metadata (#18830) --- libs/langchain/langchain/hub.py | 14 ++++++++++++-- libs/langchain/poetry.lock | 17 ++++++++++++++++- libs/langchain/pyproject.toml | 1 + .../tests/integration_tests/test_hub.py | 15 +++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 libs/langchain/tests/integration_tests/test_hub.py diff --git a/libs/langchain/langchain/hub.py b/libs/langchain/langchain/hub.py index 25c5598a1693c..ca1af1ab99749 100644 --- a/libs/langchain/langchain/hub.py +++ b/libs/langchain/langchain/hub.py @@ -1,10 +1,13 @@ """Interface with the LangChain Hub.""" + from __future__ import annotations +import json from typing import TYPE_CHECKING, Any, Optional from langchain_core.load.dump import dumps from langchain_core.load.load import loads +from langchain_core.prompts import BasePromptTemplate if TYPE_CHECKING: from langchainhub import Client @@ -77,5 +80,12 @@ def pull( :param api_key: The API key to use to authenticate with the LangChain Hub API. """ client = _get_client(api_url=api_url, api_key=api_key) - resp: str = client.pull(owner_repo_commit) - return loads(resp) + res_dict = client.pull_repo(owner_repo_commit) + obj = loads(json.dumps(res_dict["manifest"])) + if isinstance(obj, BasePromptTemplate): + if obj.metadata is None: + obj.metadata = {} + obj.metadata["lc_hub_owner"] = res_dict["owner"] + obj.metadata["lc_hub_repo"] = res_dict["repo"] + obj.metadata["lc_hub_commit_hash"] = res_dict["commit_hash"] + return obj diff --git a/libs/langchain/poetry.lock b/libs/langchain/poetry.lock index 2107c381cbec3..37361fed9bef7 100644 --- a/libs/langchain/poetry.lock +++ b/libs/langchain/poetry.lock @@ -3523,6 +3523,21 @@ extended-testing = ["lxml (>=5.1.0,<6.0.0)"] type = "directory" url = "../text-splitters" +[[package]] +name = "langchainhub" +version = "0.1.15" +description = "The LangChain Hub API client" +optional = false +python-versions = ">=3.8.1,<4.0" +files = [ + {file = "langchainhub-0.1.15-py3-none-any.whl", hash = "sha256:89a0951abd1db255e91c6d545d092a598fc255aa865d1ffc3ce8f93bbeae60e7"}, + {file = "langchainhub-0.1.15.tar.gz", hash = "sha256:fa3ff81a31946860f84c119f1e2f6b7c7707e2bd7ed2394a7313b286d59f3bda"}, +] + +[package.dependencies] +requests = ">=2,<3" +types-requests = ">=2.31.0.2,<3.0.0.0" + [[package]] name = "langsmith" version = "0.1.17" @@ -9159,4 +9174,4 @@ text-helpers = ["chardet"] [metadata] lock-version = "2.0" python-versions = ">=3.8.1,<4.0" -content-hash = "2ae3ce56a55cf274b9a22afab29637260681470c1f27517540bf1770cc9563b0" +content-hash = "2624db6d40913f97c10fbc494886435849e70d20c59eb1c5a44b95856cd39857" diff --git a/libs/langchain/pyproject.toml b/libs/langchain/pyproject.toml index 2ccd39cd17904..010831ecc0446 100644 --- a/libs/langchain/pyproject.toml +++ b/libs/langchain/pyproject.toml @@ -172,6 +172,7 @@ anthropic = "^0.3.11" langchain-core = {path = "../core", develop = true} langchain-community = {path = "../community", develop = true} langchain-text-splitters = {path = "../text-splitters", develop = true} +langchainhub = "^0.1.15" [tool.poetry.group.lint] optional = true diff --git a/libs/langchain/tests/integration_tests/test_hub.py b/libs/langchain/tests/integration_tests/test_hub.py new file mode 100644 index 0000000000000..4d90011afccda --- /dev/null +++ b/libs/langchain/tests/integration_tests/test_hub.py @@ -0,0 +1,15 @@ +from langchain_core.prompts import ChatPromptTemplate + +from langchain import hub + + +def test_hub_pull_public_prompt() -> None: + prompt = hub.pull("efriis/my-first-prompt") + assert isinstance(prompt, ChatPromptTemplate) + assert prompt.metadata is not None + assert prompt.metadata["lc_hub_owner"] == "efriis" + assert prompt.metadata["lc_hub_repo"] == "my-first-prompt" + assert ( + prompt.metadata["lc_hub_commit_hash"] + == "52668c2f392f8f52d2fc0d6b60cb964e3961934fdbd5dbe72b62926be6b51742" + )