1.什么是Few-Shot
Few-Shot也就是在提词中写的几个示例或者样本,让AI理解你想要得格式和输出风格,然后应用到新的输出上。
2.为什么需要Few-Shot
因为不给示例,直接让AI做,输出不稳定,AI可能理解偏差。
给出2-5个示例,输出格式会稳定,输出准确率高,同时也会消耗更多token。
核心公式:Few-shot Prompt = 任务描述 + 示例1 + 示例2 + … + 实际输入
常见问题与解决
*****输出格式不一致,原因temperature太高,设temperature=0.1。
*****分类错误率高,原因示例不够典型,更换更典型得示例。
*****返回得不是Json,原因没有强制要求,可添加response_format参数,设置此参数后,输入必须带有json字样,否则调API报错。
*****响应太慢,原因样本太多,控制示例数量在3-5个。
核心代码示例:
//2.少样本分类(给示例)varexamples=GetFewShotExamples(3);varuserPrompt=newStringBuilder();userPrompt.AppendLine("将用户输入分类为:技术问题、产品反馈、闲聊。");userPrompt.AppendLine("只输出分类名称,不要有其他内容。");userPrompt.AppendLine();userPrompt.AppendLine("示例:");userPrompt.AppendLine(examples);userPrompt.AppendLine();userPrompt.AppendLine($"用户输入:说一下今天得天气 输出json格式");userPrompt.AppendLine("分类:");varmessages=new[]{new{role="user",content=userPrompt.ToString()}};//1.零样本分类(不给示例)//{// var messages = new[]// {// new { role = "system", content = "将用户输入分类为:技术问题、产品反馈、闲聊。只输出分类名称,不要有其他内容。" },// new { role = "user", content = "今天下小雨" } //用户输入// };//}varrequestBody=new{model="qwen-turbo",messages=messageIn,temperature=0.1,// 低温度让输出更稳定response_format=new{type="json_object"}//};Few-Shot学习完成!