langchain: default to langsmith sdk for pulling prompts, fallback to langchainhub (#24156)

**Description:** Deprecating langchainhub, replacing with langsmith sdk
This commit is contained in:
Maddy Adams 2024-08-11 13:30:52 -07:00 committed by GitHub
parent bc60cddc1b
commit a82c0533f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,28 +3,38 @@
from __future__ import annotations from __future__ import annotations
import json import json
from typing import TYPE_CHECKING, Any, Optional from typing import Any, Optional, Sequence
from langchain_core.load.dump import dumps from langchain_core.load.dump import dumps
from langchain_core.load.load import loads from langchain_core.load.load import loads
from langchain_core.prompts import BasePromptTemplate from langchain_core.prompts import BasePromptTemplate
if TYPE_CHECKING:
from langchainhub import Client
def _get_client(
def _get_client(api_url: Optional[str] = None, api_key: Optional[str] = None) -> Client: api_key: Optional[str] = None,
api_url: Optional[str] = None,
) -> Any:
try: try:
from langchainhub import Client 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
else:
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: except ImportError as e:
raise ImportError( raise ImportError(
"Could not import langchainhub, please install with `pip install " "Could not import langsmith or langchainhub (deprecated),"
"langchainhub`." "please install with `pip install langsmith`."
) from e ) from e
# Client logic will also attempt to load URL/key from environment variables
return Client(api_url, api_key=api_key)
def push( def push(
repo_full_name: str, repo_full_name: str,
@ -32,27 +42,43 @@ def push(
*, *,
api_url: Optional[str] = None, api_url: Optional[str] = None,
api_key: Optional[str] = None, api_key: Optional[str] = None,
parent_commit_hash: Optional[str] = "latest", parent_commit_hash: Optional[str] = None,
new_repo_is_public: bool = True, new_repo_is_public: bool = False,
new_repo_description: str = "", new_repo_description: Optional[str] = None,
readme: Optional[str] = None,
tags: Optional[Sequence[str]] = None,
) -> str: ) -> str:
""" """
Push an object to the hub and returns the URL it can be viewed at in a browser. Push an object to the hub and returns the URL it can be viewed at in a browser.
:param repo_full_name: The full name of the repo to push to in the format of :param repo_full_name: The full name of the prompt to push to in the format of
`owner/repo`. `owner/prompt_name` or `prompt_name`.
:param object: The LangChain to serialize and push to the hub. :param object: The LangChain to serialize and push to the hub.
:param api_url: The URL of the LangChain Hub API. Defaults to the hosted API service :param api_url: The URL of the LangChain Hub API. Defaults to the hosted API service
if you have an api key set, or a localhost instance if not. 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. :param api_key: The API key to use to authenticate with the LangChain Hub API.
:param parent_commit_hash: The commit hash of the parent commit to push to. Defaults :param parent_commit_hash: The commit hash of the parent commit to push to. Defaults
to the latest commit automatically. to the latest commit automatically.
:param new_repo_is_public: Whether the repo should be public. Defaults to :param new_repo_is_public: Whether the prompt should be public. Defaults to
True (Public by default). False (Private by default).
:param new_repo_description: The description of the repo. Defaults to an empty :param new_repo_description: The description of the prompt. Defaults to an empty
string. string.
""" """
client = _get_client(api_url=api_url, api_key=api_key) 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) manifest_json = dumps(object)
message = client.push( message = client.push(
repo_full_name, repo_full_name,
@ -67,20 +93,28 @@ def push(
def pull( def pull(
owner_repo_commit: str, owner_repo_commit: str,
*, *,
include_model: Optional[bool] = None,
api_url: Optional[str] = None, api_url: Optional[str] = None,
api_key: Optional[str] = None, api_key: Optional[str] = None,
) -> Any: ) -> Any:
""" """
Pull an object from the hub and returns it as a LangChain object. Pull an object from the hub and returns it as a LangChain object.
:param owner_repo_commit: The full name of the repo to pull from in the format of :param owner_repo_commit: The full name of the prompt to pull from in the format of
`owner/repo:commit_hash`. `owner/prompt_name:commit_hash` or `owner/prompt_name`
or just `prompt_name` if it's your own prompt.
:param api_url: The URL of the LangChain Hub API. Defaults to the hosted API service :param api_url: The URL of the LangChain Hub API. Defaults to the hosted API service
if you have an api key set, or a localhost instance if not. 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. :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) client = _get_client(api_key=api_key, api_url=api_url)
# Then it's langsmith
if hasattr(client, "pull_prompt"):
response = client.pull_prompt(owner_repo_commit, include_model=include_model)
return response
# Then it's langchainhub
if hasattr(client, "pull_repo"): if hasattr(client, "pull_repo"):
# >= 0.1.15 # >= 0.1.15
res_dict = client.pull_repo(owner_repo_commit) res_dict = client.pull_repo(owner_repo_commit)
@ -93,6 +127,6 @@ def pull(
obj.metadata["lc_hub_commit_hash"] = res_dict["commit_hash"] obj.metadata["lc_hub_commit_hash"] = res_dict["commit_hash"]
return obj return obj
# Then it's < 0.1.15 # Then it's < 0.1.15 langchainhub
resp: str = client.pull(owner_repo_commit) resp: str = client.pull(owner_repo_commit)
return loads(resp) return loads(resp)