Skip to content

Commit

Permalink
langchain[patch]: attach hub metadata (#18830)
Browse files Browse the repository at this point in the history
  • Loading branch information
efriis authored Mar 9, 2024
1 parent 34b31a8 commit b48865b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
14 changes: 12 additions & 2 deletions libs/langchain/langchain/hub.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
17 changes: 16 additions & 1 deletion libs/langchain/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/langchain/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions libs/langchain/tests/integration_tests/test_hub.py
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit b48865b

Please sign in to comment.