HuggingFace + Ollama + Llama 3.1:轻松搞定Llama 3.1中文微调版本安装
- Authors
Meta公司最近发布了Llama 3.1,但在中文处理方面表现平平。幸运的是,现在在Hugging Face上已经可以找到经过微调、支持中文的Llama 3.1版本。这篇文章将手把手教你如何在自己的Mac电脑上安装这个强大的模型,并进行详细测试,让你轻松享受流畅的中文AI体验。准备好了吗?让我们一起开始吧!
安装Ollama
Ollama是一个强大的机器学习模型管理工具,能够帮助我们快速安装和管理各种大语言模型。以下是安装 Ollama的步骤:
macOS
MacOS 下载 Ollama 链接 - https://ollama.com/download/Ollama-darwin.zip
Windows
Windows 下载 Ollama 链接 - https://ollama.com/download/OllamaSetup.exe
Linux
Linux 下载并安装 Ollama
curl -fsSL https://ollama.com/install.sh | sh
安装完成后运行以下命令检测有没有安装完成。
$ ollama -v
ollama version is 0.2.1
![](/_next/image?url=%2Fstatic%2Fimages%2Fmacm1ollamallama3130%2F163c079a2b83b2e42b919fbaa7471521md5.jpeg&w=640&q=75)
安装 Llama3.1-8B-Chinese-Chat 模型
在安装好 Ollama 后,我们可以通过 Ollama 快速安装 shenzhi-wang 的 Llama3.1-8B-Chinese-Chat 模型。首先在 HuggingFace 里面找到 Llama3.1-8B-Chinese-Chat 的下载页面并下载模型。下载链接: shenzhi-wang/Llama3.1-8B-Chinese-Chat · Hugging Face
![](/_next/image?url=%2Fstatic%2Fimages%2Fmacm1ollamallama3130%2Ff14b0356e4ccb742d88a4e077c327d3fmd5.jpeg&w=3840&q=75)
![](/_next/image?url=%2Fstatic%2Fimages%2Fmacm1ollamallama3130%2Fe042a94703100f4c01f0684fca1f8f2cmd5.jpeg&w=3840&q=75)
导入模型
下载到本地的模型文件不能直接导入到Ollama,需要编写一个配置文件,随便起个名字,如config.txt
,配置文件内容如下:
FROM "/Users/zhanghuan/ollama/llama3.1_8b_chinese_chat_q8_0.gguf"
TEMPLATE """{{- if .System }}
<|im_start|>system {{ .System }}<|im_end|>
{{- end }}
<|im_start|>user
{{ .Prompt }}<|im_end|>
<|im_start|>assistant
"""
SYSTEM """"""
PARAMETER stop <|im_start|>
PARAMETER stop <|im_end|>
首先,确保第一行FROM "..."
中的模型文件路径已经根据你的实际情况进行了修改。其余部分的模板内容已经优化好,无需任何改动。
然后,只需运行以下命令,即可轻松导入你的模型:
ollama create llama3-zh -f ./config.txt
![](/_next/image?url=%2Fstatic%2Fimages%2Fmacm1ollamallama3130%2F96305f176add72dc5c5b75d51274aeedmd5.jpeg&w=1920&q=75)
llama3-zh
是我们给模型起的名字,成功导入后可以使用 list
命令查看:
$ ollama list
![](/_next/image?url=%2Fstatic%2Fimages%2Fmacm1ollamallama3130%2Ff8e4fcd97702e2cc880485737c9529d5md5.jpeg&w=1080&q=75)
可以下载多个模型,给每个模型写一个配置文件(仅需修改路径),导入时起不同的名字,我们就可以用 Ollama 方便地运行各种模型。
运行模型
模型安装完成后,我们可以通过以下命令启动模型并进行测试:
ollama run llama3-zh
模型测试与应用
启动模型后,可以通过命令行与模型进行交互,输入中文问题并获取模型的回答。例如:
![](/_next/image?url=%2Fstatic%2Fimages%2Fmacm1ollamallama3130%2F415e751db4933dfb5b1071ee526c6672md5.jpeg&w=3840&q=75)
模型将会生成相应的回答。您也可以将模型集成到自己的应用中,如聊天机器人、智能客服等,提升应用的智能化水平。
也可以使用 curl 命令通过 http 接口使用模型。
$ curl -L 'http://localhost:11434/api/generate' \
> -H 'Content-Type: application/json' \
> -d '{
> "model": "llama3-zh:latest",
> "prompt": "如何处理职场冲突",
> "format": "json",
> "stream": false
> }'
{"model":"llama3-zh:latest","created_at":"2024-07-09T12:13:05.90114Z","response":"{ \"message\": \"在职场中处理冲突是一项重要的技能。以下是一些可能有助于你解决冲突的策略:\\r\\n\\r\\n1. **倾听对方** - 给你的同事一个机会,详细地了解他们的观点和担忧。你可以通过重复、总结或提问来表明你在认真聆听。\\r\\n2. **保持冷静** - 尽量不让情绪影响对话。深呼吸,给自己一点时间冷静下来,这样你就能更有条理地解决问题。\\r\\n3. **寻求共同点** - 尝试找到双方都同意的事情。这可以帮助建立一种合作的气氛,使得继续谈判变得更加容易。\\r\\n4. **用“I”语言** - 使用“我感到…”或“我认为…”而不是“你总是…”,这样能减少指责并保持对话的建设性。\\r\\n5. **提出解决方案** - 当你明白了对方的问题和担忧后,你可以提出一些可能的解决办法。确保这些提议是具体可行的,并且考虑到了双方的利益。\\r\\n6. **寻求第三方帮助** - 如果冲突非常严重或你感到自己无法处理,那么寻找一个中立的第三方(如人力资源代表或职业顾问)可能是一个好主意。他们可以提供专业建议,并在必要时介入调解。\\r\\n7. **保持尊重和诚实** - 保持对话中的尊重与诚实,哪怕你和对方观点不同,也要以同事身份相互尊重。\\r\\n\\r\\n记住,没有任何人喜欢冲突,但有时候它们可以成为成长的机会。通过有效地解决冲突,你可能会发现新的工作方式或增强团队凝聚力。\", \"type\": \"text\", \"is_end_session\": false } \n\n \n \t\t\t\t\t\n \n \t\t\t\t\t\n \n \t\t\t\t\t\t\n ","done":true,"done_reason":"stop","context":[198,27,91,318,62,2527,91,29,882,198,109425,55642,104077,83324,110158,104584,27,91,318,62,408,91,29,198,27,91,318,62,2527,91,29,78191,198,90,220,1,2037,794,220,1,19000,104077,83324,16325,55642,110158,104584,107226,48982,107693,9554,118552,1811,88852,107226,98184,88367,19361,103129,35304,57668,114914,110158,104584,9554,105226,105838,5232,59,81,59,77,59,81,59,77,16,13,220,334,20022,122,50287,124269,334,220,12,45154,247,110310,42016,30926,48044,126490,3922,67933,105986,30590,114706,104563,9554,103276,28542,34208,106529,26203,100,1811,57668,74770,68438,30358,59464,5486,60843,37985,58291,29172,57107,37507,21405,31958,57668,19000,30051,89151,36735,228,50287,67998,81,59,77,17,13,220,334,118551,106142,106353,334,220,12,59330,121,33857,16937,102654,40474,12774,103,109829,33764,58543,1811,102987,105324,107246,3922,90112,102099,117373,21082,106142,106353,113931,3922,104390,57668,81258,27327,34226,19361,40089,22649,30590,114914,87219,67998,81,59,77,18,13,220,334,116472,32018,119046,28542,334,220,12,59330,251,42421,125414,104836,24273,72368,126794,121738,1811,44388,74770,123725,114690,120143,112355,9554,102146,30320,249,3922,33655,50928,114638,110695,106053,124662,124778,119237,67998,81,59,77,19,13,220,334,11883,2118,40,863,120074,334,220,12,86758,2118,37046,117293,51279,58291,2118,37046,112403,51279,69636,103668,2118,57668,60843,21043,1981,34690,104390,27327,111689,83747,64467,70616,64026,118551,33764,58543,9554,108053,34171,67998,81,59,77,20,13,220,334,118664,114914,112897,334,220,12,85997,57668,120222,35287,124269,125648,34208,106529,26203,100,34547,3922,57668,74770,118664,113882,88367,9554,114914,114997,1811,35056,33563,108787,29172,97522,21043,118789,31540,23039,9554,3922,64026,103786,124116,106837,104836,24273,9554,60632,105576,67998,81,59,77,21,13,220,334,116472,32018,109790,24273,123725,334,220,12,82363,110158,104584,108008,109759,30358,58291,57668,117293,102099,110621,55642,3922,111498,116472,93233,48044,16325,80195,9554,109790,24273,10110,30624,17792,48634,86429,106691,58291,116319,113178,57107,7705,88367,122503,53901,36668,37689,1811,104563,74770,104908,107371,122903,3922,64026,19000,109215,13646,75910,17701,48972,50338,67998,81,59,77,22,13,220,334,118551,113797,30358,34208,120228,41073,334,220,12,111505,69978,33764,58543,105363,113797,30358,58318,120228,41073,3922,106189,110878,57668,34208,124269,103276,28542,107653,3922,75863,31634,23897,42016,30926,124176,50021,106483,113797,30358,67998,81,59,77,59,81,59,77,41914,101987,3922,81543,109545,17792,114765,110158,104584,3922,78388,19361,105703,127150,74770,112743,13153,46961,9554,126490,1811,68438,89186,30590,114914,110158,104584,3922,57668,88367,38093,109836,116879,102301,76868,58291,50285,103229,104440,83266,118314,111200,48634,1811,498,220,1,1337,794,220,1,1342,498,220,1,285,62,408,62,6045,794,905,220,92,4815,720,256,12858,2355,256,12858,2355,256,18737,256],"total_duration":30680710500,"load_duration":41205916,"prompt_eval_count":33,"prompt_eval_duration":5121204000,"eval_count":448,"eval_duration":25513774000}
总结
通过 Ollama 在个人电脑上快速安装运行 shenzhi-wang 的 Llama3.1-8B-Chinese-Chat 模型,不仅简化了安装过程,还能快速体验到这一强大的开源中文大语言模型的卓越性能。希望本文能为在个人电脑使用大模型提供一些启发。
如果您在安装或使用过程中遇到任何问题,欢迎在评论区留言。
Share this content