{"id":48562,"date":"2024-05-09T09:43:24","date_gmt":"2024-05-09T01:43:24","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=48562"},"modified":"2024-05-10T17:18:08","modified_gmt":"2024-05-10T09:18:08","slug":"ollama-ru-he-zheng-que-dao-ru-mo-xing","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2024\/05\/09\/ollama-ru-he-zheng-que-dao-ru-mo-xing\/","title":{"rendered":"Ollama\u5982\u4f55\u6b63\u786e\u5bfc\u5165\u6a21\u578b"},"content":{"rendered":"<h1><a id=\"ollama%E5%A6%82%E4%BD%95%E6%AD%A3%E7%A1%AE%E5%AF%BC%E5%85%A5%E6%A8%A1%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Ollama\u5982\u4f55\u6b63\u786e\u5bfc\u5165\u6a21\u578b<\/h1>\n<p>\u4eca\u5929\u6211\u4eec\u4e00\u8d77\u6765\u770b\u5982\u4f55\u7ed9\u81ea\u5df1\u7684\u672c\u5730ollama\u5bfc\u5165GGUF\u3001PyTorch\u6216Safetensors\u6a21\u578b\u3002<\/p>\n<h2><a id=\"%E5%AF%BC%E5%85%A5gguf\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5bfc\u5165 GGUF<\/h2>\n<h3><a id=\"%E7%AC%AC%E4%B8%80%E6%AD%A5%EF%BC%9A%E7%BC%96%E5%86%99modelfile%E6%96%87%E4%BB%B6\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b2c\u4e00\u6b65\uff1a\u7f16\u5199Modelfile\u6587\u4ef6<\/h3>\n<p>\u9996\u5148\u521b\u5efa\u4e00\u4e2aModelfile\u3002Modelfile\u6587\u4ef6\u6628\u5929\u5df2\u7ecf\u8bb2\u8fc7\u662f\u6a21\u578b\u7684\u6a21\u7248\uff0c\u6307\u5b9a\u6743\u91cd\u3001\u53c2\u6570\u3001\u63d0\u793a\u6a21\u677f\u7b49\u3002<\/p>\n<pre><code class=\"language-dockerfile\">FROM .\/mistral-7b-v0.1.Q4_0.gguf\n<\/code><\/pre>\n<p>\u8bb8\u591a\u804a\u5929\u6a21\u578b\u9700\u8981\u4e00\u4e2a\u63d0\u793a\u6a21\u677f\uff0c\u4ee5\u4fbf\u6b63\u786e\u56de\u7b54\u3002\u53ef\u4ee5\u4f7f\u7528Modelfile\u4e2d\u7684template\u6307\u4ee4\u6307\u5b9a\u9ed8\u8ba4\u63d0\u793a\u6a21\u677f<\/p>\n<pre><code class=\"language-dockerfile\">FROM .\/mistral-7b-v0.1.Q4_0.gguf\nTEMPLATE \"[INST] {{ .Prompt }} [\/INST]\"\n<\/code><\/pre>\n<h3><a id=\"%E7%AC%AC%E4%BA%8C%E6%AD%A5%EF%BC%9A%E5%88%9B%E5%BB%BAollama%E6%A8%A1%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b2c\u4e8c\u6b65\uff1a\u521b\u5efaOllama\u6a21\u578b<\/h3>\n<p>\u4eceModelfile\u4e2d\u521b\u5efa\u4e00\u4e2a\u6a21\u578b<\/p>\n<pre><code class=\"language-dockerfile\">ollama create example -f Modelfile\n<\/code><\/pre>\n<p>\u7b2c\u4e8c\u6b65\uff1a\u8fd0\u884c\u4f60\u65b0\u5efa\u7684\u6a21\u578b<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u7528ollama run\u6d4b\u8bd5\u6a21\u578b<\/p>\n<pre><code class=\"language-sh\">ollama run example \"What is your favourite condiment?\"\n<\/code><\/pre>\n<h2><a id=\"%E5%AF%BC%E5%85%A5pytorch%E6%88%96-safetensors\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5bfc\u5165 PyTorch \u6216 Safetensors<\/h2>\n<p>\u4ecePyTorch\u548cSafetensors\u5bfc\u5165\u6bd4\u4eceGGUF\u5bfc\u5165\u8981\u957f\u3002\u4e3a\u4e86\u4f7f\u5b83\u4eec\u66f4\u5bb9\u6613\u5bfc\u5165\u548c\u4f7f\u7528\uff0c\u5b98\u65b9\u6b63\u5728\u8fdb\u884c\u6539\u8fdb\uff0c\u540e\u7eed\u63a8\u51fa\u6211\u4e5f\u4f1a\u7b2c\u4e00\u65f6\u95f4\u63a8\u8350\u7ed9\u5927\u5bb6\u3002<\/p>\n<h3><a id=\"%E6%AD%A5%E9%AA%A4\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u6b65\u9aa4<\/h3>\n<h3><a id=\"%E7%AC%AC%E4%B8%80%E6%AD%A5%EF%BC%9A%E5%85%8B%E9%9A%86ollamaollama%E4%BB%93%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b2c\u4e00\u6b65\uff1a\u514b\u9686ollama\/ollama\u4ed3\u5e93<\/h3>\n<pre><code class=\"language-sh\">git clone git@github.com:ollama\/ollama.git ollama\ncd ollama\n<\/code><\/pre>\n<p>\u7136\u540e\u83b7\u53d6\u5b83\u7684llama.cpp\u5b50\u6a21\u5757<\/p>\n<pre><code class=\"language-sh\">git submodule init\ngit submodule update llm\/llama.cpp\n<\/code><\/pre>\n<h3><a id=\"%E7%AC%AC%E4%BA%8C%E6%AD%A5%EF%BC%9A%E5%AE%89%E8%A3%85python%E4%BE%9D%E8%B5%96\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b2c\u4e8c\u6b65\uff1a\u5b89\u88c5python\u4f9d\u8d56<\/h3>\n<pre><code class=\"language-sh\">python3 -m venv llm\/llama.cpp\/.venv\nsource llm\/llama.cpp\/.venv\/bin\/activate\npip install -r llm\/llama.cpp\/requirements.txt\n<\/code><\/pre>\n<p>\u7136\u540e\u6784\u5efa <code>quantize<\/code> \u5de5\u5177<\/p>\n<pre><code class=\"language-sh\">make -C llm\/llama.cpp quantize\n<\/code><\/pre>\n<h2><a id=\"%E5%85%8B%E9%9A%86huggingface%E5%AD%98%E5%82%A8%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u514b\u9686HuggingFace\u5b58\u50a8\u5e93<\/h2>\n<p>\u5982\u679c\u6a21\u578b\u5f53\u524d\u6258\u7ba1\u5728HuggingFace\u5b58\u50a8\u5e93\u4e2d\uff0c\u9996\u5148\u514b\u9686\u8be5\u5b58\u50a8\u5e93\u4ee5\u4e0b\u8f7d\u539f\u59cb\u6a21\u578b\u3002\u5b89\u88c5Git LFS\uff0c\u786e\u8ba4\u5b83\u5df2\u7ecf\u5b89\u88c5\uff0c\u7136\u540e\u514b\u9686\u6a21\u578b\u7684\u5b58\u50a8\u5e93<\/p>\n<pre><code class=\"language-sh\">git lfs install\ngit clone https:\/\/huggingface.co\/mistralai\/Mistral-7B-Instruct-v0.1 model\n<\/code><\/pre>\n<h3><a id=\"%E8%BD%AC%E6%8D%A2%E6%A8%A1%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8f6c\u6362\u6a21\u578b<\/h3>\n<p>\u6ce8\u610f\uff1a\u67d0\u4e9b\u6a21\u578b\u67b6\u6784\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u8f6c\u6362\u811a\u672c\u3002\u4f8b\u5982\uff0cQwen\u6a21\u578b\u9700\u8981\u8fd0\u884cconvert-hf-to-gguf.py\u800c\u4e0d\u662fconvert.py\u3002<\/p>\n<pre><code class=\"language-sh\">python llm\/llama.cpp\/convert.py .\/model --outtype f16 --outfile converted.bin\n<\/code><\/pre>\n<h3><a id=\"%E8%8E%B7%E5%8F%96quantize%E6%A8%A1%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u83b7\u53d6<code>quantize<\/code>\u6a21\u578b<\/h3>\n<pre><code class=\"language-sh\">llm\/llama.cpp\/quantize converted.bin quantized.bin q4_0\n<\/code><\/pre>\n<p>\u5728\u6709\u4e86quantize\u6a21\u578b\u4e4b\u540e\u5c31\u53ef\u4ee5\u6309\u7167\u4e0a\u9762\u7684\u6b65\u9aa4\u6b63\u5e38\u7684\u53bb\u6784\u5efa\u4e86\u3002<\/p>\n<h3><a id=\"%E7%AC%AC%E4%B8%80%E6%AD%A5%EF%BC%9A%E7%BC%96%E5%86%99modelfile%E6%96%87%E4%BB%B6\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b2c\u4e00\u6b65\uff1a\u7f16\u5199Modelfile\u6587\u4ef6<\/h3>\n<pre><code class=\"language-dockerfile\">FROM quantized.bin\nTEMPLATE \"[INST] {{ .Prompt }} [\/INST]\"\n<\/code><\/pre>\n<h3><a id=\"%E7%AC%AC%E4%BA%8C%E6%AD%A5%EF%BC%9A%E6%9E%84%E5%BB%BAollama%E6%A8%A1%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b2c\u4e8c\u6b65\uff1a\u6784\u5efaOllama\u6a21\u578b<\/h3>\n<pre><code class=\"language-sh\">ollama create example -f Modelfile\n<\/code><\/pre>\n<h3><a id=\"%E7%AC%AC%E4%B8%89%E6%AD%A5%EF%BC%9A%E8%BF%90%E8%A1%8Collama%E6%A8%A1%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b2c\u4e09\u6b65\uff1a\u8fd0\u884cOllama\u6a21\u578b<\/h3>\n<pre><code class=\"language-sh\">ollama run example \"What is your favourite condiment?\"\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ollama\u5982\u4f55\u6b63\u786e\u5bfc\u5165\u6a21\u578b \u4eca\u5929\u6211\u4eec\u4e00\u8d77\u6765\u770b\u5982\u4f55\u7ed9\u81ea\u5df1\u7684\u672c\u5730ollama\u5bfc\u5165GGUF\u3001PyTorch\u6216Saf [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-48562","post","type-post","status-publish","format-standard","hentry","category-javabase"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/48562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=48562"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/48562\/revisions"}],"predecessor-version":[{"id":48563,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/48562\/revisions\/48563"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=48562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=48562"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=48562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}