llama.cpp本地部署大模型

llama.cpp 是一个C++库,用于简化LLM推理的设置,它使得在本地机器上运行大模型(GGUF格式)成为可能。

官网:https://github.com/ggerganov/llama.cpp

模型库:

https://huggingface.co/

HF-Mirror

魔搭社区

安装并且使用llama.cpp

0.安装llama.cpp

官方文档:https://github.com/ggerganov/llama.cpp/blob/master/docs/build.md

git clone https://github.com/ggerganov/llama.cpp

cd llama.cpp

make -j 8

1. 下载模型(HF)

# obtain the official LLaMA model weights and place them in ./models/mymodels

sudo yum install git-lfs

git lfs install

git clone 魔搭社区

2. 转换与量化

官方文档:https://github.com/ggerganov/llama.cpp/blob/master/examples/quantize/README.md

将HuggingFace的safetensors格式的模型文件转换成gguf格式才能使用llama.cpp推理。最新版本只能用convert-hf-to-gguf.py来转换,convert.py已经过期了。

#install Python dependencies

python -m pip install -r requirements.txt

#convert the model to ggml FP16 format

python3 convert_hf_to_gguf.py ./models/mymodels/glm-4-9b-chat

#quantize the model to 4-bits (using Q4_K_M method)

./llama-quantize ./models/mymodels/glm-4-9b-chat/glm-4-9B-chat-F16.gguf ./models/mymodels/glm-4-9b-chat/glm-4-9B-chat_Q4_K_M.gguf Q4_K_M

3.模型加载与推理

官方文档: https://github.com/ggerganov/llama.cpp

#You can run a basic completion using this command

./llama-cli -m ./models/mymodels/glm-4-9B-chat/glm-4-9B-chat_Q4_K_M.gguf -p "I believe the meaning of life is" -n 2048

#Like ChatGPT-like experience, you can run in conversation mode by passing -cnv as a parameter

./llama-cli -m ./models/mymodels/glm-4-9B-chat/glm-4-9B-chat_Q4_K_M.gguf -cnv -c 2048 --temp 0.2 -n 2048 -p "你是一个作家助手"

# If you want to use another chat template, pass --chat-template NAME as a parameter../llama-cli -m ./models/mymodels/glm-4-9B-chat/glm-4-9B-chat_Q4_K_M.gguf -p "You are a helpful assistant" -cnv --chat-template chatml -cnv -c 2048 --temp 0.2 -n 2048

#Web server

./llama-server -m ./models/mymodels/gemma_2_chinese_chat_q8_0.gguf --port 8080 --host 192.168.19.180 -c 2048 --temp 0.2 -n 2048

  • -c 2048: 设置上下文长度为 2048。这决定了模型在推理时可以参考的上下文范围。
  • --temp 0.2: 设置采样温度为 0.2。较低的温度通常会使输出更加保守和确定性更高。
  • -n 2048: 设置生成的最大token数为 2048。即模型生成的文本最多包含2048 个 token。

# Basic web UI can be accessed via browser: http://192.168.19.180:18080

# Chat completion endpoint: http://192.168.19.180:18080/v1/chat/completions

#关闭web server

使用 Ctrl+C 组合键 或者 ps aux | grep llama-server 再kill.

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值