LangChain FewShotPromptTemplate 少样本提示模板

发布时间:2026/7/21 16:44:00
LangChain FewShotPromptTemplate 少样本提示模板 # Concept: # Few-shot templates teach the AI by showing examples. The AI learns the pattern from examples and applies it to new inputs. # Pattern Learning Process: # Example 1: happy → sad ✓ # Example 2: tall → short ✓ # New Input: hot → ??? # AI Prediction: hot → cold from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate # Define examples that teach the pattern examples [ {input: happy, output: sad}, {input: tall, output: short}, {input: fast, output: slow}, {input: hot, output: cold} ] # Template for each example example_template PromptTemplate( input_variables[input, output], templateInput: {input}\nOutput: {output} ) # Few-shot template few_shot_template FewShotPromptTemplate( examplesexamples, example_promptexample_template, prefixFind the opposite of each word:, suffixInput: {word}\nOutput:, input_variables[word] ) # Test the pattern learning test_words [big, light, expensive, difficult] print( Few-Shot Learning in Action:) print( * 40) for word in test_words: prompt few_shot_template.format(wordword) print(f\n Generated Prompt for {word}:) print(prompt) print(- * 30) # Advanced: Dynamic example selection print(\n Advanced: Selective Examples) selected_examples examples[:2] # Use only first 2 examples dynamic_template FewShotPromptTemplate( examplesselected_examples, example_promptexample_template, prefixLearn the pattern from these examples:, suffixInput: {word}\nOutput:, input_variables[word] ) prompt dynamic_template.format(wordbright) print(prompt) # with open(/root/few-shot-templates.txt, w) as f: # f.write(FEW_SHOT_TEMPLATES_COMPLETE)完整代码逐行详解LangChain FewShotPromptTemplate 少样本提示模板一、核心概念前置注释翻译python运行# Concept: # Few-shot templates teach the AI by showing examples. The AI learns the pattern from examples and applies it to new inputs. # 核心概念少样本提示模板通过提供样例教会AI执行任务。模型从样例中总结规律再套用在全新输入上。 # Pattern Learning Process: # Example 1: happy → sad ✓ # Example 2: tall → short ✓ # New Input: hot → ??? # AI Prediction: hot → cold # 规律学习流程 # 样例1开心 → 悲伤 # 样例2高 → 矮 # 新输入热 → # AI推理输出热 → 冷专业名词Few-shot少样本给模型少量示例不用微调模型权重仅靠 Prompt 引导输出格式 / 逻辑Zero-shot不给任何样例One-shot1 个样例Few-shot多个样例二、导入模块python运行from langchain_core.prompts import PromptTemplate, FewShotPromptTemplatePromptTemplate基础单条文本模板用于定义单组输入输出样例的格式FewShotPromptTemplate少样本专用模板负责把多条示例、任务说明、用户新问题拼接成完整 Prompt注意langchain_core是 LangChain 新版拆分包区别于旧版langchain.prompts三、定义样例数据集 examplespython运行examples [ {input: happy, output: sad}, {input: tall, output: short}, {input: fast, output: slow}, {input: hot, output: cold} ]存储多条输入 - 标准答案配对每条是字典任务目标输入形容词输出它的反义词这些示例会全部塞进 Prompt给模型做参考标准四、单条示例格式化模板 example_templatepython运行example_template PromptTemplate( input_variables[input, output], templateInput: {input}\nOutput: {output} )input_variables[input, output]声明模板内两个占位变量template规定每一条样例的固定排版 渲染后单条样例plaintextInput: happy Output: sad作用统一所有示例的展示格式避免排版混乱、模型识别出错五、组装完整少样本模板 few_shot_template核心python运行few_shot_template FewShotPromptTemplate( examplesexamples, # 传入全部样例数据 example_promptexample_template, # 指定每条样例用什么格式渲染 prefixFind the opposite of each word:, # 样例前面的任务总说明前缀 suffixInput: {word}\nOutput:, # 样例之后放用户新问题后缀 input_variables[word] # 最终留给用户传入的变量名 )五大参数拆解examples样例列表example_prompt单条样例的渲染模板prefix全局任务指令写在所有示例最前面告诉模型整体要做什么suffix示例结束后拼接用户待预测的新输入{word}是待填充变量input_variables模板末尾后缀需要接收的外部变量用户新词拼接完成后的完整 Prompt 结构plaintextFind the opposite of each word: Input: happy Output: sad Input: tall Output: short Input: fast Output: slow Input: hot Output: cold Input: big Output:模型读到这段文本就会明白任务是找反义词并按Output: xxx格式给出答案。六、批量测试基础版模板python运行# Test the pattern learning test_words [big, light, expensive, difficult] print( Few-Shot Learning in Action:) print( * 40) for word in test_words: prompt few_shot_template.format(wordword) print(f\n Generated Prompt for {word}:) print(prompt) print(- * 30)test_words批量待预测单词.format(wordword)把循环里的单词填充到模板{word}占位符生成完整可发给 LLM 的字符串 Prompt循环打印每个单词对应的完整提示词直观查看拼接效果七、进阶动态选取部分样例 Dynamic Example Selectionpython运行# Advanced: Selective examples print(\n Advanced: Selective Examples) selected_examples examples[:2] # 只截取前2条样例happy/sad、tall/short dynamic_template FewShotPromptTemplate( examplesselected_examples, example_promptexample_template, prefixLearn the pattern from these examples:, suffixInput: {word}\nOutput:, input_variables[word] ) prompt dynamic_template.format(wordbright) print(prompt)核心知识点examples[:2]切片只取前 2 个示例减少 Prompt 长度、降低 token 消耗实际工程中可搭配SemanticSimilarityExampleSelector根据用户问题语义自动挑选最相关样例不用固定截取适合 RAG、问答场景输出效果仅 2 条样例plaintextLearn the pattern from these examples: Input: happy Output: sad Input: tall Output: short Input: bright Output:三、完整代码核心用途总结工程价值统一管理提示词样例不用手动拼接超长字符串便于维护、修改样例适用场景分类、翻译、抽取、格式生成、问答等需要固定输出格式的任务优势对比 Zero-shot少量样例能大幅约束 LLM 输出格式减少乱回答、格式错乱扩展方向语义动态选样例 ExampleSelector搭配 ChatPromptTemplate 实现对话式少样本和 RAG 结合把检索文档作为 Few-shot 示例注入 Prompt四、补充运行依赖执行代码前必须安装库bash运行pip install langchain-core