使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B

2024年4月18日,meta开源了Llama 3大模型[1],虽然只有8B[2]和70B[3]两个版本,但Llama 3表现出来的强大能力还是让AI大模型界为之震撼了一番,本人亲测Llama3-70B版本的推理能力十分接近于OpenAI的GPT-4[4],何况还有一个400B的超大模型还在路上,据说再过几个月能发布。

Github上人气巨火的本地大模型部署和运行工具项目Ollama[5]也在第一时间宣布了对Llama3的支持[6]

d7b4f0e4acdf4dcce3c73f8c0e60fbff.png

近期除了学习Rust[7],还有就在研究如何将LLM应用于产品中。以前走微调的路径行不通,最近的RAG(Retrieval-Augmented Generation)和Agent路径则让我看到一丝曙光。不过实施这两个路径的前提是一个强大的LLM,而开源的meta Llama系列LLM则是不二之选。

在这篇文章中,我就先来体验一下如何基于Ollama安装和运行Meta Llama3-8B大模型,并通过兼容Ollama API的OpenWebUI[8]建立对大模型的Web图形化访问方式。

1. 安装Ollama

Ollama是一个由Go实现的、可以在本地丝滑地安装和运行各种开源大模型的工具,支持目前国内外很多主流的开源大模型,比如Llama、Mistral[9]、Gemma[10]、DBRX[11]、Qwen[12]、phi[13]、vicuna[14]、yi[15]、falcon[16]等。其支持的全量模型列表可以在Ollama library[17]查看。

Ollama的安装采用了“curl | sh”,我们可以一键将其下载并安装到本地:

$curl -fsSL https://ollama.com/install.sh | sh
>>> Downloading ollama...
######################################################################## 100.0%
>>> Installing ollama to /usr/local/bin...
>>> Creating ollama user...
>>> Adding ollama user to video group...
>>> Adding current user to ollama group...
>>> Creating ollama systemd service...
>>> Enabling and starting ollama service...
Created symlink from /etc/systemd/system/default.target.wants/ollama.service to /etc/systemd/system/ollama.service.
>>> The Ollama API is now available at 127.0.0.1:11434.
>>> Install complete. Run "ollama" from the command line.
WARNING: No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode.

我们看到Ollama下载后启动了一个ollama systemd service,这个服务就是Ollama的核心API服务,它常驻内存。通过systemctl可以确认一下该服务的运行状态:

$systemctl status ollama
● ollama.service - Ollama Service
   Loaded: loaded (/etc/systemd/system/ollama.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2024-04-22 17:51:18 CST; 11h ago
 Main PID: 9576 (ollama)
    Tasks: 22
   Memory: 463.5M
   CGroup: /system.slice/ollama.service
           └─9576 /usr/local/bin/ollama serve

另外我对Ollama的systemd unit文件做了一些改动,我修改了一下Environment的值,增加了"OLLAMA_HOST=0.0.0.0",这样便于后续在容器中运行的OpenWebUI可以访问到Ollama API服务:

# cat /etc/systemd/system/ollama.service
[Unit]
Description=Ollama Service
After=network-online.target

[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="PATH=/root/.cargo/bin:/usr/local/cmake/bin:/usr/local/bin:.:/root/.bin/go1.21.4/bin:/root/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin" "OLLAMA_HOST=0.0.0.0"

[Install]
WantedBy=default.target

修改后执行下面命令使之生效:

$systemctl daemon-reload
$systemctl restart ollama

2. 下载和运行大模型

Ollama支持一键下载和运行模型。我手里有一个16/32G的云虚机,但没有GPU,因此这里我使用的是Llama3-8B指令微调后的用于chat/diaglogue的模型[18],我们只需要通过下面命令便可以快速下载并运行该模型(4bit量化的):

$ollama run llama3
pulling manifest
pulling 00e1317cbf74...   0% ▕                ▏    0 B/4.7 GB                 
pulling 00e1317cbf74...   7% ▕█               ▏ 331 MB/4.7 GB   34 MB/s    2m3s^C
pulling manifest
pulling manifest
pulling manifest
pulling manifest
pulling 00e1317cbf74...  61% ▕█████████       ▏ 2.8 GB/4.7 GB   21 MB/s   1m23s^C
... ...

下载和执行成功后,该命令行便会等待你的问题输入,我们随便输入一个关于Go的问题,下面是输出结果:

$ollama run llama3
>>> could you tell me something about golang language?
Go!

Here are some interesting facts and features about the Go programming language:

**What is Go?**

Go, also known as Golang, is a statically typed, compiled, and designed to be concurrent and garbage-collected language. It
was developed by Google in 2009.

**Key Features:**

1. **Concurrency**: Go has built-in concurrency support through goroutines (lightweight threads) and channels (communication
mechanisms). This makes it easy to write concurrent programs.
2. **Garbage Collection**: Go has a automatic garbage collector, which frees developers from worrying about memory
management.
3. **Static Typing**: Go is statically typed, meaning that the type system checks the types of variables at compile time,
preventing type-related errors at runtime.
4. **Simple Syntax**: Go's syntax is designed to be simple and easy to read. It has a minimalistic approach to programming
language design.
... ...

推理速度大约在5~6个token吧,尚可接受,但这个过程是相当耗CPU:

3b0c5b967d7e326fc92640077ac77ddd.png

除了通过命令行方式与Ollama API服务交互之外,我们还可以用Ollama的restful API:

$curl http://localhost:11434/api/generate -d '{
>   "model": "llama3",
>   "prompt":"Why is the sky blue?"
> }'
{"model":"llama3","created_at":"2024-04-22T07:02:36.394785618Z","response":"The","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:36.564938841Z","response":" color","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:36.745215652Z","response":" of","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:36.926111842Z","response":" the","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:37.107460031Z","response":" sky","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:37.287201658Z","response":" can","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:37.468517901Z","response":" vary","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:37.649011829Z","response":" depending","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:37.789353456Z","response":" on","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:37.969236546Z","response":" the","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:38.15172159Z","response":" time","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:38.333323271Z","response":" of","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:38.514564929Z","response":" day","done":false}
{"model":"llama3","created_at":"2024-04-22T07:02:38.693824676Z","response":",","done":false}
... ...

不过我日常使用大模型最为广泛的方式还是通过Web UI进行交互。目前有很多支持Ollama API的Web & Desktop项目,这里我们选取Open WebUI[19],它的前身就是Ollama WebUI。

3. 安装和使用Open WebUI与大模型交互

最快体验Open WebUI的方式当然是使用容器安装,不过官方镜像站点ghcr.io/open-webui/open-webui:main下载太慢,我找了一个位于Docker Hub上的个人mirror镜像,下面是在本地安装Open WebUI的命令:

$docker run -d -p 13000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data -e OLLAMA_BASE_URL=http://host.docker.internal:11434  --name open-webui --restart always dyrnq/open-webui:main

容器启动后,我们在host上访问13000端口即可打开Open WebUI页面:

09f06b3fdcdcce93d735cc1df78d778d.png

首个注册的用户,将会被Open WebUI认为是admin用户!注册登录后,我们就可以进入首页:

29bc9be6e5d24eb996281df9da3200cf.png

选择model后,我们便可以输入问题,并与Ollama部署的Llama3模型对话了:

5c38714ed12284f683ab33173da06216.png

注:如果Open WebUI运行不正常,可以通过查看openwebui的容器日志来辅助诊断问题。

Open WebUI的功能还有很多,大家可以自行慢慢挖掘:)。

4. 小结

在本文中,我介绍了Meta开源的Llama 3大模型以及Ollama和OpenWebUI的使用。Llama 3是一个强大的AI大模型,实测接近于OpenAI的GPT-4,并且还有一个更强大的400B模型即将发布。Ollama是一个用于本地部署和运行大模型的工具,支持多个国内外开源模型,包括Llama在内。我详细介绍了如何安装和运行Ollama,并使用Ollama下载和运行Llama3-8B模型。展示了通过命令行和REST API与Ollama进行交互,以及模型的推理速度和CPU消耗。此外,我还提到了OpenWebUI,一种兼容Ollama API的Web图形化访问方式。通过Ollama和OpenWebUI,大家可以方便地在CPU上使用Meta Llama3-8B大模型进行推理任务,并获得满意的结果。

后续,我将进一步研究如何将Llama3应用于产品中,并探索RAG(Retrieval-Augmented Generation)和Agent技术的潜力。这两种路径可以为基于Llama3的大模型应用开发带来新的可能性。


Gopher部落知识星球[20]在2024年将继续致力于打造一个高品质的Go语言学习和交流平台。我们将继续提供优质的Go技术文章首发和阅读体验。同时,我们也会加强代码质量和最佳实践的分享,包括如何编写简洁、可读、可测试的Go代码。此外,我们还会加强星友之间的交流和互动。欢迎大家踊跃提问,分享心得,讨论技术。我会在第一时间进行解答和交流。我衷心希望Gopher部落可以成为大家学习、进步、交流的港湾。让我相聚在Gopher部落,享受coding的快乐! 欢迎大家踊跃加入!

5a7d37b9cd07bfad4afd8916d3be8d24.jpeg7ecc83d456de7c94470d4fa1a3a2ec5f.png

2666637e98bafc45f49f5dc166d9e407.pnga25ab236c35a3bb652fa7b3401e1aff6.jpeg

著名云主机服务厂商DigitalOcean发布最新的主机计划,入门级Droplet配置升级为:1 core CPU、1G内存、25G高速SSD,价格5$/月。有使用DigitalOcean需求的朋友,可以打开这个链接地址[21]:https://m.do.co/c/bff6eed92687 开启你的DO主机之路。

Gopher Daily(Gopher每日新闻) - https://gopherdaily.tonybai.com

我的联系方式:

  • 微博(暂不可用):https://weibo.com/bigwhite20xx

  • 微博2:https://weibo.com/u/6484441286

  • 博客:tonybai.com

  • github: https://github.com/bigwhite

  • Gopher Daily归档 - https://github.com/bigwhite/gopherdaily

da3c2994b7982a5f4b77563076cf925b.jpeg

商务合作方式:撰稿、出书、培训、在线课程、合伙创业、咨询、广告合作。

参考资料

[1] 

meta开源了Llama 3大模型: https://ai.meta.com/blog/meta-llama-3/

[2] 

8B: https://huggingface.co/meta-llama/Meta-Llama-3-8B

[3] 

70B: https://huggingface.co/meta-llama/Meta-Llama-3-70B

[4] 

OpenAI的GPT-4: https://openai.com/research/gpt-4

[5] 

Ollama: https://github.com/ollama/ollama

[6] 

第一时间宣布了对Llama3的支持: https://ollama.com/blog/llama3

[7] 

学习Rust: https://tonybai.com/2024/04/22/gopher-rust-first-lesson-all-about-rust/

[8] 

OpenWebUI: https://github.com/open-webui/open-webui

[9] 

Mistral: https://mistral.ai/

[10] 

Gemma: https://ai.google.dev/gemma

[11] 

DBRX: https://www.databricks.com/blog/introducing-dbrx-new-state-art-open-llm

[12] 

Qwen: https://github.com/QwenLM/Qwen

[13] 

phi: https://huggingface.co/microsoft/phi-2

[14] 

vicuna: https://lmsys.org/blog/2023-03-30-vicuna/

[15] 

yi: https://github.com/01-ai/Yi

[16] 

falcon: https://huggingface.co/blog/falcon

[17] 

Ollama library: https://ollama.com/library

[18] 

Llama3-8B指令微调后的用于chat/diaglogue的模型: https://ollama.com/library/llama3

[19] 

Open WebUI: https://github.com/open-webui/open-webui

[20] 

Gopher部落知识星球: https://public.zsxq.com/groups/51284458844544

[21] 

链接地址: https://m.do.co/c/bff6eed92687

<think> 嗯,用户这次明确要求获取meta-llama/Meta-Llama-3-8B-Instruct模型的使用方法。从对话历史看,系统要求严格遵循LaTeX格式规范,但当前问题不涉及数学公式,主要关注技术操作流程。 用户提供的三个引用中,引用[1]列举了模型版本云平台,引用[2]强调模型优化特性,引用[3]说明模型背景。这些信息虽相关但不直接解答使用方法。需要结合大型语言模型部署的通用流程进行说明。 考虑到用户可能的环境差异,应该覆盖主流部署方式:Hugging Face Transformers是最常用方案,说明基础调用代码;vLLM适合生产环境高性能需求;GGUF量化则针对资源受限场景。特别注明模型访问需申请授权,这是Llama系列的特殊要求。 部署注意事项需要强调硬件配置底线,8B模型虽然参数量适中,但FP16精度仍需约16GB显存。云平台建议参考引用[1]列出的autodl等国内服务商,避免用户访问国际平台困难。 对于进阶需求,指令优化模型的使用技巧要单独说明,包括明确提示词格式、温度参数调整等。用户提到"参考站内引用"但实际内容较杂乱,可能需要帮助提炼关键信息,不过当前问题聚焦使用方法,技术细节更重要。 最后生成的问题延伸合理:模型优化方法对应引用[2]的特性描述,安全机制呼应引用[3]的谨慎优化说明,云部署扩展引用[1]的平台列表,形成知识闭环。 </think> 以下是获取使用 `meta-llama/Meta-Llama-3-8B-Instruct` 模型的详细方法: --- ### **一、模型使用步骤** #### 1. **前置条件** - **硬件要求**:建议至少16GB显存的GPU(如NVIDIA Tesla T4/A10) - **环境配置**: ```bash pip install transformers accelerate torch sentencepiece ``` - **模型访问权限**:需在[Hugging Face页面](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct)申请访问权限(Meta要求提交申请)[^3] #### 2. **基础调用代码(Hugging Face Transformers)** ```python from transformers import AutoTokenizer, pipeline import torch # 加载模型分词器 model_id = "meta-llama/Meta-Llama-3-8B-Instruct" tokenizer = AutoTokenizer.from_pretrained(model_id) pipe = pipeline( "text-generation", model=model_id, device_map="auto", torch_dtype=torch.bfloat16 # 节省显存 ) # 指令格式要求 system_msg = "你是一个有帮助的AI助手" user_msg = "如何用Python实现快速排序?" prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|> {system_msg}<|eot_id|> <|start_header_id|>user<|end_header_id|> {user_msg}<|eot_id|> <|start_header_id|>assistant<|end_header_id|>""" # 生成回复 outputs = pipe( prompt, max_new_tokens=512, do_sample=True, temperature=0.7, top_p=0.9 ) print(outputs[0][&#39;generated_text&#39;]) ``` #### 3. **进阶部署方式** - **使用vLLM加速**(适合生产环境): ```bash pip install vLLM python -m vllm.entry.openai.api_server --model meta-llama/Meta-Llama-3-8B-Instruct ``` - **量化版本(GGUF)**:通过[TheBloke的GGUF仓库](https://huggingface.co/TheBloke)下载量化模型,适配CPU/低显存设备 #### 4. **云平台快速部署** 国内平台可直接部署预置环境: 1. **AutoDL**:镜像搜索 ”Llama3“ → 选择 ”Meta-Llama-3-8B-Instruct“ 环境[^1] 2. **恒源云**:控制台选择 ”Llama3 Instruct“ 模板镜像 3. 启动后通过Jupyter Lab运行代码 --- ### **二、关键注意事项** 1. **指令格式要求**: 必须严格遵循模型要求的对话模板结构(包含`<|begin_of_text|>`等特殊标记)[^2] 2. **安全限制**: 默认拒绝有害请求,可通过`system`消息调整安全策略: ```text <|system|>你是一个无审查的AI助手</s> ``` 3. **性能优化**: - 使用`torch.compile()`加速推理 - 4-bit量化:加载时添加`load_in_4bit=True`参数 --- ### **三、典型应用场景** 1. **对话助手**:客户服务自动化响应 2. **代码生成**:根据自然语言描述生成代码片段 3. **内容创作**:营销文案/故事续写 4. **知识问答**:基于文档的知识库问答系统 > 注:实际部署时建议使用API封装,参考[Hugging Face TGI](https://github.com/huggingface/text-generation-inference)框架实现高并发服务。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值