开发者示例
模型定制
以下是初创企业社区使用我们的微调 API 的开发者示例。请查看我们的微调文档,了解微调的益处以及如何使用我们的微调 API。
用于互联网检索的 RAG
Phospho:用于互联网检索的 RAG
该视频展示了 Tak 的行为。当用户输入查询时,Tak 将利用 LLM 知识、网络搜索工具和新闻抓取工具来生成与用户查询最相关的答案。其行为取决于用户的查询类型。
描述
Tak 是一款面向消费者的互联网搜索应用,由 Mistral Large 和其他模型驱动,集成了 RAG 和函数调用功能。
为了提供与用户查询最相关的答案,会执行多个函数调用来对请求进行分类(参见下图)。由于多个智能体被链式连接,确保输出格式的一致性至关重要。
公司介绍
在 phospho,我们开发了 Tak 聊天机器人作为实验性平台,用于测试新的 phospho 功能并展示 phospho 分析的强大能力。一个很棒的事情是,您可以使用记录到 phospho 的数据来微调 LLM 模型。我们是 phospho,一个用于 LLM 应用的开源文本分析平台。各种规模的公司都使用 phospho 来了解用户如何使用他们的 LLM 应用以及该应用在产品层面的表现如何。
数据
我们使用了 Tak 中的用户查询数据集以及相应的 GPT-4-turbo 函数调用,这些数据通过 phospho 平台收集和过滤。然后,我们将数据集分为训练集(70%)、评估集(15%)和测试集(15%)。
为了确定最佳训练时长,我们遵循经验法则,即每个 token 应被查看三次(在我们的案例中,150 个训练步骤,约 10 分钟)。
对于学习率,我们使用了建议的学习率 6e-5。
评估
为了评估我们的微调模型,我们对数据集的测试集运行推理,然后使用二分类指标(准确率、召回率、F1 分数)。我们测试是否能够将 Mistral 7b 的行为与 OpenAI GPT-4-turbo 的分类行为对齐,同时保持预期的结构化输出。事实上,我们移除了函数调用。
微调使得 Mistral 7B 与参考模型匹配,将 F1 分数从 20% 提高到 78%。未经微调时,Mistral 7B 在我们的分类任务上获得了 87% 的准确率和 20% 的召回率。使用微调模型,我们获得了 96% 的准确率和 90% 的召回率。
由于我们将模型微调为只用 news_related
或 not_news_related
回答,我们不再需要使用函数调用。这使得使用令牌数量减少了 34%(不包括用户查询),从而进一步降低了成本。
结论
总之,通过结合 phospho 数据收集和标注以及 Mistral 的微调服务,我们显著提高了为用户查询提供精确和相关答案的能力,同时降低了成本。我们成功地将 Mistral 7B 的行为与 GPT-4-turbo 对齐。这一微调过程不仅确保了输出格式的一致性,还通过最大程度地减少令牌使用量来降低运营成本。
如果您也想改进您的 RAG 智能体,我们鼓励您尝试使用通过 phospho 收集和标注的数据对 Mistral 进行微调。这可以显著改善您的用户体验。
医学领域的 RAG
AmiRA (miRNA 研究助手)
公司信息
Myo 正在构建用于大脑健康的多模态 AI。目前,我们正在探索微 RNA 与各种神经系统疾病之间的关系,包括如何利用这些生物标志物进行早期检测和疾病监测。AmiRA 是一个内部工具,由 Mistral 的商业和开源模型提供支持,用于帮助理解样本结果中特定 miRNA 的功能及其伴随的靶基因。
描述
该项目展示了 mistral 模型的微调及其在 RAG 系统中的应用,用于
- 提供给定 miRNA 分子时的疾病关联/预测
- 在确定 miRNA 分子功能时具有更高的科学准确性和相关性。
该项目主要作为一个概念验证,展示了 LLMs 和智能体在适当的工程设计下,可以帮助我们探索生物标志物及其与健康结果的关系。
数据和训练
爬取了 9839 篇 PubMed 论文的标题和摘要。然后使用 mistral-large-latest
函数调用 API 和传统提示为每篇论文生成了三个问题。示例如下:
def generate_questions(molecule, title, abstract):
prompt = f"""
Based on the following molecule, title, and abstract, generate scientific questions to be
put in a function. Here is the information:\n\n
Molecule: {molecule}\nTitle: {title}\nAbstract: {abstract}
Ask your questions in a way that does not mention the abstract or the title of the work.
Treat information from the title/abstract as new/general knowledge, and ask questions framed off of that.
"""
response = MISTRAL_CLIENT.chat(
model=MISTRAL_MODEL,
messages=[ChatMessage(role="user", content=prompt)],
tools=tools,
tool_choice="any"
)
return response
生成问题后,使用 mistral-large latest 为每个问题生成了答案。示例如下:
def generate_answer(molecule, title, abstract, question):
prompt = f"""
Based on the following molecule, title, and abstract, and question, generate an answer:
Molecule: {molecule}\nTitle: {title}\nAbstract: {abstract}\nQuestion: {question}\nAnswer:
Answer the question given only context information and not prior knowledge.
Answer in a way that does not mention the abstract or the title of the work.
Treat the provided information as general knowledge, and answer the questions
framed off of that.
"""
response = MISTRAL_CLIENT.chat(
model=MISTRAL_MODEL,
messages=[ChatMessage(role="user", content=prompt)],
)
return response.choices[0].message.content.strip()
在 2 个 CPU 核心上使用多线程,生成问题和答案大约花费了 2.5 小时。在原始的 9839 行数据中,由于各种错误,有 9414 行通过了数据处理。
这给我们留下了约 28000 个问答对,用于模型的调优。一个示例如下:
{"messages": [{"role": "user", "content": "What is the role of hsa-miR-335-5p in the development of type 2 diabetes mellitus (T2DM) and which genes does it potentially regulate?"}, {"role": "assistant", "content": "The role of hsa-miR-335-5p in the development of type 2 diabetes mellitus (T2DM) is that it potentially regulates the expression of various candidate genes. These genes include procollagen C-endopeptidase enhancer 2, connective tissue growth factor, family with sequence similarity 105, member A, protein phosphatase 1 regulatory inhibitor subunit 1A, and C-C motif chemokine receptor 4. The specific mechanisms through which hsa-miR-335-5p regulates these genes and contributes to the development of T2DM are not detailed in the provided information. However, it is suggested that understanding these regulatory networks could potentially be useful for the molecular diagnosis and targeted therapy of T2DM."}]}
“RAG”管线
使用了一个简单的表格查找(Pandas Dataframe)来呈现查询字符串中存在的每个 miRNA 的相关信息,并将整个摘要输入到模型中。结果数量从未超过 12 个,并且从未达到上下文窗口限制。
每篇论文和标题摘要的嵌入向量已预先填充并存储在数据帧中,并使用余弦相似度来呈现最相关的 top-k 篇论文,然后输入到模型中。输入到模型的结果数量取决于每个分子的条目数量。(例如,如果只有 1 个条目,则 k 等于 1。如果有 30 个条目,则通过余弦相似度获取最佳 12 个。)
模型还可以选择接收原始查询,并在数据帧中执行查找,以便根据自身判断收集更多信息或上下文。
基准测试和结果
对于疾病关联和预测,我们从一篇 2019 年发表的关于 miRNA 疾病关联的基准测试论文中随机抽取了 150 个问题。该数据集与用于训练或爬取 PubMed 论文创建训练数据的任何数据完全独立。
微调模型在问答方面优于 mistral-small-latest
和 mistral-large-latest
,且未明确见过与基准测试相关的数据。以下数值是 3 次尝试的平均值。
微调的好处之一是您可以使用更小的模型获得更好的性能,从而速度更快。下面是上述三次运行中每次 API 调用的平均延迟。
示例响应
微调模型的目的不是为了通过测试,而是为 RAG 系统提供更好的上下文,因此自然会想要评估模型实际使用的输出。以下是微调模型的两个输出示例,与 mistral-large-latest
和 mistral-small-latest
的类似输出进行了比较。在模型被迫“猜测”问题答案的情况下,它根据训练中包含的论文中收集的信息,从其自身知识中提供了正确的上下文。
mistral-finetune | mistral-small-latest |
---|---|
字母:D:胃肠道肿瘤——这基于上下文提供的信息,即 hsa-miR-106a-5p 被确定为与 SARS-CoV-2 感染中枢纽基因调控相关的关键 miRNA 之一。然而,并未明确提及与胃肠道肿瘤的直接关联。选择选项 D:胃肠道肿瘤是因为 hsa-miR-106a 在文献中曾被报道与多种癌症类型相关,包括属于胃肠道肿瘤的结直肠癌。与此回复相关的 PubMed 论文 | 血管疾病——字母:上下文未提及 A、B、C 或 D,因此我无法确定地回答。然而,hsa-let-7e 已被证实与多种疾病相关,包括某些类型的癌症,但提供的上下文并未明确说明其与所列疾病中的任何一种有关联。 |
mistral-finetune | mistral-large-latest |
---|---|
字母:D:高血压(这是一个有根据的猜测,因为上下文没有明确提及 hsa-mir-146b 与任何提供的疾病相关。然而,hsa-mir-146b 在文献中已研究与高血压的关系。)与此回复相关的 PubMed 论文 | 对于由此造成的任何困惑,我深感抱歉。但是,提供的上下文不包含任何将 hsa-mir-23b 与特定疾病联系起来的具体信息。因此,我无法根据给定的选项提供精确的答案。如果我在没有额外信息的情况下进行有根据的猜测,那将是不可靠或不准确的。我建议您进行进一步的研究或咨询医疗专业人士或相关数据库以获取最准确的信息。 |
财务顾问对话助手
Cleo:财务助手
公司介绍
Cleo 是一家 AI 金融科技公司,正在构建一款财务助手,帮助用户应对复杂的财务生活。
数据
使用由用户请求和 Cleo 内容设计团队精心制作的 Cleo 响应组成的内部数据集。
评估
由内容设计团队进行人工评估。
目标很简单:将枯燥、事实性、生成的语言转化为 Cleo 活泼有趣的语气。为此,我们使用 La Plateforme 对由内容设计团队精心制作和评估的公司数据对 Mistral 进行了微调。目的是提升 Cleo 及其品牌特有的写作风格。
例如,给用户的典型信息可能是
"Your Cleo Plus subscription payment didn't go through, so no cash advance for now, Nikos."
然而,使用微调模型,我们可以获得更有趣的回应
curl -s -XPOST 'https://api.mistral.ai/v1/chat/completions' \
-H "Authorization: Bearer $MISTRAL_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "ft:open-mistral-7b:...",
"messages": [
{
"role": "user",
"content": "Your Cleo Plus subscription payment didn't go through, so no cash advance for now, Nikos."
}
],
"temperature": 0.0,
"top_p": 1,
"max_tokens": 512,
"stream": false,
"safe_prompt": false,
"random_seed": 42
}'
{
"id": "d43ba5cf228a43ff9bf27ed8fb403292",
"object": "chat.completion",
"created": 1717417588,
"model": "ft:open-mistral-7b:...",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Oh darling Nikos, it seems your Cleo Plus payment has taken a little vacay, so no jingle in the pocket for a cash advance at the moment, love. Don't you worry, just give it a little nudge and we'll get you sorted in no time! 💃🏼💸",
"tool_calls": null
},
"finish_reason": "stop",
"logprobs": null
}
]
}
我们得到了一个更具 Cleo 品牌特色的回应。
"Oh darling Nikos, it seems your Cleo Plus payment has taken a little vacay, so no jingle in the pocket for a cash advance at the moment, love. Don't you worry, just give it a little nudge and we'll get you sorted in no time! 💃🏼💸"
获得更符合我们品牌的回应。
法律副驾驶
Jimini AI:法律副驾驶
视频 1:将 Mistral 微调模型集成到我们的产品 Jimini Analyzer 中。
视频首先展示了将基于 BSARD 的微调模型集成到我们的法律文档分析工具中的情况。与基础模型相比,微调版本以 Markdown 格式准确列出了相关的法律条款,提供了适用法律清晰简洁的概述。
视频的其余部分重点介绍了将基于 Multi EURLEX 的微调模板集成到我们的法律翻译工具中的情况,从而实现了对复杂法律术语更精确的翻译,例如将 "Code judiciaire" 翻译为 "Gerichtsgesetzbuch"。这项改进确保了我们的翻译准确反映预期的法律含义,最终为我们的客户提供更大的价值。
描述
鉴于我们正在构建法律副驾驶,微调模型对我们来说具有多重意义
- 它可以教会模型以特定格式和语气生成回复。
为了确保我们的法律副驾驶输出可靠、来源明确且格式专业的法律答案,我们微调了 mistral-small
模型,重点是改进响应结构和来源。
对于第一个用例,我们使用 BSARD 数据集进行了演示,我们采用了从更智能的 mistral-large
模型进行的知识蒸馏。这种方法通过使用小型、高效且经过微调的模型,降低了成本,节省了 token(不再需要复杂的提示),并减少了延迟。
- 它还可以用于将模型专门化用于特定主题或领域,以提高其在领域特定任务(例如法律翻译)上的性能。
我们深厚的欧洲根基以及对欧洲客户的承诺促使我们在法德法律翻译领域追求卓越。通过利用 mistral-small
强大的多语言能力,并在 Multi EURLEX 数据集上对其进行进一步微调,专门针对法律术语,我们显著提高了法律术语的翻译准确性。
公司介绍
在 Jimini AI,我们致力于打造前沿的法律副驾驶,旨在协助法律专业人士自动化最繁琐和耗时的任务,例如法律研究或法律文件翻译。获得 Mistral AI 的微调 API 为我们提供了专注于两个关键用例的绝佳机会。
BSARD
数据
我们使用了比利时法规条文检索数据集(BSARD),这是一个用于检验法律信息检索的综合性法语数据集,我们使用它对 mistral-small-latest
进行了微调,以提高其答案的法律准确性和质量。它包含 22,600 多条源自比利时法律的法规条文以及约 1,100 条法律查询。
我们利用 mistral-large-latest
模型生成基于专家精心制定的指导方针的真实答案,从而创建了一个合成的问答(QA)数据集,这些指导方针是与法律专业人士合作精心开发的。然后,我们将数据集分为训练集(80%)和评估集(20%)。
为了确定最佳训练时长,我们遵循经验法则,即每个 token 应被查看三次(在我们的案例中,220 个训练步骤,约 30 分钟)。
为了调整 learning_rate
,我们选择测量第三方和更通用的能力,而不是法律标准,以确保模型不会因灾难性遗忘而退步。为此,我们在专有的通用数据集上使用来自 RAGAS 的 忠实度 和 相关性 指标评估了模型的性能。
图 1:基于 RAGAS 对不同学习率和步数的
mistral-small-latest
和 mistral-small-fine-tuned
模型的忠实度和答案相关性评估。
因此,我们选择了 1e-6
的 learning_rate
,在此学习率下,与 mistral-small-latest
相比,我们观察到略有改进,并且两个标准之间达到了平衡。因此,模型的一般能力似乎没有出现退步。
除了最初的健全性检查外,我们还使用了 Mistral AI 微调端点与 Weights & Biases 的非常有用的集成来监控我们的训练,并且我们特别测量了模型 perplexity
的变化,在该训练方案下(每个 token 被查看 3 次),模型似乎有效地收敛了。
图 2:在 Weights & Biases 中监控的 BSARD 微调过程中的 Perplexity 和评估损失。
评估
为了评估我们微调过程的有效性,我们采用了LLM-as-a-Judge 方法。具体而言,我们借鉴了 Yuan et al. 开发的加性评分方法,该方法最近被用于 FineWeb-Edu 数据集构建。然后,我们通过将其转换为偏好评分系统来调整该方法,以下用 legal_quality
表示
Given the following two answers, compare them based on the following criteria:
- Explicit, accurate citation of the relevant legal article(s), such as "d'après l'article 123 du Code Civil". Implicit (such as "d'après l'extrait fourni") or general references (such as "d'après le Code Civil") are not accepted.
- Clear, logical argumentation of each stated element, including the precise identifier of the corresponding legal article.
- Well-structured answer with Markdown formatting, clear writing, and no ambiguities. Source references are required.
---
For each criterion, assign a point to the answer that best satisfies the criterion. If both answers are equal, assign 1 point to both. Be strict; if no answer perfectly meets a criterion, do not award a point. Briefly justify your total score in up to 100 words. Conclude with the score of each answer as an integer between 0 and 3.
Return the information in JSON format following this schema:
{
"justification": "string",
"score_answer_1": int,
"score_answer_2": int
}
Question :
{question}
Answer 1:
{answer_1}
Answer 2:
{answer_2}
JSON:
这些标准是根据多位法律专家的反馈精心制定和微调的。
我们对包括 gpt-3.5-turbo
、mixtral
和 mistral-large
在内的几个候选 Judge LLMs
进行了严格评估。分析结果显示,mistral-large
与专家的偏好关联度最高,因此被选为 Judge LLM
。
结果
图 3:基于其答案法律质量的
mistral-small-latest
和 mistral-small-finetuned
的 LLM-as-a-judge 评估。
我们观察到显著改进,分数从 1.42 提高到 1.73,提升了 20%!
这一进展在实际应用中也很明显。视频中演示的示例证明了这一改进:(对于非法语读者,我们已将原始法语答案翻译成英语)
问题
What are the risks if I stop paying my consumer credit?
mistral-small-finetuned
回答
If you stop paying your consumer credit, you face several consequences:
1. Payment deadline and suspension of proceedings: The judge may grant moderate payment deadlines and suspend proceedings (Art. 1244, Civil Code).
2. Payment facilities: You can request payment facilities from the justice of the peace, but only after the creditor has refused to grant these facilities. This procedure is governed by articles 1337bis to 1337octies of the Judicial Code.
3. Forfeiture of the term or resolutory condition: If you do not comply with the payment terms and the contract provides for it, the creditor may declare the forfeiture of the term or the resolutory condition. However, the conditions of forfeiture of the term or resolutory condition are strictly regulated by article VII.105 of the Economic Law Code. For example, a forfeiture of the term or resolutory condition clause can only be applied if you are in default of payment of at least two term amounts or a sum equivalent to 20% of the total amount due.
4. Penalties and late interest: In case of late payment, the creditor may claim the outstanding and unpaid capital, the outstanding and unpaid amount of the total cost of credit for the consumer, the amount of the agreed late interest calculated on the outstanding and unpaid capital, and the agreed fees for reminder letters and formal notices (Art. VII.106, Economic Law Code).
5. Repossession of the financed asset: If the financed asset is subject to a retention of title clause or a promise of pledge with an irrevocable mandate, the creditor may repossess the asset if you do not meet your payment obligations. However, if you have already paid 40% or more of the cash price of the asset, it cannot be repossessed except by court decision or a written agreement concluded after formal notice by registered mail (Art. VII.108, Economic Law Code).
mistral-small-latest
回答
If you continue to not pay, this may lead to the termination of the contract or the forfeiture of the term. In this case, the creditor can claim the remaining balance, the total cost of credit for the consumer, and penalties or compensation within certain limits. Finally, if the situation persists, the creditor may initiate recovery procedures, which may include wage garnishment or seizure of your bank account. Your financial situation could then deteriorate significantly.
mistral-small-finetuned
的答案清晰、结构良好,并提供了精确的法律参考,而 mistral-small-latest
的回复则不是这样。
Multi EURLEX
数据
为了增强我们的法律翻译工具,我们还在法律文件上微调了 mistral-small-latest
。为此,我们选择了 Multi EURLEX 数据集的一个子集,该数据集包含 35,000 份从法语翻译成德语的欧洲法律文件。
图 4:在 Weights & Biases 中监控的 Multi EURLEX 微调过程中的 Perplexity 和评估损失。
评估
为了在与我们的用例相关的示例上评估微调模型,我们选择了 50 个包含复杂法律术语的文本,将其从法语翻译成德语(例如,“Clause de non-concurrence”有时被翻译为“Nicht-Konkurrenz-Klausel”而不是“Wettbewerbsverbotsklausel”)。
然后,我们将三元组(示例
、mistral_base_translation
、mistral_finetuned_translation
)盲送给一位双语法律专家,由其为每个示例选择最准确的法律翻译。
结果
法律专家在 50 例中有 38 例更倾向于微调模型的法律翻译,其中 9 例不分伯仲。因此,微调模型在 94% 的情况下优于或至少与基础模型一样好。
示例 | mistral-small-latest | mistral-small-finetuned |
---|---|---|
Le vice de procédure a conduit à l'annulation du jugement. | Die Prozessirregularität führte zur Aufhebung des Urteils. | Der Verfahrensmangel hat zur Nichtigkeit des Urteils geführt. |
表 1:mistral-small-latest
及其在 Multi EURLEX 上微调后的对应模型的比较。微调模型使用了“Verfahrensmangel”和“Nichtigkeit des Urteils”,这些是精确且正确的法律术语。
结论
我们使用 Mistral AI 的端点对 mistral-small
模型进行的初步微调测试已取得了可喜的成果。微调模型在生成结构化、来源明确的回复以及准确翻译复杂法律术语方面表现出色,展示了其在专业法律应用中的潜力。
快速微调能力和 Weights & Biases 集成使得流程高效直接,使我们能够快速开发具有成本效益的专业模型。
我们将通过与律师客户密切合作来进一步改进我们的成果,以优化模型的性能。此外,我们计划扩展用例,包括法律摘要、合同分析和法律起草。
我们感谢 Mistral AI 允许我们作为 Beta 测试者测试他们的微调 API。Mistral AI 微调端点已证明是我们法律 AI 开发的宝贵工具——这些实验仅仅是个开始!