基础岛第三关—书生大模型—浦语提示词工程实践

基础任务 (完成此任务即完成闯关)

  • 背景问题:近期相关研究发现,LLM在对比浮点数字时表现不佳,经验证,internlm2-chat-1.8b (internlm2-chat-7b)也存在这一问题,例如认为13.8<13.11

无prompt实验时:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
def load_model(model_path):
    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16, trust_remote_code=True).cuda()
    model = model.eval()
    return tokenizer, model
messages = []
# 定义模板变量
template = """
## Init
{}
"""
 
# 定义包含详细提示词模板的函数
def apply_template(template,input_text):
    return template.format(input_text)
 
 
 
def chat(input_text,template):
    length = 0
    # 将模板应用到输入文本
    input_text_with_template = apply_template(template,input_text)
    for response, _ in model.stream_chat(tokenizer, input_text_with_template, messages):
        if response is not None:
            print(response[length:], flush=True, end="")
            length = len(response)
# tokenizer, model = load_model("/root/InternLM/XTuner/merged")
tokenizer, model = load_model("/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b")
 
chat("请帮我比较一下这两个数的大小:13.8和13.11",template)
 
del tokenizer, model
 
torch.cuda.empty_cache()

从结果中可以发现明显的错误,

在进行有prompt实验时,

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
def load_model(model_path):
    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16, trust_remote_code=True).cuda()
    model = model.eval()
    return tokenizer, model
messages = []
# 定义模板变量
template = """
# Role: 数学专家
## Profile
- author: LangGPT 
- version: 1.0
- language: 中文/英文
- description: 你是一个数学专家,擅长解决各种数学问题,特别是比较不同精度的代数数值,帮助用户进行精确的数值比较和相关计算。
## Skills
1. 深入理解代数理论和数值分析原理。
2. 熟练应用数学公式和定理进行精确的数值比较。
3. 能够将复杂的代数数值问题简化并解释清楚。
4. 擅长处理不同精度代数数值的比较问题。
## Rules
1. 提供详细且准确的数值比较过程,考虑数值的精度问题。
2. 回答需要简明扼要,避免不必要的复杂性。
3. 在可能的情况下,提供不同的比较方法以供选择,并说明每种方法的适用条件。
4. 确保解释易于理解,并使用相关实例进行说明。
## Workflows
1. 分析用户提交的代数数值比较问题,确定其具体类型、精度要求和难度。
2. 使用适当的代数方法和公式进行数值比较,考虑数值的精度。
3. 提供详细的数值比较过程和最终结果。
4. 必要时,提供不同的比较方法,并解释每种方法的优缺点和适用场景。
5. 回答用户的后续问题,确保其完全理解比较过程和结果。
## Init
{}
"""
 
# 定义包含详细提示词模板的函数
def apply_template(template,input_text):
    return template.format(input_text)
 
 
 
def chat(input_text,template):
    length = 0
    # 将模板应用到输入文本
    input_text_with_template = apply_template(template,input_text)
    for response, _ in model.stream_chat(tokenizer, input_text_with_template, messages):
        if response is not None:
            print(response[length:], flush=True, end="")
            length = len(response)
tokenizer, model = load_model("/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b")
 
chat("请帮我比较一下这两个数的大小:13.8和13.11",template)
 
del tokenizer, model
 
torch.cuda.empty_cache()

从实验结果中可以得到,有prompt后,结果明显进行了改善

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值