将代理连接到结构化数据

根据代理所需的数据和由谁编写查询来选择工具:

你的智能体需要 Use
回答在整个工作区中的业务问题 Genie One MCP
从 Python 查询一个特选的 Genie 智能体 AI 桥接工具
编写并运行SQL Databricks SQL MCP
运行一个带有参数的预定义查询 Unity 目录函数工具

使用 Genie One 查询数据

Azure Databricks 建议对以自然语言跨工作区回答业务问题的智能体使用 Genie One MCP。 它使用 Genie Ontology 来解释业务术语,并利用调用者的权限查询您的工作区数据。

请遵循 在 Python 代理中使用 MCP 工具,并将 server_url 设置为 Genie One MCP URL:

https://<workspace-hostname>/ai-gateway/mcp-services/system.ai.genie_one_mcp

用你的工作区主机名替换 <workspace-hostname> 。 然后向你的智能体提出一个关于你数据的问题,比如“上季度按收入排名前十的客户是谁?”

对于 Claude Code 或 Codex,安装 Databricks AI 工具 ,并使用 数据发现技能,该技能通过 Databricks CLI 查询 Genie One。

查询Python中的特定Genie代理

使用 AI Bridge 在你的 Python 代理中将特定的 Genie 代理作为工具暴露出来。 AI Bridge 通过 Genie API 处理请求、轮询和查询结果。

对于 Claude Code 或 Codex,安装 Databricks AI 工具 ,通过 Databricks CLI 使用 Genie Agents 技能 。

开始之前

  1. 用 Python 3.12。 本地开发时,使用Databricks的CLI登录,使用DEFAULT配置文件,这样Python客户端就能找到你的凭证。
  2. 选择你的身份可以访问且具有所需仓库和数据权限的Genie 代理。 从 设置 复制它的空间ID,然后替换 <genie-agent-id> 成该ID。
  3. 用你有查询权限且支持工具调用的<model-endpoint>替换。
  4. 在终端里运行你框架的安装命令。 在笔记本中,使用 %pip 并在安装后重启 Python。 在支持顶层 await 的笔记本中运行带有 await 的示例。

运行你的智能体

这两个例子都让模型调用 ask_genie 并在其回答中使用结果。 Genie databricks-ai-bridge客户端也可以在其他框架中被包装成 Python 函数工具。 OpenAI Agents SDK 标签页显示该客户端没有依赖 LangChain。

LangGraph

此示例将 AI Bridge 的 GenieAgent 帮助程序包装为 LangChain 工具。

pip install --upgrade databricks-langchain langgraph
from databricks_langchain import ChatDatabricks
from databricks_langchain.genie import GenieAgent
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent

genie = GenieAgent(genie_space_id="<genie-agent-id>")


@tool
def ask_genie(question: str) -> str:
    """Ask the configured Genie Agent a question about its business data."""
    response = genie.invoke({
        "messages": [{"role": "user", "content": question}]
    })
    return response["messages"][-1].content


agent = create_react_agent(
    ChatDatabricks(endpoint="<model-endpoint>"),
    tools=[ask_genie],
)
result = agent.invoke({
    "messages": [{
        "role": "user",
        "content": "Use Genie to find total sales last month.",
    }]
})
print(result["messages"][-1].content)

OpenAI 代理 SDK

这个例子使用了 AI Bridge 客户端Genie和 OpenAI Agents SDK 的function_tool装饰器。 该模型运行在 Azure Databricks 上。

pip install --upgrade databricks-ai-bridge databricks-openai openai-agents
from agents import Agent, Runner, function_tool
from agents import set_default_openai_api, set_default_openai_client
from agents.tracing import set_trace_processors
from databricks_ai_bridge.genie import Genie
from databricks_openai import AsyncDatabricksOpenAI

set_default_openai_client(AsyncDatabricksOpenAI())
set_default_openai_api("chat_completions")
set_trace_processors([])
genie = Genie(space_id="<genie-agent-id>", return_pandas=False)


@function_tool
def ask_genie(question: str) -> str:
    """Ask the configured Genie Agent a question about its business data."""
    return genie.ask_question(question).result


agent = Agent(
    name="Data analyst",
    instructions="Use ask_genie to answer questions about business data.",
    model="<model-endpoint>",
    tools=[ask_genie],
)
result = await Runner.run(agent, "What were total sales last month?")
print(result.final_output)

检查代理的输出或跟踪,确认它调用了 ask_genie 并使用了结果。 这些示例为每次工具调用开启新的 Genie 对话。 为了保持上下文,请保存每个用户对话返回的对话 ID,并在下一次调用时传递:

  • GenieAgent:阅读response["conversation_id"],并将"conversation_id"与"messages"一并包含在下一个输入词典中。
  • Genie:读取 response.conversation_id 并将其作为 conversation_id 传递给 ask_question()。

有关客户端选项和实现细节,请参见 AI Bridge 仓库,包括 GenieAgent 助手 和 Genie 客户端。

在 Databricks Apps 托管代理时,将 Genie Agent 添加为应用资源,并授予 “可运行” 权限。 应用的服务主体还需要访问模型端点以及底层仓库和数据。

使用 Databricks SQL MCP 运行 SQL

当你的代理需要对你的 Unity Catalog 表编写并运行 SQL 时,可以使用 Databricks 的 SQL MCP。

要使用 system.ai.dbsql、system.ai.sandbox 或 system.ai.web_search,账户管理员必须在账户控制台的 预览 页面启用 Unity Gateway 测试版。 请参阅管理帐户预览。

参照 在 Python 代理中使用 MCP 工具 中的说明,并将 server_url 设置为:

https://<workspace-hostname>/ai-gateway/mcp-services/system.ai.dbsql

用你的工作区主机名替换 <workspace-hostname> 。 让客服运行 SELECT 1 AS result 并确认它已经调用了工具并返回 1。

对于编码智能体,请使用智能体的设置指南。 关于仓库选择和SQL专用选项,请参见Databricks SQL MCP参考。

用 Unity Catalog 函数运行预定义查询

当提前知道查询并且代理提供参数时,使用 Unity 目录 SQL 函数创建结构化检索工具。

以下示例创建了一个名为 lookup_customer_info 的 Unity Catalog 函数。 它假设你有一个 customer_data 包含 customer_name、 customer_id和 customer_email 列的表。

请审查功能要求和权限。 用可在其中创建函数的目录和模式替代 my_catalog.my_schema,并根据数据调整表和列名。 在SQL编辑器中运行以下代码:

CREATE FUNCTION my_catalog.my_schema.lookup_customer_info(
  customer_name_input 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 my_catalog.my_schema.customer_data
  WHERE customer_name = customer_name_input
  LIMIT 1;

用你表格中的客户名测试函数:

SELECT my_catalog.my_schema.lookup_customer_info('Example Customer');

然后用Unity Catalog AI把这个函数添加到你的智能体中。 该指南包括LangGraph、OpenAI、Anthropic和LlamaIndex的示例。

在多代理系统中使用Genie

要与其他智能体和工具协调 Genie 智能体,请参阅 Supervisor Agent。 关于现有的模型服务集成,请参见 “在多智能体系统中使用Genie”。

使用 MCP 查询 Genie 代理

要通过 MCP 连接到特定的 Genie 代理,请参阅 Genie Agent MCP 参考 了解其 URL 和要求。 要在没有MCP的情况下从Python查询Genie代理,可以使用AI桥接工具示例。