使用 Databricks Unity 目錄,將 SQL 和 Python 函式整合為 LangChain 和 LangGraph 工作流程中的工具。 此整合結合了 Unity 目錄的治理與 LangChain 功能,以建置功能強大的 LLM 型應用程式。
需求
- 安裝 Python 3.10 和更新版本。
整合 LangChain 與 Databricks Unity Catalog
在此範例中,您會建立 Unity 目錄工具、測試其功能,並將其新增至代理程式。 在 Databricks 筆記本中執行下列程式代碼。
安裝依賴項
使用 Databricks 選擇性安裝 Unity 目錄 AI 套件,並安裝 LangChain 整合套件。
此範例使用 LangChain,但類似的方法也可以套用至其他函式庫。 請參閱 整合 Unity 目錄工具與第三方產生 AI 架構。
# 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 函式用戶端。
from unitycatalog.ai.core.base import get_uc_function_client
client = get_uc_function_client()
定義工具的邏輯
建立包含工具邏輯的 Unity 目錄函式。
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
)
測試函式
測試您的函式,以確保它如預期地運行:
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 封裝函式,以便代理程式開發庫存取。 此工具組可確保不同庫的一致性,並新增實用的功能,例如檢索器的自動追踪。
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
在代理程式中使用工具
使用 tools 中的 UCFunctionToolkit 屬性將工具新增至 LangChain 代理程式。
本範例使用 LangChain 的 AgentExecutor API 撰寫簡單的代理程式,以求簡單。 針對生產工作負載,請使用範例中所ResponsesAgent見的代理程式撰寫工作流程。
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?"})