上下文至关重要:Gemini.md(管理上下文与记忆)
摘要
本课程深入讲解 Gemini CLI 的上下文(Context)管理机制,强调上下文在 AI 代理协作中的核心作用。通过 gemini.md 文件系统,用户可以为 AI 代理提供个性化偏好、项目技术栈、工作流程等关键信息。课程介绍了全局与项目级上下文的层级加载机制,以及 /memory add、/memory show 和 /init 等实用命令,帮助用户高效配置和管理 AI 代理的知识库,从而提升协作效率。
要点
- 上下文即信息:上下文决定了 AI 代理的知识范围和行为模式,类似于培训个人助理了解偏好与习惯
- gemini.md 文件系统:支持全局(用户级)和项目级上下文文件,层级查找机制从当前目录向上遍历至
.git文件夹 - 记忆管理命令:
/memory add添加全局记忆(如姓名、偏好),/memory show显示当前记忆内容 - 项目初始化:
/init命令自动分析项目结构、依赖和关键文件,快速生成上下文配置 - 最佳实践:为每个项目创建独立的
gemini.md,提供技术栈概述、项目结构和常用命令,避免重复探索
视频信息:Context is Key: Gemini.md (Managing Context and Memory)
中文翻译
在与 AI 代理协作时,上下文(Context)就是一切。它决定了你的代理知道什么以及它如何表现。这有点像拥有你自己的私人助理。你会教给他们你的偏好和习惯。例如,你喜欢什么样的咖啡。让我们来看一下。
我喜欢把上下文想象成你雇佣 Gemini CLI 来做你的私人助理。你需要给它提供上下文,告诉它你喜欢什么,以及你如何处理某些事情。比方说,我喝咖啡喜欢加 12 块糖。如果一个助理是刚入职的,他们就不会知道这个信息。这也是我们在使用 Gemini CLI 时的思考方式。我们需要为它注入所需的上下文,以便它能回答查询并高效地做出响应。
上下文是 AI 代理用来理解用户请求的信息集合。上下文可以有多种不同的形式,比如通用指令。它也可以是特定于某个项目的指令,例如"这个项目使用的是 Python",或者"这个项目使用 UV 而不是 pip"。你还可以给 Gemini CLI 设定一个角色(Persona),比如"你是一名 QA 测试员。请确保在每次更改后都进行端到端的测试。"
那么,我们要把所有这些上下文放在哪里呢?其实,Gemini CLI 会从名为 gemini.md 的文件中读取上下文。你可以拥有一个全局上下文文件。这是在你的用户层级的。因此,这里面包含的是适用于你所有项目的指令,是你希望 Gemini CLI 在不同项目之间都能记住的内容。你也可以在特定的项目中拥有 gemini.md 文件。
Gemini 加载这些文件的方式是:在当前文件夹中,它会查找上级(ancestor)上下文文件。所以,如果你在当前文件夹中有一个 gemini.md,但在上一级文件夹中也有一个,它会一直向上遍历,直到找到一个 .git 文件夹,并认为这些都在作用范围内。你还可以在子目录中拥有 gemini.md 文件。如果你有某些特定的文件夹,如测试(testing)或构建(builds),这非常有用。你可以为这些文件夹中的每一个设置不同的定制化 gemini.md 文件,告诉 Gemini CLI 如何在该文件夹内进行交互。
我们的一位同事一直在为我们的 TechStack 会议制作网站。他们刚刚把所有代码移交给了我们。我们想从头开始,了解这个项目及其结构。Gemini CLI 中有几个命令可以帮助你管理记忆和上下文。有一个 show 命令,你可以用它来显示当前的记忆内容。我们的记忆目前是空的。
/memory add 命令让我们能够向全局 gemini.md 上下文中添加记忆。这会在我们所有的项目中保存。这非常适合那些你希望 Gemini CLI 在所有会话中都保存的通用事项,比如你的名字、你希望它如何与你交谈,以及其他关键信息,比如"我只在周二喜欢紫色"。这会继续提示你确认是否要将其添加到你的记忆中。再次说明,我们可以看到它位于全局上下文文件中,由波浪线标记。让我们允许这个操作。它会自动刷新记忆,以便现在将其拉入上下文中。
现在,如果我们去运行 /memory show,我们应该能看到我们的新记忆已被保存。所以如果我们问它,“我的名字是什么”,它应该能够利用它拥有的记忆来回答这个提示。Bingo,它答对了。这就很完美。
让我们更进一步。养成每个项目创建一个新的 gemini.md 上下文文件的好习惯是个好主意。向代理提供关于你正在使用的技术的一般知识、项目结构的概述,以及某些文件通常放置的位置,真的可以帮助它更高效地工作。用上下文引导代理将有助于引导它走上正确的道路,而不需要它在每次你开始新会话时都去探索你的代码或文件。
幸运的是,有一个内置命令可以帮助你快速为任何现有项目创建一个良好的起始上下文文件:/init。它会遍历并读取不同的关键文件。你可以看到对于我们的项目,它正在读取 package.json。它将开始查看依赖项,它还会查看一些关键文件,如我们的 index 以及我们不同的数据文件。
你会看到 Gemini CLI 现在想要创建 gemini.md 文件,详细说明项目结构和我们正在使用的技术。我们的网站使用的是 React 和 Vite。它将遍历技术栈、文件夹组织,并将所有这些存储在上下文文件中。再一次,你可以使用 Ctrl-S 展开输出,查看它正在存储的确切细节。
让我们继续创建我们的 gemini.md 文件。我将详细查看这个文件,因为我还在补课,了解网站是如何构建的。你会看到诸如入门指南、项目先决条件等关键细节。它还涵盖了关键命令,比如如何 npm install,如何运行开发服务器,甚至如何为生产环境构建项目。
现在,如果我们开始一个新的会话,如果我们运行 /memory show,我们实际上可以看到我们的 gemini.md 文件的内容正在被加载到记忆中。
至此,我们已经涵盖了上下文,以及如何使用 gemini.md 从现有项目中加载上下文。在下一节课中,我们将重点介绍如何通过模型上下文协议(MCP)加载外部上下文。
English Script
Context is everything when working with AI agents. It determines what your agent knows and how it behaves. It’s kind of like having your own assistant. You’ll teach them your preferences and habits. For example, how you like your coffee. Let’s take a look.
I like to think of context as if you’re hiring Gemini CLI to be your personal assistant. You need to give context on what you like, how you do certain things. For instance, I like to take 12 sugars in my coffee. If an assistant was new on the job, they wouldn’t know this information. This is also how we can think about it with Gemini CLI. We need to prime it with the context it needs in order to answer the queries and respond efficiently.
Context is a collection of information an AI agent uses to understand the user’s request. Context can come in various different forms, general instructions. It could be instructions specific to a given project, such as this is using Python, or this is using UV over pip. You can also give Gemini CLI persona, such as you are a QA tester. Make sure to test the changes end to end after every change.
So where do we put all of this context? Well, Gemini CLI reads context from files called gemini.md. You can have a global context file. So this is at your user level. So this would be instructions that apply to all of your projects that you want Gemini CLI to remember project to project. You can also have gemini.md files in a given project.
How Gemini loads these is in the current folder, it looks for ancestor context files. So if you have one gemini.md in your current folder, but also one in the folder above, it will keep on traversing that until it finds a .git folder and says these are all in scope. You can also have gemini.md files in subdirectories. This is great for if you have certain folders such as testing or builds. You could have different customized gemini.md files for each one of these folders that tell Gemini CLI how to interact within the folder.
One of our coworkers has been working on the website for our TechStack Conference. They just handed off all the code to us. We want to start fresh and learn about the project and how it’s structured. There are several commands within Gemini CLI to help you manage your memory and context. There is show where you can show the current memory contents. Our memory is currently empty.
The /memory add command lets us add memories to our global gemini.md context. This is saved across all of our projects. This is great for generic things that you want Gemini CLI to save across all your sessions, such as your name, how you want it to talk to you, and other critical information such as I only like purple on Tuesdays. This is going to go ahead and prompt you to confirm that you want to add this to your memory. And again, we can see that it is in the global context file, marked by the squiggly line. Let’s go ahead and allow this. It’s going to go ahead and automatically refresh the memory so that now it’s being pulled into the context.
Now if we go and run /memory show, we should see our new memory being saved. So if we ask it, “what is my name”, it should be able to use the memory it has in order to answer the prompt. Bingo, it got it right. So that’s perfect.
Let’s take it a step further. It’s a good idea to get in the habit of creating a new gemini.md context file per project. Giving the agent general knowledge on the technologies you are using, an overview of the structure of your project, and where certain files tend to go can really help it work more efficiently. Guiding the agent with context will help steer it down the right path without it needing to explore your code or files every single time you start a new session.
Luckily, there’s a built-in command to help you quickly create a good starting context file for any existing project: /init. It will go through, read in different key files. You can see for our project, it’s reading our package.json. It’s going to start to look at the dependencies, it’s also going to go through some of the key files such as our index as well as our different data.
You’ll see that Gemini CLI is now wanting to create the gemini.md file, detailing the project structure and technologies we are using. Our website is using React and Vite. It’s going to go through the tech stack, folder organization, and store all of these in the context file. And once again, you can do ctrl-s to expand the output and see the exact details of what it’s storing.
Let’s go ahead and create our gemini.md file. I’m going to look through this in detail as I’m still getting caught up on how the website is built. You’ll see key details like getting started, prerequisites for the project. It also covers key commands like how to npm install, how to run the development server, or even build the project for production.
Now if we start a new session, if we run /memory show, we can actually see that our contents of our gemini.md file are being loaded into memory.
So now we’ve covered context and how to load context from our existing project with gemini.md. In the next lesson, we’ll focus on how we can load external context through model context protocol or MCP.