自学内容网 自学内容网

模型部署系列 | 如何本地部署LLM服务?以ollama为例

简介

小伙伴们好,我是微信公众号《小窗幽记机器学习》的小编:卖沙茶面的小男孩。这篇小作文主要介绍如何使用 ollama 在本地部署大模型服务。更多关于大模型相关,如模型解读、模型微调、模型部署、推理加速等,可以留意本微信公众号《小窗幽记机器学习》。

安装ollama

安装过程需要访问github,如果网络不好,可以根据自己的实际需要预先进行如下代理设置(所以看自己的代理,):

git config --global url."https://github-proxy,XXX.com/".insteadOf "https://github.com/"

也可以使用如下的方式配置代理:

export HTTPS_PROXY=XXX.com:8080
export HTTP_PROXY=XXX.com:8080

再执行以下命令:

curl -fsSL https://ollama.com/install.sh | sh

如果由于网络原因,也可以直接下载Linux版本,然后重命名为ollama,再修改install.sh中的TEMP_DIR路径为ollama文件所在目录。最后执行bash install.sh直接进行安装。

Linux上更具体的安装细节可以参考官方说明。

在安装完Ollama之后,我们需要准备一个已经训练好的大型语言模型。Ollama支持多种不同的模型格式,包括Hugging Face的Transformers模型、PyTorch模型等。

启动服务

如何利用ollama快速启动一个大模型服务?

方法1:Ollama官方下载

可以通过ollama library直接查阅目标模型是否存在,比如想要运行Qwen,可以直接运行如下:

ollama run qwen

方法2:本地模型

将从hf上下载的pytorch模型文件转为UUGF格式。

clone ollama/ollama 仓库:

git clone git@github.com:ollama/ollama.git ollama
cd ollama

fetch 该仓库中的 llama.cpp submodule:

git submodule init
git submodule update llm/llama.cpp

安装Python依赖:

pip3 install -r llm/llama.cpp/requirements.txt -i https://mirrors.cloud.tencent.com/pypi/simple

模型格式转换:

python3 llm/llama.cpp/convert-hf-to-gguf.py /model_zoo/LLM/Qwen/Qwen1.5-4B-Chat/ --outtype f16 --outfile qwen1.5-4B-chat.gguf

构建quantize工具:

make -C llm/llama.cpp quantize

当然,如果此前已经使用过llama.cpp,那么可以直接使用已经编译出的llama.cpp/build/bin/quantize工具。

模型量化:

/Repository/LLM/llama.cpp/build/bin/quantize qwen1.5-4B-chat.gguf qwen1.5-4B-chat_q4_0.gguf q4_0

至此,生成量化后的模型qwen1.5-4B-chat_q4_0.gguf

-rw-r--r--  1 root        root        7.4G Apr 13 08:08 qwen1.5-4B-chat.gguf
-rw-r--r--  1 root        root        2.2G Apr 13 08:12 qwen1.5-4B-chat_q4_0.gguf

新建Modelfile,比如名为 qwen1.5-4B-chat_q4_0.mf

FROM qwen1.5-4B-chat_q4_0.gguf

# set the temperature to 1 [higher is more creative, lower is more coherent]
PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER repeat_penalty 1.05
PARAMETER top_k 20

TEMPLATE """{{ if and .First .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}<|im_start|>user
{{ .Prompt }}<|im_end|>
<|im_start|>assistant
{{ .Response }}"""

# set the system message
SYSTEM """
You are a helpful assistant.
"""

在创建模型之前需要注意检测是否已经启动 ollama 服务,如果没有启动,则通过以下命令启动:

ollama serve &

可以通过以下命令查看model清单:

ollama list

结果如下:

[GIN] 2024/04/13 - 08:29:44 | 200 |       43.91µs |       127.0.0.1 | HEAD     "/"
[GIN] 2024/04/13 - 08:29:44 | 200 |    5.550372ms |       127.0.0.1 | GET      "/api/tags"

创建模型:

ollama create qwen1.5-4B -f qwen1.5-4B-chat_q4_0.mf

此时再运行ollama list,结果如下:

[GIN] 2024/04/13 - 08:33:33 | 200 |      31.789µs |       127.0.0.1 | HEAD     "/"
[GIN] 2024/04/13 - 08:33:33 | 200 |   13.834699ms |       127.0.0.1 | GET      "/api/tags"
NAME                    ID              SIZE    MODIFIED
qwen1.5-4B:latest       2ca4f59f16eb    2.3 GB  About a minute ago

启动模型服务:

ollama run qwen1.5-4B

输入,"你好,你是谁"

API测试

如何通过API测试模型服务?API的详情可以参考官方说明。

对Chat接口进行测试:

curl http://localhost:11434/api/chat -d '{
  "model": "qwen1.5-4B",
  "messages": [
    { "role": "user", "content": "你好,你是谁" }
  ],
  "stream": false
}'

以上设置非流式返回:

{"model":"qwen1.5-4B","created_at":"2024-04-13T08:41:52.481530089Z","message":{"role":"assistant","content":"你好,我是通义千问。我是一个基于阿里云的大规模语言模型,能够回答各种问题、创作文字,还能表达观点、撰写代码。有什么我可以帮助你的吗?"},"done":true,"total_duration":367232392,"load_duration":8591084,"prompt_eval_duration":26868000,"eval_count":40,"eval_duration":331108000}

对 generate 结果进行测试:

curl http://localhost:11434/api/generate -d '{
  "model": "qwen1.5-4B",
  "prompt": "你是谁?",
  "stream": false
}'

接口返回:

{"model":"qwen1.5-4B","created_at":"2024-04-13T08:50:19.674913783Z","response":"我是阿里云开发的超大规模语言模型,我叫通义千问。","done":true,"context":[151644,872,198,105043,100165,11319,151645,198,151644,77091,198,104198,102661,99718,100013,9370,71304,105483,102064,104949,3837,35946,99882,31935,64559,99320,56007,1773],"total_duration":180988339,"load_duration":7694382,"prompt_eval_duration":26834000,"eval_count":18,"eval_duration":145800000}

总结

本文主要介绍如何安装ollama,并演示2种加载模型的方法: 

(1)拉取ollama官方已经有的模型,进行LLM服务部署。

(2)加载本地模型部署大模型服务。 

最后,对部署的LLM服务的接口进行测试。#大模型部署 #LLM部署 #如何部署ollama #模型框架 #AI入门 #推理加速


原文地址:https://blog.csdn.net/ljp1919/article/details/142442803

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!