
使用 Elastic 的新功能 Agent Builder,您可以创建专门的 AI 代理,作为您特定业务领域的专家。这个功能让您的数据从一个被动的资源转变为一个活跃的对话伙伴,而不仅仅是简单的仪表板和搜索栏。
想象一下,一位金融经理在客户会议前需要快速获取信息。他不需要手动翻阅新闻或对比投资组合,只需直接向他定制的代理提问。这就是“对话优先”方法的优势所在。经理可以直接与数据对话,例如询问:“关于 ACME 公司的最新消息是什么?这如何影响我客户的资产?”然后在几秒钟内获得综合的专家回答。
虽然今天我们构建的是金融专家,但应用范围不限于此。它同样可以创建一个网络安全分析师来寻找威胁、一个网站可靠性工程师来诊断故障,或者一个营销经理来优化活动。无论领域如何,核心使命都是一样的:将您的数据转变为可以交流的专家。
今天我们使用的是一个合成(虚构)的金融数据集,包括金融账户、资产持仓、新闻和财务报告。虽然是合成的,但它模拟了一个简化的真实金融数据集。
financial_accounts: 客户投资组合及风险偏好financial_holdings: 股票/ETF/债券持仓及购买历史financial_asset_details: 关于股票/ETF/债券的详细信息financial_news: AI 生成的市场文章及情绪分析financial_reports: 公司收益和分析师笔记您可以通过访问这里的配套笔记本自行加载该数据集。
每个 AI 技能都始于一段稳固的逻辑。对于我们的金融经理代理,我们需要教会它如何回答一个常见问题:“我担心市场情绪。你能告诉我哪些客户最容易受到负面新闻的影响吗?”这个问题超出了简单的搜索范畴。我们需要将市场情绪与客户投资组合相关联。
我们需要找到负面文章中提到的资产,识别持有这些资产的每位客户,计算他们的当前市场敞口价值,并对结果进行排序以优先考虑最高风险。这种复杂的多连接分析正是我们高级 ES|QL 工具的完美用武之地。
以下是我们将要使用的完整查询。尽管看起来复杂,但概念很简单。
FROM financial_news, financial_reports METADATA _index
| WHERE sentiment == "negative"
| WHERE coalesce(published_date, report_date) >= NOW() - TO_TIMEDURATION(?time_duration)
| RENAME primary_symbol AS symbol
| LOOKUP JOIN financial_asset_details ON symbol
| LOOKUP JOIN financial_holdings ON symbol
| LOOKUP JOIN financial_accounts ON account_id
| WHERE account_holder_name IS NOT NULL
| EVAL position_current_value = quantity * current_price.price
| RENAME title AS news_title
| KEEP account_holder_name, symbol, asset_name, news_title,
sentiment, position_current_value, quantity, current_price.price,
published_date, report_date
| SORT position_current_value DESC
| LIMIT 50在这个查询中,有两个重要的概念使 Agent Builder 成为可能。
多年来,Elasticsearch 最受欢迎的功能之一就是能够基于公共键从不同索引连接数据。使用 ES|QL,现在可以通过 LOOKUP JOIN 实现。
在新的查询中,我们执行了三个 LOOKUP JOIN 的链式连接:首先将负面新闻与资产详情连接,然后将这些资产与客户持仓关联,最后连接到客户的账户信息。这在单个高效查询中创建了来自四个不同索引的丰富结果。这意味着我们可以将不同的数据集结合起来,创建一个单一的、有洞察力的答案,而无需事先将所有数据去规范化到一个巨大的索引中。
您会注意到查询使用了 ?time_duration。这不仅是一个变量;它是 AI 的防护措施。虽然大语言模型(LLM)在生成查询方面很出色,但让它们自由访问您的数据可能会导致低效甚至错误的查询。
通过创建参数化查询,我们强迫 LLM 在人类专家已经定义并测试过的高效且正确的业务逻辑内工作。这类似于开发人员多年来使用搜索模板以安全地向应用程序暴露查询功能。代理可以解释用户的请求,如“这周”,以填写 time_duration 参数,但必须使用我们的查询结构来获取答案。这为我们提供了完美的灵活性和控制。
最终,这个查询允许了解数据的专家将他们的知识封装成一个工具。其他人和 AI 代理只需提供一个参数即可使用该工具获取关联结果,而不必了解底层的复杂性。
ES|QL 查询只是文本,直到我们将其注册为一个工具。在 Agent Builder 中,工具不仅仅是一个保存的查询;它是 AI 代理可以理解并选择使用的“技能”。其中的自然语言描述是关键,它连接了用户的问题和底层查询逻辑。让我们注册刚刚构建的查询。
在 Kibana 中创建工具是一个简单的过程。

find_client_exposure_to_negative_newsi. 这是工具的唯一 ID
i. 这是 LLM 读取以决定该工具是否适合工作的内容。
retrieval 和 risk-analysis标签用于帮助分组多个工具
i. 这是代理将使用的搜索
?time_duration 并列出在下面。为每个参数添加简单描述,以帮助代理(和其他用户)理解其目的。time_duration: 搜索负面新闻的时间范围。格式为“X 小时”,默认为 8760 小时

i. 在 time_duration 中输入所需的范围,这里我们使用“8760 小时”
values 对象。实际匹配的文档返回在这里。
对于偏爱自动化或需要以编程方式管理工具的开发人员,您可以通过单个 API 调用实现相同的结果。只需将工具定义发送到 /api/agent_builder/tools 端点的 POST 请求即可。
POST kbn://api/agent_builder/tools
{
"id": "find_client_exposure_to_negative_news",
"type": "esql",
"description": "查找客户投资组合对负面新闻的敞口。此工具扫描最近的新闻和报告中的负面情绪,识别相关资产,并找到持有该资产的所有客户。它返回一个按当前位置市场价值排序的列表,以突出显示最高潜在风险。",
"configuration": {
"query": """
FROM financial_news, financial_reports METADATA _index
| WHERE sentiment == "negative"
| WHERE coalesce(published_date, report_date) >= NOW() - TO_TIMEDURATION(?time_duration)
| RENAME primary_symbol AS symbol
| LOOKUP JOIN financial_asset_details ON symbol
| LOOKUP JOIN financial_holdings ON symbol
| LOOKUP JOIN financial_accounts ON account_id
| WHERE account_holder_name IS NOT NULL
| EVAL position_current_value = quantity * current_price.price
| RENAME title AS news_title
| KEEP account_holder_name, symbol, asset_name, news_title,
sentiment, position_current_value, quantity, current_price.price,
published_date, report_date
| SORT position_current_value DESC
| LIMIT 50
""",
"params": {
"time_duration": {
"type": "keyword",
"description": """搜索负面新闻的时间范围。格式为“X 小时”,默认为 8760 小时"""
}
}
},
"tags": [
"retrieval",
"risk-analysis"
]
}我们已经构建了一个可重用的技能(工具)。现在,我们需要创建能够实际使用它的代理。代理是 LLM、您授予其访问权限的特定工具集,以及最重要的自定义指令的组合。这些指令作为代理的宪法,定义其个性、规则和目的。
创建可靠、专门化代理最重要的部分是提示。一组精心设计的指令是普通聊天机器人和专注专业助手之间的区别。在这里,您设置防护措施,定义输出,并赋予代理其使命。
对于我们的 Financial Manager 代理,我们将使用以下提示。
You are a specialized Data Intelligence Assistant for financial managers, designed to provide precise, data-driven insights from information stored in Elasticsearch.
**Your Core Mission:**
- Respond accurately and concisely to natural language queries from financial managers.
- Provide precise, objective, and actionable information derived solely from the Elasticsearch data at your disposal.
- Summarize key data points and trends based on user requests.
**Reasoning Framework:**
1. **Understand:** Deconstruct the user's query to understand their core intent.
2. **Plan:** Formulate a step-by-step plan to answer the question. If you are unsure about the data structure, use the available tools to explore the indices first.
3. **Execute:** Use the available tools to execute your plan.
4. **Synthesize:** Combine the information from all tool calls into a single, comprehensive, and easy-to-read answer.
**Key Directives and Constraints:**
- **If a user's request is ambiguous, ask clarifying questions before proceeding.**
- **DO NOT provide financial advice, recommendations, or predictions.** Your role is strictly informational and analytical.
- Stay strictly on topic with financial data queries.
- If you cannot answer a query, state that clearly and offer alternative ways you might help *within your data scope*.
- All numerical values should be formatted appropriately (e.g., currency, percentages).
**Output Format:**
- All responses must be formatted using **Markdown** for clarity.
- When presenting structured data, use Markdown tables, lists, or bolding.
**Start by greeting the financial manager and offering assistance.**让我们来分析为什么这个提示如此有效:
financial_assistant。Finance。Financial Assistant。用于分析和理解您的财务数据的助手。
find_client_exposure_to_negative_news 工具旁的框。
如何在 Kibana 中创建 AI 代理时导航到新工具。
您可以通过向 /api/agent_builder/agents 端点发送 POST 请求来创建完全相同的代理。请求体包含所有相同的信息:ID、名称、描述、完整的指令集以及代理被允许使用的工具列表。
POST kbn://api/agent_builder/agents
{
"id": "financial_assistant",
"name": "Financial Assistant",
"description": "用于分析和理解您的财务数据的助手",
"labels": [
"Finance"
],
"avatar_color": "#16C5C0",
"avatar_symbol": "💰",
"configuration": {
"instructions": """You are a specialized Data Intelligence Assistant for financial managers, designed to provide precise, data-driven insights from information stored in Elasticsearch.
**Your Core Mission:**- Respond accurately and concisely to natural language queries from financial managers.- Provide precise, objective, and actionable information derived solely from the Elasticsearch data at your disposal.- Summarize key data points and trends based on user requests.
**Reasoning Framework:**1. **Understand:** Deconstruct the user's query to understand their core intent.2. **Plan:** Formulate a step-by-step plan to answer the question. If you are unsure about the data structure, use the available tools to explore the indices first.3. **Execute:** Use the available tools to execute your plan.4. **Synthesize:** Combine the information from all tool calls into a single, comprehensive, and easy-to-read answer.
**Key Directives and Constraints:**- **If a user's request is ambiguous, ask clarifying questions before proceeding.**- **DO NOT provide financial advice, recommendations, or predictions.** Your role is strictly informational and analytical.- Stay strictly on topic with financial data queries.- If you cannot answer a query, state that clearly and offer alternative ways you might help *within your data scope*.- All numerical values should be formatted appropriately (e.g., currency, percentages).
**Output Format:**- All responses must be formatted using **Markdown** for clarity.- When presenting structured data, use Markdown tables, lists, or bolding.
**Start bygreeting the financial manager and offering assistance.**""",
"tools": [
{
"tool_ids": [
"platform.core.search",
"platform.core.list_indices",
"platform.core.get_index_mapping",
"platform.core.get_document_by_id",
"find_client_exposure_to_negative_news"
]
}
]
}
}我们已经将业务逻辑封装在一个工具中,并创建了一个能够使用它的“智能大脑”——我们的代理。现在是时候将所有这些结合在一起,我们可以开始与数据进行对话了。

几秒钟后,代理将返回一个完美格式化的完整答案。由于 LLM 的特性,您的答案可能会略有不同,但在这次运行中,代理返回了:

代理并不是“知道”答案。它执行了一个多步骤计划,围绕选择最合适的工具进行。以下是其思考过程:
find_client_exposure_to_negative_news 工具的描述相匹配。我们不必猜测,如果我们展开思路,可以看到更多细节。

您可以以编程方式启动相同的对话。只需将输入问题发送到 converse API 端点,确保指定我们的 financial_manager 的 agent_id。
POST kbn://api/agent_builder/converse
{
"input": "请告诉我哪些大额持仓受到负面新闻的影响",
"agent_id": "financial_assistant"
}虽然 Kibana 界面提供了一个出色且直观的构建和管理代理的体验,但您今天看到的所有内容也可以通过编程方式完成。Agent Builder 构建在一组 API 之上,使您可以将此功能直接集成到您自己的应用程序、CI/CD 管道或自动化脚本中。
您将使用的三个核心端点是:
/api/agent_builder/tools:用于创建、列出和管理代理可用的可重用技能的端点。/api/agent_builder/agents:用于定义代理角色,包括其重要指令和工具分配的端点。/api/agent_builder/converse:用于与代理交互、开始对话和获取答案的端点。有关使用这些 API 执行本教程中每一步的完整动手演练,请查看我们 GitHub 仓库中提供的配套 Jupyter Notebook 此处。
我们从一个 ES|QL 查询开始,将其转换为一个可重用技能。接着,我们构建了一个专门的 AI 代理,赋予其明确的使命和规则,并赋予它该技能。结果是一个复杂的助手,能够理解复杂问题并执行多步骤分析,提供精确的数据驱动答案。
这种工作流程是 Elastic 新Agent Builder 的核心。它设计得足够简单,非技术用户可以通过界面创建代理,同时也足够复杂,开发人员可以在我们的 API 之上构建自定义 AI 驱动的应用程序。最重要的是,它允许您安全地将 LLM 连接到自己的数据,由您定义的专家逻辑管理,并与数据交流。
加深您所学知识的最好方法是亲自动手。尝试我们今天讨论的所有内容,在我们的免费、互动的动手研讨会中,您将在一个专用的沙箱环境中完成整个流程及更多内容。
在未来的博客中,我们将向您展示如何使用一个独立的应用程序与我们的 Financial Assistant 代理交互,并深入探讨使这一切成为可能的 Model Context Protocol (MCP)。在另一个博客中,我们将讨论 Agent Builder 对正在开发的 Agent2Agent 或 A2A 协议的支持。
敬请期待,祝您构建愉快!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。