我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。
整理目前手上有的素材:
模型訓練完也執行完儲存的動作後,現在要來製作一個 Inference 函數,方便後續使用:
from transformers import RobertaConfig, RobertaForCausalLM
from transformers import RobertaTokenizer
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
model = RobertaForCausalLM.from_pretrained(model_path)
def generate_response(prompt):
inputs = tokenizer(prompt, return_tensors = "pt", max_length = 50, truncation = True)
output = model.generate(**inputs, max_length = 200, temperature = 0.9, num_return_sequences = 1)
generated_text = tokenizer.decode(output[0], skip_special_tokens = True)
return generated_text