Elastic AI agent builder 介绍(五)- 备份 tools 及 agents

我经常会在我自己的电脑里重新安装我的 Elasticsearch 及 Kibana。如果我之前已经创建好的 agents 及 tools 不能保存下来,那么我在每次重新安装的时候,就需要手动一个一个地恢复原来的 tools 及 agents。这将是一个很大的工作。.在今天的文章里,我来介绍一下我创建的的一个小工具。它用来备份我们已经创建好的 agents 及 tools,并且可以把备份好的 tools 及 agents 恢复到 AI Agents Builder 里去。我还可以一次性地删除所有的 tools 及 agents。

安装

如果你还没有安装好 Elasticsearch 及 Kibana,还没有创建自己的 agents,那么请参考我之前的文章 "Elastic AI agent builder 介绍(一)"。这里就不再赘述。

接下来,我们下载已经写好的代码:

bash 复制代码
`git clone https://github.com/liu-xiao-guo/es_agents_dump`AI写代码

在运行上面的应用之前,我们必须做如下的配置。在根目录下,创建如下的一个叫做 .env 的文件:

.env

ini 复制代码
`

1.  ES_AGENT_URL=http://localhost:5601/api/agent_builder
2.  KIBANA_URL=http://localhost:5601
3.  ES_URL=https://localhost:9200
4.  ES_API_KEY=WnJwR3RKb0JGQVVCVjdnb29yUkI6RHotbGZBTmJzMDJWUWszbTAtbDVjQQ==

`AI写代码

你需要根据自己的配置进行相应的修改。

备份 tools 及 agents

进入到项目的根目录中,然后运行如下的命令:

go 复制代码
 `python es_agents_dump.py` AI写代码

我们可以在当前的目录下看到一个生成的 agent_tools.json 文件:

swift 复制代码
``

1.  [
2.    {
3.      "id": "platform.core.search",
4.      "type": "builtin",
5.      "description": "A powerful tool for searching and analyzing data within your Elasticsearch cluster.\nIt supports both full-text relevance searches and structured analytical queries.\n\nUse this tool for any query that involves finding documents, counting, aggregating, or summarizing data from a known index.\n\nExamples of queries:\n- \"find articles about serverless architecture\"\n- \"search for support tickets mentioning 'billing issue' or 'refund request'\"\n- \"what is our policy on parental leave?\"\n- \"list all products where the category is 'electronics'\"\n- \"show me the last 5 documents from that index\"\n- \"show me the sales over the last year break down by month\"\n\nNote:\n- The 'index' parameter can be used to specify which index to search against.\n If not provided, the tool will decide itself which is the best index to use.\n- It is perfectly fine not to specify the 'index' parameter. It should only be specified when you already\n know about the index and fields you want to search on, e.g. if the user explicitly specified it.\n    ",
6.      "tags": [],
7.      "configuration": {},
8.      "readonly": true
9.    },
10.    {
11.      "id": "platform.core.get_document_by_id",
12.      "type": "builtin",
13.      "description": "Retrieve the full content (source) of an Elasticsearch document based on its ID and index name.",
14.      "tags": [],
15.      "configuration": {},
16.      "readonly": true
17.    },
18.    {
19.      "id": "platform.core.execute_esql",
20.      "type": "builtin",
21.      "description": "Execute an ES|QL query and return the results in a tabular format.\n\n**IMPORTANT**: This tool only **runs** queries; it does not write them.\nThink of this as the final step after a query has been prepared.\n\nYou **must** get the query from one of two sources before calling this tool:\n1.  The output of the `platform.core.generate_esql` tool (if the tool is available).\n2.  A verbatim query provided directly by the user.\n\nUnder no circumstances should you invent, guess, or modify a query yourself for this tool.\nIf you need a query, use the `platform.core.generate_esql` tool first.",
22.      "tags": [],
23.      "configuration": {},
24.      "readonly": true
25.    },
26.   ...
27.  ]

``AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

它的内容如上。这样我们就把我们所有的 tools 保存下来了。同时,我们可以看见一个生成的 agent_agents.json 文件。它的内容如下:

bash 复制代码
`

1.  [
2.    {
3.      "id": "elastic-ai-agent",
4.      "name": "Elastic AI Agent",
5.      "description": "Elastic AI Agent",
6.      "configuration": {
7.        "tools": [
8.          {
9.            "tool_ids": [
10.              "platform.core.search",
11.              "platform.core.list_indices",
12.              "platform.core.get_index_mapping",
13.              "platform.core.get_document_by_id"
14.            ]
15.          }
16.        ]
17.      },
18.      "type": "chat",
19.      "readonly": true
20.    },
21.    {
22.      "id": "find_people_in_age_range",
23.      "type": "chat",
24.      "name": "Find people in age range",
25.      "description": "Find people in age range",
26.      "labels": [],
27.      "avatar_color": "",
28.      "avatar_symbol": "",
29.      "configuration": {
30.        "instructions": "Search for the people between ages",
31.        "tools": [
32.          {
33.            "tool_ids": [
34.              "platform.core.search",
35.              "platform.core.list_indices",
36.              "platform.core.get_index_mapping",
37.              "platform.core.get_document_by_id",
38.              "find_people_in_ages"
39.            ]
40.          }
41.        ]
42.      },
43.      "readonly": false
44.   ...
45.  ]

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

这样,我们就保存下来我们所有的 agents。

导入 tools 及 agents

接下来,我们从 agent_agent.json 及 agent_tools.json 中导入我们的 tools 及 agents。我们使用如下的命令:

go 复制代码
`python es_agents_import.py` AI写代码
arduino 复制代码
`

1.  $ pwd
2.  /Users/liuxg/python/es_agents_dump
3.  $ python es_agents_import.py 
4.  Skipping import of tool platform.core.search
5.  Skipping import of tool platform.core.get_document_by_id
6.  Skipping import of tool platform.core.execute_esql
7.  Skipping import of tool platform.core.generate_esql
8.  Skipping import of tool platform.core.get_index_mapping
9.  Skipping import of tool platform.core.list_indices
10.  Skipping import of tool platform.core.index_explorer
11.  Tool search_for_parents already exists, skipping import
12.  Tool software_developers already exists, skipping import
13.  Tool people_profession_search already exists, skipping import
14.  Tool find_people_in_ages already exists, skipping import
15.  Tool find_people_in_time_range already exists, skipping import
16.  Tool find_client_exposure_to_negative_news already exists, skipping import
17.  Tool find_cheapest_ticket_from_cn_us already exists, skipping import
18.  Tool example-esql-tool already exists, skipping import
19.  Tool find_average_age already exists, skipping import
20.  Skipping import of agent elastic-ai-agent
21.  Agent find_people_in_age_range already exists, skipping import
22.  Agent people_profession_search already exists, skipping import
23.  Agent software_developers already exists, skipping import
24.  Agent find_parents already exists, skipping import
25.  Agent find_people_in_time_range already exists, skipping import
26.  Agent financial_assistant already exists, skipping import
27.  Agent find_the_cheapest_ticket_from_cn_us already exists, skipping import
28.  Agent find_out_average_age already exists, skipping import

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

如果 tools 及 agents 的 ID 已经存在,那么就跳过。同时我们跳过系统自带的 agents 及 tools。

清除所有的 tools 及 agents

我们可以通过如下的命令来清除所有非系统自带的 agents 及 tools:

go 复制代码
 `python es_agents_clear.py` AI写代码
yaml 复制代码
`

1.  $ pwd
2.  /Users/liuxg/python/es_agents_dump
3.  $ python es_agents_clear.py 
4.  Deleted tool: search_for_parents
5.  Deleted tool: software_developers
6.  Deleted tool: people_profession_search
7.  Deleted tool: find_people_in_ages
8.  Deleted tool: find_people_in_time_range
9.  Deleted tool: find_client_exposure_to_negative_news
10.  Deleted tool: find_cheapest_ticket_from_cn_us
11.  Deleted tool: example-esql-tool
12.  Deleted tool: find_average_age
13.  Deleted agent: find_people_in_age_range
14.  Deleted agent: people_profession_search
15.  Deleted agent: software_developers
16.  Deleted agent: find_parents
17.  Deleted agent: find_people_in_time_range
18.  Deleted agent: financial_assistant
19.  Deleted agent: find_the_cheapest_ticket_from_cn_us
20.  Deleted agent: find_out_average_age

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

一旦运行完上面的命令后,我们可以看到所有定制的 tools 及 agents 都已经被彻底删除了。

注意:在运行上面的命令之前,确保你已经下载了所有的 tools 及 agents。

在清除所有的 tools 及 agents 之后,我们可以再次运行 es_agents_import.py 来看看:

go 复制代码
`python es_agents_import.py` AI写代码
markdown 复制代码
`

1.  $ pwd
2.  /Users/liuxg/python/es_agents_dump
3.  $ python es_agents_import.py 
4.  Skipping import of tool platform.core.search
5.  Skipping import of tool platform.core.get_document_by_id
6.  Skipping import of tool platform.core.execute_esql
7.  Skipping import of tool platform.core.generate_esql
8.  Skipping import of tool platform.core.get_index_mapping
9.  Skipping import of tool platform.core.list_indices
10.  Skipping import of tool platform.core.index_explorer
11.  Importing tool search_for_parents
12.  Successfully imported tool search_for_parents
13.  Importing tool software_developers
14.  Successfully imported tool software_developers
15.  Importing tool people_profession_search
16.  Successfully imported tool people_profession_search
17.  Importing tool find_people_in_ages
18.  Successfully imported tool find_people_in_ages
19.  Importing tool find_people_in_time_range
20.  Successfully imported tool find_people_in_time_range
21.  Importing tool find_client_exposure_to_negative_news
22.  Successfully imported tool find_client_exposure_to_negative_news
23.  Importing tool find_cheapest_ticket_from_cn_us
24.  Successfully imported tool find_cheapest_ticket_from_cn_us
25.  Importing tool example-esql-tool
26.  Successfully imported tool example-esql-tool
27.  Importing tool find_average_age
28.  Successfully imported tool find_average_age
29.  Skipping import of agent elastic-ai-agent
30.  Importing agent find_people_in_age_range
31.  Successfully imported agent find_people_in_age_range
32.  Importing agent people_profession_search
33.  Successfully imported agent people_profession_search
34.  Importing agent software_developers
35.  Successfully imported agent software_developers
36.  Importing agent find_parents
37.  Successfully imported agent find_parents
38.  Importing agent find_people_in_time_range
39.  Successfully imported agent find_people_in_time_range
40.  Importing agent financial_assistant
41.  Successfully imported agent financial_assistant
42.  Importing agent find_the_cheapest_ticket_from_cn_us
43.  Successfully imported agent find_the_cheapest_ticket_from_cn_us
44.  Importing agent find_out_average_age
45.  Successfully imported agent find_out_average_age

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

我们可以看到它成功地把备份好的 tools 及 agents 都已经导入到系统了。我们查看最新的页面:

最后,祝大家使用 AI Agents Builder 创建更多的 agents!

相关推荐
练习时长一年3 小时前
git常用命令总结
大数据·git·elasticsearch
little_xianzhong1 天前
把一个本地项目导入gitee创建的仓库中
大数据·elasticsearch·gitee
哈__1 天前
exa 在 HarmonyOS 上的构建与适配
elasticsearch·华为·harmonyos
Hello.Reader2 天前
Flink CDC 用 Oracle CDC 实时同步数据到 Elasticsearch
elasticsearch·oracle·flink
Elastic 中国社区官方博客2 天前
开始使用 Elastic Agent Builder 和 Microsoft Agent Framework
数据库·人工智能·elasticsearch·microsoft·搜索引擎·ai·全文检索
青靴2 天前
Git Hooks 实现 CI/CD 进阶实践 -- 根据实际需求添加功能
git·elasticsearch·ci/cd
0***R5152 天前
SpringBoot集成Elasticsearch实战
java·spring boot·elasticsearch
e***0962 天前
Springboot中使用Elasticsearch(部署+使用+讲解 最完整)
spring boot·elasticsearch·jenkins