项目简介
本项目是基于Python语言的ChatYuan功能型对话大模型系统。ChatYuan-large-v2在ChatYuan-large-v1基础上多方面优化,支持中英双语,可用于问答、结合上下文对话、各类生成任务(如创意写作),还能解答法律、新冠等领域问题。该模型基于PromptCLUE-large结合数亿条功能对话多轮对话数据进一步训练得到,可在消费级显卡、PC甚至手机上进行推理。
项目的主要特性和功能
- 多语言支持:支持中文和英文对话,方便不同语言背景的用户使用。
- 功能丰富:具备问答、创意写作、代码生成、表格生成、数学运算等多种功能,满足多样化需求。
- 多轮对话:能够理解和维持多轮对话的上下文,使对话更加连贯。
- 安全拒答:对于危险、有害的问题,会拒绝回答,保障使用安全性。
- 能力增强:相比旧版本,基础能力(如上下文问答、创意写作)显著提升,最大长度从1024token数扩展到4096,模拟情景能力也得到增强。
安装使用步骤
前提条件
用户已下载本项目的源码文件,且已安装Python环境,推荐使用具有GPU的计算机以提高推理速度。
安装依赖
为确保项目正常运行,需安装以下依赖:
bash
pip install clueai==0.0.2.2.4 gradio==3.20.1 transformers==4.26.1
加载模型
以下是加载模型的代码示例:
python
from transformers import T5Tokenizer, T5ForConditionalGeneration
tokenizer = T5Tokenizer.from_pretrained("ClueAI/ChatYuan-large-v2")
model = T5ForConditionalGeneration.from_pretrained("ClueAI/ChatYuan-large-v2")
进行推理
以下是使用模型进行预测推理的方法: ```python import torch from transformers import AutoTokenizer device = torch.device('cuda') model.to(device) def preprocess(text): text = text.replace("\n", "\n").replace("\t", "\t") return text
def postprocess(text): return text.replace("\n", "\n").replace("\t", "\t").replace('%20',' ')
def answer(text, sample=True, top_p=0.9, temperature=0.7, context = ""): '''sample:是否抽样。生成任务,可以设置为True; top_p:0-1之间,生成的内容越多样''' text = f"{context}\n用户:{text}\n小元:" text = text.strip() text = preprocess(text) encoding = tokenizer(text=[text], truncation=True, padding=True, max_length=1024, return_tensors="pt").to(device) if not sample: out = model.generate(encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=1024, num_beams=1, length_penalty=0.6) else: out = model.generate(encoding, return_dict_in_generate=True, output_scores=False, max_new_tokens=1024, do_sample=True, top_p=top_p, temperature=temperature, no_repeat_ngram_size=12) out_text = tokenizer.batch_decode(out["sequences"], skip_special_tokens=True) return postprocess(out_text[0]) ```
运行交互
一键启动gradio网页交互对话
直接运行 app_gradio.py
即可打开网页进行交互。
简洁对话方式
python
from transformers import AutoTokenizer, AutoModel
import os
model_dir='ClueAI/ChatYuan-large-v2'
tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True)
history = []
print("starting")
while True:
query = input("\n用户:")
if query == "stop":
break
if query == "clear":
history = []
os.system('clear')
continue
response, history = model.chat(tokenizer, query, history=history)
print(f"小元:{response}")
多轮对话示例
python
history = []
while True:
query = input("\n用户:")
context = "\n".join(history[-5:])
response = answer(query, context=context)
history.append(f"用户:{query}\n小元:{response}")
print(f"小元:{response}")
下载地址
点击下载 【提取码: 4003】