LangChain Tutorial Uses Semantic Retrieval for Few-Shot Prompts
A tutorial demonstrates how to build a dynamic few-shot prompting pipeline with LangChain. It loads a local bge-large-zh-v1.5 embedding model, uses Chroma and SemanticSimilarityExampleSelector to retrieve the three examples most semantically similar to each input, and combines them with the current review through FewShotPromptTemplate. The example constructs a sentiment-classification prompt but does not directly call a large language model.
The report provides a concrete implementation of retrieval-based few-shot prompting. It explains that selecting only examples similar to the current input can control prompt length and improve example relevance, especially when the example library is large.
基于语义相似度的动态少样本提示。它不是把所有示例都写死在提示词里,而是根据用户输入,从示例库中自动挑选最相关的几条,再拼成提示词交给大模型使用。
整体流程
先准备了一组评论情感分类示例,例如“这个手机拍照真好看”对应“正面”,“物流太慢了”对应“负面”。然后加载本地中文嵌入模型 bge-large-zh-v1.5,把文本转换成向量。接着用 Chroma 向量库和 SemanticSimilarityExampleSelector 构建一个语义相似度选择器,每次根据用户输入选出最相似的 3 条示例。最后通过 FewShotPromptTemplate 把这些示例和用户当前评论组合成完整提示词。
大模型在这里扮演什么角色
本身并没有直接调用大模型,它生成的是给大模型使用的提示词。
在这个例子里,大模型会收到类似这样的输入:
#LangChain#Chroma#SemanticSimilarityExampleSelector#FewShotPromptTemplate#bge-large-zh-v1.5#HuggingFaceEmbeddings