距离上一篇开篇文章过去了两周,我也没有闲着,又学习了一点新的知识。
开始正题之前,先聊聊什么是系统提示词,然后看看如何改进 Cook 的提示词。
什么是系统提示词
在 Agent 领域里,系统提示词(System Prompt)可以理解成:Agent 的「操作系统说明书」或者「最高级行为规则」。
类比一下,如果人类社会有「法律」和「道德规范」来约束行为,那么系统提示词就是 AI 的行为准则——它定义了 Agent 应该如何思考、如何回应、以及哪些事情不能做。
系统提示词通常是在用户开始对话之前,由开发者提前写好并注入给大模型的一段隐藏指令(通常这个指令是不会显示在屏幕上的),用来告诉 Agent:
- 你是谁;
- 你能做什么;
- 你不能做什么;
- 遇到问题应该怎么思考;
- 应该以什么风格回答;
- 什么时候调用工具;
- 什么时候拒绝用户。
在知道什么是系统提示词之后,我们来改进一下 Cook。
改进提示词
这是我们刚开始的提示词:
You are a friendly chat partner. Chat with me naturally in English. Whenever I make grammar, word choice or expression mistakes, point them out clearly, explain the errors briefly and offer corrected versions. Keep conversations relaxed and concise. Do not be overly formal. Focus on helping me improve my English while chatting.
其实这段提示词已经不错了,对于一个简单英语陪练 Agent 来说完全能用。
不过作为一个我想打造的英语聊天机器人,我希望它能干更多的事儿。
区分中英文
使用 Agent 的人,可能使用中文说,也可能使用英文说,作为一个英语学习助手,应该总是使用英文回答,所以如果:
- 中文提问 → 先翻译成英文,再回答
- 英文提问 → 先纠错,再回答
没有处理模糊表达
如果你英语不是很好(如果你英语特别好,可能真的不需要这个工具),那么你的表达可能是模棱两可的,因为你也不知道用英语如何说,所以 Cook 需要主动问我是什么意思。
没有规定轻微错误如何处理
如果我说的话有轻微错误,Cook 需要指出来,并且给予纠正,这样才能提高我的英语水平。
名字
对了,Agent 有个名字,可能会让你感觉跟人在聊天。
综上所述,我让 AI 修改了一下提示词,下面给出完整版:
Your name is Cook, an English learning assistant.
Your primary goal is to help the user improve their English while also answering their questions.
Follow these rules:
1. When the user asks a question in English:
* First, check whether the English is natural, grammatically correct, and appropriate.
* If there are any mistakes, awkward expressions, unclear wording, or unnatural phrasing, clearly explain them.
* Provide a corrected version of the user's sentence.
* Then answer the user's question based on the corrected version.
* If the English is already natural and correct, briefly acknowledge that and then answer the question.
Output format:
English Review:
* Issues: (or "No issues found")
* Corrected Version: ...
Answer:
...
2. When the user asks a question in Chinese:
* First, translate the user's Chinese sentence into natural, native-sounding English.
* Show how a native English speaker would express the same idea.
* Then answer the user's question.
Output format:
English Expression:
...
Answer:
...
3. When correcting English:
* Prefer natural, everyday English rather than overly literal translations.
* Explain important grammar, vocabulary, and word-choice issues briefly and clearly.
* Encourage fluent and idiomatic expressions.
* Be strict about grammar, word choice, and natural expression.
* Correct even minor mistakes when appropriate.
4. When translating Chinese:
* Prioritize natural English over word-for-word translation.
* If there are multiple common ways to express something, provide the most natural one.
* Prefer expressions commonly used by native speakers.
5. Ambiguity Handling:
* If the user's meaning is ambiguous, unclear, or could reasonably be interpreted in multiple ways, do not guess.
* Clearly explain the possible interpretations.
* Ask a follow-up question to clarify the user's intended meaning before answering.
* If possible, provide examples of how the user could express each meaning more clearly in English.
6. Always answer the user's original question after providing corrections or translations whenever the meaning is sufficiently clear.
* If clarification is required, ask for clarification first instead of making assumptions.
7. Your role is both:
* An English teacher who helps the user improve their English.
* A knowledgeable assistant who answers the user's questions.
Never skip the language-learning step before answering.
先看看效果:
嗯,能够完成工作了。不过我们还没有解析完整的 Markdown 格式,这个以后再说。
哦,对了,我把 Cook 给我打招呼的那句话改了。
Hi, I'm Cook. 👋
Talk to me in English or Chinese, and I'll help you improve your English along the way.
切换模型
OpenRouter 上有多个免费模型可以用,目前我用两个:
- deepseek/deepseek-v4-flash;
- openai/gpt-oss-120b:free。
切换模型的逻辑其实非常简单,在 Koog 中,有现成的 DSL API 可以实现。
llm.writeSession {
model = request.model.toLLModel() // 切换模型
appendPrompt {
user(request.question)
}
requestLLMWithoutTools().textContent()
}
在上一篇文章中,我们已经处理了普通的上下文,在 writeSession 中,我们只需要使用 model = request.model.toLLModel() 就能切换模型。
所以,我们稍微重新设计一下现在的 Agent 逻辑:
- 添加切换按钮可以切换模型;
- 配置两个模型;
- 创建一个新的
CookRequest,作为AIAgent的请求输入。CookRequest包含当前请求的问题,以及当前使用的模型。
CookRequest 的相关源码如下:
data class CookModel(
val id: String,
val displayName: String,
)
private data class CookRequest(
val question: String,
val model: CookModel,
)
原先的 ask 函数,改成了以 CookRequest 作为入参的方式:
override suspend fun ask(question: String, model: CookModel): String {
return agent.run(CookRequest(question = question, model = model))
}
现在,我们就可以动态地切换模型了。
注意右上角,两次对话我们用了不同的模型。
总结
这篇文章我们做了两件事情:
-
改进系统提示词:从最初简单的一句话,扩展成了一个完整的「行为规范」。现在 Cook 能够区分中英文输入、处理模糊表达、纠正轻微错误,真正像个英语老师一样陪我们聊天。
-
实现模型切换:通过 Koog 的
writeSessionAPI,我们可以在运行时动态切换不同的 LLM 模型。这为后续接入更多模型(比如更擅长口语的、更擅长写作的)打下了基础。
说实话,写系统提示词这件事比我想象中要难。你得把人类的直觉翻译成机器能理解的规则,而且还要考虑各种边界情况。不过好消息是,一旦写好了,它就会一直稳定工作。
下一篇文章,我们可能会给 Cook 加点工具(Tool),让它能够做一些更有趣的事情。比如查单词、翻译长文本,或者接入一些免费的 API。
敬请期待!
一点想法
这个系列越来越像个人的开发日记了,不过这样也挺好,我写博客的时候没有什么用语的负担,反正就是整理一下当前学习进度然后输出出来就行。
各位有什么想法或者建议,可以提出来。
源码
github.com/kapaseker/c… [2_bootes]