You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Hugging Face LLM生成符合预期的护士职场合成评价?

如何让OpenLLaMA/Guanaco生成符合需求的护士职场评价?

问题背景

需求为生成10条护士对工作场所的真实感受评价,但使用openlm-research/open_llama_7b或guanaco-7b模型时,生成结果偏离需求,输出与写作相关内容而非目标评价。当前使用的代码如下:

from transformers import AutoModelForCausalLM
from transformers import AutoTokenizer


model = AutoModelForCausalLM.from_pretrained(
    "openlm-research/open_llama_7b", device_map={"": 0}, load_in_4bit=True
)
tokenizer = AutoTokenizer.from_pretrained("openlm-research/open_llama_7b")

model_inputs = tokenizer(["Your task is to generate 10 reviews written by nurses about how they feel about their workplace. Generate:"], return_tensors="pt").to("cuda")
generated_ids = model.generate(**model_inputs, do_sample=True,top_k=50, top_p=0.95, max_new_tokens = 1300)
tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]

核心问题分析

  1. Prompt引导不足:原Prompt过于简洁,未给模型明确的输出范式,大语言模型容易偏离任务方向,生成无关内容。
  2. 缺少关键Token配置:OpenLLaMA的tokenizer默认未设置pad_token,可能导致生成时的序列处理异常,影响输出连贯性和准确性。
  3. 解码参数可优化:当前采样参数未针对性调整,无法有效强化任务聚焦性。

解决方案

1. 优化Prompt(核心改进)

给模型提供Few-shot示例,明确输出格式和内容方向,让模型清晰理解任务要求。示例Prompt(中文适配版):

请生成10条护士对工作场所的真实感受评价,每条评价要结合具体工作场景,体现真实情绪:
1. 科室的同事氛围特别好,遇到紧急抢救时大家总能无缝配合,虽然夜班熬得人难受,但这份信任感让我觉得值。
2. 最近三个月患者量暴涨,排班表排得满满当当,连轴转的节奏快把我压垮了,希望医院能尽快补充人力。
3. 医院新换的护理系统太不人性化,录个医嘱要绕好几个步骤,每天光耗在系统操作上的时间就占了三分之一,真的很烦躁。
请继续生成剩下的7条评价:

若使用英文Prompt,同样补充示例:

Generate 10 authentic workplace reviews from nurses, each focusing on specific scenarios and real emotions:
1. My unit has the best team—we always have each other's backs during code blues. Nights are tough, but this camaraderie makes it bearable.
2. Patient loads have been through the roof lately, and our schedule is non-stop. I haven't had a full day off in two weeks, and I'm burnt out. Wish we had more staff.
3. The new EHR system is so clunky; it takes twice as long to document care now. I spend more time typing than with my patients, and it's frustrating.
Continue generating the remaining 7 reviews:

2. 补充Token配置

初始化tokenizer后,添加pad_token设置,避免生成时的序列错误:

tokenizer = AutoTokenizer.from_pretrained("openlm-research/open_llama_7b")
# 新增:设置pad_token为eos_token,解决无pad_token的问题
tokenizer.pad_token = tokenizer.eos_token

3. 调整解码参数

微调采样参数,强化输出的聚焦性:

generated_ids = model.generate(
    **model_inputs,
    do_sample=True,
    top_k=30,  # 缩小top_k范围,减少无关内容生成
    top_p=0.9,
    temperature=0.7,  # 降低temperature,减少随机性,提升任务贴合度
    max_new_tokens=1300,
    pad_token_id=tokenizer.pad_token_id,  # 明确指定pad_token_id
    eos_token_id=tokenizer.eos_token_id
)

4. 模型选择补充

若OpenLLaMA效果仍不理想,可尝试针对指令微调的模型(如lmsys/vicuna-7b-v1.5),这类模型对指令理解能力更强,更易生成符合要求的内容。

内容的提问来源于stack exchange,提问作者ad309432

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 20:47:13