使用 Databricks Unity Catalog 将 SQL 和 Python 函数集成为 LlamaIndex 工作流中的工具。 此集成将 Unity Catalog 治理与 LlamaIndex 的功能相结合,以索引和查询 LLM 的大型数据集。
要求
- 安装 Python 3.10 或更高版本。
将 Unity Catalog 工具与 LlamaIndex 集成
在笔记本或 Python 脚本中运行以下代码以创建 Unity Catalog 工具并在 LlamaIndex 代理中使用它。
安装适用于 LlamaIndex 的 Databricks Unity Catalog 集成包。
%pip install unitycatalog-llamaindex[databricks] dbutils.library.restartPython()创建 Unity 目录函数客户端的实例。
from unitycatalog.ai.core.base import get_uc_function_client client = get_uc_function_client()创建用 Python 编写的 Unity 目录函数。
CATALOG = "your_catalog" SCHEMA = "your_schema" func_name = f"{CATALOG}.{SCHEMA}.code_function" def code_function(code: str) -> str: """ Runs Python code. Args: code (str): The Python code to run. Returns: str: The result of running the Python code. """ import sys from io import StringIO stdout = StringIO() sys.stdout = stdout exec(code) return stdout.getvalue() client.create_python_function( func=code_function, catalog=CATALOG, schema=SCHEMA, replace=True )将 Unity Catalog 函数的实例创建为工具包,并运行它以验证该工具是否正常运行。
from unitycatalog.ai.llama_index.toolkit import UCFunctionToolkit import mlflow # Enable traces mlflow.llama_index.autolog() # Create a UCFunctionToolkit that includes the UC function toolkit = UCFunctionToolkit(function_names=[func_name]) # Fetch the tools stored in the toolkit tools = toolkit.tools python_exec_tool = tools[0] # Run the tool directly result = python_exec_tool.call(code="print(1 + 1)") print(result) # Outputs: {"format": "SCALAR", "value": "2\n"}通过将 Unity Catalog 函数定义为 LlamaIndex 工具集合的一部分,在 LlamaIndex ReActAgent 中使用该工具。 然后通过调用 LlamaIndex 工具集合来验证代理是否行为正常。
from llama_index.llms.openai import OpenAI from llama_index.core.agent import ReActAgent llm = OpenAI() agent = ReActAgent.from_tools(tools, llm=llm, verbose=True) agent.chat("Please run the following python code: `print(1 + 1)`")