langchain[patch]: attach hub metadata (#18830)

This commit is contained in:
Erick Friis 2024-03-08 18:40:49 -08:00 committed by GitHub
parent 34b31a8cc7
commit b48865bf94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 3 deletions

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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"
)