使用 Elastic 和 Mistral 构建多语言 RAG(一)
作者:来自 Elastic Gustavo Llermaly
使用 Elastic 和 Mixtral 8x22B 模型构建多语言 RAG 应用程序。
Mixtral 8x22B 是性能最高的开放式模型,其最强大的功能之一是能够流利使用多种语言;包括英语、西班牙语、法语、意大利语和德语。
想象一下,一家跨国公司拥有不同语言的支持单和解决方案,并希望跨部门利用这些知识。目前,知识仅限于代理使用的语言。让我们解决这个问题!
在本文中,将向你展示如何通过创建多语言 RAG 系统来测试 Mixtral 的语言能力。
你可以按照此笔记本在此处复现本文的示例。
如果你想在自己的自托管 Elasticsearch 集群中完成下面的展示,请详细阅读文章 “使用 Elastic 和 Mistral 构建多语言 RAG(二)”。
步骤
- 创建嵌入端点
- 创建映射
- 索引数据
- 提出问题
创建嵌入端点
我们针对此示例的支持票(suport tickets)将以英语、西班牙语和德语提供。Mistral 嵌入模型不是多语言的(嵌入模型和大模型还不是一回事!),但我们可以使用 e5 模型生成多语言嵌入,因此我们可以索引不同语言的文本并将其作为单一来源进行管理,从而为我们提供更丰富的上下文。
要创建 e5 多语言嵌入,你可以使用 Kibana:
或者 _inference API:
PUT _inference/text_embedding/multilingual-embeddings { "service": "elasticsearch", "service_settings": { "model_id": ".multilingual-e5-small", "num_allocations": 1 , "num_threads": 1 } }
创建映射
对于映射,我们将使用 semantic_text 映射类型,这是我最喜欢的功能之一。它可以为你处理数据分块、生成嵌入和查询嵌入的过程!
PUT multilingual-mistral { "mappings": { "properties": { "super_body": { "type": "semantic_text", "inference_id": "multilingual-embeddings" } } } }
我们将文本字段称为 super_body,因为它通过单一映射类型来处理块和嵌入。
索引数据
我们将用两种语言索引几张包含问题和解决方案的支持单,然后用第三种语言询问有关许多文档中的问题。
以下文档将添加到索引中:
1. 英语支持单:日历同步问题
Support Ticket #EN1234 Subject: Calendar sync not working with Google Calendar
Description: I'm having trouble syncing my project deadlines with Google Calendar. Whenever I try to sync, I get an error message saying "Unable to connect to external calendar service."
Resolution: The issue was resolved by following these steps:
- Go to Settings > Integrations
- Disconnect the Google Calendar integration
- Clear browser cache and cookies
- Reconnect the Google Calendar integration
- Authorize the app again in Google's security settings
The sync should now work correctly. If problems persist, ensure that third-party cookies are enabled in your browser settings.
2. 德语支持单:文件上传问题
Support-Ticket #DE5678 Betreff: Datei-Upload funktioniert nicht
Beschreibung: Ich kann keine Dateien mehr in meine Projekte hochladen. Jedes Mal, wenn ich es versuche, bleibt der Ladebalken bei 99% stehen und dann erscheint eine Fehlermeldung.
Lösung: Das Problem wurde durch folgende Schritte gelöst:
- Überprüfen Sie die Dateigröße. Die maximale Uploadgröße beträgt 100 MB.
- Deaktivieren Sie vorübergehend den Virenschutz oder die Firewall.
- Versuchen Sie, die Datei im Inkognito-Modus hochzuladen.
- Wenn das nicht funktioniert, leeren Sie den Browser-Cache und die Cookies.
- Als letzten Ausweg, versuchen Sie einen anderen Browser zu verwenden.
In den meisten Fällen lag das Problem an zu großen Dateien oder an Interferenzen durch Sicherheitssoftware. Nach Anwendung dieser Schritte sollte der Upload funktionieren.
3. 营销活动创意(噪音)
Q3 Marketing Campaign Ideas
-
Social media contest: "Share Your Productivity Hack"
- Users share tips using our software, best entry wins a premium subscription.
-
Webinar series: "Mastering Project Management"
- Invite industry experts to share insights using our tool.
-
Email campaign: "Unlock Hidden Features"
- Series of emails highlighting lesser-known but powerful features.
-
Partner with a productivity podcast for sponsored content.
-
Create a "Project Management Memes" social media account for lighter, shareable content.
4. Mitarbeiter des Monats (noise)
Mitarbeiter des Monats: Juli 2023
Wir freuen uns, bekannt zu geben, dass Sarah Schmidt zur Mitarbeiterin des Monats Juli gewählt wurde!
Sarah hat außergewöhnliche Leistungen in folgenden Bereichen gezeigt:
- Kundenbetreuung: Sarah hat durchschnittlich 95% positive Bewertungen erhalten.
- Teamarbeit: Sie hat maßgeblich zur Verbesserung unseres internen Wissensmanagementsystems beigetragen.
- Innovation: Sarah hat eine neue Methode zur Priorisierung von Support-Tickets vorgeschlagen, die unsere Reaktionszeiten um 20% verbessert hat.
Bitte gratulieren Sie Sarah zu dieser wohlverdienten Anerkennung!
文档在 Elasticsearch 中的样子如下:
{ "took": 9, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 0.9155389, "hits": [ { "_index": "multilingual-mistral", "_id": "1", "_score": 0.9155389, "_source": { "super_body": { "text": "\n _Support Ticket #EN1234_\n **Subject**: Calendar sync not working with Google Calendar\n\n **Description**:\n I'm having trouble syncing my project deadlines with Google Calendar. Whenever I try to sync, I get an error message saying \"Unable to connect to external calendar service.\"\n\n **Resolution**:\n The issue was resolved by following these steps:\n 1. Go to Settings > Integrations\n 2. Disconnect the Google Calendar integration\n 3. Clear browser cache and cookies\n 4. Reconnect the Google Calendar integration\n 5. Authorize the app again in Google's security settings\n\n The sync should now work correctly. If problems persist, ensure that third-party cookies are enabled in your browser settings.\n ", "inference": { "inference_id": "multilingual-embeddings", "model_settings": { "task_type": "text_embedding", "dimensions": 384, "similarity": "cosine", "element_type": "float" }, "chunks": [ { "text": "passage: \n _Support Ticket #EN1234_\n **Subject**: Calendar sync not working with Google Calendar\n\n **Description**:\n I'm having trouble syncing my project deadlines with Google Calendar. Whenever I try to sync, I get an error message saying \"Unable to connect to external calendar service.\"\n\n **Resolution**:\n The issue was resolved by following these steps:\n 1. Go to Settings > Integrations\n 2. Disconnect the Google Calendar integration\n 3. Clear browser cache and cookies\n 4. Reconnect the Google Calendar integration\n 5. Authorize the app again in Google's security settings\n\n The sync should now work correctly. If problems persist, ensure that third-party cookies are enabled in your browser settings.", "embeddings": [ 0.0059651174, 0.0016363655, -0.064753555, 0.0093298275, 0.05689768, -0.049640983, 0.02504726, 0.0048340675, 0.08093895, ... ] } ] } } } } ] } }
问问题
现在,我们要用西班牙语提出一个问题:
Hola, estoy teniendo problemas para ocupar su aplicación, estoy teniendo problemas para sincronizar mi calendario, y encima al intentar subir un archivo me da error.
预期是检索文档 #1 和 #2,然后将它们作为附加上下文发送给 LLM,最后得到西班牙语的答案。
检索文档
为了检索相关文档,我们可以使用这个简短的查询,它将对嵌入进行搜索,并返回与问题最相关的支持票。
GET multilingual-mistral/_search { "size": 2, "_source": { "excludes": ["*embeddings", "*chunks"] }, "query": { "semantic": { "field": "super_body", "query": "Hola, estoy teniendo problemas para ocupar su aplicación, estoy teniendo problemas para sincronizar mi calendario, y encima al intentar subir un archivo me da error." } } }
关于参数设置的说明:size: 2 因为我们知道我们想要前 2 个文档。excludes 为了响应清晰。文档很短,因此每个文档的长度为一个块。
回答问题
现在我们可以使用 Python 库调用 Mistral 完成 API 来回答问题。
from mistralai.client import MistralClient from mistralai.models.chat_completion import ChatMessage api_key = os.environ["MISTRAL_API_KEY"] model = "open-mixtral-8x22b" client = MistralClient(api_key=api_key) system_message = "You are a helpful multilingual agent that helps users with their problems. You have access to a knowledge base of different languages and you must answer in the same language the question was asked." user_message = """ ## Question: Hola, estoy teniendo problemas para ocupar su aplicación, estoy teniendo problemas para sincronizar mi calendario, y encima al intentar subir un archivo me da error. ## Related knowledge: Support Ticket #EN1234 Subject: Calendar sync not working with Google Calendar... (the rest of the content of the document) \n Support-Ticket #DE5678 Betreff: Datei-Upload funktioniert nicht... (the rest of the content of the document) ANSWER: """ messages = [ ChatMessage(role="system", content=system_message), ChatMessage(role="user", content=user_message) ] chat_response = client.chat( model=model, messages=messages, ) print(chat_response.choices[0].message.content)
答案是完美的西班牙语,而且切中要点!
答案是:
Hola, me alegra que te hayas comunicado con nosotros. Parece que hay dos problemas distintos. En cuanto a la sincronización del calendario, puedes seguir estos pasos para resolver el problema: Ve a Configuración > Integraciones Desconecta la integración del Calendario de Google Borra la caché y las cookies del navegador Vuelve a conectar la integración del Calendario de Google Autoriza de nuevo la aplicación en la configuración de seguridad de Google Si sigues teniendo problemas, asegúrate de que las cookies de terceros están habilitadas en la configuración de tu navegador. En cuanto al problema de subir un archivo, hay varias cosas que puedes probar: Comprueba el tamaño del archivo. El tamaño máximo de carga es de 100 MB. Desactiva temporalmente el antivirus o el cortafuegos. Intenta cargar el archivo en modo incógnito. Si eso no funciona, borra la caché y las cookies del navegador. Como último recurso, prueba a usar un navegador diferente. En la mayoría de los casos, el problema se debe a archivos demasiado grandes o a interferencias causadas por software de seguridad. Al seguir estos pasos, deberías poder cargar el archivo correctamente. ¡Espero que esto te ayude a resolver tus problemas! Si tienes alguna otra pregunta, no dudes en preguntar.
结论
Mixtral 8x22B 是一个强大的模型,它使我们能够利用不同语言的数据源,能够回答、理解和翻译多种语言。这种能力——与多语言嵌入一起 —— 使你能够在数据检索和答案生成阶段获得多语言支持,完全消除语言障碍。
如果你有兴趣重现本文的示例,你可以在此处找到包含请求的 Python Notebook。
准备好自己尝试一下了吗?开始免费试用。
Elasticsearch 集成了 LangChain、Cohere 等工具。加入我们的高级语义搜索网络研讨会,构建你的下一个 GenAI 应用程序!
原文:Building multilingual RAG with Elastic and Mistral — Search Labs

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
请不要自己写,Spring Boot非常实用的内置功能
在 Spring Boot 框架中,内置了许多实用的功能,这些功能可以帮助开发者高效地开发和维护应用程序。 松哥来和大家列举几个。 一 请求数据记录 Spring Boot提供了一个内置的日志记录解决方案,通过 AbstractRequestLoggingFilter 可以记录请求的详细信息。 AbstractRequestLoggingFilter 有两个不同的实现类,我们常用的是 CommonsRequestLoggingFilter。 通过 CommonsRequestLoggingFilter 开发者可以自定义记录请求的参数、请求体、请求头和客户端信息。 启用方式很简单,加个配置就行了: @Configuration public class RequestLoggingConfig { @Bean public CommonsRequestLoggingFilter logFilter() { CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter(); filter.setIncludeQue...
- 下一篇
仅需一篇,吃透持续集成对IT团队和企业的好处
对于各行各业的公司而言,软件是关键的竞争优势。公司越快地将新的增强功能和特性推向市场,所获得的竞争优势就越大。为了获得这种领先优势,企业开发团队需要优化其工作流程以提高效率、质量和可靠性。 因此,开发团队采用持续集成(CI)来加速和自动化软件交付生命周期。CI是持续交付(CD)不可或缺的DevOps流程,它将代码提交到源代码管理中,并自动“持续”执行构建。持续集成代码以使IT团队及其业务同行都受益的方式改进了流程。 一、持续集成对IT团队的好处 CI持续处理、测试并上传对代码库所做的更改或添加。该代码保存在源代码管理管理系统中,所有开发人员都可以使用该系统进行测试和参考。任何使用该应用程序的开发人员都可以访问最新代码。持续集成为IT团队带来了很多好处,以下是对IT团队及其个人成员影响最大的三个。 1.减少错误和手动任务 CI要求团队标准化流程并应用最佳实践,以便可以轻松地重复执行。然后,团队就可以使可重复过程自动化,从而减少了人工工作,并减少了人为错误的概率。由于团队减少了对手动任务的关注,他们可以将更多的时间用于战略计划和其他高价值项目。 2.及早发现并解决集成挑战 持续集成会定期测...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS关闭SELinux安全模块
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- MySQL8.0.19开启GTID主从同步CentOS8
- Mario游戏-低调大师作品