模型简介
模型特点
模型能力
使用案例
🚀 基于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
📄 许可证
文档未提及许可证相关信息。
⚠️ 重要提示
请在使用本仓库提供的大语言模型前仔细阅读以下免责声明。使用该模型即表示您同意遵守以下条款和条件:
- 偏差与冒犯性:该大语言模型在多种互联网文本数据上进行训练,这些数据可能包含有偏差、种族主义、冒犯性或其他不适当的内容。使用此模型即表示您承认并接受生成的内容有时可能存在偏差或产生冒犯性、不适当的内容。本仓库的开发者不认可、支持或推广任何此类内容或观点。
- 局限性:该大语言模型是基于人工智能的工具,并非人类。它可能会产生错误、无意义或不相关的回复。用户有责任批判性地评估生成的内容,并自行决定是否使用。
- 风险自担:使用此大语言模型的用户必须对使用该工具可能产生的任何后果承担全部责任。本仓库的开发者和贡献者不对因使用或滥用所提供模型而导致的任何损害、损失或伤害承担责任。
- 道德考量:鼓励用户负责任且合乎道德地使用该大语言模型。使用此模型即表示您同意不将其用于宣扬仇恨言论、歧视、骚扰或任何形式的非法或有害活动。
- 问题报告:如果您遇到该大语言模型生成的有偏差、冒犯性或其他不适当的内容,请通过提供的渠道向仓库维护者报告。您的反馈将有助于改进模型并减少潜在问题。
- 免责声明变更:本仓库的开发者保留随时修改或更新此免责声明的权利,无需事先通知。用户有责任定期查看免责声明,以了解任何变更。
使用本仓库提供的大语言模型即表示您同意接受并遵守本免责声明中规定的条款和条件。如果您不同意本免责声明的任何部分,应避免使用该模型及其生成的任何内容。



