Support for Unity Catalog functions as Gemini tools
Project description
Using Unity Catalog AI with the Gemini SDK
You can use the Unity Catalog AI package with the Gemini SDK to utilize functions that are defined in Unity Catalog to be used as tools within Gemini LLM calls.
Installation
Client Library
To use this package with Unity Catalog, you will need to install:
pip install unitycatalog-gemini
To use this package with Databricks Unity Catalog, you will need to install:
pip install unitycatalog-gemini[databricks]
Getting started
Creating a Unity Catalog Client
To interact with your Unity Catalog server, initialize the UnitycatalogFunctionClient as shown below:
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:
# replace with your own catalog and schema
CATALOG = "catalog"
SCHEMA = "schema"
func_name = f"{CATALOG}.{SCHEMA}.add_numbers"
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 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.
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)
Using the Function as a GenAI Tool
Create a UCFunctionToolkit instance
Tool use through the Google GenAI SDK allows you to connect external client-side tools and functions to provide Gemini with a greater range of capabilities to augment its ability to respond to user messages.
To begin, we will need an instance of the tool function interface from the unitycatalog.ai.gemini toolkit.
from unitycatalog.ai.gemini.toolkit import UCFunctionToolkit
# Create an instance of the toolkit with the function that was created earlier.
toolkit = UCFunctionToolkit(function_names=[func_name], client=client)
# Access the tool definitions that are in the interface that Gemini's SDK expects
tools = toolkit.generate_callable_tool_list()
Now that we have the defined tools from Unity Catalog, we can directly pass this definition into a messages request.
Use the tools within a request to Gemini models
When you send a query to the Gemini model, it will automatically detect if it needs to call a tool (your UC function) to answer the question:
# Interface with Gemini via their SDK
from google.generativeai import GenerativeModel
multi = "What is 49 + 82?"
model = GenerativeModel(
model_name="gemini-2.0-flash-exp", tools=tools
)
chat = model.start_chat(enable_automatic_function_calling=True)
response = chat.send_message(multi)
print(response)
Showing Details of the Tool Call
You can review the conversation history and see how the LLM decided to call the function:
for content in chat.history:
print(content.role, "->", [type(part).to_dict(part) for part in content.parts])
print("-" * 80)
Manually execute function calls
if you prefer more control, you can manually detect and execute function calls:
from google.generativeai.types import content_types
from unitycatalog.ai.gemini.utils import get_function_calls,generate_tool_call_messages
history = []
question = "What is 23 + 99?"
content = content_types.to_content(question)
if not content.role:
content.role = "user"
history.append(content)
response = model.generate_content(
history)
while function_calls := get_function_calls(response):
history , function_calls = generate_tool_call_messages(model=model ,response= response ,conversation_history = history )
response = model.generate_content(history)
response
Configurations for Databricks-only UC function execution
We provide configurations for the Databricks Client to control the function execution behaviors, check function execution arguments section.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unitycatalog_gemini-0.1.0.tar.gz.
File metadata
- Download URL: unitycatalog_gemini-0.1.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ed0f870e4ae62e98ffaa7bf64d7fc8a106782b96aacb7e0b29d5ff72505092d
|
|
| MD5 |
ee15974d58609b4644c27235655eb704
|
|
| BLAKE2b-256 |
ae87b3a3f874e6a9cd08326a1d4362e1e668875f93947208324012c204dd0791
|
File details
Details for the file unitycatalog_gemini-0.1.0-py3-none-any.whl.
File metadata
- Download URL: unitycatalog_gemini-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57358ed40ec014bab480de7d8183c05c38e608e3ea31c7333cc147704d8bdad6
|
|
| MD5 |
012f48622cb4f17732a71787e675393a
|
|
| BLAKE2b-256 |
b15fe8f5f31db1b7363fdb7fafa0b3362bebfd32cf612811b631008711ce4738
|