メインコンテンツまでスキップ

Unity Catalogツールをサードパーティの生成AIフレームワークと統合してください。

Unity Catalog AI エージェントツールは、LangChain、LlamaIndex、OpenAI、Anthropic などの人気のある生成AIライブラリで使用できます。これらの統合は、Unity Catalog ツールのガバナンスとサードパーティのエージェントオーサリングフレームワークの機能を組み合わせたものです。例えば:

  • LangChainでは、Unity Catalog 関数は、データをクエリしたり変換したりするタスクを実行するために、エージェントのワークフローの一部になります。
  • OpenAIまたはAnthropicの統合では、実行中にAIモデルによって関数が直接呼び出されます。

次のタブでフレームワークを選択し、Unity Catalog ツールを作成してそのフレームワークで使用します。Databricks ノートブックまたは Python スクリプトでコードを実行します。

要件

  • Python 3.10 以降をインストールします。

Databricks Unity Catalog を使用して、SQL 関数と Python 関数を LangChain および LangGraph ワークフローのツールとして統合します。この統合により、Unity Catalog のガバナンスと LangChain の機能が組み合わされ、強力な LLM ベースのアプリケーションを構築できます。

この例では、Unity Catalog ツールを作成し、その機能をテストして、エージェントに追加します。

依存関係をインストールする

Databricks オプションを使用して Unity Catalog AI パッケージをインストールし、LangChain 統合パッケージをインストールします。

Python
# Install the Unity Catalog AI integration package with the Databricks extra
%pip install unitycatalog-langchain[databricks]

# Install Databricks Langchain integration package
%pip install databricks-langchain
dbutils.library.restartPython()

Databricks ファンクションクライアントを初期化

Databricks 関数クライアントを初期化します。

Python
from unitycatalog.ai.core.base import get_uc_function_client

client = get_uc_function_client()

ツールのロジックを定義します。

ツールのロジックを含む Unity Catalog 関数を作成します。

Python

CATALOG = "my_catalog"
SCHEMA = "my_schema"

def add_numbers(number_1: float, number_2: float) -> float:
"""
A function that accepts two floating point numbers adds them,
and returns the resulting sum as a float.

Args:
number_1 (float): The first of the two numbers to add.
number_2 (float): The second of the two numbers to add.

Returns:
float: The sum of the two input numbers.
"""
return number_1 + number_2

function_info = client.create_python_function(
func=add_numbers,
catalog=CATALOG,
schema=SCHEMA,
replace=True
)

関数をテストする

期待通りに機能することを確認するために、関数をテストしてください。

Python
result = client.execute_function(
function_name=f"{CATALOG}.{SCHEMA}.add_numbers",
parameters={"number_1": 36939.0, "number_2": 8922.4}
)

result.value # OUTPUT: '45861.4'

UCFunctionToolKitを使用して関数をラップします。

UCFunctionToolkitを使用して関数をラップして、エージェントオーサリングライブラリからアクセスできるようにします。このツールキットは、異なるライブラリ間での一貫性を確保し、レトリーバーの自動トレースなどの便利な機能を追加します。

Python
from databricks_langchain import UCFunctionToolkit

# Create a toolkit with the Unity Catalog function
func_name = f"{CATALOG}.{SCHEMA}.add_numbers"
toolkit = UCFunctionToolkit(function_names=[func_name])

tools = toolkit.tools

エージェントでツールを使用します

UCFunctionToolkittools プロパティを使用して、ツールを LangChain エージェントに追加します。

この例では、わかりやすくするためにLangChainのAgentExecutor APIを使用してシンプルなエージェントを作成します。本番運用ワークロードの場合は、「AIエージェントを作成してDatabricks Appsにデプロイする」に示されているエージェント作成ワークフローを使用してください。

Python
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain.prompts import ChatPromptTemplate
from databricks_langchain import (
ChatDatabricks,
UCFunctionToolkit,
)
import mlflow

# Initialize the LLM (replace with your LLM of choice, if desired)
LLM_ENDPOINT_NAME = "databricks-meta-llama-3-3-70b-instruct"
llm = ChatDatabricks(endpoint=LLM_ENDPOINT_NAME, temperature=0.1)

# Define the prompt
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a helpful assistant. Make sure to use tools for additional functionality.",
),
("placeholder", "{chat_history}"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
]
)

# Enable automatic tracing
mlflow.langchain.autolog()

# Define the agent, specifying the tools from the toolkit above
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": "What is 36939.0 + 8922.4?"})