unitycatalog-langchain


Nameunitycatalog-langchain JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummarySupport for Unity Catalog functions as LangChain tools
upload_time2025-02-13 00:25:34
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 🦜🔗 Using Unity Catalog AI with Langchain

Integrate the Unity Catalog AI package with `Langchain` to allow seamless usage of UC functions as tools in agentic applications.

## Installation

### Client Library

To install the Unity Catalog function client SDK and the `LangChain` (and `LangGraph`) integration, simply install from PyPI:

```sh
pip install unitycatalog-langchain
```

If you are working with **Databricks Unity Catalog**, you can install the optional package:

```sh
pip install unitycatalog-langchain[databricks]
```

## Getting started

### Creating a Unity Catalog Client

To interact with your Unity Catalog server, initialize the `UnitycatalogFunctionClient` as shown below:

```python
import asyncio
from unitycatalog.ai.core.client import UnitycatalogFunctionClient
from unitycatalog.client import ApiClient, Configuration

# Configure the Unity Catalog API client
config = Configuration(
    host="http://localhost:8080/api/2.1/unity-catalog"  # Replace with your UC server URL
)

# Initialize the asynchronous ApiClient
api_client = ApiClient(configuration=config)

# Instantiate the UnitycatalogFunctionClient
uc_client = UnitycatalogFunctionClient(api_client=api_client)

# Example catalog and schema names
CATALOG = "my_catalog"
SCHEMA = "my_schema"
```

### Creating a Unity Catalog Function

You can create a UC function either by providing a Python callable or by submitting a `FunctionInfo` object. Below is an example (recommended) of using the `create_python_function` API that accepts a Python callable (function) as input.

To create a UC function from a Python function, define your function with appropriate type hints and a Google-style docstring:

```python
def add_numbers(a: float, b: float) -> float:
    """
    Adds two numbers and returns the result.

    Args:
        a (float): First number.
        b (float): Second number.

    Returns:
        float: The sum of the two numbers.
    """
    return a + b

# Create the function within the Unity Catalog catalog and schema specified
function_info = uc_client.create_python_function(
    func=add_numbers,
    catalog=CATALOG,
    schema=SCHEMA,
    replace=False,  # Set to True to overwrite if the function already exists
)

print(function_info)
```

### Databricks-managed Unity Catalog

To use Databricks-managed Unity Catalog with this package, follow the [instructions](https://docs.databricks.com/en/dev-tools/cli/authentication.html#authentication-for-the-databricks-cli) to authenticate to your workspace and ensure that your access token has workspace-level privilege for managing UC functions.

#### Client setup

Initialize a client for managing UC functions in a Databricks workspace, and set it as the global client.

```python
from unitycatalog.ai.core.base import set_uc_function_client
from unitycatalog.ai.core.databricks import DatabricksFunctionClient

client = DatabricksFunctionClient()

# sets the default uc function client
set_uc_function_client(client)
```

#### Create a function in UC

Create a python UDF in Unity Catalog with the client

```python
# replace with your own catalog and schema
CATALOG = "catalog"
SCHEMA = "schema"

func_name = f"{CATALOG}.{SCHEMA}.python_exec"
# define the function body in SQL
sql_body = f"""CREATE OR REPLACE FUNCTION {func_name}(code STRING COMMENT 'Python code to execute. Remember to print the final result to stdout.')
RETURNS STRING
LANGUAGE PYTHON
COMMENT 'Executes Python code and returns its stdout.'
AS $$
    import sys
    from io import StringIO
    stdout = StringIO()
    sys.stdout = stdout
    exec(code)
    return stdout.getvalue()
$$
"""

client.create_function(sql_function_body=sql_body)
```

Now the function is created and stored in the corresponding catalog and schema.

## Using the Function as a GenAI Tool

### Create a UCFunctionToolkit instance

[Langchain tools](https://python.langchain.com/v0.2/docs/concepts/#tools) are utilities designed to be called by a model, and UCFunctionToolkit provides the ability to use UC functions as tools that are recognized natively by LangChain.

```python
from unitycatalog.ai.langchain.toolkit import UCFunctionToolkit

# create a UCFunctionToolkit that includes the above UC function
toolkit = UCFunctionToolkit(function_names=[f"{CATALOG}.{SCHEMA}.python_exec"])

# fetch the tools stored in the toolkit
tools = toolkit.tools
python_exec_tool = tools[0]

# execute the tool directly
python_exec_tool.invoke({"code": "print(1)"})
```

### Use the tools in a Langchain Agent

Now we create an agent and use the tools.

```python
from langchain_community.chat_models.databricks import ChatDatabricks
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate

# Use Databricks foundation models
llm = ChatDatabricks(endpoint="databricks-meta-llama-3-1-70b-instruct")
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are a helpful assistant. Make sure to use tool for information.",
        ),
        ("placeholder", "{chat_history}"),
        ("human", "{input}"),
        ("placeholder", "{agent_scratchpad}"),
    ]
)
agent = create_tool_calling_agent(llm, tools, prompt)

# Create the agent executor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "36939 * 8922.4"})
```

### Configurations for Databricks managed UC functions execution

We provide configurations for databricks client to control the function execution behaviors, check [function execution arguments section](../../README.md#function-execution-arguments-configuration).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "unitycatalog-langchain",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "Serena Ruan <serena.ruan@databricks.com>",
    "download_url": "https://files.pythonhosted.org/packages/c1/40/87e3cfd5caf35d2cac01723662728316b6204393cb87fa96690c7d6f7768/unitycatalog_langchain-0.1.1.tar.gz",
    "platform": null,
    "description": "# \ud83e\udd9c\ud83d\udd17 Using Unity Catalog AI with Langchain\n\nIntegrate the Unity Catalog AI package with `Langchain` to allow seamless usage of UC functions as tools in agentic applications.\n\n## Installation\n\n### Client Library\n\nTo install the Unity Catalog function client SDK and the `LangChain` (and `LangGraph`) integration, simply install from PyPI:\n\n```sh\npip install unitycatalog-langchain\n```\n\nIf you are working with **Databricks Unity Catalog**, you can install the optional package:\n\n```sh\npip install unitycatalog-langchain[databricks]\n```\n\n## Getting started\n\n### Creating a Unity Catalog Client\n\nTo interact with your Unity Catalog server, initialize the `UnitycatalogFunctionClient` as shown below:\n\n```python\nimport asyncio\nfrom unitycatalog.ai.core.client import UnitycatalogFunctionClient\nfrom unitycatalog.client import ApiClient, Configuration\n\n# Configure the Unity Catalog API client\nconfig = Configuration(\n    host=\"http://localhost:8080/api/2.1/unity-catalog\"  # Replace with your UC server URL\n)\n\n# Initialize the asynchronous ApiClient\napi_client = ApiClient(configuration=config)\n\n# Instantiate the UnitycatalogFunctionClient\nuc_client = UnitycatalogFunctionClient(api_client=api_client)\n\n# Example catalog and schema names\nCATALOG = \"my_catalog\"\nSCHEMA = \"my_schema\"\n```\n\n### Creating a Unity Catalog Function\n\nYou can create a UC function either by providing a Python callable or by submitting a `FunctionInfo` object. Below is an example (recommended) of using the `create_python_function` API that accepts a Python callable (function) as input.\n\nTo create a UC function from a Python function, define your function with appropriate type hints and a Google-style docstring:\n\n```python\ndef add_numbers(a: float, b: float) -> float:\n    \"\"\"\n    Adds two numbers and returns the result.\n\n    Args:\n        a (float): First number.\n        b (float): Second number.\n\n    Returns:\n        float: The sum of the two numbers.\n    \"\"\"\n    return a + b\n\n# Create the function within the Unity Catalog catalog and schema specified\nfunction_info = uc_client.create_python_function(\n    func=add_numbers,\n    catalog=CATALOG,\n    schema=SCHEMA,\n    replace=False,  # Set to True to overwrite if the function already exists\n)\n\nprint(function_info)\n```\n\n### Databricks-managed Unity Catalog\n\nTo use Databricks-managed Unity Catalog with this package, follow the [instructions](https://docs.databricks.com/en/dev-tools/cli/authentication.html#authentication-for-the-databricks-cli) to authenticate to your workspace and ensure that your access token has workspace-level privilege for managing UC functions.\n\n#### Client setup\n\nInitialize a client for managing UC functions in a Databricks workspace, and set it as the global client.\n\n```python\nfrom unitycatalog.ai.core.base import set_uc_function_client\nfrom unitycatalog.ai.core.databricks import DatabricksFunctionClient\n\nclient = DatabricksFunctionClient()\n\n# sets the default uc function client\nset_uc_function_client(client)\n```\n\n#### Create a function in UC\n\nCreate a python UDF in Unity Catalog with the client\n\n```python\n# replace with your own catalog and schema\nCATALOG = \"catalog\"\nSCHEMA = \"schema\"\n\nfunc_name = f\"{CATALOG}.{SCHEMA}.python_exec\"\n# define the function body in SQL\nsql_body = f\"\"\"CREATE OR REPLACE FUNCTION {func_name}(code STRING COMMENT 'Python code to execute. Remember to print the final result to stdout.')\nRETURNS STRING\nLANGUAGE PYTHON\nCOMMENT 'Executes Python code and returns its stdout.'\nAS $$\n    import sys\n    from io import StringIO\n    stdout = StringIO()\n    sys.stdout = stdout\n    exec(code)\n    return stdout.getvalue()\n$$\n\"\"\"\n\nclient.create_function(sql_function_body=sql_body)\n```\n\nNow the function is created and stored in the corresponding catalog and schema.\n\n## Using the Function as a GenAI Tool\n\n### Create a UCFunctionToolkit instance\n\n[Langchain tools](https://python.langchain.com/v0.2/docs/concepts/#tools) are utilities designed to be called by a model, and UCFunctionToolkit provides the ability to use UC functions as tools that are recognized natively by LangChain.\n\n```python\nfrom unitycatalog.ai.langchain.toolkit import UCFunctionToolkit\n\n# create a UCFunctionToolkit that includes the above UC function\ntoolkit = UCFunctionToolkit(function_names=[f\"{CATALOG}.{SCHEMA}.python_exec\"])\n\n# fetch the tools stored in the toolkit\ntools = toolkit.tools\npython_exec_tool = tools[0]\n\n# execute the tool directly\npython_exec_tool.invoke({\"code\": \"print(1)\"})\n```\n\n### Use the tools in a Langchain Agent\n\nNow we create an agent and use the tools.\n\n```python\nfrom langchain_community.chat_models.databricks import ChatDatabricks\nfrom langchain.agents import AgentExecutor, create_tool_calling_agent\nfrom langchain_core.prompts import ChatPromptTemplate\n\n# Use Databricks foundation models\nllm = ChatDatabricks(endpoint=\"databricks-meta-llama-3-1-70b-instruct\")\nprompt = ChatPromptTemplate.from_messages(\n    [\n        (\n            \"system\",\n            \"You are a helpful assistant. Make sure to use tool for information.\",\n        ),\n        (\"placeholder\", \"{chat_history}\"),\n        (\"human\", \"{input}\"),\n        (\"placeholder\", \"{agent_scratchpad}\"),\n    ]\n)\nagent = create_tool_calling_agent(llm, tools, prompt)\n\n# Create the agent executor\nagent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\nagent_executor.invoke({\"input\": \"36939 * 8922.4\"})\n```\n\n### Configurations for Databricks managed UC functions execution\n\nWe provide configurations for databricks client to control the function execution behaviors, check [function execution arguments section](../../README.md#function-execution-arguments-configuration).\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Support for Unity Catalog functions as LangChain tools",
    "version": "0.1.1",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a2a035e490d49b9684abced53889a436f1205be9ab36ece273060f603f09689f",
                "md5": "82eb360cf037bb40e56855243abe7e05",
                "sha256": "fd01c56b9b71f99a9fa8ad89c2dacab9cd38a3ce0a6caf078faf879876b3ce9c"
            },
            "downloads": -1,
            "filename": "unitycatalog_langchain-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "82eb360cf037bb40e56855243abe7e05",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5093,
            "upload_time": "2025-02-13T00:25:32",
            "upload_time_iso_8601": "2025-02-13T00:25:32.532948Z",
            "url": "https://files.pythonhosted.org/packages/a2/a0/35e490d49b9684abced53889a436f1205be9ab36ece273060f603f09689f/unitycatalog_langchain-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c14087e3cfd5caf35d2cac01723662728316b6204393cb87fa96690c7d6f7768",
                "md5": "6778f17e18c58f6df17b1c06935a8c3e",
                "sha256": "fe004ee07cfbb90f78be927941f400e319f44df9f591b5248f9d0eec81f0624b"
            },
            "downloads": -1,
            "filename": "unitycatalog_langchain-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6778f17e18c58f6df17b1c06935a8c3e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 4751,
            "upload_time": "2025-02-13T00:25:34",
            "upload_time_iso_8601": "2025-02-13T00:25:34.470153Z",
            "url": "https://files.pythonhosted.org/packages/c1/40/87e3cfd5caf35d2cac01723662728316b6204393cb87fa96690c7d6f7768/unitycatalog_langchain-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-13 00:25:34",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "unitycatalog-langchain"
}
        
Elapsed time: 0.40319s