Freigeben über


Integrieren von LangChain in Databricks Unity-Katalogtools

Verwenden Sie databricks Unity Catalog, um SQL- und Python-Funktionen als Tools in LangChain- und LangGraph-Workflows zu integrieren. Diese Integration kombiniert die Governance des Unity-Katalogs mit LangChain-Funktionen, um leistungsstarke LLM-basierte Anwendungen zu erstellen.

Anforderungen

  • Installieren Sie Python 3.10 und höher.

Integrieren von LangChain in Databricks Unity-Katalog

In diesem Beispiel erstellen Sie ein Unity Catalog-Tool, testen seine Funktionalität und fügen es einem Agent hinzu. Führen Sie den folgenden Code in einem Databricks-Notizbuch aus.

Installieren von Abhängigkeiten

Installieren Sie Unity Catalog AI-Pakete mit den optionalen Databricks-Paketen, und installieren Sie das LangChain-Integrationspaket.

In diesem Beispiel wird LangChain verwendet, aber ein ähnlicher Ansatz kann auf andere Bibliotheken angewendet werden. Siehe Integration von Unity-Katalogtools in generative KI-Frameworks von Drittanbietern.

# 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()

Initialisieren des Databricks-Funktionsclients

Initialisieren Sie den Databricks-Funktionsclient.

from unitycatalog.ai.core.base import get_uc_function_client

client = get_uc_function_client()

Definieren der Logik des Tools

Erstellen Sie eine Unity-Katalogfunktion, die die Logik des Tools enthält.


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
)

Testen der Funktion

Testen Sie Ihre Funktion so, dass sie wie erwartet funktioniert:

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'

Die Funktion mithilfe des UCFunctionToolKit umschließen

Umschließen Sie die Funktion mithilfe des UCFunctionToolkit, um sie für die Agenterstellungsbibliotheken zugänglich zu machen. Das Toolkit sorgt für Konsistenz in verschiedenen Bibliotheken und fügt hilfreiche Funktionen wie das automatische Tracking für Retriever hinzu.

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

Verwenden des Tools in einem Agent

Fügen Sie das Tool einem LangChain-Agent mithilfe der Eigenschaft tools aus dem UCFunctionToolkit hinzu.

In diesem Beispiel wird der Einfachheit halber ein einfacher Agent mit der LangChain-API AgentExecutor erstellt. Verwenden Sie für Produktionsworkloads den Workflow für die Agenterstellung, der in ResponsesAgent-Beispielen zu sehen ist.

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?"})