🦜🔗 Using Unity Catalog AI with LangChain
Integrate Unity Catalog AI with LangChain to seamlessly use Unity Catalog (UC) functions as tools in agent applications. This guide covers installation, setup, and examples to help you get started.
Installation
Section titled “Installation”Install the Unity Catalog AI LangChain integration from PyPI:
pip install unitycatalog-langchain
Prerequisites
Section titled “Prerequisites”- Python version: Python 3.10 or higher is required.
Unity Catalog
Section titled “Unity Catalog”Ensure that you have a functional UC server set up and that you are able to access the catalog and schema where defined functions are stored.
Databricks Unity Catalog
Section titled “Databricks Unity Catalog”To interact with Databricks Unity Catalog, install the optional package dependency when installing the integration package:
pip install unitycatalog-langchain[databricks]
Tutorial
Section titled “Tutorial”Client Setup
Section titled “Client Setup”Create an instance of the Functions Client
from unitycatalog.client import ApiClient, Configurationfrom unitycatalog.ai.core.client import UnitycatalogFunctionClient
config = Configuration()# This is the default address when starting a UnityCatalog server locally. Update this to the uri# of your running UnityCatalog server.config.host = "http://localhost:8080/api/2.1/unity-catalog"
# Create the UnityCatalog clientapi_client = ApiClient(configuration=config)
# Use the UnityCatalog client to create an instance of the AI function clientclient = UnitycatalogFunctionClient(api_client=api_client)
Client Setup - Databricks
Section titled “Client Setup - Databricks”Create an instance of the Unity Catalog Functions client
from unitycatalog.ai.core.databricks import DatabricksFunctionClient
client = DatabricksFunctionClient()
Creating a UC function
Section titled “Creating a UC function”Create a Python function within Unity Catalog
CATALOG = "your_catalog"SCHEMA = "your_schema"
func_name = f"{CATALOG}.{SCHEMA}.code_function"
def code_function(code: str) -> str: """ Executes Python code.
Args: code (str): The python code to execute. Returns: str: The result of the execution of the Python code. """ import sys from io import StringIO stdout = StringIO() sys.stdout = stdout exec(code) return stdout.getvalue()
client.create_python_function( func=code_function, catalog=CATALOG, schema=SCHEMA)
Creating a toolkit instance
Section titled “Creating a toolkit instance”Here we create an instance of our UC function as a toolkit, then verify that the tool is behaving properly by executing the function.
from unitycatalog.ai.langchain.toolkit import UCFunctionToolkit
# Create a UCFunctionToolkit that includes the UC functiontoolkit = UCFunctionToolkit(function_names=[func_name], client=client)
# Fetch the tools stored in the toolkittools = toolkit.toolspython_exec_tool = tools[0]
# Execute the tool directlyresult = python_exec_tool.invoke({"code": "print(1 + 1)"})print(result) # Outputs: 2
Using the tool in a LangChain Agent
Section titled “Using the tool in a LangChain Agent”from langchain.agents import AgentExecutor, create_tool_agentfrom langchain.llms import OpenAIfrom langchain.prompts import ChatPromptTemplate
# Initialize the LLM (replace with your LLM of choice, if desired)llm = OpenAI(temperature=0)
# Define the promptprompt = ChatPromptTemplate.from_messages( [ ( "system", "You are a helpful assistant. Make sure to use tool for information.", ), ("placeholder", "{chat_history}"), ("human", "{input}"), ("placeholder", "{agent_scratchpad}"), ])
# Define the agent, specifying the tools from the toolkit aboveagent = create_tool_calling_agent(llm, tools, prompt)
# Create the agent executoragent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)agent_executor.invoke({"input": "What is 36939 * 8922.4?"})