管理 Elasticsearch Serverless 项目的人工智能代理
一个由自然语言驱动的人工智能代理,可轻松管理 Elasticsearch Serverless 项目--实现项目创建、删除和状态检查。
如何使用人工智能代理管理无服务器 Elasticsearch 项目
克隆版本库:使用
git clone https://github.com/elastic/elasticsearch-labs/supporting-blog-content/serverless-ai-agenta从 GitHub 下载该工具的代码,然后使用cd serverless-ai-agent导航到该目录。设置环境: 使用
python -m venv venv创建虚拟环境(可选)并激活(Windows 上为source venv/bin/activate或venv\Scripts\activate)。然后,使用pip install -r requirements.txt安装必要的 Python 软件包。配置凭证: 在项目根目录下创建
.env文件,并在其中填入 Elasticsearch API URL (ES_URL)、API 密钥 (API_KEY)、地区 (REGION) 和 OpenAI API 密钥 (OPENAI_API_KEY)。运行工具: 在终端运行
python main.py,执行该工具。这将启动人工智能代理,并提示您执行命令。用自然语言管理项目:使用纯英文命令与工具交互,如"Create a serverless project named my\_project","Get status of the serverless project named my\_project", 或"Delete the serverless project named my\_project" 。人工智能将解读您的命令并执行相应的功能。
背景
这个小命令行工具能让你用简单的英语管理你的无服务器 Elasticsearch 项目。它会与人工智能(本例中为 OpenAI)对话,以了解您的意思,并使用 LlamaIndex 调用正确的函数!
Elasticsearch Serverless AI 代理能做什么
创建项目:启动一个新的无服务器 Elasticsearch 项目。
删除项目删除现有项目(是的,它会在你删除后进行清理)。
获取项目状态:查看项目进展情况
获取项目详情:获取项目的所有细节。
在GitHub上查看代码。
Elasticsearch Serverless AI 代理如何工作
当您输入以下内容时
"创建一个名为 my_project 的无服务器项目"
......下面是幕后花絮:
用户输入& 上下文:您的自然语言命令将发送给人工智能代理。
功能描述:人工智能代理已经知道一些函数,如创建项目、删除项目、获取项目状态和获取项目细节,因为我们给了它详细的说明。这些说明会告诉人工智能每个函数的作用以及需要的参数。
LLM 处理:将您的查询和功能信息发送给 LLM。这意味着人工智能可以看到
用户查询:您的简明指令
可用功能& 说明:详细说明每个工具的功能,以便选择正确的工具。
上下文/历史聊天信息:既然是对话,就会记住之前说过的话。
函数调用& 响应:人工智能会找出要调用的函数,传递正确的参数(如项目名称),然后执行函数。回复会以友好的格式发回给您。
简而言之,我们将您的自然语言查询和详细的工具描述列表同时发送给 LLM,这样它就能 "理解 "并为您的请求选择正确的操作。
设置人工智能代理
先决条件
运行人工智能代理之前,请确保已设置好以下内容:
已安装Python(v3.7 或更高版本)。
在 Elastic Cloud 上设置Elasticsearch 无服务器账户。
OpenAI 账户与语言模型进行交互。
步骤:
1.克隆版本库:
git clone https://github.com/elastic/elasticsearch-labs/supporting-blog-content/serverless-ai-agent
cd serverless-ai-agent2.创建虚拟环境(可选但推荐):如果遇到与环境相关的问题,可以建立虚拟环境进行隔离:
python -m venv venv
source venv/bin/activate # On Windows, use venv\Scripts\activate3.安装依赖项:运行以下命令,确保已安装所有必需的依赖项:
pip install -r requirements.txt4.配置环境:创建 .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 的值正确无误。您可以在相应的服务仪表板中找到您的 API 密钥。
5.项目文件:该工具使用projects.json 文件来存储项目映射(项目名称与其详细信息)。如果该文件不存在,将自动创建。
运行人工智能代理
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'输入指令,人工智能代理就会施展魔法!完成后,请输入exit 或quit 离开。
更多细节
LLM 集成:LLM 可同时收到您的查询和每个可用功能的详细说明。这有助于它理解上下文,并决定是调用
create_ess_project还是delete_ess_project等。工具说明:每个函数工具(使用 FunctionTool.from_defaults 创建)有一个友好的描述。该说明包含在发送给 LLM 的提示中,以便 LLM "知道 "有哪些可用的操作,以及每个操作的期望值。
持久性:项目及其详细信息保存在 projects.json 中、这样您就不必每次都重新输入信息了。
详细记录:代理被设置为 "详细 "模式,非常适合调试和查看指令如何被转换为函数调用。
Elasticsearch 人工智能代理利用示例
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.



