我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。
回顧一下目前手上有的素材:
一切都完整後,準備來做一個聊天介面,但是我們先準備一個函數,以方便後續使用:
from transformers import BertTokenizer, BertForSequenceClassification
import torch
model.eval()
def predict(sentence, model, tokenizer):
sentence = "[CLS] " + sentence + " [SEP]"
tokenized_text = tokenizer.tokenize(sentence)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
segments_ids = [0] * len(tokenized_text) # 0 for Seq 1, and 1 for seq 2
tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])
with torch.no_grad():
outputs = model(tokens_tensor, token_type_ids = segments_tensors)
logits = outputs.logits
predicted_label = torch.argmax(logits, dim = 1).item()
return predicted_label