繁体   English   中英

Flan T5 - 如何给出正确的提示/问题?

[英]Flan T5 - How to give the correct prompt/question?

为 Flan T5 语言 model 提供正确类型的提示,以便为聊天机器人/选项匹配用例获得正确/准确的响应。

我正在尝试使用 Flan T5 model 来完成以下任务。 给定一个向用户显示选项列表的聊天机器人,model 必须进行语义选项匹配。 例如,如果选项是“烤鸡、烟熏三文鱼”,如果用户说“我想要鱼”,则 model 应该是 select 烟熏三文鱼。 另一个用例可能是“第一个”,在这种情况下,model 应该是 select 烤鸡。 第三个用例可能是“The BBQ one”,在这种情况下,model 应该是 select Barbeque chicken。

我正在使用 huggingface 文档中的一些代码来玩弄 flan-t5,但我没有得到正确的 output。


model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")

inputs = tokenizer('''Q:Select from the following options 
(a) Quinoa Salad 
(b) Kale Smoothie 
A:Select the first one
''', return_tensors="pt")
outputs = model.generate(**inputs)
print(tokenizer.batch_decode(outputs, skip_special_tokens=True))

output 是

['(b) Kale Smoothie']

我应该如何给出正确的提示/问题来引起 Flan t5 的正确响应?

原始论文"Question: abc Context: xyz"格式显示了一个示例,它似乎运行良好。 我使用flan-t5-xl等较大的模型获得了更准确的结果。 下面是一个flan-t5-base的例子,说明大部分匹配良好,但也有一些虚假的结果:

注意:像这样将用户生成的输入与固定模板连接起来会带来“提示注入”攻击的可能性。 将 model 中的 output 视为不受信任或潜在恶意的用户生成的输入; 例如,不要将其作为未转义的 HTML 返回给用户。

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")

def query_from_list(query, options):
    t5query = f"""Question: Select the item from this list which is "{query}". Context: * {" * ".join(options)}"""
    inputs = tokenizer(t5query, return_tensors="pt")
    outputs = model.generate(**inputs, max_new_tokens=20)
    return tokenizer.batch_decode(outputs, skip_special_tokens=True)

tests = ["the first one", "the fish", "the chicken", "2nd", "bbq", "salmon", "roasted turkey", "dried halibut"]
options = ["Barbecue Chicken", "Smoked Salmon"]
for t in tests:
    result = query_from_list(t, options)
    print(f"{t:<24} {result[0]}")

回报:

the first one            Barbecue Chicken
the fish                 Smoked Salmon
the chicken              Barbecue Chicken
2nd                      Barbecue Chicken
bbq                      Barbecue Chicken
salmon                   salmon
roasted turkey           Barbecue Chicken
dried halibut            Smoked Salmon

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM