用于管理 Elasticsearch Serverless 项目的 AI Agent

ops/2025/3/6 10:17:44/

作者:来自 Elastic  Fram Souza

由自然语言驱动的 AI 代理,可轻松管理 Elasticsearch Serverless 项目 - 支持项目创建、删除和状态检查。

这个小型命令行工具让你可以用简单的英语管理你的无服务器 Elasticsearch 项目。它通过AI(这里是 OpenAI)理解你的意思,并使用 LlamaIndex 调用正确的函数!

它能做什么?

  • 创建项目:创建一个新的无服务器 Elasticsearch 项目。
  • 删除项目:删除一个已有项目(是的,它还会帮你清理干净)。
  • 获取项目状态:查看项目的运行状态。
  • 获取项目详情:获取项目的详细信息。
  • 在 GitHub 上查看代码

工作原理

当你输入类似:

Create a serverless project named my_project"

这样的指令时,系统背后会执行以下步骤:

  • 用户输入 & 上下文
    你的自然语言命令会发送给 AI 智能体(agent)。

  • 函数描述
    这个 AI 智能体预先了解了一些函数,比如:

    • create_ess_project
    • delete_ess_project
    • get_ess_project_status
    • get_ess_project_details
      这些函数都附带详细描述,告诉 AI 每个函数的用途和所需的参数。
  • LLM处理
    你的请求和这些函数信息会一起发送到大语言模型(LLM),也就是说 AI 会看到:

    • 你的自然语言指令
    • 可用的函数和它们的描述
    • 对话历史和上下文信息(因为是对话式的,AI 会记住之前的交流内容)
  • 函数调用 & 响应
    AI 判断该调用哪个函数,并把正确的参数(比如项目名)传递过去,然后执行函数,并以友好的格式把结果返回给你。

设置

先决条件

在运行 AI 智能体之前,请确保你已经完成以下准备工作:

  • 已安装Python(3.7 或更高版本)。
  • 已在 Elastic Cloud 上设置好 Elasticsearch 无服务器账号。
  • 已拥有 OpenAI 账号,用于与语言模型交互。

步骤

1)克隆代码仓库

git clone https://github.com/elastic/elasticsearch-labs/supporting-blog-content/serverless-ai-agent
cd serverless-ai-agent

2)创建虚拟环境(可选,但推荐)

如果遇到环境相关的问题,可以创建一个虚拟环境来进行隔离:

python -m venv venv
source venv/bin/activate  # On Windows, use venv\Scripts\activate

3)安装依赖

运行以下命令,确保所有必需的依赖都已安装:

pip install -r requirements.txt

4)配置环境

在项目根目录下创建一个 .env 文件,并添加以下变量。你可以参考示例文件 .env.example 作为模板:

ES_URL=your_elasticsearch_api_url  # The base URL for your Elasticsearch service (e.g., https://your-cluster-id.es.region.aws.elastic-cloud.com)
API_KEY=your_elasticsearch_api_key  # Your API key for Elasticsearch
REGION=your_region  # Example: aws-eu-west-1
OPENAI_API_KEY=your_openai_api_key  # Your OpenAI API key

确保你已经正确设置以下变量的值:ES_URLAPI_KEYOPENAI_API_KEY。这些密钥可以在对应服务的控制台找到。

5)项目文件

这个工具使用一个名为 projects.json 的文件来保存项目的映射关系(即项目名称和项目详情)。如果该文件不存在,工具会自动创建。

运行 AI 智能体

完成设置后,你可以通过以下方式运行 AI 智能体:

python main.py

你将看到类似这样的提示:

Welcome to the Serverless Project AI Agent Tool!
You can ask things like:- 'Create a serverless project named my_project'- 'Delete the serverless project named my_project'- 'Get the status of the serverless project named my_project'- 'Get the details of the serverless project named my_project'

输入你的命令,AI 智能体将自动处理!完成后,输入 exitquit 退出。

更多细节

  • LLM 集成
    LLM 接收到你的查询和每个可用函数的详细描述。这帮助它理解上下文,并决定是否调用 create_ess_projectdelete_ess_project 等函数。

  • 工具描述
    每个函数工具(通过 FunctionTool.from_defaults 创建)都有一个友好的描述。这个描述会包含在发送给 LLM 的提示中,帮助它了解可用的操作以及每个操作的预期。

  • 持久化
    你的项目及其详情会保存在 projects.json 文件中,这样你就不需要每次都重新输入信息。

  • 详细日志
    智能体被设置为详细日志模式,这对于调试和查看你的指令如何转换为函数调用非常有用。

示例使用

假设你已经完成了所有设置并成功启动了 AI 智能体。以下是一些常见的使用示例:

python main.py Welcome to the Serverless Project AI Agent Tool!You can ask things like:- 'Create a serverless project named my_project'- 'Delete the serverless project named my_project'- 'Get the status of the serverless project named my_project'- 'Get the details of the serverless project named my_project'User: Create a serverless project named my-february-test
Added user message to memory: Create a serverless project named my-february-test=== Calling Function ===
Calling function: create_ess_project with args: {"project_name":"my-february-test"}
========================Agent: The serverless Elasticsearch project named "my-february-test" has been created successfully. Here are the details:- **Project ID:** <>>
- **Elasticsearch Endpoint:** [https://my-february-test-ccc399.es.eu-west-1.aws.qa.elastic.cloud](https://my-february-test-ccc399.es.eu-west-1.aws.qa.elastic.cloud)
- **Kibana Endpoint:** [https://my-february-test-ccc399.kb.eu-west-1.aws.qa.elastic.cloud](https://my-february-test-ccc399.kb.eu-west-1.aws.qa.elastic.cloud)User: get my-february-test status
Added user message to memory: get my-february-test status=== Calling Function ===
Calling function: get_ess_project_status with args: {"project_name":"my-february-test"}
========================Agent: The status of the "my-february-test" project is currently **initializing**.User: get my-february-test info
Added user message to memory: get my-february-test info=== Calling Function ===
Calling function: get_ess_project_details with args: {"project_name":"my-february-test"}
========================Agent: Here are the details for the "my-february-test" project:- **Project Name:** my-february-test
- **Alias:** my-february-test-ccc399
- **Cloud ID:** ""
- **Project ID:** ""
- **Region ID:** aws-eu-west-1
- **Created At:** 2025-02-12T15:42:00.530177619Z
- **Created By:** 571213384
- **Organization ID:** ""
- **Type:** Elasticsearch
- **Optimized For:** General Purpose
- **Search Lake:**- **Boost Window:** 7- **Search Power:** 100
- **Endpoints:**- **Elasticsearch:** https://my-february-test-ccc399.es.eu-west-1.aws.qa.elastic.cloud- **Kibana:** https://my-february-test-ccc399.kb.eu-west-1.aws.qa.elastic.cloud
- **Credentials:**- **Username:** ""- **Password:** ""Please ensure to keep the credentials secure.User: please delete the my-february-test project
Added user message to memory: please delete the my-february-test project=== Calling Function ===
Calling function: delete_ess_project with args: {"project_name":"my-february-test"}
========================Agent: The "my-february-test" project has been deleted successfully.

了解有关 Elastic Cloud Serverless 的更多信息,并开始 14 天免费试用来亲自测试。

原文:The AI Agent to manage Elasticsearch Serverless projects - Elasticsearch Labs


http://www.ppmy.cn/ops/163573.html

相关文章

如何将飞书多维表格与DeepSeek R1结合使用:效率提升的完美搭档

将飞书的多维表格与DeepSeek R1结合使用&#xff0c;就像为你的数据管理和分析之旅装上一台涡轮增压器。两者的合作&#xff0c;不仅仅在速度上让人耳目一新&#xff0c;更是将智能化分析带入了日常的工作场景。以下是它们如何相辅相成并改变我们工作方式的一些分享。 --- 在…

Diffusion模型中时间t嵌入的方法

Diffusion模型中时间t嵌入的方法 class PositionalEmbedding(nn.Module):def __init__(self, dim, scale1.0):super().__init__()assert dim % 2 0self.dim dimself.scale scaledef forward(self, x):device x.devicehalf_dim self.dim // 2emb math.log(10000) / half_…

FastGPT 源码:混合检索调用链路

文章目录 FastGPT 源码&#xff1a;混合检索调用链路1. 入口函数2. 核心搜索函数3. RRF合并函数4. Rerank重排序函数5. 完整流程 FastGPT 源码&#xff1a;混合检索调用链路 主要调用链路如下&#xff1a; 1. 入口函数 在 dispatchDatasetSearch&#xff08;packages/servic…

Mac OS升级后变慢了,如何恢复老系统?

我的一台Mac Air闲置很久了&#xff0c;原因是某次系统升级后用着会卡&#xff0c;有差不多10年没用了。今天想试着恢复一下出厂系统&#xff0c;目前看这条路可以走通。记录如下&#xff1a; 1、去哪里下载旧版系统&#xff1f; https://support.apple.com/zh-cn/102662 2、…

游戏引擎学习第136天

回顾 今天&#xff0c;我们的工作重点是继续探索之前搭建的资产系统&#xff0c;目的是最终定义我们的资产包文件格式。通过这个工作&#xff0c;我们希望能够创建一个符合我们要求的资产包文件。这样&#xff0c;我可以在直播之外的时间完成它&#xff0c;并为我们提供一个符…

JAVA入门——网络编程简介

自己学习时的笔记&#xff0c;可能有点水&#xff08; 以后可能还会补充&#xff08;大概率不会&#xff09; 一、基本概念 网络编程三要素&#xff1a; IP 设备在网络中的唯一标识 端口号 应用软件在设备中的唯一标识两个字节表示的整数&#xff0c;0~1023用于知名的网络…

基于 Kubernetes 搭建 DevOps 持续集成环境

环境准备 在部署 Kubernetes&#xff08;K8s&#xff09;以及相关 DevOps 工具&#xff08;如 Jenkins、Kuboard、Harbor&#xff09;时&#xff0c;我们需要确保服务器和软件环境符合要求。 服务器及软件环境 服务器配置&#xff1a;2 核 4G 及以上&#xff08;推荐至少 2 …

【Vue3】实现一个超过高度后可控制显示隐藏的组件

组件效果图 未达到最大高度 达到设置的最大高度 进行展开 实现代码 组件代码 备注&#xff1a;通过tailwindcss设置的样式&#xff0c;通过element-plus/icons-vue设置的图标&#xff0c;可根据情况进行替换 <template><!-- 限制高度组件 --><div ref"…