Humanlayer integrates seamlessly with LangChain to add human oversight to your AI applications. This guide shows you how to use Humanlayer with LangChain.
Let’s create a customer onboarding assistant that sends emails with human oversight.
from humanlayer import ContactChannel, SlackContactChannelfrom langchain.agents import AgentType, initialize_agentimport langchain_core.tools as langchain_toolsfrom langchain_openai import ChatOpenAIfrom dotenv import load_dotenvfrom humanlayer.core.approval import HumanLayerload_dotenv()hl = HumanLayer( verbose=True, contact_channel=ContactChannel( slack=SlackContactChannel( channel_or_user_id="", experimental_slack_blocks=True,)),# run_id is optional - it can be used to identify the agent in approval history run_id="langchain-customer-email",)task_prompt ="""You are the email onboarding assistant. You check on the progress customersare making and then based on that info, you send friendly and encouragingemails to customers to help them fully onboard into the product.Your task is to send an email to the customer danny@example.com"""defget_info_about_customer(customer_email:str)->str:"""get info about a customer"""return""" This customer has completed most of the onboarding steps, but still needs to invite a few team members before they can be considered fully onboarded"""# require approval to send an email@hl.require_approval()defsend_email(to:str, subject:str, body:str)->str:"""Send an email to a user"""returnf"Email sent to {to} with subject: {subject}"tools =[ langchain_tools.StructuredTool.from_function(get_info_about_customer), langchain_tools.StructuredTool.from_function(send_email),]llm = ChatOpenAI(model="gpt-4", temperature=0)agent = initialize_agent( tools=tools, llm=llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True, handle_parsing_errors=True,)if __name__ =="__main__": result = agent.run(task_prompt)print("\n\n----------Result----------\n\n")print(result)