Databricks Unity 카탈로그를 사용하여 SQL 및 Python 함수를 LlamaIndex 워크플로의 도구로 통합합니다. 이 통합은 Unity 카탈로그 거버넌스와 LlamaIndex의 기능을 결합하여 LLM에 대한 큰 데이터 세트를 인덱싱하고 쿼리합니다.
요구 사항
- Python 3.10 이상을 설치합니다.
LlamaIndex와 Unity 카탈로그 도구 통합
Notebook 또는 Python 스크립트에서 다음 코드를 실행하여 Unity 카탈로그 도구를 만들고 LlamaIndex 에이전트에서 사용합니다.
LlamaIndex용 Databricks Unity 카탈로그 통합 패키지를 설치합니다.
%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"}LlamaIndex 도구 컬렉션의 일부로 Unity 카탈로그 함수를 정의하여 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)`")