【Ollama】大模型运行框架
·
安装与运行
安装教程
安装
wget https://ollama.com/download/ollama-linux-amd64.tgz
tar -xzvf ollama-linux-amd64.tgz
添加ollama的环境变量:export OLLAMA_HOME=/data1/ztshao/programs/ollama-linux-amd64
然后把ollama/bin添加到path里。
运行:ollama serve
检测运行:ollama -v
导入LLM
GGUF是一种存储LLM的格式。ollama选用了这种格式。所以hugginface下下来的llm需要转换为gguf格式。
Hugginface模型-转换为-GGUF模型
- 先下载GGUF的转换代码。
git clone https://github.com/ggerganov/llama.cpp.git
- 进行转换得到
.gguf文件。格式为python convert_hf_to_gguf.py <iput_model_path> --outfile <out_gguf_path> --outtype <outtype>。注意out_gguf_path的后缀为.gguf
python convert_hf_to_gguf.py ../Qwen2.5-7B-Instruct --outfile Qwen2.5-7B-Instruct.gguf --outtype f16
注意.gguf文件存储在model文件夹内部
- ollama运行模型
先构造Modelfile文件:
FROM ./Qwen2.5-7B-Instruct.gguf
无量化版本:ollama create MyQwen2.5-7B-Instruct -f ./Modelfile
带量化版本:ollama create -q Q4_K_M MyQwen2.5-7B-Instruct -f ./Modelfile
- 查看ollama内部模型列表:
ollama list - 运行模型:
ollama run MyQwen2.5-7B-Instruct - 删除模型:
ollama rm MyQwen2.5-7B-Instruct
在指定gpu上运行
失败版本:
创建./ollama_gpu_selector.sh,内容为:
参考代码
#!/bin/bash
# Validate input
validate_input(){
if [[ ! $1 =~ ^[0-4](,[0-4])*$ ]];then
echo "Error: Invalid input. Please enter numbers between 0 and 4, separated by commas."
exit 1
fi
}
# Update the service file with CUDA_VISIBLE_DEVICES values
update_service(){
# Check if CUDA_VISIBLE_DEVICES environment variable exists in the service file
if grep -q '^Environment="CUDA_VISIBLE_DEVICES='/etc/systemd/system/ollama.service;then
# Update the existing CUDA_VISIBLE_DEVICES values
sudo sed -i 's/^Environment="CUDA_VISIBLE_DEVICES=.*/Environment="CUDA_VISIBLE_DEVICES='"$1"'"/'/etc/systemd/system/ollama.service
else
# Add a new CUDA_VISIBLE_DEVICES environment variable
sudo sed -i '/\[Service\]/a Environment="CUDA_VISIBLE_DEVICES='"$1"'"'/etc/systemd/system/ollama.service
fi
# Reload and restart the systemd service
sudo systemctl daemon-reload
sudo systemctl restart ollama.service
echo "Service updated and restarted with CUDA_VISIBLE_DEVICES=$1"
}
# Check if arguments are passed
if [[ "$#" -eq 0 ]];then
# Prompt user for CUDA_VISIBLE_DEVICES values if no arguments are passed
read -p "Enter CUDA_VISIBLE_DEVICES values (0-4, comma-separated): " cuda_values
validate_input "$cuda_values"
update_service "$cuda_values"
else
# Use arguments as CUDA_VISIBLE_DEVICES values
cuda_values="$1"
validate_input "$cuda_values"
update_service "$cuda_values"
fi
成功版:
我没有root权限,所以直接在.bashrc里修改了变量:
export CUDA_DEVICE_ORDER="PCI_BUS_ID"
export CUDA_VISIBLE_DEVICES=4
然后执行bashrc,重启ollama:
source ~/.bashrc
ollama serve
ollama run MyQwen2.5-7B-Instruct
查看ollama的模型运行情况:ollama ps
model存储路径设置
导入微调后的模型
非llama-factory微调,参考该教程合并base模型和lora模型。
llama-factory进行lora微调为例,lora合并。更加方便。并且会在对应的merge文件夹内部生成Modelfile文件。
后续导入:
ollama create qwen2.5_sft -f ./Modelfile
配置文件
### examples/merge_lora/llama3_lora_sft.yaml
### model
model_name_or_path: meta-llama/Meta-Llama-3-8B-Instruct
adapter_name_or_path: saves/llama3-8b/lora/sft
template: llama3
finetuning_type: lora
### export
export_dir: models/llama3_lora_sft
export_size: 2
export_device: cpu
export_legacy_format: false
其中export_size: 2表示merge的模型分片大小是2GB。
执行命令llamafactory-cli export merge_config.yaml
ollama接口
from openai import OpenAI
client = OpenAI(
base_url='http://localhost:11434/v1/',
api_key='ollama',
)
chat_completion = client.chat.completions.create(
messages=[
{
'role': 'user',
'content': 'what is the color of sun? Please only response with one word.',
}
],
model='MyQwen2.5-7B-Instruct',
)
docker访问和端口地址
更多推荐



所有评论(0)