AI 代理常需要查詢或操作結構化資料,以回答問題、更新紀錄或建立資料管線。
Databricks 提供多種方式,將代理程式連接到 Unity 目錄資料表及外部資料儲存中的結構化資料。 使用預先設定的 MCP 伺服器,立即存取 Genie 空間與 SQL 倉庫,或為專門的工作流程打造客製化工具。
此頁面顯示如何:
Unity 目錄資料表中的查詢資料
如果你的代理需要查詢 Unity 目錄資料表的資料,Databricks 建議使用 Genie 空間。 Genie 空間是最多 25 個 Unity 目錄資料表的集合,Genie 可以將它們置於上下文中,並以自然語言查詢。 代理可透過預先設定的 MCP URL 存取 Genie 空間。
要連接到 Genie 空間:
- 建立一個 Genie 空間,裡面有你想查詢的資料表,並與必須存取該資料的使用者或服務主體共享空間。 請參見 設置與管理精靈空間。
- 建立一個代理並將其連接到空間專用的已預先設定的管理MCP URL:
https://<workspace-hostname>/api/2.0/mcp/genie/{genie_space_id}。
備註
Genie 的管理 MCP 伺服器會以 MCP 工具的方式呼叫 Genie,這表示在呼叫 Genie API 時不會傳遞歷史紀錄。
為你的代理人新增一個 Genie 空間工具
以下範例說明如何將您的代理連接到 Genie Space MCP 伺服器。 將 <genie-space-id> 替換為你的 Genie 空間的 ID。
OpenAI Agents SDK (Apps)
from agents import Agent, Runner
from databricks.sdk import WorkspaceClient
from databricks_openai.agents import McpServer
workspace_client = WorkspaceClient()
host = workspace_client.config.host
async with McpServer(
url=f"{host}/api/2.0/mcp/genie/<genie-space-id>",
name="genie-space",
workspace_client=workspace_client,
) as genie_server:
agent = Agent(
name="Data analyst agent",
instructions="You are a data analyst. Use the Genie tool to query structured data and answer questions.",
model="databricks-claude-sonnet-4-5",
mcp_servers=[genie_server],
)
result = await Runner.run(agent, "What were the top 10 customers by revenue last quarter?")
print(result.final_output)
授權應用程式存取databricks.yml中的 Genie 空間:
resources:
apps:
my_agent_app:
resources:
- name: 'my_genie_space'
genie_space:
space_id: '<genie-space-id>'
permission: 'CAN_RUN'
LangGraph(應用程式)
from databricks.sdk import WorkspaceClient
from databricks_langchain import ChatDatabricks, DatabricksMCPServer, DatabricksMultiServerMCPClient
from langgraph.prebuilt import create_react_agent
workspace_client = WorkspaceClient()
host = workspace_client.config.host
mcp_client = DatabricksMultiServerMCPClient([
DatabricksMCPServer(
name="genie-space",
url=f"{host}/api/2.0/mcp/genie/<genie-space-id>",
workspace_client=workspace_client,
),
])
async with mcp_client:
tools = await mcp_client.get_tools()
agent = create_react_agent(
ChatDatabricks(endpoint="databricks-claude-sonnet-4-5"),
tools=tools,
)
result = await agent.ainvoke(
{"messages": [{"role": "user", "content": "What were the top 10 customers by revenue last quarter?"}]}
)
print(result["messages"][-1].content)
授權應用程式存取databricks.yml中的 Genie 空間:
resources:
apps:
my_agent_app:
resources:
- name: 'my_genie_space'
genie_space:
space_id: '<genie-space-id>'
permission: 'CAN_RUN'
模型服務
from databricks.sdk import WorkspaceClient
from databricks_mcp import DatabricksMCPClient
import mlflow
workspace_client = WorkspaceClient()
host = workspace_client.config.host
# Connect to the Genie space MCP server
mcp_client = DatabricksMCPClient(
server_url=f"{host}/api/2.0/mcp/genie/<genie-space-id>",
workspace_client=workspace_client,
)
# List available tools from the Genie space
tools = mcp_client.list_tools()
# Log the agent with the required resources for deployment
mlflow.pyfunc.log_model(
"agent",
python_model=my_agent,
resources=mcp_client.get_databricks_resources(),
)
要部署代理,請參見 「為生成式 AI 應用部署代理(模型服務)」。 關於使用 MCP 資源的日誌代理的詳細資訊,請參見 Use Databricks 管理的 MCP 伺服器。
Genie 多智能體系統
這很重要
這項功能目前處於 公開預覽版。
對於進階的多代理系統,你也可以將 Genie 當作代理,而不必用 MCP 整合。 當你以代理人身份呼叫 Genie 時,你可以決定性地將現有對話情境傳給 Genie。
關於以程式碼為先的方法,請參見「 在多代理系統中使用Genie(模型服務)」。 關於以 UI 為優先的方法,請參見 「使用 Supervisor Agent 建立協調多代理系統」。
使用 Unity 目錄 SQL 函式工具查詢資料
當查詢事先已知且代理提供參數時,使用 Unity Catalog 的 SQL 函式建立結構化檢索工具。
以下範例建立了一個名為lookup_customer_info的 Unity Catalog 函式,允許 AI 代理從假設的customer_data資料表中擷取結構化資料。
在 SQL 編輯器中執行下列程式代碼。
CREATE OR REPLACE FUNCTION main.default.lookup_customer_info(
customer_name STRING COMMENT 'Name of the customer whose info to look up'
)
RETURNS STRING
COMMENT 'Returns metadata about a particular customer, given the customer's name, including the customer's email and ID. The
customer ID can be used for other queries.'
RETURN SELECT CONCAT(
'Customer ID: ', customer_id, ', ',
'Customer Email: ', customer_email
)
FROM main.default.customer_data
WHERE customer_name = customer_name
LIMIT 1;
建立 Unity 目錄工具之後,請將它新增至您的代理程式。 請參閱 建立 Unity 目錄功能工具。