簡體   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