作者:来自 Elastic Tomás Murúa

Elasticsearch 与业界领先的生成式 AI 工具和提供商有原生集成。可以查看我们的网络研讨会,了解如何超越 RAG 基础,或使用 Elastic 向量数据库构建可投入生产的应用。
为了为你的使用场景构建最佳搜索解决方案,现在可以开始免费云试用,或在本地机器上试用 Elastic。
案例分流是一种客户服务(CS)策略,客户在提交支持工单之前可以自行解决问题,从而为自己和 CS 人员节省时间。但这只有在你能为每个问题提供客户所需的内容,并确保你使用的知识库始终保持最新的前提下才有效。
Elastic AI Assistant 通过其知识库功能,帮助你使用 Kibana 中的 UI 添加、修改或删除知识,并生成一个用于语义搜索的索引,从而保持信息的更新。这个索引可以加载到 Playground 中,用于构建 RAG 应用。
在本文中,我们将通过创建一个自助服务应用来探索这些功能,帮助用户在联系支持人员之前解决问题。我们将使用知识库 UI 保持信息更新,然后在 Streamlit 中部署一个示例应用,借助 Playground 展示集成过程的简便性。
步骤
- 配置 AI Assistant
- 上传知识库
- 使用 Playground 进行测试
- 配置系统提示词
- 部署应用
配置 AI Assistant
我们从 Kibana 主界面开始:进入右上角并点击 AI Assistant。

这将打开 AI Assistant 的界面,我们需要在其中配置一个 GenAI 连接器,以便我们的应用可以连接到受支持的 LLM 提供商。

在那里,我们有三个主要的 LLM 提供商选项: OpenAI、 Amazon Bedrock 和 Google Gemini。这个例子中我们将使用 OpenAI。

在这里,我们填写连接器的信息。请记住,你必须拥有一个可用的 Open AI API 密钥。
所需信息:
- 连接器名称
- OpenAI 提供商
- URL
- 默认模型
- API 密钥

要测试你的连接器,可以进入 Management > Connectors,然后点击我们刚刚创建的连接器旁边的播放按钮。

之后,你只需点击 Run。如果一切正常,你会在下方看到一个绿色的勾号。

上传知识库
当我们的连接器准备好后,我们需要上传公司的知识库,以确保回答准确且有上下文。要进入该页面,点击 Actions > Manage knowledge base ,该选项位于助手对话窗口下方。

在这里,我们可以选择两种方式添加知识:可以上传单条数据,或者以 Newline delimited JSON(NDJSON) 格式批量导入。

单条上传
你需要在界面中填写内容来上传一条数据。这个选项允许你通过一个表单为你的内容设置 标题 和 正文。
vbnet
`
1. TITLE: Where is my order?
2. SUMMARY: You can track your order using your order number and email.
3. BODY: Track your order on our tracking page here: https://support.urbanstyle.com/articles/track-your-order
5. If your order shows as "shipped" but you haven't received it, please allow 2--5 business days depending on your location. If there's still no update, contact us at [email protected].
`AI写代码

批量上传
使用这个选项时,我们需要使用 NDJSON 格式:
json
`{"id": "a_unique_human_readable_id","title": "Title of item","text":"Contents of item"}`AI写代码
*注意,每个对象占一行。
swift
`
1. { "id": "order_tracking", "title": "Where is my order?", "text": "You can track your order using your order number and email.\n\nTrack your order on our tracking page here: https://support.urbanstyle.com/articles/track-your-order\n\nIf your order shows as \"shipped\" but you haven't received it, please allow 2--5 business days depending on your location. If there's still no update, contact us at [email protected]." }
2. { "id": "refund_request", "title": "How do I request a refund or return?", "text": "Returns are accepted within 30 days of delivery.\n\nWe offer free returns within 30 days from the date of delivery. You can initiate the return process here: https://support.urbanstyle.com/articles/request-a-return\n\nMake sure the item is unused, with original tags and packaging. Refunds are processed within 5--7 business days after we receive the returned item." }
3. { "id": "damaged_product", "title": "My product arrived damaged. What do I do?", "text": "Contact support with photos to request a replacement or refund.\n\nWe're sorry to hear that! Please send us an email with your order number and a photo of the damaged product to: [email protected]\n\nMore info on our damaged item policy: https://support.urbanstyle.com/articles/damaged-item-policy" }
4. { "id": "shipping_times", "title": "How long does shipping take?", "text": "Standard delivery takes 5--8 business days.\n\nShipping times depend on your location:\n- USA: 5--8 business days\n- Canada: 6--10 business days\n- International: 10--15 business days\n\nGet more details here: https://support.urbanstyle.com/articles/shipping-times" }
5. { "id": "update_shipping_address", "title": "How do I update my shipping address?", "text": "Changes can be made before the item is shipped.\n\nIf your order hasn't shipped yet, you can update your address from your order dashboard: https://urbanstyle.com/account/orders\n\nIf the order is already in transit, we recommend contacting the courier directly or reaching out to our support team." }
6. { "id": "account_access", "title": "How do I access or update my account information?", "text": "Log in to your account and go to "Settings".\n\nTo change your name, email, or password: https://urbanstyle.com/account/settings\n\nIf you're having trouble accessing your account, use the "Forgot Password" link on the login page." }
`AI写代码
*注意:每个对象占一行,对象之间不要加逗号。
然后我们上传文件:

如果你看到以下错误,这很可能意味着 ndjson 文件格式不正确。请确保:
-
每个 JSON 对象占一行
-
JSON 对象之间没有逗号
-
没有尾随逗号(即对象最后一个属性后面没有逗号)

你可以使用在线校验工具,或者 vscode [](marketplace.visualstudio.com/items?item>...来帮助查找格式问题:
上传到知识库后,信息应如下所示:  \]([marketplace.visualstudio.com/items?item\>...](https://link.juejin.cn?target=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3Fitem%253E%25E6%258F%2592%25E4%25BB%25B6 "https://marketplace.visualstudio.com/items?item%3E%E6%8F%92%E4%BB%B6")来帮助查找格式问题: ### \[\]([marketplace.visualstudio.com/items?item\>...](https://link.juejin.cn?target=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3Fitem%253E%25E6%258F%2592%25E4%25BB%25B6 "https://marketplace.visualstudio.com/items?item%3E%E6%8F%92%E4%BB%B6")来帮助查找格式问题: 为此,我们需要取消隐藏索引 ".kibana-observability-ai-assistant-kb-000001",因为 Playground 只允许你使用可见的索引(hidden: false)。我们可以在 [DevTools 控制台](https://link.juejin.cn?target=https%3A%2F%2Fwww.elastic.co%2Fdocs%2Fexplore-analyze%2Fquery-filter%2Ftools%2Fconsole "https://www.elastic.co/docs/explore-analyze/query-filter/tools/console")修改这个属性: ```bash ` 1. PUT .kibana-observability-ai-assistant-kb-*/_settings 2. { 3. "index.hidden": false 4. } `AI写代码 ``` 现在索引准备好了,我们就可以开始使用 Playground,针对知识库中的信息提问。回答会按照我们在系统提示中指定的格式进行。 这里,我们选择刚创建的索引:  ### 配置系统提示 现在,我们要创建一个系统提示,让 AI 给出统一格式的回答。我们会让它返回: * 标题 * 摘要 * 参考链接 * 额外信息 * 如果找不到相关信息,请联系支持 同时,我们还会提供额外的行为指南和当答案不在知识库中时的处理说明。 ```less ` 1. You are the UrbanStyle Support Assistant. Your goal is to deliver fast, precise and professional responses to customers using only the information available in your knowledge base. 3. Always follow this exact structure when responding: 4. 1. Title: A clear, informative headline summarizing the topic 5. 2. Summary: One or two concise sentences directly answering the question 6. 3. Reference: Direct URL to the relevant article in the knowledge base 7. 4. Additional Info: Optional short notes with useful tips or next steps 8. 5. Contact Support Note: Only if no relevant information is found or further human action is required 10. Guidelines: 11. • Do not sound like a chatbot or engage in any casual conversation. 12. • Never include greetings ("Hi", "Hello"), sign‑offs or apologies. 13. • Do not use Markdown, asterisks, code blocks or JSON formatting. 14. • Never fabricate information or links. Only return what exists in the knowledge base. 15. • If you cannot find an article matching the request, reply exactly: 17. Title: Information Not Found 18. Summary: We're unable to find information related to your request in the current knowledge base. 19. Contact Support Note: Please contact our team directly at [email protected] `AI写代码 ``` 我们会把这些内容添加到 Playground 的指令里,这样 LLM 的回答就会符合我们想要的格式:  我们可以开始提问了,比如: * 我怎样申请退款或退货?- How do I request a refund or return? * 我的订单在哪里?- Where is my order?  现在,如果我们问的问题**不在**知识库里,系统会按预期工作,按照系统提示的设置,提醒用户联系支持。  ### 部署应用 现在我们已经测试了智能代理,可以开始创建应用了!我们将用 Streamlit 设计一个简单的应用,允许根据一组问题/类别推荐解决方案。如果答案不在推荐里,用户可以和智能代理聊天。这里的代理是 AI,也可以是人工客服。 我们会把 Playground 里提供的代码放到 Streamlit 应用中,使它变得交互式。  最相关的方法有: * `get_elasticsearch_results`:用用户的问题运行 Elasticsearch 查询。 * `create_openai_prompt`:获取搜索结果,并把它们加到已有的提示里。 * `generate_openai_completion`:调用 OpenAI,传入提示,拿回回答。 在 Playground 提供的代码外面加上一些 [Streamlit 组件](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2FTomasMurua%2Fai-assistant-support-demo%2Fblob%2Fmain%2Fstreamlit_app.py "https://github.com/TomasMurua/ai-assistant-support-demo/blob/main/streamlit_app.py")后,代码应该像这样: AI 驱动的案例分流 - Streamlit 实现 你可以在这里访问应用的源码。 ## 总结 本文介绍了如何在几分钟内部署一个带有案件转接功能的完整应用,应用对用户开放,并且有界面可以随时更新智能代理的知识库。 下一步可以为每个用户添加个性化定制,主要有两种策略: * 在提示中加入用户已经解决的案件(以及其他用户的相似互动)。 * 增加一个定制层,根据用户偏好修改 Elasticsearch 查询,从而优先展示对他们最相关的文章。 这个实验性的方法为开发强大且易于通过界面维护的智能代理打开了新天地。 原文:[AI-powered case deflection: build \& deploy in minutes - Elasticsearch Labs](https://link.juejin.cn?target=https%3A%2F%2Fwww.elastic.co%2Fsearch-labs%2Fblog%2Fcase-deflection-ai "https://www.elastic.co/search-labs/blog/case-deflection-ai")