作者:来自 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_URL
、API_KEY
和 OPENAI_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 智能体将自动处理!完成后,输入 exit
或 quit
退出。
更多细节
LLM 集成:
LLM 接收到你的查询和每个可用函数的详细描述。这帮助它理解上下文,并决定是否调用create_ess_project
或delete_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