Elasticsearch代理构建器:意图分类器、摄取和查询管道-让您的数据具有对话性!
转 Telegram chat 和 voice messages 使用可搜索的知识 n8n编排,利用AWS Bedrock LLM、Elasticsearch MCP和Elastic Agent Builder实现自定义函数调用工具和检索工作流。
此存储库包含一个自动化系统的架构图和文档,该系统连接Telegram>n8n工作流>AWS Bedrock(LLM)>Elasticsearch,支持摄取(索引结构化JSON)和会话查询流。
概述
用户向Telegram机器人发送消息(文本或语音)。n8n工作流接收消息,并可选择通过 speech-to-text 并构建统一的文本有效载荷。该有效载荷被发送到分类器代理(通过AWS Bedrock托管的LLM),该代理返回一个结构化的JSON,其中包含检测到的意图、信号和元数据(置信度、实体等)。
根据工作流程的意图:
摄入流量:
- 摄入流的路由:摄入代理根据Elastic Common Schema准备一个JSON文档,然后使用
semantic_text字段以激活语义搜索。
搜索流程:
- 查询流的路由:AI查询代理查询
Elasticsearch MCP Server,格式化自然语言,并在Telegram中响应用户。
Low-confidence classifier 当置信度低于阈值时,输出会触发Telegram中的交互式澄清步骤。最终目标
该系统接受语音或文本输入,对其进行处理,使用您的支出数据查询Elasticsearch,应用语义检索,并在Telegram机器人中生成自然语言响应。
建筑
组件
- 电报:消息入口点和用户通知。
- n8n:协调工作流程;路由、转换以及与外部API的集成。
- 语音到文本:用于将语音消息转换为文本的外部API。你可以随心所欲地使用它。使用 萨尔瓦姆API 用于转录,因为它提供了一些学分可供使用。
- AWS基岩:用于分类、摄取代理和查询代理的主机LLM。我用过
Claude-3.5-Sonnet-v2. - Elasticsearch用于摄取:摄入文档的持久存储和查询的主要数据源。
- Elasticsearch用于搜索/查询:当我摄入相关数据时,我使用Elasticsearch的托管MCP服务器进行查询。
- Elasticsearch代理生成器:它能够使用
ES|QL指导代理人。
低置信度处理
- 如果分类器置信度\<可配置阈值(例如0.7),工作流将通过Telegram向用户发送澄清消息,并暂停处理,直到澄清为止。
- 如果用户未能澄清或提供无效输入,工作流将遵循错误路径,并可选择记录事件以供审查。
Elasticsearch代理生成器
elasticsearch_agent_builder_chat_interface
边缘案例和注意事项
- 音质:糟糕的音频会导致不正确的转录;考虑记录音频和成绩单的置信度。
- LLM幻觉:验证结构化字段,并使用提取/解析步骤来防止格式错误的JSON。
- 重复摄入:在索引时使用稳定的重复数据删除键(内容哈希+用户+时间戳)。
- 费率限制和成本:基岩和外部短期租约服务可能有费率限制或使用成本——增加限制和监控。
- 安全:将凭据(Telegram bot令牌、Elasticsearch凭据、AWS凭据)存储在n8n凭据管理器或加密密钥存储中。不要泄露秘密。
如何跑步(高级)
*(有关完整的分步体验,请查看我的 教程)*
此repo包含文档 architecture diagram, n8n_workflow_scaffold 您可以直接导入。对于工作部署,实现以下部分:
- n8n工作流程
- 在n8n中创建如图所示的工作流。 - 添加Telegram触发器节点、允许的用户过滤器、STT集成、LLM代理HTTP节点和Elasticsearch节点。
- AWS基岩
- 为分类和聊天提供模型(或使用带有提示的单一灵活模型)。 - 安全地存储凭据和端点。
- 弹性搜索
- 为摄入的文档创建一个具有适当映射的索引。
Elasticsearch索引映射 semantic_text 字段已启用:
{
"mappings": {
"properties": {
"amount": {
"type": "double"
},
"attachments": {
"type": "nested",
"properties": {
"hash": {
"type": "keyword"
},
"linked_expense_ids": {
"type": "keyword"
},
"type": {
"type": "keyword"
},
"url": {
"type": "keyword"
}
}
},
"audio_hash": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"category": {
"type": "keyword"
},
"chat_id": {
"type": "keyword"
},
"currency": {
"type": "keyword"
},
"merchant": {
"type": "keyword"
},
"normalized_inr": {
"type": "double"
},
"note": {
"type": "text",
"copy_to": [
"semantic_all"
]
},
"payment_method": {
"type": "keyword",
"copy_to": [
"semantic_all"
]
},
"raw_transcript": {
"type": "text",
"copy_to": [
"semantic_all"
]
},
"segments": {
"type": "nested",
"properties": {
"amount": {
"type": "double"
},
"category": {
"type": "keyword"
},
"end_ms": {
"type": "integer"
},
"merchant": {
"type": "keyword"
},
"payment_method": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"start_ms": {
"type": "integer"
},
"text": {
"type": "text",
"copy_to": [
"semantic_all"
]
}
}
},
"semantic_all": {
"type": "semantic_text",
"inference_id": "bedrock-embeddings",
"model_settings": {
"service": "amazonbedrock",
"task_type": "text_embedding",
"dimensions": 1024,
"similarity": "cosine",
"element_type": "float"
}
},
"stt_confidence": {
"type": "double"
},
"stt_model": {
"type": "keyword"
},
"stt_provider": {
"type": "keyword"
},
"ts": {
"type": "date"
},
"user_id": {
"type": "keyword"
}
}
}
}通过semantic_text摄取和处理的样本数据
{
"_index": "expenses",
"_id": "224",
"_version": 1,
"_source": {
"normalized_inr": 4500,
"note": "Burger purchase",
"amount": 4500,
"user_id": "567876545",
"merchant": "McDonald's",
"currency": "INR",
"category": "food",
"raw_transcript": "Can you add my expense on McDonald's burger yesterday which is 25th of September which was Thursday and I paid via my credit card and I spent around 4500.",
"payment_method": "credit_card",
"chat_id": "45678456784567-iyn678",
"ts": "2025-09-25T12:00:00+05:30"
},
"fields": {
"normalized_inr": [
4500
],
"note": [
"Burger purchase"
],
"amount": [
4500
],
"semantic_all": [
"Burger purchase",
"credit_card",
"Can you add my expense on McDonald's burger yesterday which is 25th of September which was Thursday and I paid via my credit card and I spent around 4500."
],
"user_id": [
"567876545"
],
"merchant": [
"McDonald's"
],
"currency": [
"INR"
],
"category": [
"food"
],
"raw_transcript": [
"Can you add my expense on McDonald's burger yesterday which is 25th of September which was Thursday and I paid via my credit card and I spent around 4500."
],
"payment_method": [
"credit_card"
],
"chat_id": [
"45678456784567-iyn678"
],
"ts": [
"2025-09-25T06:30:00.000Z"
]
}
}- 秘密和证书
- 在环境或n8n凭据管理器中配置Telegram bot令牌、Bedrock auth、STT API密钥和Elasticsearch凭据。
示例数据和Colab快速入门
- CSV示例:
data/expense_dataset_elasticsearch.csv包含2025年综合支出,字段如上图所示。将其导入到您的expensesindex(或您喜欢的索引名称)来测试语义搜索和n8n摄入流。
- 在Colab试试:打开 建筑塑料助剂 在Google Colab中运行摄取和查询示例,而无需设置本地工具。在运行之前,更新笔记本单元格中的Elasticsearch端点、身份验证和索引名称。
许可证
该项目将根据Apache许可证2.0获得许可。看 许可证.
