Llama-2大模型本地部署研究与应用测试

最近在研究自然语言处理过程中,正好接触到大模型,特别是在年初chatgpt引来的一大波AIGC热潮以来,一直都想着如何利用大模型帮助企业的各项业务工作,比如智能检索、方案设计、智能推荐、智能客服、代码设计等等,总得感觉相比传统的搜索和智能化辅助手段,大模型提供的方式更高效、直接和精准等,而且结合chat,能够实现多轮次的迭代,更接近或了解用户需求,提供更精准的答复。目前正在开展大模型部署应用测试,目前开源大模型主要就是Llama、ChatGLM大模型等,包括Llama-1和Llama-2,在其基础上的改进大模型有Chinese-LLaMA、OpenChineseLLaMA、Moss、baichuan等等,本文主要对原始Llama大模型进行了本地部署与测试,后续再逐步扩展,结合行业数据资源进行finetune,希望在开源模型的基础上对油气行业大模型构建有所帮助,Llama-2大模型部署及应用测试如下。

一、部署环境

环境:利用anaconda管理python环境

conda:conda 4.3.30

python:Python 3.10.4

cuda version:11.0,安装低于该版本的包即可,我安装的是cu102,GPU采用Tesla V100,详见GPU监测情况

env:/root/anaconda3/envs/torch/

require包如下,主要看torch、torchaudio、torchvision、transformers、uvicorn、fastapi、accelerate。

二、目前已部署的大模型和运行比较

Chinese-Llama-2-7b,运行速度慢,加载速度快

Chinese-Llama-2-7b-4bit,运行速度相对快,加载速度最快

chinese-alpaca-2-7b-hf,运行速度更快,加载速度慢

chinese-alpaca-2-13b-hf,运行速度更快,加载速度慢

open-chinese-llama-7b-patch,运行速度中等,加载速度慢

三、目前支持的运行方式:

1.控制台运行,详见chinese-llama2Test2.py,运行命令:python chinese-llama2Test2.py Chinese-Llama-2-7b

2.Rest服务运行,restful运行,详见restApi.py,运行命令:python restApi.py Chinese-Llama-2-7b

对于Rest服务的调用,主要用postman或DHC客户端模拟POST请求,Content-Type=application/json,post参数是json格式,如 {"prompt": "北京最佳的旅游时间", "history": []}

四、应用测试

1.单次测试代码

python 复制代码
# 一次性访问
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
model_path = "model/Chinese-Llama-2-7b"
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(model_path).half().cuda()
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)

instruction = """[INST] <<SYS>>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.

            If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n{} [/INST]"""

prompt = instruction.format("用中文回答,When is the best time to visit Beijing, and do you have any suggestions for me?")
generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096, streamer=streamer)

2.输出结果

3.循环交互模式测试代码

python 复制代码
#循环交互模式
import torch
import sys, getopt
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
if (__name__ == '__main__') or (__name__ == 'main'):
	# 检查参数个数
	argc = len(sys.argv)
	if (argc <= 1):
		print('missingParms' % locals())
		sys.exit()
	#处理命令行参数
	modelName = sys.argv[1]
	#model_path = "model/Chinese-Llama-2-7b"
	model_path = "model/"+modelName
	tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
	if model_path.endswith("4bit"): #支持q4的轻量化模型,选择对应模型即可。
		model = AutoModelForCausalLM.from_pretrained(
				model_path,
				torch_dtype=torch.float16,
				device_map='auto'
			)
	else:
		model = AutoModelForCausalLM.from_pretrained(model_path).half().cuda()
	streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
	 
	instruction = """[INST] <<SYS>>\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
				If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n{} [/INST]"""
	 
	while True:
		text = input("请输入提问 prompt\n")
		if text == "q":
			break
		prompt = instruction.format(text)
		generate_ids = model.generate(tokenizer(prompt, return_tensors='pt').input_ids.cuda(), max_new_tokens=4096, streamer=streamer)

4.输出结果

五、监测GPU的使用情况

命令:watch -n 1 -d nvidia-smi

1.启动时的GPU状态

2.运行过程中的GPU状态

相关推荐
我就是全世界4 小时前
TensorRT-LLM:大模型推理加速的核心技术与实践优势
人工智能·机器学习·性能优化·大模型·tensorrt-llm
文浩(楠搏万)8 小时前
用OBS Studio录制WAV音频,玩转语音克隆和文本转语音!
大模型·音视频·tts·wav·obs·声音克隆·语音录制
Mr.zwX11 小时前
【大模型】到底什么是Function Calling和MCP,以及和ReAct推理的关系是什么?
大模型·mcp协议
李师兄说大模型15 小时前
KDD 2025 | 地理定位中的群体智能:一个多智能体大型视觉语言模型协同框架
人工智能·深度学习·机器学习·语言模型·自然语言处理·大模型·deepseek
Sherlock Ma15 小时前
百度开源文心一言4.5:论文解读和使用入门
人工智能·百度·自然语言处理·开源·大模型·文心一言·多模态
喜欢吃豆16 小时前
目前最火的agent方向-A2A快速实战构建(二): AutoGen模型集成指南:从OpenAI到本地部署的全场景LLM解决方案
后端·python·深度学习·flask·大模型
喜欢吃豆17 小时前
快速手搓一个MCP服务指南(九): FastMCP 服务器组合技术:构建模块化AI应用的终极方案
服务器·人工智能·python·深度学习·大模型·github·fastmcp
一 铭21 小时前
AI领域新趋势:从提示(Prompt)工程到上下文(Context)工程
人工智能·语言模型·大模型·llm·prompt
静心问道1 天前
self-consistency:自洽性提升语言模型中的链式思维推理能力
人工智能·语言模型·大模型
胡耀超1 天前
标签体系设计与管理:从理论基础到智能化实践的综合指南
人工智能·python·深度学习·数据挖掘·大模型·用户画像·语义分析