LearnLM 入门指南:使用 Gemini API 构建个性化学习助手

本教程面向有编程基础的开发者,深入讲解如何利用 Google 的 LearnLM 实验模型构建个性化 AI 导师。通过系统指令(System Instructions)控制模型行为,实现测试准备、概念教学、结构化活动引导和作业辅导等场景。文章提供完整代码示例、API 配置方法、核心原理剖析、架构设计思路、常见问题排查及生产环境优化建议,帮助开发者快速掌握基于学习科学原则的 AI 教育应用开发。

技术背景

传统教育软件缺乏个性化,难以适应不同学习者的认知负荷和动机。LearnLM 是 Google 推出的实验性任务特定模型,经过训练以符合学习科学原则,旨在激发主动学习、管理认知负荷、适应学习者、激发好奇心并深化元认知。通过系统指令,开发者可以灵活定制 AI 导师的行为,无需微调模型。该技术适用于在线辅导、智能题库、作业辅助等场景,但受限于实验性质,需注意模型可能产生不准确内容,且对复杂推理支持有限。

核心原理

LearnLM 基于 Gemini 模型,通过系统指令(System Instruction)在推理时注入教学策略。系统指令作为对话的初始上下文,指导模型遵循特定教学法,如苏格拉底式提问、分步引导、即时反馈等。模型利用其语言理解和生成能力,结合对话历史,动态调整回应。核心数据流为:用户输入 → 系统指令 + 对话历史 → LearnLM 模型 → 生成教学回应。关键设计决策包括:使用 client.chats.create 创建会话以保持上下文,通过 GenerateContentConfig 传递系统指令,以及利用 chat.send_message 进行多轮交互。

架构分析

系统架构分为三层:客户端层(Colab 或应用)、API 层(Gemini API)和模型层(LearnLM)。客户端通过 google-genai SDK 与 API 交互,API 负责认证、请求路由和响应返回。模型层包含 LearnLM 模型,其内部结合了预训练知识和学习科学规则。组件关系:client 对象管理 API 调用,chats.create 创建会话并绑定系统指令,send_message 发送用户消息并接收模型响应。设计思想强调无状态 API 调用与会话状态管理分离,系统指令作为会话级配置,确保教学行为的一致性。可扩展点包括自定义系统指令模板、多会话管理、以及集成外部知识库。

完整实战教程

##### 版权所有 2026 Google LLC.

#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

使用系统指令构建 LearnLM AI 导师指南

本笔记本演示了如何利用 LearnLM(一个实验性的任务特定模型,经过训练以符合学习科学原则)来创建各种 AI 辅导体验。引导 LearnLM 能力的关键在于为教学和学习用例制定有效的系统指令。

LearnLM 旨在促进以下行为:

* 激发主动学习

* 管理认知负荷

* 适应学习者

* 激发好奇心

* 深化元认知

本指南通过说明系统指令和用户提示如何使 LearnLM 扮演不同类型的导师,来演示这些原则。

<!-- 社区贡献者徽章 -->

<table>

<tr>

<!-- 作者头像单元格 -->

<td bgcolor="#d7e6ff">

<a href="https://github.com/andycandy" target="_blank" title="在 GitHub 上查看 Anand Roy 的个人资料">

</a>

</td>

<!-- 文本内容单元格 -->

<td bgcolor="#d7e6ff">

<h2><font color='black'>本笔记本由 <a href="https://github.com/andycandy" target="_blank"><font color='#217bfe'><strong>Anand Roy</strong></font></a> 贡献。</font></h2>

<h5><font color='black'><a href="https://www.linkedin.com/in/anand-roy-61a2b529b"><font color="#078efb">LinkedIn</font></a> - 查看 <a href="https://github.com/andycandy" target="_blank"><font color="#078efb"><strong>Anand</strong></font></a> 的其他笔记本 <a href="https://github.com/search?q=repo%3Agoogle-gemini%2Fcookbook%20%22Anand%20Roy%22&type=code" target="_blank"><font color="#078efb">此处</font></a>。</h5></font><br>

<!-- 页脚 -->

<font color='black'><small><em>有很酷的 Gemini 示例?欢迎 <a href="https://github.com/google-gemini/cookbook/blob/main/CONTRIBUTING.md" target="_blank"><font color="#078efb">也分享出来</font></a>!</em></small></font>

</td>

</tr>

</table>

%pip install -U -q google-genai

设置你的 API 密钥

要运行以下单元格,你的 API 密钥必须存储在名为 GEMINI_API_KEY 的 Colab Secret 中。如果你还没有 API 密钥,或者不确定如何创建 Colab Secret,请参阅 [身份验证]

image](../quickstarts/Authentication.ipynb) 快速入门指南,获取详细演练。

from google.colab import userdata
from google import genai

GEMINI_API_KEY = userdata.get('GEMINI_API_KEY')
client = genai.Client(api_key=GEMINI_API_KEY)

为 LearnLM 编写系统指令

系统指令是您告诉 LearnLM 应成为何种导师以及如何表现的主要方式。LearnLM 经过专门训练,能够有效解读与学习和教学相关的指令。以下是一些利用 LearnLM 能力的系统指令示例,与您提供的示例相匹配。

LEARNLM_MODEL_ID = "learnlm-2.0-flash-experimental" # @param ["learnlm-2.0-flash-experimental", "learnlm-1.5-pro-experimental"] {"allow-input":true, isTemplate: true}

测试准备

此系统指令用于AI导师帮助学生准备测试。它侧重于适应性(调整问题难度)和主动学习(要求解释)。

test_prep_instruction = """
    You are a tutor helping a student prepare for a test. If not provided by
    the student, ask them what subject and at what level they want to be tested
    on. Then,

    *   Generate practice questions. Start simple, then make questions more
        difficult if the student answers correctly.
    *   Prompt the student to explain the reason for their answer choice.
        Do not debate the student.
    *   **After the student explains their choice**, affirm their correct
        answer or guide the student to correct their mistake.
    *   If a student requests to move on to another question, give the correct
        answer and move on.
    *   If the student requests to explore a concept more deeply, chat
        with them to help them construct an understanding.
    *   After 5 questions ask the student if they would like to continue with
        more questions or if they would like a summary of their session.
        If they ask for a summary, provide an assessment of how they have
        done and where they should focus studying.
"""

现在,让我们使用这个系统指令与LearnLM开始一次聊天会话,看看它是如何启动测试准备的。

from google.genai import types

chat = client.chats.create(
    model=LEARNLM_MODEL_ID,
    config=types.GenerateContentConfig(
        system_instruction=test_prep_instruction,
    )
)
from IPython.display import Markdown

prompt = """
  Help me study for a undergrad cognition test on theories of emotion
  generation.
"""

response = chat.send_message(message=prompt)
Markdown(response.text)

模型会回应一个关于情绪产生理论的练习题,并提示用户回答问题并提供答案。

现在,让我们模拟学生回答该问题并解释其推理过程。

response = chat.send_message("""
  It is James-Lange Theory, as that theory suggests that one feels a certain
  emotion because their body is reacting in that specific way.
""")
Markdown(response.text)

如您所见,通过在创建的聊天对象上使用 chat.send_message() 方法,模型会保持对话历史,并继续遵循创建聊天时提供的 system_instruction。

同样,在准备测试时,您可以继续来回交流。模型会根据 test_prep_instruction 生成新问题,随着您正确回答而增加难度,提示解释并给出反馈。

教授一个概念

此系统指令引导 LearnLM 成为一位友好、支持性的导师,专注于帮助学生逐步理解概念。它强调主动学习(通过提问)、适应性(根据学生的回答调整指导)、激发好奇心以及管理认知负荷(每轮只提一个问题)。

concept_teaching_instruction = """
    Be a friendly, supportive tutor. Guide the student to meet their goals,
    gently nudging them on task if they stray. Ask guiding questions to help
    your students take incremental steps toward understanding big concepts,
    and ask probing questions to help them dig deep into those ideas. Pose
    just one question per conversation turn so you don't overwhelm the student.
    Wrap up this conversation once the student has shown evidence of
    understanding.
"""

让我们使用这条指令与LearnLM开启一个新的聊天会话,来探索诸如“情感与认知相互关联的重要性”这样的概念。

prompt = "Explain the significance of Interconnectedness of Emotion & Cognition"

chat = client.chats.create(
    model=LEARNLM_MODEL_ID,
    config=types.GenerateContentConfig(
        system_instruction=concept_teaching_instruction,
    )
)

response = chat.send_message(message=prompt)
Markdown(response.text)

如你所见,LearnLM 的回应并非完整的解释,而是一个旨在引导学生逐步思考该概念的问题。

让我们模拟学生对该初始引导问题的回应。

response = chat.send_message("""
  Cognition plays a crucial role in shaping and regulating emotions.
  Our interpretation of a situation determines the emotion and its intensity.
""")
Markdown(response.text)

这种交互模式展示了LearnLM如何在指令的引导下,通过一系列有针对性的问题来促进理解,而不是直接提供信息。

引导学生完成学习活动

此指令引导 LearnLM 充当特定结构化活动(如“4A”精读协议)的促进者。它强调主动学习(参与任务)、管理认知负荷(分步协议)和深化元认知(反思)。

structured_activity_instruction = """
    Be an excellent tutor for my students to facilitate close reading and
    analysis of the Gettysburg Address as a primary source document. Begin
    the conversation by greeting the student and explaining the task.

    In this lesson, you will take the student through "The 4 A's." The 4 A's
    requires students to answer the following questions about the text:

    *   What is one part of the text that you **agree** with? Why?
    *   What is one part of the text that you want to **argue** against? Why?
    *   What is one part of the text that reveals the author's **assumptions**?
        Why?
    *   What is one part of the text that you **aspire** to? Why?

    Invite the student to choose which of the 4 A's they'd like to start with,
    then direct them to quote a short excerpt from the text. After, ask a
    follow up question to unpack their reasoning why they chose that quote
    for that A in the protocol. Once the student has shared their reasoning,
    invite them to choose another quote and another A from the protocol.
    Continue in this manner until the student completes the 4 A's, then
    invite them to reflect on the process.

    Be encouraging and supportive.
    Only display the full text if the student asks.
"""

让我们开始一个会话,学生想要开始这项活动。

prompt = "Hey, I'm ready to start the close reading activity."

chat = client.chats.create(
    model=LEARNLM_MODEL_ID,
    config=types.GenerateContentConfig(
        system_instruction=structured_activity_instruction,
    )
)

response = chat.send_message(message=prompt)
Markdown(response.text)

在解释之后,LearnLM邀请学生选择他们想从哪个“A”开始,并提供一个引言。

作业帮助

此指令使 LearnLM 能够针对作业问题提供有针对性的帮助,提供不同的帮助模式(答案、指导、反馈),并迅速接受正确答案。这突显了主动学习(指导/反馈选项)、深化元认知(反馈)以及管理认知负荷(结构化选项、逐步解答)。

homework_help_instruction = """
    You are an expert tutor assisting a student with their homework. If the
    student provides a homework problem, ask the student if they want:

    *   The answer: if the student chooses this, provide a structured,
        step-by-step explanation to solve the problem.
    *   Guidance: if the student chooses this, guide the student to solve
        their homework problem rather than solving it for them.
    *   Feedback: if the student chooses/ this, ask them to provide their
        current solution or attempt. Affirm their correct answer even if
        they didn't show work or give them feedback to correct their mistake.

    Always be on the lookout for correct answers (even if underspecified) and
    accept them at any time, even if you asked some intermediate question to
    guide them. If the student reaches a correct answer, affirm it and
    do not ask them to do any more work. Be supportive and patient.
"""

让我们通过提交一个问题并观察模型如何协助你来演示作业帮助流程。

prompt = """
  Can you help me with this homework problem?\n
  In a box of pears, 20% of pears are rotten. If there
  are 10 pears in a box, find the number of pears that could be rotten.
"""

chat = client.chats.create(
    model=LEARNLM_MODEL_ID,
    config=types.GenerateContentConfig(
        system_instruction=homework_help_instruction,
    )
)

response = chat.send_message(message=prompt)
Markdown(response.text)

如你所见,LearnLM 提供了一系列选项:答案、指导或反馈。

现在,让我们演示一下当你选择“指导”并随后提交正确答案时会发生什么。

response = chat.send_message(
    message="I'd like guidance, please."
)
Markdown(response.text)

LearnLM 认可了学生的选择,并提出了一个引导性问题,帮助学生开始解决问题。

现在,模拟学生思考并给出最终答案的过程。

response = chat.send_message(
    message="""
      Okay, I think I figured it out. 20% of 10 would be one-fifth of 10, that
      is 2. Is the answer 2?
    """
)
Markdown(response.text)

根据 homework_help_instruction,LearnLM 识别出“2”是正确答案并予以肯定,尽管学生处于“引导”模式,并未完成 LearnLM 引导的所有中间步骤。这体现了指令“始终留意正确答案……并随时接受它们。”

后续步骤

* 在 Google AI Studio 或 Colab 环境中进一步试验这些系统指令(如果可以使用 API)。尝试不同的提示和学生回复,看看 LearnLM 如何适应。

* 修改这些指令或编写新指令,以创建针对特定科目、活动或学生需求定制的辅导行为。

* 研究其他学习科学原理,并考虑如何将它们转化为 LearnLM 的系统指令。

有用的 API 参考:

* 在 AI Studio 中试验 LearnLM

* LearnLM 官方文档

* 系统指令指南

常见问题

常见错误:1) API 密钥未设置或无效,导致认证失败;2) 模型 ID 拼写错误或不可用;3) 系统指令格式错误,导致模型行为异常;4) 对话历史过长,超出上下文窗口。排查方法:检查环境变量 GEMINI_API_KEY 是否正确设置;确认模型 ID 在官方文档中可用;验证系统指令为字符串且无语法错误;监控 token 使用量。生产优化:使用缓存减少重复调用;实现指数退避重试机制;对敏感内容进行过滤;通过 A/B 测试优化系统指令;利用异步调用提升吞吐量。

企业应用扩展

开发者可进一步探索:1) 结合 RAG 技术,为 LearnLM 提供外部知识源,增强专业领域辅导能力;2) 设计多角色系统指令,实现协作学习场景;3) 集成语音交互,提升用户体验;4) 利用评估指标(如 BLEU、ROUGE)自动评估教学回应质量;5) 将 LearnLM 与学习管理系统(LMS)集成,实现个性化学习路径推荐。

总结

本教程面向有编程基础的开发者,深入讲解如何利用 Google 的 LearnLM 实验模型构建个性化 AI 导师。通过系统指令(System Instructions)控制模型行为,实现测试准备、概念教学、结构化活动引导和作业辅导等场景。文章提供完整代码示例、API 配置方法、核心原理剖析、架构设计思路、常见问题排查及生产环境优化建议,帮助开发者快速掌握基于学习科学原则的 AI 教育应用开发。

内容来源与验证

继续系统学习

站内还有更多完整实战教程

加入会员,解锁全部教程、完整代码与持续更新内容。

加入会员
查看全部提示工程教程