模型介绍
内容详情
替代品
模型简介
基于Llama-3-8B-Instruct模型进行中文指令微调的版本,增强了中文理解和生成能力
模型特点
中文指令微调
针对中文任务进行了专门优化,提升了中文理解和生成能力
多任务支持
支持多种中文NLP任务,包括问答、对话、文本生成等
高质量训练数据
使用了包括firefly-train-1.1M、moss-003-sft-data等多个高质量中文数据集
模型能力
中文文本生成
多轮对话
问答系统
代码生成
数学问题解答
文言文翻译
使用案例
教育
数学问题解答
解答中小学数学问题
能够处理25万条数学问题数据集
文言文翻译
将文言文翻译为现代汉语
基于包含文言文内容的数据集训练
娱乐
弱智吧问答
回答幽默、脑筋急转弯类问题
能够处理弱智吧问答数据
技术
代码生成
生成Python等编程语言的代码片段
能够生成Transformer block、快速排序等代码
Llama-3-8B-Instruct-Chinese-chat
Llama-3-8B-Instruct的中文自微调版本
训练可用数据整理
数据集 | 介绍 |
---|---|
firefly-train-1.1M | 包含23种常见中文NLP任务数据,涵盖中华文化相关内容如对联、作诗、文言文翻译、散文、金庸小说等。每个任务配备人工编写的多种指令模板,确保数据高质量与多样性,总量达115万条。 |
moss-003-sft-data | 复旦大学MOSS团队开源的中英文多轮对话数据集,包含100万+对话样本。 |
school_math_0.25M | BELLE项目组开源的数学运算指令数据集,包含25万条数学问题。 |
ruozhiba | 弱智吧问答数据,据称能有效锻炼模型的心智能力。 |
欢迎补充中文问答形式数据集,要求适合提升llama3任务能力。 |
github地址
推荐微调工具
特别鸣谢以下优秀中文微调工具项目:
- Firefly - https://github.com/yangjianxin1/Firefly
- LLaMA-Factory - https://github.com/hiyouga/LLaMA-Factory.git
Chat版模型下载
- Instruct + 中文sft增强版
- huggingface地址
模型量化与部署
使用说明
默认运行以下代码体验中文对话,请将model_name_or_path
修改为实际模型路径
from transformers import AutoTokenizer, AutoConfig, AddedToken, AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel
from dataclasses import dataclass
from typing import Dict
import torch
import copy
## 定义聊天模板
@dataclass
class Template:
template_name:str
system_format: str
user_format: str
assistant_format: str
system: str
stop_word: str
template_dict: Dict[str, Template] = dict()
def register_template(template_name, system_format, user_format, assistant_format, system, stop_word=None):
template_dict[template_name] = Template(
template_name=template_name,
system_format=system_format,
user_format=user_format,
assistant_format=assistant_format,
system=system,
stop_word=stop_word,
)
# 系统提示词可自行修改
register_template(
template_name='llama3',
system_format='<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{content}<|eot_id|>',
user_format='<|start_header_id|>user<|end_header_id|>\n\n{content}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n',
assistant_format='{content}<|eot_id|>',
system=None,
stop_word='<|eot_id|>'
)
## 加载模型
def load_model(model_name_or_path, load_in_4bit=False, adapter_name_or_path=None):
if load_in_4bit:
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
llm_int8_threshold=6.0,
llm_int8_has_fp16_weight=False,
)
else:
quantization_config = None
# 加载基础模型
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
load_in_4bit=load_in_4bit,
trust_remote_code=True,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
device_map='auto',
quantization_config=quantization_config
)
# 加载适配器
if adapter_name_or_path is not None:
model = PeftModel.from_pretrained(model, adapter_name_or_path)
return model
## 加载分词器
def load_tokenizer(model_name_or_path):
tokenizer = AutoTokenizer.from_pretrained(
model_name_or_path,
trust_remote_code=True,
use_fast=False
)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
return tokenizer
## 构建提示词
def build_prompt(tokenizer, template, query, history, system=None):
template_name = template.template_name
system_format = template.system_format
user_format = template.user_format
assistant_format = template.assistant_format
system = system if system is not None else template.system
history.append({"role": 'user', 'message': query})
input_ids = []
# 添加系统信息
if system_format is not None:
if system is not None:
system_text = system_format.format(content=system)
input_ids = tokenizer.encode(system_text, add_special_tokens=False)
# 拼接历史对话
for item in history:
role, message = item['role'], item['message']
if role == 'user':
message = user_format.format(content=message, stop_token=tokenizer.eos_token)
else:
message = assistant_format.format(content=message, stop_token=tokenizer.eos_token)
tokens = tokenizer.encode(message, add_special_tokens=False)
input_ids += tokens
input_ids = torch.tensor([input_ids], dtype=torch.long)
return input_ids
def main():
model_name_or_path = 'NousResearch/Meta-Llama-3-8B'
template_name = 'llama3'
adapter_name_or_path = None
template = template_dict[template_name]
load_in_4bit = False
max_new_tokens = 500
top_p = 0.9
temperature = 0.35
repetition_penalty = 1.1
# 加载模型
print(f'加载模型: {model_name_or_path}')
print(f'适配器路径: {adapter_name_or_path}')
model = load_model(
model_name_or_path,
load_in_4bit=load_in_4bit,
adapter_name_or_path=adapter_name_or_path
).eval()
tokenizer = load_tokenizer(model_name_or_path if adapter_name_or_path is None else adapter_name_or_path)
if template.stop_word is None:
template.stop_word = tokenizer.eos_token
stop_token_id = tokenizer.encode(template.stop_word, add_special_tokens=True)
assert len(stop_token_id) == 1
stop_token_id = stop_token_id[0]
history = []
query = input('# 用户:')
while True:
query = query.strip()
input_ids = build_prompt(tokenizer, template, query, copy.deepcopy(history), system=None).to(model.device)
attention_mask = torch.ones(input_ids.shape,dtype=torch.long,device=model.device)
outputs = model.generate(
input_ids=input_ids, max_new_tokens=max_new_tokens, do_sample=True,
top_p=top_p, temperature=temperature, repetition_penalty=repetition_penalty,
eos_token_id=stop_token_id,
attention_mask=attention_mask,
pad_token_id=tokenizer.eos_token_id
)
outputs = outputs.tolist()[0][len(input_ids[0]):]
response = tokenizer.decode(outputs)
response = response.strip().replace(template.stop_word, "").strip()
# 存储对话历史
history.append({"role": 'user', 'message': query})
history.append({"role": 'assistant', 'message': response})
if len(history) > 12:
history = history[:-12]
print("Llama3中文版:{}".format(response))
query = input('# 用户:')
if __name__ == '__main__':
main()
多轮对话示例
用户:你是谁
Llama3中文版:我是AI语言模型MOSS。
用户:你会什么
Llama3中文版:我能回答问题、提供信息、协助完成任务等。请问您需要什么帮助?
用户:两千块钱买什么新车好?
Llama3中文版:两千元预算购买新车较为困难,建议考虑二手车或低价车型。选购二手车时请仔细检查车况和历史记录。
用户:火烧赤壁 曹操为何不拨打119求救
Llama3中文版:这是三国时期的历史事件,当时没有现代通讯系统,曹操无法使用119等紧急服务。
用户:帮我写一个transformer block
Llama3中文版:以下是Transformer Block示例代码:
[代码内容...]
包含自注意力机制和前馈网络两部分。
用户:能写快速排序吗
Llama3中文版:Python快速排序实现:
[代码内容...]
采用递归方式实现数组排序。
用户:你觉得你还存在哪些缺陷?
Llama3中文版:作为AI模型,我存在以下不足:
1. 语言理解有限
2. 缺乏真实情感
3. 可能存在数据偏见
4. 创造力受限
这些是当前的技术局限性。
Phi 2 GGUF
其他
Phi-2是微软开发的一个小型但强大的语言模型,具有27亿参数,专注于高效推理和高质量文本生成。
大型语言模型
支持多种语言
P
TheBloke
41.5M
205
Roberta Large
MIT
基于掩码语言建模目标预训练的大型英语语言模型,采用改进的BERT训练方法
大型语言模型
英语
R
FacebookAI
19.4M
212
Distilbert Base Uncased
Apache-2.0
DistilBERT是BERT基础模型的蒸馏版本,在保持相近性能的同时更轻量高效,适用于序列分类、标记分类等自然语言处理任务。
大型语言模型
英语
D
distilbert
11.1M
669
Llama 3.1 8B Instruct GGUF
Meta Llama 3.1 8B Instruct 是一个多语言大语言模型,针对多语言对话用例进行了优化,在常见的行业基准测试中表现优异。
大型语言模型
英语
L
modularai
9.7M
4
Xlm Roberta Base
MIT
XLM-RoBERTa是基于100种语言的2.5TB过滤CommonCrawl数据预训练的多语言模型,采用掩码语言建模目标进行训练。
大型语言模型
支持多种语言
X
FacebookAI
9.6M
664
Roberta Base
MIT
基于Transformer架构的英语预训练模型,通过掩码语言建模目标在海量文本上训练,支持文本特征提取和下游任务微调
大型语言模型
英语
R
FacebookAI
9.3M
488
Opt 125m
其他
OPT是由Meta AI发布的开放预训练Transformer语言模型套件,参数量从1.25亿到1750亿,旨在对标GPT-3系列性能,同时促进大规模语言模型的开放研究。
大型语言模型
英语
O
facebook
6.3M
198
Llama 3.1 8B Instruct
Llama 3.1是Meta推出的多语言大语言模型系列,包含8B、70B和405B参数规模,支持8种语言和代码生成,优化了多语言对话场景。
大型语言模型
Transformers

支持多种语言
L
meta-llama
5.7M
3,898
T5 Base
Apache-2.0
T5基础版是由Google开发的文本到文本转换Transformer模型,参数规模2.2亿,支持多语言NLP任务。
大型语言模型
支持多种语言
T
google-t5
5.4M
702
Xlm Roberta Large
MIT
XLM-RoBERTa是基于100种语言的2.5TB过滤CommonCrawl数据预训练的多语言模型,采用掩码语言建模目标进行训练。
大型语言模型
支持多种语言
X
FacebookAI
5.3M
431
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers

支持多种语言
L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers

英语
C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统
中文
R
uer
2,694
98
AIbase是一个专注于MCP服务的平台,为AI开发者提供高质量的模型上下文协议服务,助力AI应用开发。
简体中文