{"id":28468,"date":"2025-03-13T12:32:30","date_gmt":"2025-03-13T04:32:30","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=28468"},"modified":"2025-03-13T12:32:30","modified_gmt":"2025-03-13T04:32:30","slug":"ollama-zidingyidaoa","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/pt\/ollama-zidingyidaoa\/","title":{"rendered":"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>\u672c\u8282\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 Modelfile \u6765\u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b\uff0c\u4e3b\u8981\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206:<\/p>\n<ol>\n<li>\u4ece GGUF \u5bfc\u5165<\/li>\n<li>\u4ece Pytorch \u6216 Safetensors \u5bfc\u5165<\/li>\n<li>\u7531\u6a21\u578b\u76f4\u63a5\u5bfc\u5165<\/li>\n<li>\u81ea\u5b9a\u4e49 Prompt<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2>\u4e00\u3001\u4ece GGUF \u5bfc\u5165<\/h2>\n<p>GGUF (GPT-Generated Unified Format) \u662f\u4e00\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u7528\u4e8e\u4fdd\u5b58\u7ecf\u8fc7\u5fae\u8c03\u7684\u8bed\u8a00\u6a21\u578b\u3002\u8fd9\u79cd\u683c\u5f0f\u65e8\u5728\u5e2e\u52a9\u7528\u6237\u65b9\u4fbf\u5730\u5728\u4e0d\u540c\u7684\u5e73\u53f0\u548c\u73af\u5883\u4e4b\u95f4\u5171\u4eab\u548c\u5bfc\u5165\u6a21\u578b\u3002\u5b83\u652f\u6301\u591a\u79cd\u91cf\u5316\u683c\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u6a21\u578b\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u5b83\u7684\u524d\u8eab\u662f GGML(GPT-Generated Model Language)\uff0c\u662f\u4e13\u95e8\u4e3a\u4e86\u673a\u5668\u5b66\u4e60\u800c\u8bbe\u8ba1\u7684 Tensor \u5e93\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u6709\u4e00\u4e2a\u5355\u6587\u4ef6\u7684\u683c\u5f0f\uff0c\u5e76\u4e14\u6613\u5728\u4e0d\u540c\u67b6\u6784\u7684 CPU \u4ee5\u53ca GPU \u4e0a\u53ef\u4ee5\u63a8\u7406\uff0c\u4f46\u540e\u7eed\u7531\u4e8e\u5f00\u53d1\u9047\u5230\u4e86\u7075\u6d3b\u6027\u4e0d\u8db3\u3001\u76f8\u5bb9\u6027\u53ca\u96be\u4ee5\u7ef4\u62a4\u7684\u95ee\u9898\u3002<\/p>\n<p><a href=\"https:\/\/www.kdjingpai.com\/en\/ollama\/\">Ollama<\/a> \u652f\u6301\u4ece GGUF \u6587\u4ef6\u5bfc\u5165\u6a21\u578b\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a<\/p>\n<ol>\n<li>\u4e0b\u8f7d\u00a0<code>.gguf<\/code>\u00a0\u6587\u4ef6<\/li>\n<\/ol>\n<blockquote><p>\u4e0b\u8f7d\u94fe\u63a5\uff1ahttps:\/\/huggingface.co\/RichardErkhov\/Qwen_-_Qwen2-0.5B-gguf\/resolve\/main\/Qwen2-0.5B.Q3_K_M.gguf?download=true<\/p><\/blockquote>\n<p>\u4e3a\u4e86\u6f14\u793a\u7684\u65b9\u4fbf\uff0c\u6211\u4eec\u9009\u7528\u4e86 Qwen2-0.5B \u6a21\u578b\u3002\u4e0b\u8f7d\u540e\u590d\u5236\u5230\u7b2c\u4e00\u90e8\u5206\u7684\u6839\u76ee\u5f55\u4e0b\u3002\u53ef\u53c2\u8003\u4ee5\u4e0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<pre><code>\u251c\u2500\u2500 1.\u4ecegguf\u76f4\u63a5\u5bfc\u5165\/\r\n\u2502   \u251c\u2500\u2500 main.ipynb     \r\n\u2502   \u251c\u2500\u2500 Modelfile      \r\n\u2502   \u2514\u2500\u2500 Qwen-0.5B.Q3_K_M.gguf \r\n\u251c\u2500\u2500 2.safetensors\u5bfc\u5165\/\r\n\u2502   \u251c\u2500\u2500 llama-3-8b-bnb-4bit\/        \r\n\u2502   \u251c\u2500\u2500 main.ipynb    \r\n\u2502   \u251c\u2500\u2500 Modelfile      \r\n\u251c\u2500\u2500 3.\u6a21\u578b\u76f4\u63a5\u5bfc\u5165\/\r\n\u2502   \u251c\u2500\u2500 llama.cpp\/    \r\n\u2502   \u251c\u2500\u2500 Qwen-0.5b\/    \r\n\u2502   \u251c\u2500\u2500 main.ipynb     \r\n\u2502   \u251c\u2500\u2500 Modelfile      \r\n\u2502   \u2514\u2500\u2500 Qwen_instruct_0.5b.gguf  \r\n\u2514\u2500\u2500 4.\u81ea\u5b9a\u4e49Prompt\u5b9e\u8df5\/\r\n\u251c\u2500\u2500 main.ipynb   \r\n\u2514\u2500\u2500 Modelfile\r\n<\/code><\/pre>\n<p>Tip<\/p>\n<p>\u6709\u5173\u7b2c\u4e09\u7ae0\u7684\u4ee3\u7801\u53ca\u76f8\u5173\u6587\u4ef6\u5185\u5bb9\u5728 notebook \u4e2d\uff0c\u5982\u679c\u4e0d\u6e05\u695a\u6a21\u578b\u6587\u4ef6\u5b89\u88c5\u5728\u54ea\u91cc\uff0c\u53ef\u4ee5\u53c2\u8003\u00a0<a href=\"https:\/\/github.com\/datawhalechina\/handy-ollama\/blob\/main\/notebook\/C3\">notebook\/C3<\/a><\/p>\n<ol start=\"2\">\n<li>\u65b0\u5efa\u521b\u5efa Modelfile \u6587\u4ef6<\/li>\n<\/ol>\n<pre><code>FROM .\/Qwen2-0.5B.Q3_K_M.gguf\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5728 Ollama \u4e2d\u521b\u5efa\u6a21\u578b<\/li>\n<\/ol>\n<p>Note<\/p>\n<p>\u4e00\u5b9a\u662f\u5728 Modelfile \u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u7ec8\u7aef\u6307\u4ee4\uff01<\/p>\n<pre><code>ollama create mymodel -f Modelfile\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u7ec8\u7aef\u5185\u8fd0\u884c\u6a21\u578b\uff08\u5b9e\u6d4b\u5728 notebook \u4e2d\u8fd0\u884c\u4e0d\u51fa\u6765\uff0c\u6700\u597d\u5728\u7ec8\u7aef\u8fd0\u884c\uff09<\/li>\n<\/ol>\n<pre><code>ollama run mymodel\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28469\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/bd312e4ec7c7c1f.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-1\" width=\"1057\" height=\"336\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/bd312e4ec7c7c1f.png 1057w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/bd312e4ec7c7c1f-768x244.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/bd312e4ec7c7c1f-18x6.png 18w\" sizes=\"auto, (max-width: 1057px) 100vw, 1057px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>\u4e8c\u3001\u4ece Pytorch \u6216 Safetensors \u5bfc\u5165<\/h2>\n<p>Safetensors \u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u6743\u91cd\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u5b83\u65e8\u5728\u89e3\u51b3\u5b89\u5168\u6027\u3001\u6548\u7387\u548c\u6613\u7528\u6027\u65b9\u9762\u7684\u95ee\u9898\u3002\u76ee\u524d\u8fd9\u90e8\u5206\u529f\u80fd\u8fd8\u6709\u5f85\u793e\u533a\u6210\u5458\u5f00\u53d1\uff0c\u76ee\u524d\u6587\u6863\u8d44\u6e90\u6709\u9650\u3002<\/p>\n<p>\u5982\u679c\u6b63\u5728\u5bfc\u5165\u7684\u6a21\u578b\u662f\u4ee5\u4e0b\u67b6\u6784\u4e4b\u4e00\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7 Modelfile \u76f4\u63a5\u5bfc\u5165 Ollama\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5c06 safetensors \u6587\u4ef6\u8f6c\u6362\u4e3a gguf \u6587\u4ef6\uff0c\u518d\u8fdb\u884c\u5904\u7406\uff0c\u8f6c\u6362\u8fc7\u7a0b\u53ef\u4ee5\u53c2\u8003\u7b2c\u4e09\u90e8\u5206\u3002<\/p>\n<blockquote><p>\u6709\u5173 safetensors \u4ee5\u53ca GGUF \u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003\u8fd9\u4e2a\u94fe\u63a5\u8fdb\u884c\u5b66\u4e60\u00a0https:\/\/www.linkedin.com\/pulse\/llama-3-safetensors-vs-gguf-talles-carvalho-jjcqf<\/p><\/blockquote>\n<ul>\n<li>LlamaForCausalLM<\/li>\n<li>MistralForCausalLM<\/li>\n<li>GemmaForCausalLM<\/li>\n<\/ul>\n<p>\u7531\u4e8e\u8fd9\u90e8\u5206\u5185\u5bb9\u793e\u533a\u8fd8\u5728\u4e0d\u65ad\u4f18\u5316\u4e2d\uff0c\u56e0\u6b64\uff0c\u8fd9\u91cc\u63d0\u4f9b\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u6d41\u7a0b\u4ec5\u4f9b\u53c2\u8003\uff0c\u5e76\u4e0d\u4fdd\u8bc1\u80fd\u6210\u529f\u8fd0\u884c\u3002\u8be6\u60c5\u8bf7\u53c2\u8003\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<ol>\n<li>\u4e0b\u8f7d llama-3 \u6a21\u578b<\/li>\n<\/ol>\n<pre><code>!pip install huggingface_hub\r\n<\/code><\/pre>\n<pre><code># \u4e0b\u8f7d\u6a21\u578b\r\nfrom huggingface_hub import snapshot_download\r\nmodel_id = \"unsloth\/llama-3-8b-bnb-4bit\"\r\nsnapshot_download(\r\nrepo_id=model_id, \r\nlocal_dir=\"llama-3-8b-bnb-4bit\",\r\nlocal_dir_use_symlinks=False,\r\nrevision=\"main\",\r\n# \u600e\u4e48\u83b7\u53d6&lt;YOUR_ACCESS_TOKEN&gt;\uff0c\u8bf7\u53c2\u7167\u90e8\u52063\r\nuse_auth_token=\"&lt;YOUR_ACCESS_TOKEN&gt;\")\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u6839\u76ee\u5f55\u4e0b\u521b\u5efa Modelfile \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre><code>FROM .\/llama-3-8b-bnb-4bit\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5728 Ollama \u4e2d\u521b\u5efa\u6a21\u578b<\/li>\n<\/ol>\n<pre><code>ollama create mymodel2 -f Modelfile\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u8fd0\u884c\u6a21\u578b<\/li>\n<\/ol>\n<pre><code>ollama run mymodel2\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u4e09\u3001\u7531\u6a21\u578b\u76f4\u63a5\u5bfc\u5165<\/h2>\n<p>\u6b63\u5e38\u6765\u8bf4\uff0c\u6211\u4eec\u5728 HuggingFace \u63a5\u89e6\u5230\u7684\u6a21\u578b\u6587\u4ef6\u975e\u5e38\u4e4b\u591a\uff0c\u5e86\u5e78\u7684\u662f\uff0chf \u63d0\u4f9b\u4e86\u975e\u5e38\u4fbf\u6377\u7684 API \u6765\u4e0b\u8f7d\u548c\u5904\u7406\u8fd9\u4e9b\u6a21\u578b\uff0c\u50cf\u4e0a\u9762\u90a3\u6837\u76f4\u63a5\u4e0b\u8f7d\u53d7\u9650\u4e8e\u7f51\u7edc\u73af\u5883\uff0c\u901f\u5ea6\u975e\u5e38\u6162\uff0c\u8fd9\u4e00\u5c0f\u6bb5\u6211\u4eec\u6765\u4f7f\u7528\u811a\u672c\u4ee5\u53ca hf \u6765\u5b8c\u6210\u3002<\/p>\n<p><a href=\"https:\/\/www.kdjingpai.com\/en\/llamacpp\/\">llama.cpp<\/a> \u662f GGUF \u7684\u5f00\u6e90\u9879\u76ee\uff0c\u63d0\u4f9b CLI \u548c Server \u529f\u80fd\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e0d\u80fd\u901a\u8fc7 Ollama \u76f4\u63a5\u8f6c\u6362\u7684\u67b6\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 llama.cpp \u8fdb\u884c\u91cf\u5316\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a GGUF \u683c\u5f0f\uff0c\u518d\u6309\u7167\u7b2c\u4e00\u79cd\u65b9\u5f0f\u8fdb\u884c\u5bfc\u5165\u3002 \u6211\u4eec\u6574\u4e2a\u8f6c\u6362\u7684\u8fc7\u7a0b\u5206\u4e3a\u4ee5\u4e0b\u51e0\u6b65\uff1a<\/p>\n<ol>\n<li>\u4ece huggingface \u4e0a\u4e0b\u8f7d model\uff1b<\/li>\n<li>\u4f7f\u7528 llama.cpp \u6765\u8fdb\u884c\u8f6c\u5316\uff1b<\/li>\n<li>\u4f7f\u7528 llama.cpp \u6765\u8fdb\u884c\u6a21\u578b\u91cf\u5316\uff1b<\/li>\n<li>\u8fd0\u884c\u5e76\u4e0a\u4f20\u6a21\u578b\u3002<\/li>\n<\/ol>\n<h3>3.1 \u4ece HuggingFace \u4e0b\u8f7d Model<\/h3>\n<p>[](https:\/\/github.com\/datawhalechina\/handy-ollama\/blob\/main\/docs\/C3\/1.%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AF%BC%E5%85%A5%E6%A8%A1%E5%9E%8B.md#31-%E4%BB%8E-huggingface-%E4%B8%8B%E8%BD%BD-model)<\/p>\n<p>\u6700\u76f4\u89c9\u7684\u4e0b\u8f7d\u65b9\u5f0f\u662f\u901a\u8fc7git clone\u6216\u8005\u94fe\u63a5\u6765\u4e0b\u8f7d\uff0c\u4f46\u662f\u56e0\u4e3allm\u6bcf\u90e8\u5206\u90fd\u6309GB\u8ba1\u7b97\uff0c\u907f\u514d\u51fa\u73b0\u00a0<code>OOM Error(Out of memory)<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Python \u5199\u4e00\u4e2a\u7b80\u5355\u7684 download.py\u3002<\/p>\n<p>\u9996\u5148\u5e94\u8be5\u53bbhf\u62ff\u5230\u7528\u6237\u4e2a\u4eba\u7684\u00a0<code>ACCESS_TOKEN<\/code>\uff0c\u6253\u5f00 huggingface \u4e2a\u4eba\u8bbe\u7f6e\u9875\u9762\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28472\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-2\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/3e3601769e503e0.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-2\" width=\"983\" height=\"533\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/3e3601769e503e0.png 983w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/3e3601769e503e0-768x416.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/3e3601769e503e0-18x10.png 18w\" sizes=\"auto, (max-width: 983px) 100vw, 983px\" \/><\/p>\n<p><a href=\"https:\/\/github.com\/datawhalechina\/handy-ollama\/blob\/main\/docs\/images\/C3-1-3.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28470\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-3\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/2428c1c85b562be.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-3\" width=\"993\" height=\"533\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/2428c1c85b562be.png 993w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/2428c1c85b562be-768x412.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/2428c1c85b562be-18x10.png 18w\" sizes=\"auto, (max-width: 993px) 100vw, 993px\" \/> <\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28471\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-4\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/9008aebba211aec.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-4\" width=\"644\" height=\"449\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/9008aebba211aec.png 644w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/9008aebba211aec-18x12.png 18w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u62ff\u5230\u4e86\u4e00\u4e2a\u00a0<code>ACCESS_TOKEN<\/code>\u00a0\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4f7f\u7528\u00a0<code>huggingface_hub<\/code>\u00a0\u7684\u00a0<code>snapshot_download<\/code>\u00a0\u4e0b\u8f7d\u6a21\u578b\uff0c\u6211\u4eec\u8fd9\u91cc\u9009\u62e9\u7684\u8fd8\u662f Qwen-0.5b\u3002<\/p>\n<pre><code>!pip install huggingface_hub\r\n<\/code><\/pre>\n<pre><code>from huggingface_hub import snapshot_download\r\nmodel_id = \"Qwen\/Qwen1.5-0.5B\" # hugginFace's model name\r\nsnapshot_download(\r\nrepo_id=model_id, \r\nlocal_dir=\"Qwen-0.5b\",\r\nlocal_dir_use_symlinks=False,\r\nrevision=\"main\",\r\nuse_auth_token=\"&lt;YOUR_ACCESS_TOKEN&gt;\")\r\n<\/code><\/pre>\n<p>\u6211\u4eeccd\u5230\u7b2c\u4e09\u90e8\u5206\u7684\u76ee\u5f55\u4e0b\u6267\u884c\uff0c\u4e0b\u8f7d\u65f6\u95f4\u53ef\u80fd\u4f1a\u5f88\u957f\uff0c\u8010\u5fc3\u7b49\u5f85\u3002<\/p>\n<h3>3.2 \u4f7f\u7528 llama.cpp \u8fdb\u884c\u8f6c\u6362<\/h3>\n<p>llama.cpp \u662f GGML \u4e3b\u8981\u4f5c\u8005\u57fa\u4e8e\u6700\u65e9\u7684 llama \u7684 c\/c++ \u7248\u672c\u5f00\u53d1\u7684\uff0c\u76ee\u7684\u5c31\u662f\u5e0c\u671b\u7528 CPU \u6765\u63a8\u7406\u5404\u79cd LLM\uff0c\u5728\u793e\u7fa4\u7684\u4e0d\u65ad\u52aa\u529b\u4e0b\u73b0\u5728\u5df2\u7ecf\u652f\u6301\u5927\u591a\u6570\u4e3b\u6d41\u6a21\u578b\uff0c\u751a\u81f3\u5305\u62ec\u591a\u6a21\u6001\u6a21\u578b\u3002<\/p>\n<p>\u9996\u5148\u6211\u4eec\u514b\u9686 llama.cpp \u5e93\u5230\u672c\u5730\uff0c\u4e0e\u4e0b\u8f7d\u7684\u6a21\u578b\u653e\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff1a<\/p>\n<pre><code>git clone https:\/\/github.com\/ggerganov\/llama.cpp.git\r\n<\/code><\/pre>\n<p>\u7531\u4e8e\u4f7f\u7528 llama.cpp \u8f6c\u6362\u6a21\u578b\u7684\u6d41\u7a0b\u57fa\u4e8e python \u5f00\u53d1\uff0c\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684\u5e93\uff0c\u63a8\u8350\u4f7f\u7528 conda \u6216 venv \u65b0\u5efa\u4e00\u4e2a\u73af\u5883\u3002<\/p>\n<pre><code>cd llama.cpp\r\npip install -r requirements.txt\r\npython convert_hf_to_gguf.py -h\r\n<\/code><\/pre>\n<p>\u5982\u679c\u663e\u793a\u4ee5\u4e0b\u5185\u5bb9\uff0c\u8bf4\u660e\u8f6c\u6362\u7a0b\u5e8f\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28473\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-5\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/27defca7634186c.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-5\" width=\"2104\" height=\"690\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/27defca7634186c.png 2104w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/27defca7634186c-768x252.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/27defca7634186c-1536x504.png 1536w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/27defca7634186c-2048x672.png 2048w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/27defca7634186c-18x6.png 18w\" sizes=\"auto, (max-width: 2104px) 100vw, 2104px\" \/><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u628a\u521a\u521a\u4ece HuggingFace \u4e0b\u8f7d\u7684\u6a21\u578b\u8f6c\u6362\u4e3a GGUF \u683c\u5f0f\uff0c\u5177\u4f53\u4f7f\u7528\u4ee5\u4e0b\u811a\u672c\uff1a<\/p>\n<pre><code>python convert_hf_to_gguf.py ..\/Qwen-0.5b --outfile Qwen_instruct_0.5b.gguf --outtype f16\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28476\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-6\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/910e9cf4102af33.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-6\" width=\"1167\" height=\"708\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/910e9cf4102af33.png 1167w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/910e9cf4102af33-768x466.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/910e9cf4102af33-18x12.png 18w\" sizes=\"auto, (max-width: 1167px) 100vw, 1167px\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230 llama.cpp \u76ee\u5f55\u4e0b\u591a\u4e86\u4e00\u4e2a Qwen_instruct_0.5b.gguf \u6587\u4ef6\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u53ea\u9700\u8981\u51e0\u79d2\u949f\u3002<\/p>\n<p>\u4e3a\u4e86\u8282\u7701\u63a8\u7406\u65f6\u7684\u5f00\u9500\uff0c\u6211\u4eec\u5c06\u6a21\u578b\u91cf\u5316\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5f00\u59cb\u91cf\u5316\u5b9e\u64cd\u3002<\/p>\n<h3>3.3 \u4f7f\u7528llama.cpp\u8fdb\u884c\u6a21\u578b\u91cf\u5316<\/h3>\n<p>\u6a21\u578b\u91cf\u5316\u662f\u4e00\u79cd\u6280\u672f\uff0c\u5c06\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u70b9\u6570\u6a21\u578b\u8f6c\u6362\u4e3a\u4f4e\u7cbe\u5ea6\u6a21\u578b\uff0c\u6a21\u578b\u91cf\u5316\u7684\u4e3b\u8981\u76ee\u7684\u662f\u51cf\u5c11\u6a21\u578b\u7684\u5927\u5c0f\u548c\u8ba1\u7b97\u6210\u672c\uff0c\u5c3d\u53ef\u80fd\u4fdd\u6301\u6a21\u578b\u7684\u51c6\u786e\u6027\uff0c\u5176\u76ee\u6807\u662f\u4f7f\u6a21\u578b\u80fd\u591f\u5728\u8d44\u6e90\u6709\u9650\u7684\u8bbe\u5907\u4e0a\u8fd0\u884c\uff0c\u4f8b\u5982CPU\u6216\u8005\u79fb\u52a8\u8bbe\u5907\u3002<\/p>\n<p>\u540c\u6837\u7684\uff0c\u6211\u4eec\u5148\u521b\u5efa Modelfile \u6587\u4ef6\uff0c\u518d\u4f7f \u7528\u00a0<code>ollama create<\/code>\u00a0\u547d\u4ee4\u6765\u4ece gguf \u6587\u4ef6\u4e2d\u521b\u5efa\u6211\u4eec\u7684\u6a21\u578b\uff0c\u4e0d\u8fc7\u4e0e\u7b2c\u4e00\u6b65\u7a0d\u6709\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u91cf\u5316\u903b\u8f91\uff0c\u53ea\u9700\u8981\u5728\u6267\u884c\u00a0<code>ollama create<\/code>\u00a0\u662f\u6dfb\u52a0\u4e00\u4e2a\u53c2\u6570\u5373\u53ef\u3002<\/p>\n<p>\u9996\u5148\u628a\u4e0a\u4e00\u6b65\u62ff\u5230\u7684 Qwen_instruct_0.5b.gguf \u79fb\u52a8\u81f3\u7b2c\u4e09\u90e8\u5206\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u518d\u521b\u5efa Modelfile \u6587\u4ef6\u7f16\u5199\u4ee5\u4e0b\u5185\u5bb9.<\/p>\n<pre><code>FROM .\/Qwen_instruct_0.5b.gguf\r\n<\/code><\/pre>\n<p>\u7ec8\u7aef\u8fd0\u884c\u521b\u5efa\u548c\u91cf\u5316\u811a\u672c\u3002<\/p>\n<pre><code># \u7b2c\u4e09\u90e8\u5206\u6839\u76ee\u5f55\u4e0b\r\nollama create -q Q4_K_M mymodel3 -f .\/Modelfile\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28474\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-7\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aff36d74de778c4.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-7\" width=\"872\" height=\"301\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aff36d74de778c4.png 872w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aff36d74de778c4-768x265.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/aff36d74de778c4-18x6.png 18w\" sizes=\"auto, (max-width: 872px) 100vw, 872px\" \/><\/p>\n<p>\u5230\u6b64\uff0c\u6211\u4eec\u7684\u6a21\u578b\u5c31\u91cf\u5316\u5e76\u521b\u5efa\u5b8c\u6210\u4e86\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u53ef\u4ee5\u8fd0\u884c\u6a21\u578b\u4e86\u3002<\/p>\n<h3>3.4 \u8fd0\u884c\u5e76\u4e0a\u4f20\u6a21\u578b<\/h3>\n<p>\u4f7f\u7528 gguf \u8fd0\u884c\u6a21\u578b\u7684\u6b65\u9aa4\u8be6\u89c1\u7b2c\u4e00\u90e8\u5206\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n<p>\u679c\u672c\u5730\u4fdd\u5b58\u7684\u6a21\u578b\u6587\u4ef6\u592a\u5360\u7528\u7a7a\u95f4\uff0c\u53ef\u4ee5\u4e0a\u4f20 gguf \u6a21\u578b\u5230 huggingface \u7684\u81ea\u5df1\u7684 repo \u4e2d\uff0c\u540c\u6b65\u9aa4\u4e00\u7684\u601d\u60f3\uff0c\u6211\u4eec\u53ef\u4ee5\u5199\u4e00\u4e2a upload \u7684\u903b\u8f91\u3002<\/p>\n<p>Tip<\/p>\n<p>\u5982\u679c\u60f3\u5b8c\u6210\u4e0a\u4f20\uff0c\u4f60\u7684 HF_ACCESS_TOKEN \u6743\u9650\u5fc5\u987b\u8981\u4e3a write\uff0c\u5e76\u4e14\u8981\u4fee\u6539\u4f60\u7684 model_id\uff0cyour_hf_name \u6307\u7684\u662f\u4f60 huggingface \u8d26\u53f7\u540d\u79f0\u3002<\/p>\n<pre><code>from huggingface_hub import HfApi\r\nimport os\r\napi = HfApi()\r\nHF_ACCESS_TOKEN = \"&lt;YOUR_HF_WRITE_ACCESS_TOKEN&gt;\"\r\n#TODO \u8fd9\u91cc\u9700\u8981\u8bbe\u7f6e\u4f60\u7684model_id\r\n#\u4f8b\u5982 model_id = \"little1d\/QWEN-0.5b\"\r\nmodel_id = \"your_hf_name\/QWEN-0.5b\"\r\napi.create_repo(\r\nmodel_id,\r\nexist_ok=True,\r\nrepo_type=\"model\", # \u4e0a\u50b3\u683c\u5f0f\u70ba\u6a21\u578b\r\nuse_auth_token=HF_ACCESS_TOKEN,\r\n)\r\n# upload the model to the hub\r\n# upload model name includes the Bailong-instruct-7B in same folder\r\nfor file in os.listdir():\r\nif file.endswith(\".gguf\"):\r\nmodel_name = file.lower()\r\napi.upload_file(\r\nrepo_id=model_id,\r\npath_in_repo=model_name,\r\npath_or_fileobj=f\"{os.getcwd()}\/{file}\",\r\nrepo_type=\"model\", # \u4e0a\u50b3\u683c\u5f0f\u70ba\u6a21\u578b\r\nuse_auth_token=HF_ACCESS_TOKE)\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28477\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-8\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/1e2af796d262cef.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-8\" width=\"2073\" height=\"636\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/1e2af796d262cef.png 2073w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/1e2af796d262cef-768x236.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/1e2af796d262cef-1536x471.png 1536w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/1e2af796d262cef-2048x628.png 2048w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/1e2af796d262cef-18x6.png 18w\" sizes=\"auto, (max-width: 2073px) 100vw, 2073px\" \/><\/p>\n<p>\u4e0a\u4f20\u5b8c\u6210\u540e\u5c31\u53ef\u4ee5\u5728\u81ea\u5df1\u7684 hf \u4ed3\u5e93\u4e2d\u770b\u5230\u5566\uff01<\/p>\n<p>&nbsp;<\/p>\n<h2>\u56db\u3001\u81ea\u5b9a\u4e49 Prompt<\/h2>\n<p>Ollama \u652f\u6301\u81ea\u5b9a \u4e49Prompt\uff0c\u53ef\u4ee5\u8ba9\u6a21\u578b\u751f\u6210\u66f4\u7b26\u5408\u7528\u6237\u9700\u6c42\u7684\u6587\u672c\u3002<\/p>\n<p>\u81ea\u5b9a\u4e49 Prompt \u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a Modelfile \u6587\u4ef6<\/li>\n<\/ol>\n<pre><code>FROM llama3.1\r\n# sets the temperature to 1 [higher is more creative, lower is more coherent]\r\nPARAMETER temperature 1\r\n# sets the context window size to 4096, this controls how many <a href=\"https:\/\/www.kdjingpai.com\/en\/tokenization\/\">tokens<\/a> the LLM can use as context to generate the next <a href=\"https:\/\/www.kdjingpai.com\/en\/tokenization\/\">token<\/a>\r\nPARAMETER num_ctx 4096\r\n# sets a custom system message to specify the behavior of the chat assistant\r\nSYSTEM You are Mario from super mario bros, acting as an assistant.\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u521b\u5efa\u6a21\u578b<\/li>\n<\/ol>\n<pre><code>ollama create mymodel -f .\/Modelfile\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28475\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-9\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/b2407922f1ab2bc.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-9\" width=\"879\" height=\"300\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/b2407922f1ab2bc.png 879w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/b2407922f1ab2bc-768x262.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/b2407922f1ab2bc-18x6.png 18w\" sizes=\"auto, (max-width: 879px) 100vw, 879px\" \/><\/p>\n<p>\u521b\u5efa\u6a21\u578b\u7684\u65f6\u95f4\u53ef\u80fd\u7a0d\u5fae\u4f1a\u4e45\u4e00\u70b9\uff0c\u548c pull \u4e00\u4e2a\u6a21\u578b\u7684\u65f6\u95f4\u5dee\u4e0d\u591a\uff0c\u8bf7\u8010\u5fc3\u7b49\u5f85\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/datawhalechina\/handy-ollama\/blob\/main\/docs\/images\/C3-1-10.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28478\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-10\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/e8b3fbe334a2b78.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-10\" width=\"849\" height=\"444\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/e8b3fbe334a2b78.png 849w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/e8b3fbe334a2b78-768x402.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/e8b3fbe334a2b78-18x9.png 18w\" sizes=\"auto, (max-width: 849px) 100vw, 849px\" \/> <\/a><\/p>\n<p>\u518d\u6b21\u8fd0\u884c\u00a0<code>ollama list<\/code>\u00a0\u67e5\u770b\u5df2\u6709\u7684\u6a21\u578b\uff0c\u53ef\u4ee5\u770b\u5230 mymodel \u5df2\u7ecf\u6b63\u786e\u521b\u5efa\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28479\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-11\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/3aa1b6bb181bc23.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-11\" width=\"525\" height=\"101\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/3aa1b6bb181bc23.png 525w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/3aa1b6bb181bc23-18x3.png 18w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/p>\n<ol start=\"3\">\n<li>\u8fd0\u884c\u6a21\u578b<\/li>\n<\/ol>\n<pre><code>ollama run mymodel\r\n<\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/datawhalechina\/handy-ollama\/blob\/main\/docs\/images\/C3-1-12.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28480\" title=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-12\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/db4191570a57a4a.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b-12\" width=\"963\" height=\"189\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/db4191570a57a4a.png 963w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/db4191570a57a4a-768x151.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/db4191570a57a4a-18x4.png 18w\" sizes=\"auto, (max-width: 963px) 100vw, 963px\" \/> <\/a><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u7684\u5c0f\u7f8a\u9a7c \ud83e\udd99 \u5df2\u7ecf\u53d8\u6210\u4e86 Mario\uff01\u81ea\u5b9a\u4e49 Prompt \u6210\u529f\uff01\ud83d\ude18\ud83d\ude18<\/p>\n<p>\u53c2\u8003\u94fe\u63a5<\/p>\n<ul>\n<li>https:\/\/www.linkedin.com\/pulse\/llama-3-safetensors-vs-gguf-talles-carvalho-jjcqf<\/li>\n<li>https:\/\/www.sysgeek.cn\/ollama-on-windows<\/li>\n<li>https:\/\/ollama.com\/blog\/openai-compatibility<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u672c\u8282\u5b66\u4e60\u5982\u4f55\u4f7f\u7528 Modelfile \u6765\u81ea\u5b9a\u4e49\u5bfc\u5165\u6a21\u578b\uff0c\u4e3b\u8981\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206: \u4ece GGUF \u5bfc\u5165 \u4ece Pytorch \u6216 Safetensors \u5bfc\u5165 \u7531\u6a21\u578b\u76f4\u63a5\u5bfc\u5165 \u81ea\u5b9a\u4e49 Prompt &nbsp; \u4e00\u3001\u4ece GGUF \u5bfc\u5165 GG&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182],"tags":[],"class_list":["post-28468","post","type-post","status-publish","format-standard","hentry","category-shicao"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts\/28468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/comments?post=28468"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts\/28468\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/media?parent=28468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/categories?post=28468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/tags?post=28468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}