模型概述
模型特點
模型能力
使用案例
🚀 基於H2O LLM Studio訓練的問答生成模型
本模型基於H2O LLM Studio訓練,可根據給定文檔生成選擇題及答案,在問答生成任務中具有重要價值。
🚀 快速開始
安裝依賴
要在支持GPU的機器上使用transformers
庫調用此模型,首先需確保已安裝transformers
庫:
pip install transformers==4.31.0
若模型位於私有倉庫,還需向pipeline
提供Hugging Face令牌:
- 方法一:在
pipeline
中設置token=True
,並通過以下代碼登錄Hugging Face:
import huggingface_hub
huggingface_hub.login(<ACCES_TOKEN>)
- 方法二:直接在
pipeline
的token
參數中傳入<ACCES_TOKEN>
。
調用示例
from transformers import pipeline
generate_text = pipeline(
model="fbellame/llama2-pdf-to-quizz-13b",
torch_dtype="auto",
trust_remote_code=True,
use_fast=True,
device_map={"": "cuda:0"},
token=True,
)
res = generate_text(
"Why is drinking water so healthy?",
min_new_tokens=2,
max_new_tokens=256,
do_sample=False,
num_beams=1,
temperature=float(0.3),
repetition_penalty=float(1.2),
renormalize_logits=True
)
print(res[0]["generated_text"])
查看預處理後的提示
print(generate_text.preprocess("Why is drinking water so healthy?")["prompt_text"])
輸出結果如下:
<|prompt|>Why is drinking water so healthy?</s><|answer|>
自定義構建pipeline
也可以下載h2oai_pipeline.py並與筆記本放在同一目錄,然後從加載的模型和分詞器自行構建pipeline
。若模型和分詞器在transformers
包中得到完全支持,可設置trust_remote_code=False
。
from h2oai_pipeline import H2OTextGenerationPipeline
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(
"fbellame/llama2-pdf-to-quizz-13b",
use_fast=True,
padding_side="left",
trust_remote_code=True,
)
model = AutoModelForCausalLM.from_pretrained(
"fbellame/llama2-pdf-to-quizz-13b",
torch_dtype="auto",
device_map={"": "cuda:0"},
trust_remote_code=True,
)
generate_text = H2OTextGenerationPipeline(model=model, tokenizer=tokenizer)
res = generate_text(
"Why is drinking water so healthy?",
min_new_tokens=2,
max_new_tokens=256,
do_sample=False,
num_beams=1,
temperature=float(0.3),
repetition_penalty=float(1.2),
renormalize_logits=True
)
print(res[0]["generated_text"])
考慮預處理步驟構建pipeline
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "fbellame/llama2-pdf-to-quizz-13b" # 可以是本地文件夾或Hugging Face模型名稱
# 重要提示:提示語需與模型訓練時的格式一致,可在實驗日誌中找到示例提示語
prompt = "<|prompt|>How are you?</s><|answer|>"
tokenizer = AutoTokenizer.from_pretrained(
model_name,
use_fast=True,
trust_remote_code=True,
)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map={"": "cuda:0"},
trust_remote_code=True,
)
model.cuda().eval()
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False).to("cuda")
# 生成配置可按需修改
tokens = model.generate(
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
min_new_tokens=2,
max_new_tokens=256,
do_sample=False,
num_beams=1,
temperature=float(0.3),
repetition_penalty=float(1.2),
renormalize_logits=True
)[0]
tokens = tokens[inputs["input_ids"].shape[1]:]
answer = tokenizer.decode(tokens, skip_special_tokens=True)
print(answer)
✨ 主要特性
- 基於強大基礎模型:以meta-llama/Llama-2-13b-hf為基礎模型進行訓練。
- 特定任務訓練:在168個提示上進行訓練,用於生成選擇題及對應答案。
- 支持量化和分片:可通過指定
load_in_8bit=True
或load_in_4bit=True
進行量化加載,還可設置device_map=auto
在多個GPU上進行分片。
📦 安裝指南
在支持GPU的機器上使用transformers
庫調用此模型,需安裝transformers
庫:
pip install transformers==4.31.0
若模型位於私有倉庫,還需向pipeline
提供Hugging Face令牌:
- 方法一:在
pipeline
中設置token=True
,並通過以下代碼登錄Hugging Face:
import huggingface_hub
huggingface_hub.login(<ACCES_TOKEN>)
- 方法二:直接在
pipeline
的token
參數中傳入<ACCES_TOKEN>
。
📚 詳細文檔
模型訓練
本模型使用H2O LLM Studio進行訓練,訓練數據為168個用於生成選擇題及答案的提示(https://huggingface.co/datasets/fbellame/pdf_to_quizz_llama_13B )。可查看此YouTube視頻瞭解微調過程。
源代碼
可在此GitHub項目(local_model
分支和https://github.com/fbellame/pdf-to-quizz/tree/feature/tgi )中查看使用此模型的代碼。
量化和分片
可通過指定load_in_8bit=True
或load_in_4bit=True
進行量化加載,設置device_map=auto
可在多個GPU上進行分片。
模型架構
LlamaForCausalLM(
(model): LlamaModel(
(embed_tokens): Embedding(32000, 5120, padding_idx=0)
(layers): ModuleList(
(0-39): 40 x LlamaDecoderLayer(
(self_attn): LlamaAttention(
(q_proj): Linear(in_features=5120, out_features=5120, bias=False)
(k_proj): Linear(in_features=5120, out_features=5120, bias=False)
(v_proj): Linear(in_features=5120, out_features=5120, bias=False)
(o_proj): Linear(in_features=5120, out_features=5120, bias=False)
(rotary_emb): LlamaRotaryEmbedding()
)
(mlp): LlamaMLP(
(gate_proj): Linear(in_features=5120, out_features=13824, bias=False)
(up_proj): Linear(in_features=5120, out_features=13824, bias=False)
(down_proj): Linear(in_features=13824, out_features=5120, bias=False)
(act_fn): SiLUActivation()
)
(input_layernorm): LlamaRMSNorm()
(post_attention_layernorm): LlamaRMSNorm()
)
)
(norm): LlamaRMSNorm()
)
(lm_head): Linear(in_features=5120, out_features=32000, bias=False)
)
模型配置
本模型使用H2O LLM Studio進行訓練,具體配置見cfg.yaml。可訪問H2O LLM Studio瞭解如何訓練自己的大語言模型。
🔧 技術細節
訓練數據示例
You are a teacher preparing questions for a quiz. Given the following document, please generate 1 multiple-choice questions (MCQs) with 4 options and a corresponding
answer letter based on the document.
Example question:
Question: question here
CHOICE_A: choice here
CHOICE_B: choice here
CHOICE_C: choice here
CHOICE_D: choice here
Answer: A or B or C or D
These questions should be detailed and solely based on the information provided in the document.
<Begin Document>
In 1229, the King had to struggle with a long lasting strike at the University of Paris. The Quartier Latin was strongly hit by these strikes.
<End Document>"
question: What was the cause of the strike at the University of Paris in 1229?
A: The King's interference in university affairs
B: A shortage of resources for the university
C: A disagreement between faculty members
D: The Quartier Latin being strongly hit by a natural disaster
reponse: B
📄 許可證
文檔未提及許可證相關信息。
⚠️ 重要提示
請在使用本倉庫提供的大語言模型前仔細閱讀以下免責聲明。使用該模型即表示您同意遵守以下條款和條件:
- 偏差與冒犯性:該大語言模型在多種互聯網文本數據上進行訓練,這些數據可能包含有偏差、種族主義、冒犯性或其他不適當的內容。使用此模型即表示您承認並接受生成的內容有時可能存在偏差或產生冒犯性、不適當的內容。本倉庫的開發者不認可、支持或推廣任何此類內容或觀點。
- 侷限性:該大語言模型是基於人工智能的工具,並非人類。它可能會產生錯誤、無意義或不相關的回覆。用戶有責任批判性地評估生成的內容,並自行決定是否使用。
- 風險自擔:使用此大語言模型的用戶必須對使用該工具可能產生的任何後果承擔全部責任。本倉庫的開發者和貢獻者不對因使用或濫用所提供模型而導致的任何損害、損失或傷害承擔責任。
- 道德考量:鼓勵用戶負責任且合乎道德地使用該大語言模型。使用此模型即表示您同意不將其用於宣揚仇恨言論、歧視、騷擾或任何形式的非法或有害活動。
- 問題報告:如果您遇到該大語言模型生成的有偏差、冒犯性或其他不適當的內容,請通過提供的渠道向倉庫維護者報告。您的反饋將有助於改進模型並減少潛在問題。
- 免責聲明變更:本倉庫的開發者保留隨時修改或更新此免責聲明的權利,無需事先通知。用戶有責任定期查看免責聲明,以瞭解任何變更。
使用本倉庫提供的大語言模型即表示您同意接受並遵守本免責聲明中規定的條款和條件。如果您不同意本免責聲明的任何部分,應避免使用該模型及其生成的任何內容。



