Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Gebruik Databricks Unity Catalog om SQL- en Python-functies te integreren als hulpprogramma's in LangChain- en LangGraph-werkstromen. Deze integratie combineert het beheer van Unity Catalog met LangChain-mogelijkheden om krachtige LLM-toepassingen te bouwen.
Behoeften
- Installeer Python 3.10 en hoger.
LangChain integreren met Databricks Unity Catalog
In dit voorbeeld maakt u een Unity Catalog-hulpprogramma, test u de functionaliteit en voegt u deze toe aan een agent. Voer de volgende code uit in een Databricks-notebook.
Afhankelijkheden installeren
Installeer AI-pakketten van Unity Catalog met databricks optioneel en installeer het LangChain-integratiepakket.
In dit voorbeeld wordt LangChain gebruikt, maar een vergelijkbare benadering kan worden toegepast op andere bibliotheken. Zie hoe je Unity Catalog-hulpprogramma's integreert met generatieve AI-frameworks van derden.
# 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()
De Databricks-functieclient initialiseren
Initialiseer de Databricks-functieclient.
from unitycatalog.ai.core.base import get_uc_function_client
client = get_uc_function_client()
De logica van het hulpprogramma definiƫren
Maak een Unity Catalog-functie die de logica van het hulpprogramma bevat.
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
)
De functie testen
Test uw functie om te controleren of deze werkt zoals verwacht:
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'
De functie verpakken met behulp van de UCFunctionToolKit
Wikkel de functie in met behulp van de UCFunctionToolkit om deze toegankelijk te maken voor agent authoring libraries. De toolkit zorgt voor consistentie in verschillende bibliotheken en voegt nuttige functies toe, zoals automatisch traceren voor retrievers.
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
Het hulpprogramma in een agent gebruiken
Voeg het hulpprogramma toe aan een LangChain-agent met behulp van de tools eigenschap van UCFunctionToolkit.
In dit voorbeeld wordt een eenvoudige agent gemaakt met behulp van de API van AgentExecutor LangChain voor het gemak. Gebruik voor productiebelastingen de agent-ontwerpwerkstroom zoals te zien in ResponsesAgent voorbeelden.
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?"})