使用few-shot Prompt template让大模型更懂你
创建一个使用少量示例的提示模板(Prompt template)。少量示例的提示模板可以从一组示例(examples)或一个示例选择器( Exampleselector)对象构建
本事例实现了生成单词反义词的功能
直接记录代码:
from langchain_community.vectorstores import Chroma from langchain_core.example_selectors import SemanticSimilarityExampleSelector from langchain_core.messages import SystemMessage, HumanMessage from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, \ HumanMessagePromptTemplate, PromptTemplate, FewShotPromptTemplate from langchain_huggingface import HuggingFaceEmbeddings from Ai.llmqianfan import createLLM llm = createLLM() examples = [ {"question": "大", "answer": "小"}, {"question": "黑", "answer": "白"}, {"question":"高","answer":"矮"}, {"question":"胖","answer":"瘦"}, {"question":"高兴","answer":"伤心"}, {"question":"漂亮","answer":"丑陋"} ] template=""" 单词:{question} 反义词:{answer} """ prompt=PromptTemplate(input_variables=["question","answer"],template=template) embedding=HuggingFaceEmbeddings(model_name='../models/BAAI_bge-small-zh-v1.5') example_selector = SemanticSimilarityExampleSelector.from_examples( # 传入示例组 examples, # 使用阿里云的dashscope的嵌入来做相似性搜索 embedding, # 设置使用的向量数据库是什么 Chroma, # FAISS, # 结果条数 k=1, ) few_shot_prompt=FewShotPromptTemplate( #examples=examples, example_selector=example_selector,#使用样本选择器使用更接近的样本 example_prompt=prompt, prefix="请参考下面的事例,按事例格式,给出用户输入的单词的反义词:<example>", suffix="</example>\\n单词:{input}\\n反义词:", input_variables=["input"], example_separator="\\n" ) p=few_shot_prompt.format(input="black") # print(res) res=llm.invoke(p) print(res.content)
运行结果:
单词:black 反义词:white 进程已结束,退出代码为 0
代码中使用上节创建的千帆大模型接口