mirror of
https://github.com/hwchase17/langchain.git
synced 2026-07-12 02:55:21 +00:00
@@ -2,25 +2,49 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections.abc import Sequence
|
||||
from typing import Any, Optional
|
||||
from typing import Any, Literal, Optional
|
||||
|
||||
from langchain_core._api.deprecation import deprecated
|
||||
from langsmith import Client as LangSmithClient
|
||||
from langchain_core.load.dump import dumps
|
||||
from langchain_core.load.load import loads
|
||||
from langchain_core.prompts import BasePromptTemplate
|
||||
|
||||
|
||||
def _get_client(
|
||||
api_key: Optional[str] = None,
|
||||
api_url: Optional[str] = None,
|
||||
) -> Any:
|
||||
try:
|
||||
from langsmith import Client as LangSmithClient
|
||||
|
||||
ls_client = LangSmithClient(api_url, api_key=api_key)
|
||||
if hasattr(ls_client, "push_prompt") and hasattr(ls_client, "pull_prompt"):
|
||||
return ls_client
|
||||
from langchainhub import Client as LangChainHubClient
|
||||
|
||||
return LangChainHubClient(api_url, api_key=api_key)
|
||||
except ImportError:
|
||||
try:
|
||||
from langchainhub import Client as LangChainHubClient
|
||||
|
||||
return LangChainHubClient(api_url, api_key=api_key)
|
||||
except ImportError as e:
|
||||
msg = (
|
||||
"Could not import langsmith or langchainhub (deprecated),"
|
||||
"please install with `pip install langsmith`."
|
||||
)
|
||||
raise ImportError(msg) from e
|
||||
|
||||
|
||||
@deprecated(
|
||||
since="0.3.30",
|
||||
message="langchain.hub.push is deprecated. Use the LangSmith SDK instead.",
|
||||
pending=True,
|
||||
)
|
||||
def push(
|
||||
repo_full_name: str,
|
||||
object: Any, # noqa: A002
|
||||
*,
|
||||
api_url: Optional[str] = None,
|
||||
api_key: Optional[str] = None,
|
||||
parent_commit_hash: str = "latest",
|
||||
parent_commit_hash: Optional[str] = None,
|
||||
new_repo_is_public: bool = False,
|
||||
new_repo_description: Optional[str] = None,
|
||||
readme: Optional[str] = None,
|
||||
@@ -41,20 +65,33 @@ def push(
|
||||
:param new_repo_description: The description of the prompt. Defaults to an empty
|
||||
string.
|
||||
"""
|
||||
client = LangSmithClient(api_url, api_key=api_key)
|
||||
return client.push_prompt(
|
||||
client = _get_client(api_key=api_key, api_url=api_url)
|
||||
|
||||
# Then it's langsmith
|
||||
if hasattr(client, "push_prompt"):
|
||||
return client.push_prompt(
|
||||
repo_full_name,
|
||||
object=object,
|
||||
parent_commit_hash=parent_commit_hash,
|
||||
is_public=new_repo_is_public,
|
||||
description=new_repo_description,
|
||||
readme=readme,
|
||||
tags=tags,
|
||||
)
|
||||
|
||||
# Then it's langchainhub
|
||||
manifest_json = dumps(object)
|
||||
return client.push(
|
||||
repo_full_name,
|
||||
object=object,
|
||||
manifest_json,
|
||||
parent_commit_hash=parent_commit_hash,
|
||||
is_public=new_repo_is_public,
|
||||
description=new_repo_description,
|
||||
readme=readme,
|
||||
tags=tags,
|
||||
new_repo_is_public=new_repo_is_public,
|
||||
new_repo_description=new_repo_description,
|
||||
)
|
||||
|
||||
|
||||
@deprecated(
|
||||
since="0.3.30",
|
||||
since="0.3.29",
|
||||
message="langchain.hub.pull is deprecated. Use the LangSmith SDK instead.",
|
||||
pending=True,
|
||||
)
|
||||
@@ -93,5 +130,26 @@ def pull(
|
||||
if you have an api key set, or a localhost instance if not.
|
||||
:param api_key: The API key to use to authenticate with the LangChain Hub API.
|
||||
"""
|
||||
client = LangSmithClient(api_url, api_key=api_key)
|
||||
return client.pull_prompt(owner_repo_commit, include_model=include_model)
|
||||
client = _get_client(api_key=api_key, api_url=api_url)
|
||||
|
||||
# Then it's langsmith
|
||||
if hasattr(client, "pull_prompt"):
|
||||
return client.pull_prompt(owner_repo_commit, include_model=include_model)
|
||||
|
||||
# Then it's langchainhub
|
||||
if hasattr(client, "pull_repo"):
|
||||
# >= 0.1.15
|
||||
res_dict = client.pull_repo(owner_repo_commit)
|
||||
allowed_objects: Literal["all", "core"] = "all" if include_model else "core"
|
||||
obj = loads(json.dumps(res_dict["manifest"]), allowed_objects=allowed_objects)
|
||||
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
|
||||
|
||||
# Then it's < 0.1.15 langchainhub
|
||||
resp: str = client.pull(owner_repo_commit)
|
||||
return loads(resp, allowed_objects="core")
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
from typing import Any, Optional
|
||||
|
||||
from langchain_core._api.deprecation import deprecated
|
||||
from langchain_core.runnables.base import RunnableBindingBase
|
||||
from langchain_core.runnables.utils import Input, Output
|
||||
|
||||
|
||||
@deprecated(
|
||||
since="0.3.30",
|
||||
message="langchain.hub.pull is deprecated. Use the LangSmith SDK instead.",
|
||||
pending=True,
|
||||
)
|
||||
class HubRunnable(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
|
||||
"""An instance of a runnable stored in the LangChain Hub."""
|
||||
|
||||
|
||||
@@ -31,9 +31,7 @@ def _get_messages_from_run_dict(messages: list[dict]) -> list[BaseMessage]:
|
||||
return []
|
||||
first_message = messages[0]
|
||||
if "lc" in first_message:
|
||||
return [
|
||||
load(dumpd(message), allowed_objects="messages") for message in messages
|
||||
]
|
||||
return [load(dumpd(message)) for message in messages]
|
||||
return messages_from_dict(messages)
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ build-backend = "pdm.backend"
|
||||
[project]
|
||||
authors = []
|
||||
license = { text = "MIT" }
|
||||
requires-python = ">=3.10.0,<3.14"
|
||||
requires-python = ">=3.9.0,<4.0.0"
|
||||
dependencies = [
|
||||
"langchain-core>=0.3.85,<1.0.0",
|
||||
"langchain-text-splitters>=0.3.9,<1.0.0",
|
||||
@@ -79,6 +79,7 @@ test_integration = [
|
||||
"urllib3<2.0.0; python_version < \"3.10\"",
|
||||
"wrapt>=1.15.0,<2.0.0",
|
||||
"python-dotenv>=1.0.0,<2.0.0",
|
||||
"langchainhub>=0.1.16,<1.0.0",
|
||||
"langchain-core",
|
||||
"langchain-text-splitters",
|
||||
]
|
||||
@@ -102,6 +103,8 @@ typing = [
|
||||
"numpy>=2.1.0; python_version >= '3.13'",
|
||||
]
|
||||
dev = [
|
||||
"jupyter>=1.0.0,<2.0.0",
|
||||
"playwright>=1.28.0,<2.0.0",
|
||||
"setuptools>=67.6.1,<68.0.0",
|
||||
"langchain-core",
|
||||
"langchain-text-splitters",
|
||||
|
||||
@@ -16,7 +16,7 @@ class TestHubPullDeprecation:
|
||||
mock_client.pull_prompt = MagicMock(return_value=MagicMock())
|
||||
|
||||
with (
|
||||
patch("langchain.hub.LangSmithClient", return_value=mock_client),
|
||||
patch("langchain.hub._get_client", return_value=mock_client),
|
||||
warnings.catch_warnings(record=True) as w,
|
||||
):
|
||||
warnings.simplefilter("always")
|
||||
|
||||
1723
libs/langchain/uv.lock
generated
1723
libs/langchain/uv.lock
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user