文章目录
-
- [1 依赖环境设置](#1 依赖环境设置)
- [2.DeepSeek R1模型部署](#2.DeepSeek R1模型部署)
- 3.FinGPT模型部署
- 4.总结与思考
1 依赖环境设置
本地环境
-
Python 3.10
-
CUbDA (可选)
-
PyTorch
云环境-魔塔
- 一键搭建https://www.modelscope.cn/
· - 预装CUbDA
- PyTorch运行环境
- 提供GPU免费试用时长
- pip install modelscope
2.DeepSeek R1模型部署
DeepSeek R1
- 1.5B
•CPU: 4核心
•内存: 8G
•显卡:非必须
•场景:聊天机器人,简单对话 - 7B/8B
•CPU: 6-8核心
•内存: 16G
•显卡:推荐8GB显存,例如RTX 3060/4060
•场景:文本摘要,翻译,轻量级多轮对话 - 14B
•CPU: 12核心
•内存: 64G
•显卡:推荐16GB显存,例如RTX 3080/4080
•场景:复杂任务,长文本理解与生成 - 32B
•CPU: 16核心
•内存: 128G
•显卡:推荐24GB显存,例如RTX 3090/4090, A100或V100
•场景:高精度专业任务,医疗/金融/法律咨询
shell
Linux环境执行如下命令(官方)
curl-fsSL https://ollama.com/install.sh|sh
魔塔环境
modelscope download--model=modelscope/ollama-linux--local _ dir./ollama
cd ollama-linux
sudo chmod+x./ollama-modelscopeinstall.sh
./ollama-modelscopeinstall.sh
ollama serve
环境参数
OLLAMA_MODELS=自定义路径
多GPU支持
CUDA_VISIBLE_DEVICES=0,1
通过ollama命令拉取模型
ollama run deepseek-r1:7b
3.FinGPT模型部署
安装FinGPT Python依赖包
shell
pip install-Utransformers==4.40.1peft==0.5.0
pip install-U sentencepiece
pip install-U accelerate
pip install-U datasets
pip install-U bitsandbytes
如果模型在CPU上运行
pip3 install torch torchvision torchaudio
如果模型在GPU上运行
#确定CUDA版本nvcc- version
输出: Cuda compilation tools, release 12.1, V12.1.66
pip3 install torch torchvision torchaudio-index-url https://download. pytorch. org/ whl/ cu121
验证pytorch安装结果
python
import torch
print(torch._version_)
print(torch.cuda.is_available())
运行FinGPT
python
import torch
from transformers import LlamaForCausalLM, LlamaTokenizerFast
from peft import PeftModel # 0.5.0
base_model = "meta-llama/Meta-Llama-3-8B"
peft_model = "FinGPT/fingpt-mt_llama3-8b_lora"
prompt = [
"""Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
Input: FINANCING OF ASPOCOMP 'S GROWTH Aspocomp is aggressively pursuing its growth strategy by increasingly focusing on technologically more demanding HDI printed circuit boards PCBs.
Answer:""",
"""Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
Input: According to Gran , the company has no plans to move all production to Russia , although that is where the company is growing.
Answer:"""
]
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = LlamaForCausalLM.from_pretrained(base_model, trust_remote_code=True, device_map="cuda", torch_dtype=torch.float16)
model = PeftModel.from_pretrained(model, peft_model)
model = model.eval()
model = model.to(device)
tokenizer = LlamaTokenizerFast.from_pretrained(base_model, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
tokens = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).to(device)
res =model.generate(**tokens, max_new_tokens=512)
res_sentences = [tokenizer.decode(i) for i in res]
out_text=[o.split("Answer:")[1] for o in res_sentences]
for sentiment in out_text:
print(sentiment)
4.总结与思考
- 如何通过Ollama部署DeepSeek R1模型?
- FinGPT不同版本之间的差异是什么?
- FinGPT使用哪种技术调优基础模型?
- 如何设计Prompt指导FinGPT完成情感分析任务?