花式调教LLM,让你的LLM模型输出像“齐天大圣”一样不断进化!

递归提示法:让你的LLM模型像"齐天大圣"一样不断进化!

最近《黑神话:悟空》凭借UE5引擎的神优化火遍全网,而今天我们要聊的这个技术,堪称是让AI"模型"也能像游戏画面一样"满帧运行"的终极技巧------递归提示法(Iterative Prompting)!没错,就是那个能让LLM这样的超级大脑更听话、更好用的黑科技!

你是不是经常有这样的困扰:

  • 花式调教AI却总是差强人意?
  • 提示词写得再好,生成结果还是离谱?
  • 想要AI输出符合特定要求的内容却无从下手?

别担心!今天我们就来手把手教你如何用"迭代提示法"让LLM(大语言模型)像"神仙打架"一样不断进化,最终输出你想要的完美结果!


环境搭建:给你的AI装个"终极显卡"

首先,我们需要准备好修炼的"法宝",也就是安装必要的库和工具。这一步就像是在电脑上装一个高性能显卡,为后续的操作打下坚实的基础。

复制代码
pip install openai
pip install langchain
pip install sentence_transformers
javascript 复制代码
import os
import openai
from langchain.llms import OpenAI
from sentence_transformers import SentenceTransformer, util
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

os.environ["OPENAI_API_KEY"] = 'your_openai_api_key_here'
llm = OpenAI(temperature=0.9)

引入法术:初始化你的"神兵利器"

接下来,我们需要召唤出我们的"大语言模型",并赋予它初生的力量。这一步就像是在游戏里召唤出强力角色,为后续的战斗做好准备。


神兵利器:打造你的专属"AI调教秘籍"

为了更好地掌控我们的"AI大圣",我们需要炼制一些"辅助法术"。这些函数就像是游戏中的技能加成,能让我们更精准地控制模型的表现。


神奇的循环:让AI像"六耳猕猴"一样不断进化!

有了前面的准备,我们现在终于可以开始修炼"迭代提示法"的终极奥义------递归提示循环。这个过程就像是在和一个"会思考的猴子"对话,每一次对话都会让结果更上一层楼。

python 复制代码
while not meeting_criteria:
    response = model.generate(...)
    feedback = refine_response(prompt, response)
    prompt += feedback

神仙打架:传统方法 vs 迭代提示法

最后,我们来比一场"神仙打架"------看看传统的单次提问和我们的迭代提示法谁更强!

特性 传统方法 迭代提示法
输出质量 一锤子买卖 不断优化
反馈机制 实时反馈
控制能力 局限 更强

是不是感觉这个技术就像是给AI装上了"智能加速器"?别急,后面我们还会手把手教你如何炼出属于自己的"AI神兵",让你的模型输出更上一层楼!记得关注我们的频道,获取更多黑科技教程~

定义辅助函数:代码里的"神器"了解一下?

在我们的代码中,藏着两个超级实用的"神器",它们就像是《黑神话:悟空》里角色们的隐藏技能一样,帮助我们评估那些被重写的评论质量。一起来看看这些"神器"到底有多强吧!

神器一:语义相似度(Semantic Similarity)

第一个神器来自SentenceTransformer这个库,它就像是游戏中的"神兵利器",专门用来计算原文和改写后的文本之间的语义相似度。简单来说,就是确保改写后的评论不仅看起来不一样,还能百分百保留原意。

ini 复制代码
def compute_semantic_similarity(original, rephrased):
    model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
    embedding1 = model.encode(original, convert_to_tensor=True)
    embedding2 = model.encode(rephrased, convert_to_tensor=True)
    similarity = util.pytorch_cos_sim(embedding1, embedding2)
    return similarity.item()

比如,在《黑神话:悟空》里,主角悟空总能在战斗中找到最佳路径,这个神器就像是游戏里的"自动寻路"功能,让我们不用担心改写后的评论会跑题哦!

神器二:礼貌度检测(Politeness)

第二个神器则是一个预训练的BERT模型,它就像是游戏中的"情绪分析大师",专门负责判断评论的语气有多友好。这个模型会给出一个从0到4的评分,0代表"非常不客气",4则是"超有礼貌"。我们用这个评分来衡量改写后的评论是不是足够友好。

ini 复制代码
def is_polite_transformer(text):
    model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
    model = AutoModelForSequenceClassification.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    with torch.no_grad():
        outputs = model(**inputs)
    _, predicted = torch.max(outputs.logits, 1)
    label = int(predicted.item())
    return label

为了测试这个神器的效果,我们做了一系列有趣的实验,就像是在《黑神话:悟空》里挑战各种BOSS一样,来看看它能不能准确识别不同语气的句子:

ini 复制代码
rephrased_comment = "Your painting is excellent"
polite_label = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite_label}")
ini 复制代码
rephrased_comment = "Your painting is good"
polite = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite}")
ini 复制代码
rephrased_comment = "Your painting is not the best"
polite = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite}")
ini 复制代码
rephrased_comment = "I'm not a fan of your painting."
polite = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite}")
ini 复制代码
rephrased_comment = "Your painting absolutely sucks"
polite = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite}")
  • "你的画作太棒了" → 4分(满分好评,简直像击破BOSS护盾一样完美)
  • "你的画作不错" → 3分(依然很友好,但攻击力度稍微减弱了一点)
  • "你的画作一般般" → 2分(中规中矩,像是普通攻击)
  • "我不太喜欢你的画作" → 1分(有点负面,像是触发了小技能)
  • "你的画作简直烂透了" → 0分(非常不客气,直接开启大招)

这些评分就像是游戏中的战斗反馈,让我们更直观地看到改写后的评论有多"友善"。

迭代提示循环:让AI不断进化

接下来就是我们的终极技能------迭代提示循环。我们会在代码里循环处理各种"不礼貌"的原始评论,就像在《黑神话:悟空》里反复挑战同一个BOSS一样,直到找到最完美的改写方案。

ini 复制代码
rude_comments = [
    "You're making it hard for me to take you seriously.",
    "This is the most ridiculous thing I've ever heard.",
    "I've never seen someone mess up this badly.",
    "You're not just wrong, you're stupidly wrong.",
    "Is this really the best you could come up with?",
    "I don't have the time or the crayons to explain this to you.",
    "You're not just a disappointment, you're a waste of time.",
    "I've met some dumb people, but you take the cake.",
    "You're so clueless it's almost cute."]
css 复制代码
iterative_similarity_scores = []
iterative_politeness_scores = []
standard_similarity_scores = []
standard_politeness_scores = []

for comment in rude_comments:
    
    user_input = comment
    for i in range(max_iterations):  
        prompt_rephrase = f"Can you reword this comment to be more polite and kinder?: {user_input}"
        user_input_rephrased = llm(prompt_rephrase)

                similarity_prompt_check = f"Is the reworded comment semantically similar to the original comment? Original comment: {user_input}, Reworded comment: {user_input_rephrased}"
        politeness_prompt_check = f"Is the reworded comment more polite and kinder than the original comment? Original comment: {user_input}, Reworded comment: {user_input_rephrased}"
        similarity_verification_result = llm(similarity_prompt_check)
        politeness_verification_result = llm(politeness_prompt_check)

          if "YES" in similarity_verification_result.strip().upper() and "YES" in politeness_verification_result.strip().upper():  
            break
        else:
          user_input = user_input_rephrased
    print(f"Original comment: {comment}")
    print(f"Iterative rephrased comment: {user_input_rephrased}")

    semantic_similarity=compute_semantic_similarity(comment, user_input_rephrased)
    iterative_similarity_scores.append(semantic_similarity)
    print(f'semantic similarity: {semantic_similarity}')
    politeness=is_polite_transformer(user_input_rephrased)
    print(f'politeness: {politeness}')
    iterative_politeness_scores.append(politeness)

        standard_rephrased = llm(f"Can you reword this comment to be more polite and kinder?: {comment}")
    print(f"Standard rephrased comment: {standard_rephrased}")

        semantic_similarity=compute_semantic_similarity(comment, standard_rephrased)
    standard_similarity_scores.append(semantic_similarity)
    print(f'semantic similarity: {semantic_similarity}')
    politeness=is_polite_transformer(standard_rephrased)
    print(f'politeness: {politeness}')
    standard_politeness_scores.append(politeness)

比如,在这个过程中:

  • AI第一次尝试改写:"你的画作还可以"
  • 但我们觉得还不够友好
  • 所以让它重新思考...
  • 第二次尝试:"我觉得你的画作还有提升空间"
  • 这次评分直接从1分飙升到3分,效果立竿见影!

就像是游戏里通过不断升级装备,让角色变得更强大。我们的迭代提示循环也让AI的改写能力不断提升。

怎么样?是不是感觉这些"神器"和技能设定一样酷炫?让我们一起用代码打造属于自己的"黑神话"吧!Comparative Analysis: 迭代提示 vs. 标准查询

scss 复制代码
avg_iterative_similarity = sum(iterative_similarity_scores) / len(iterative_similarity_scores)
avg_iterative_politeness = sum(iterative_politeness_scores) / len(iterative_politeness_scores)
avg_standard_similarity = sum(standard_similarity_scores) / len(standard_similarity_scores)
avg_standard_politeness = sum(standard_politeness_scores) / len(standard_politeness_scores)
print(f"Avg Iterative Similarity: {avg_iterative_similarity}, Avg Iterative Politeness: {avg_iterative_politeness}")
print(f"Avg Standard Similarity: {avg_standard_similarity}, Avg Standard Politeness: {avg_standard_p

经过实验运行后,我们分别计算了迭代提示和标准查询在语义相似度与礼貌程度上的平均值:

迭代提示平均相似度:0.279,平均礼貌度:2.111

标准查询平均相似度:0.372,平均礼貌度:1.667

从实验结果来看,迭代提示在生成评论时展现出了一丝"细节怪"的风采------其平均礼貌度达到了2.11分,相比标准查询的1.67分,可谓是全方位提升了社交礼仪Buff。这就好比《黑神话:悟空》中UE5引擎优化后的画面表现,在细节处理上确实更上一层楼。

不过,就像游戏中的某些技能虽然酷炫但可能会影响整体输出一样,迭代提示在语义相似度上的得分略逊一筹。这种权衡取舍是我们在实际应用中需要深思熟虑的关键点。

好了,以上就是本次分享的全部内容啦!如果各位看官对此还有其他想法,欢迎在评论区留下宝贵的弹幕/评论~ 如果你想亲自上手体验这个"六边形战士"般的迭代提示技术,可以点击这里查看我们的实验笔记本哦!

相关推荐
大写-凌祁3 小时前
零基础入门深度学习:从理论到实战,GitHub+开源资源全指南(2025最新版)
人工智能·深度学习·开源·github
焦耳加热3 小时前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
深空数字孪生3 小时前
储能调峰新实践:智慧能源平台如何保障风电消纳与电网稳定?
大数据·人工智能·物联网
wan5555cn4 小时前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
格林威4 小时前
机器视觉检测的光源基础知识及光源选型
人工智能·深度学习·数码相机·yolo·计算机视觉·视觉检测
今天也要学习吖5 小时前
谷歌nano banana官方Prompt模板发布,解锁六大图像生成风格
人工智能·学习·ai·prompt·nano banana·谷歌ai
Hello123网站5 小时前
glean-企业级AI搜索和知识发现平台
人工智能·产品运营·ai工具
AKAMAI5 小时前
Queue-it 为数十亿用户增强在线体验
人工智能·云原生·云计算
索迪迈科技5 小时前
INDEMIND亮相2025科技创变者大会,以机器人空间智能技术解锁具身智能新边界
人工智能·机器人·扫地机器人·空间智能·陪伴机器人
栒U5 小时前
一文从零部署vLLM+qwen0.5b(mac本地版,不可以实操GPU单元)
人工智能·macos·vllm