Promptzl
v1.0.0
將最新的LLMS變成零+ -Shot Pytorch分類器,僅幾行代碼。
提示提供:
有關更多信息,請查看官方文檔。
pip install -U promptzl
在僅幾行代碼中,您可以將選擇的LLM轉換為具有所有理想屬性的老式分類器:
設置數據集:
from datasets import Dataset
dataset = Dataset . from_dict (
{
'text' : [
"The food was absolutely wonderful, from preparation to presentation, very pleasing." ,
"The service was a bit slow, but the food made up for it. Highly recommend the pasta!" ,
"The restaurant was too noisy and the food was mediocre at best. Not worth the price." ,
],
'label' : [ 1 , 1 , 0 ]
}
)定義一個提示,將語言模型引導到正確的預測:
from promptzl import FnVbzPair , Vbz
prompt = FnVbzPair (
lambda e : f"""Restaurant review classification into categories 'positive' or 'negative'.
'Best pretzls in town!'='positive'
'Rude staff, horrible food.'='negative'
' { e [ 'text' ] } '=""" ,
Vbz ({ 0 : [ "negative" ], 1 : [ "positive" ]}))初始化模型:
from promptzl import CausalLM4Classification
model = CausalLM4Classification (
'HuggingFaceTB/SmolLM2-1.7B' ,
prompt = prompt )對數據進行分類:
from sklearn . metrics import accuracy_score
output = model . classify ( dataset , show_progress_bar = True , batch_size = 1 )
accuracy_score ( dataset [ 'label' ], output . predictions )
1.0有關更多詳細的教程,請查看文檔!