Gemini 文本模型的模型调整

本页面提供了建议的步骤,可帮助您使用 Gemini 微调文本模型。本指南介绍了分类、情感分析和提取用例。

限制

  • Gemini 模型不支持置信度分数。
  • Gemini 无法提供情感分析的数字分数。这限制了根据数字阈值估算情感的能力。

为了提高 AI 模型输出的可靠性,不妨考虑采用自一致性等方法来进行置信度评分。这种技术会针对同一输入从模型生成多条输出,然后采用多数投票系统来确定最可能的输出。置信度分数可以表示为反映生成多数输出的次数所占比例的比率。

对于情感分析任务,请考虑使用 LLM 作为评分者来提供语音置信度分数。提示模型分析情感,然后返回口头评分(例如“非常可能”),而不是数值分数。在 LLM 情境中,口头分数通常更容易理解,并且不易出现偏差。

使用 Gemini 调优文本模型

您可以使用以下两个选项来调优文本模型,以使用 Gemini 进行分类、提取和情感分析,通过预训练模型和自定义微调提供提示。

  • 使用预训练的 Gemini 模型发出提示:提示是撰写有效指令的艺术,可以指导 Gemini 等 AI 模型生成所需的输出。这涉及设计提示,以清晰地传达任务、所需格式和任何相关上下文。您只需进行最少的设置即可使用 Gemini 的功能。它最适合的情况:

    • 有限的加标签数据:您有少量已加标签的数据或无法承受冗长的微调过程。
    • 快速原型设计:当您需要快速测试概念或获取基准性能,而无需投入大量精力进行微调时。
  • 自定义 Gemini 模型调优:为了获得更个性化的结果,Gemini 允许您针对特定数据集微调其模型。如需创建在特定领域表现出色的 AI 模型,不妨考虑微调。这涉及使用您自己的标记数据集重新训练基本模型,使其权重适应您的任务和数据。您可以根据自己的应用场景调整 Gemini。在以下情况下,微调最为有效:

    • 您有标记数据:用于训练的可调数据集(例如 100 个示例或更多),可让模型深入学习任务的具体细节。
    • 复杂或独特的任务:在高级提示策略不够用且必须根据您的数据量身定制模型的场景中。

请尽可能尝试这两种方法,看看哪种方法能针对您的具体用例取得更好的效果。我们建议从提示开始找到最佳提示。然后,继续进行微调(如果需要),以进一步提高性能或修复重复错误。

虽然添加更多示例可能有益,但在添加更多数据之前,请务必评估模型在哪些方面出错。无论采用哪种方法,高质量、准确添加标签的数据对于提高性能和更好的数量都至关重要。此外,用于微调的数据应反映模型在生产环境中将遇到的数据类型。如需了解模型开发、部署和管理,请参阅主要的 Vertex AI 文档

使用 Gemini 提示

您可以在提示中为 Gemini 模型提供一些任务示例(分类、提取、情感分析),以便模型使用语言理解功能。模型会从这些示例中进行学习,并将这些知识应用于您的新输入。

可用于优化结果的常见提示方法包括:

  • 零样本提示:直接指示模型执行任务,而无需提供具体示例。
  • 小样本提示:提供一些示例以及说明来引导模型理解。
  • 思维链提示:将复杂的任务分解为较小的步骤,并引导模型按顺序完成每个步骤。

虽然提示设计很灵活,但某些策略可以引导模型的输出。全面的测试和评估对于优化性能至关重要。

大语言模型 (LLM) 使用大量文本数据进行训练,以学习语言模式和关系。在给定提示的情况下,这些模型会预测最可能的接续内容,类似于高级自动补全功能。因此,在撰写问题时,请考虑影响模型预测结果的因素。

提示工程流程如下图所示:提示工程流程

准备输入数据

您可以使用几种不同的提示方法。以下示例演示了如何使用少样本提示方法:

分类

请求

               Classify the following as red wine or white wine:
               
                  Name: Chardonnay
                  Type: White wine
                  Name: Cabernet
                  Type: Red wine
                  Name: Moscato
                  Type: White wine
               
               Name: Riesling
               Type: 
            

回答

         White wine
         

提取

请求

            Extract the technical specifications from the text below in a JSON format.
INPUT: Google Nest Wifi, network speed up to 1200Mpbs, 2.4GHz and 5GHz frequencies, WP3 protocol OUTPUT: { "product":"Google Nest Wifi", "speed":"1200Mpbs", "frequencies": ["2.4GHz", "5GHz"], "protocol":"WP3" } Google Pixel 7, 5G network, 8GB RAM, Tensor G2 processor, 128GB of storage, Lemongrass

回答

         {
         "product": "Google Pixel 7",
         "network": "5G",
         "ram": "8GB",
         "processor": "Tensor G2",
         "storage": "128GB",
         "color": "Lemongrass"
         }
         

情感分析

请求

            Classify the sentiment of the message. Please only print the category name without anything else.
            
               Message: I had to compare two versions of Hamlet for my Shakespeare class and unfortunately I picked this version. Everything from the acting (the actors deliver most of their lines directly to the camera) to the camera shots (all medium or close up shots...no scenery shots and very little back ground in the shots) were absolutely terrible. I watched this over my spring break and it is very safe to say that I feel that I was gypped out of 114 minutes of my vacation. Not recommended by any stretch of the imagination.
               Category: negative
               Message: This Charles outing is decent but this is a pretty low-key performance. Marlon Brando stands out. There's a subplot with Mira Sorvino and Donald Sutherland that forgets to develop and it hurts the film a little. I'm still trying to figure out why Charlie want to change his name.
               Category: negative
Message: My family has watched Arthur Bach stumble and stammer since the movie first came out. We have most lines memorized. I watched it two weeks ago and still get tickled at the simple humor and view-at-life that Dudley Moore portrays. Liza Minelli did a wonderful job as the side kick - though I'm not her biggest fan. This movie makes me just enjoy watching movies. My favorite scene is when Arthur is visiting his fiancée's house. His conversation with the butler and Susan's father is side-spitting. The line from the butler, "Would you care to wait in the Library" followed by Arthur's reply, "Yes I would, the bathroom is out of the question", is my NEWMAIL notification on my computer.

回答

         Positive
         

获取预测响应

以下是分类示例的 Python 代码响应示例。如需了解详情,请参阅 Vertex AI 上的生成式 AI 概览

from vertexai import generative_models
from vertexai.generative_models import GenerativeModel

model = GenerativeModel(model_name="gemini-1.5-flash-001")
response = model.generate_content(["Classify the following as red wine or white wine:

<examples>
Name: Chardonnay
Type: White wine
Name: Cabernet
Type: Red wine
Name: Moscato
Type: White wine
</examples>

Name: Riesling
Type: "])

使用 Gemini 1.0 Pro 进行微调

借助微调,您可以根据自己的特定需求调整 Gemini 1.0 Pro。请按照以下步骤使用您自己的数据微调 Gemini 1.0 Pro:

准备训练数据

将训练数据转换为 Gemini 的微调格式以使用 JSONL 文件结构。文件中的每一行应代表一个训练示例。每个训练示例都应采用以下结构:

{"messages": [{"role": "system", "content": "<system_context>"},, {"role": "user", "content": "<user_input>"}, {"role": "model", "content": "<desired_output>"}]}

以下是包含两个数据点的条目示例:

{"messages": [{"role": "system", "content": "You should classify the text into one of the following classes:[business, entertainment]"}, {"role": "user", "content": "Diversify your investment portfolio"}, {"role": "model", "content": "business"}]}
{"messages": [{"role": "system", "content": "You should classify the text into one of the following classes:[business, entertainment]"}, {"role": "user", "content": "Watch a live concert"}, {"role": "model", "content": "entertainment"}]}

如需了解全面说明和更多示例,请参阅官方 Gemini 数据集准备指南

执行微调流水线

如需启动 Gemini 微调作业,请使用界面、Python 或 REST API 按照此分步指南进行操作。在设置过程中,选择 Gemini 模型版本,配置微调超参数,并指定整体设置。我们建议尝试不同的周期、学习速率调节系数和适配器大小值。对于包含 500 到 1000 个示例的数据集,请考虑从以下配置开始,以充分了解模型学习并找到任务的最佳参数设置:

  • epochs=2, learning_rate_multiplier=1, adapter_size=1;
  • epochs=4, learning_rate_multiplier=1, adapter_size=1(默认值);
  • epochs=6, learning_rate_multiplier=1, adapter_size=4;
  • epochs=12, learning_rate_multiplier=4, adapter_size=4; 和
  • epochs=12, learning_rate_multiplier=4, adapter_size=1。

通过评估一种或两种不同配置的性能,您可以确定哪些参数和修改对提升性能最有效。如果未达到性能目标水平,您可以继续试验这些前景配置。

评估工具和技巧

一致且可比较的模型评估对于了解效果和做出明智决策至关重要。以下是模型评估期间需要注意的一些技巧和工具:

  1. 保持一致的评估方法:对经过微调的模型和提示型模型使用相同的评估指标和方法。这样有助于直接、无偏见比较。如有可能,请使用模型开发和部署期间所用的同一评估数据集。这可确保对不同模型类型进行公平比较,并有助于发现质量差异。
  2. 评估工具和技术 Vertex AI 生成式 AI 评估服务:针对小批量数据提供低延迟同步评估。适用于按需评估、快速迭代和实验。通过 Python SDK 与其他 Vertex AI 服务集成。
  3. 分类、提取和情感分析建议:使用评估服务时,请添加 exact_match 指标。

部署

如需部署调优后的模型,请参阅部署调优后的模型

后续步骤