"""Interface with the LangChain Hub."""from__future__importannotationsimportjsonfromtypingimportAny,Optional,Sequencefromlangchain_core.load.dumpimportdumpsfromlangchain_core.load.loadimportloadsfromlangchain_core.promptsimportBasePromptTemplatedef_get_client(api_key:Optional[str]=None,api_url:Optional[str]=None,)->Any:try:fromlangsmithimportClientasLangSmithClientls_client=LangSmithClient(api_url,api_key=api_key)ifhasattr(ls_client,"push_prompt")andhasattr(ls_client,"pull_prompt"):returnls_clientelse:fromlangchainhubimportClientasLangChainHubClientreturnLangChainHubClient(api_url,api_key=api_key)exceptImportError:try:fromlangchainhubimportClientasLangChainHubClientreturnLangChainHubClient(api_url,api_key=api_key)exceptImportErrorase:raiseImportError("Could not import langsmith or langchainhub (deprecated),""please install with `pip install langsmith`.")frome
[docs]defpush(repo_full_name:str,object:Any,*,api_url:Optional[str]=None,api_key:Optional[str]=None,parent_commit_hash:Optional[str]=None,new_repo_is_public:bool=False,new_repo_description:Optional[str]=None,readme:Optional[str]=None,tags:Optional[Sequence[str]]=None,)->str:""" 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 prompt to push to in the format of `owner/prompt_name` or `prompt_name`. :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 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 parent_commit_hash: The commit hash of the parent commit to push to. Defaults to the latest commit automatically. :param new_repo_is_public: Whether the prompt should be public. Defaults to False (Private by default). :param new_repo_description: The description of the prompt. Defaults to an empty string. """client=_get_client(api_key=api_key,api_url=api_url)# Then it's langsmithifhasattr(client,"push_prompt"):returnclient.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 langchainhubmanifest_json=dumps(object)message=client.push(repo_full_name,manifest_json,parent_commit_hash=parent_commit_hash,new_repo_is_public=new_repo_is_public,new_repo_description=new_repo_description,)returnmessage
[docs]defpull(owner_repo_commit:str,*,include_model:Optional[bool]=None,api_url:Optional[str]=None,api_key:Optional[str]=None,)->Any:""" Pull an object from the hub and returns it as a LangChain object. :param owner_repo_commit: The full name of the prompt to pull from in the format of `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 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=_get_client(api_key=api_key,api_url=api_url)# Then it's langsmithifhasattr(client,"pull_prompt"):response=client.pull_prompt(owner_repo_commit,include_model=include_model)returnresponse# Then it's langchainhubifhasattr(client,"pull_repo"):# >= 0.1.15res_dict=client.pull_repo(owner_repo_commit)obj=loads(json.dumps(res_dict["manifest"]))ifisinstance(obj,BasePromptTemplate):ifobj.metadataisNone: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"]returnobj# Then it's < 0.1.15 langchainhubresp:str=client.pull(owner_repo_commit)returnloads(resp)