{"id":2489,"date":"2025-02-25T13:51:37","date_gmt":"2025-02-25T05:51:37","guid":{"rendered":"https:\/\/sanlangcode.com\/?p=2489"},"modified":"2025-02-25T13:51:37","modified_gmt":"2025-02-25T05:51:37","slug":"ollama%e6%9c%ac%e5%9c%b0%e9%83%a8%e7%bd%b2deepseek%e6%9e%84%e5%bb%ba%e4%b8%93%e5%b1%9e%e7%9f%a5%e8%af%86%e5%ba%93","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2025\/02\/25\/ollama%e6%9c%ac%e5%9c%b0%e9%83%a8%e7%bd%b2deepseek%e6%9e%84%e5%bb%ba%e4%b8%93%e5%b1%9e%e7%9f%a5%e8%af%86%e5%ba%93\/","title":{"rendered":"Ollama\u672c\u5730\u90e8\u7f72DeepSeek\u6784\u5efa\u4e2a\u4eba\u77e5\u8bc6\u5e93RAG"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Linux\u5b89\u88c5ollama<\/h2>\n\n\n\n<p>\u5916\u94fe\u901f\u5ea6\u975e\u5e38\u6162\uff0c\u76ee\u524d\u67e5\u8be2\u7684\u662f\u4e0b\u9762\u4e0b\u8f7d\u901f\u5ea6\u5feb\u4e00\u70b9\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/sanroimg.oss-cn-shanghai.aliyuncs.com\/img\/202502251346666.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -fsSL https:\/\/ollama.com\/install.sh -o ollama_install.shCopy\n\nsed -i 's|https:\/\/ollama.com\/download\/ollama-linux|https:\/\/gh.llkk.cc\/https:\/\/github.com\/ollama\/ollama\/releases\/download\/v0.5.7\/ollama-linux|g' ollama_install.shCopy\n\nchmod +x ollama_install.shCopy\n\nsh ollama_install.shCopy\n\n\n#\u65b9\u6cd5\u4e8c\nsudo apt update &amp;&amp; sudo apt install snapd -y\nsudo snap install ollama <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u578b\u9009\u62e9\u4e0e\u4e0b\u8f7d<\/h2>\n\n\n\n<p>\u76ee\u524d\u6d4b\u8bd5\u4f7f\u7528\u9009\u62e97B\u5927\u5c0f\u7684\u8fdb\u884c\u4f7f\u7528\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/sanroimg.oss-cn-shanghai.aliyuncs.com\/img\/202502241707754.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u8fd0\u884c\u6a21\u578b\u4e0e\u6d4b\u8bd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -fsSL https:\/\/ollama.com\/install.sh | sh\n# \u540e\u53f0\u8fd0\u884c\u670d\u52a1\nsudo systemctl start ollama\n\n# \u4fee\u6539\u76d1\u542c\u5730\u5740\uff08\u53ef\u9009\uff09\nsudo systemctl edit ollama\n# \u6dfb\u52a0\u73af\u5883\u53d8\u91cf\n&#91;Service]\nEnvironment=\"OLLAMA_HOST=0.0.0.0:11434\"\n#\u5efa\u8bae\u6539\u6210127.0.0.1\u8fdb\u884c\u5f00\u53d1\u6d4b\u8bd5\uff0c\u9632\u6b62\u6d41\u91cf\u76d7\u7528\n\n# \u91cd\u542f\u751f\u6548\nsudo systemctl daemon-reload\nsudo systemctl restart ollama\n# \u793a\u4f8b\uff1a32B\u6a21\u578b\uff08\u970024G\u663e\u5b58\uff09\nollama pull deepseek-r1:7b\n\n# \u547d\u4ee4\u884c\u4ea4\u4e92\u6d4b\u8bd5\nollama run deepseek-r1:7b\n# \u8f93\u5165\u6d4b\u8bd5\u95ee\u9898\n&gt;&gt;&gt; \u4e2d\u56fd\u7684\u9996\u90fd\u662f\u54ea\u91cc\uff1f\n\nOllama\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u547d\u4ee4\uff1a\n\n\u542f\u52a8Ollama\u670d\u52a1\uff1aollama serve\n\u4ece\u6a21\u578b\u6587\u4ef6\u521b\u5efa\u6a21\u578b\uff1aollama create &#91;\u6a21\u578b\u540d\u79f0] -f &#91;\u6a21\u578b\u6587\u4ef6\u8def\u5f84]\n\u663e\u793a\u6a21\u578b\u4fe1\u606f\uff1aollama show &#91;\u6a21\u578b\u540d\u79f0]\n\u8fd0\u884c\u6a21\u578b\uff1aollama run &#91;\u6a21\u578b\u540d\u79f0] &#91;\u8f93\u5165\u6587\u672c]\n\u4ece\u6ce8\u518c\u8868\u4e2d\u62c9\u53d6\u6a21\u578b\uff1aollama pull &#91;\u6a21\u578b\u540d\u79f0]\n\u5c06\u6a21\u578b\u63a8\u9001\u5230\u6ce8\u518c\u8868\uff1aollama push &#91;\u6a21\u578b\u540d\u79f0]\n\u5217\u51fa\u6240\u6709\u6a21\u578b\uff1aollama list\n\u590d\u5236\u6a21\u578b\uff1aollama cp &#91;\u6e90\u6a21\u578b\u540d\u79f0] &#91;\u76ee\u6807\u6a21\u578b\u540d\u79f0]\n\u5220\u9664\u6a21\u578b\uff1aollama rm &#91;\u6a21\u578b\u540d\u79f0]\n\u83b7\u53d6\u5e2e\u52a9\u4fe1\u606f\uff1aollama help<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8c03\u7528\u63a5\u53e3\u5f00\u53d1<\/h2>\n\n\n\n<p>\u6211\u4eec\u7684\u76ee\u7684\u662f\uff0c\u5e0c\u671b\u5927\u6a21\u578b\u4e13\u95e8\u56de\u7b54\u67d0\u4e2a\u8303\u56f4\u7684\u77e5\u8bc6\u6216\u8005\u89e3\u91ca\u3002\u6bd4\u5982\uff0c\u9488\u5bf9\u4e00\u4e2apdf\uff0cdocx\u6587\u4ef6\u3002\u9488\u5bf9\u67d0\u4e2a\u9886\u57df\uff0c\u6bd4\u5982\u7ed8\u753b\uff0c\u4e66\u6cd5\uff0c\u7ec6\u80de\u5b9e\u9a8c\u7b49\u3002\u5c1d\u8bd5\u6253\u9020\u6210\u4e00\u4e2a\u6240\u8c13\u7684\u4eba\u5de5\u667a\u80fd\u77e5\u8bc6\u52a9\u624b\u3002\u5176\u5b9e\u5c31\u662f\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff08<a rel=\"noreferrer noopener\" href=\"https:\/\/zhida.zhihu.com\/search?content_id=236592716&amp;content_type=Article&amp;match_order=1&amp;q=Retrieval+Augmented+Generation&amp;zhida_source=entity\" target=\"_blank\">Retrieval Augmented Generation<\/a>\uff09\uff0c\u7b80\u79f0 RAG\uff0c\u76ee\u524d\u5df2\u7ecf\u6210\u4e3a\u5f53\u524d\u6700\u706b\u70ed\u7684LLM\u5e94\u7528\u65b9\u6848\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nimport pdfplumber\nimport os\n\n\ndef read_pdf(file_path):\n    \"\"\"\u8bfb\u53d6PDF\u6587\u6863\u6838\u5fc3\u5185\u5bb9\"\"\"\n    try:\n        content = &#91;]\n        with pdfplumber.open(file_path) as pdf:\n            for page in pdf.pages:\n                text = page.extract_text()\n                if text and text.strip():\n                    content.append(text.strip())\n        return '\\n'.join(content) if content else \"\"\n    except Exception as e:\n        raise RuntimeError(f\"PDF\u8bfb\u53d6\u5931\u8d25: {str(e)}\")\n\n\ndef deepseek_chat(api_key, question, doc_path=None, model=\"deepseek-chat\", temperature=0.7):\n    \"\"\"\n    \u652f\u6301PDF\u6587\u6863\u95ee\u7b54\u7684\u589e\u5f3a\u7248\u51fd\u6570\n    :param doc_path: \u53ef\u9009\u53c2\u6570\uff0cPDF\u6587\u6863\u8def\u5f84\n    \"\"\"\n    system_prompt = \"\u4f60\u662fCellSpace\u8f6f\u4ef6\u4e13\u5c5eAI\u52a9\u624b\uff0c\u8bf7\u4e25\u683c\u6839\u636e\u63d0\u4f9b\u7684PDF\u5185\u5bb9\u56de\u7b54\u95ee\u9898\u3002\"\n\n    # \u8bfb\u53d6\u6587\u6863\u5185\u5bb9\n    doc_content = \"\"\n    if doc_path:\n        if not os.path.exists(doc_path):\n            return f\"\u6587\u4ef6\u4e0d\u5b58\u5728: {doc_path}\"\n        if not doc_path.lower().endswith('.pdf'):\n            return \"\u4ec5\u652f\u6301PDF\u683c\u5f0f\u6587\u4ef6\"\n        try:\n            raw_content = read_pdf(doc_path)\n            doc_content = f\"\\n&#91;PDF\u6587\u6863\u5185\u5bb9]\\n{raw_content&#91;:1000]}\"  # \u622a\u53d6\u524d3000\u5b57\u7b26\n        except RuntimeError as e:\n            return str(e)\n\n    # \u6784\u9020\u6d88\u606f\u961f\u5217\n    messages = &#91;\n        {\"role\": \"system\", \"content\": system_prompt},\n        {\"role\": \"user\", \"content\": f\"{question}{doc_content}\"}\n    ]\n\n    # API\u8bf7\u6c42\u914d\u7f6e\n    url = \"https:\/\/api.deepseek.com\/chat\/completions\"\n    headers = {\"Authorization\": f\"Bearer {api_key}\", \"Content-Type\": \"application\/json\"}\n    data = {\n        \"model\": model,\n        \"messages\": messages,\n        \"temperature\": temperature,\n        \"max_tokens\": 2000\n    }\n\n    try:\n        response = requests.post(url, headers=headers, json=data)\n        response.raise_for_status()\n        return response.json()&#91;'choices']&#91;0]&#91;'message']&#91;'content']\n    except requests.exceptions.RequestException as e:\n        return f\"API\u8bf7\u6c42\u5931\u8d25: {str(e)}\"\n    except KeyError:\n        return \"\u54cd\u5e94\u89e3\u6790\u5f02\u5e38\uff0c\u5efa\u8bae\u68c0\u67e5API\u7248\u672c\"\n\n\n# \u4f7f\u7528\u793a\u4f8b\nif __name__ == \"__main__\":\n    API_KEY = \"sk-194a63*****************68558\"\n    PDF_PATH = r\"C:\\test.pdf\"\n\n    # PDF\u6587\u6863\u95ee\u7b54\n    print(\"\u5f00\u59cb\u6d4b\u8bd5\")\n\n    pre_prompt = \"\u4f60\u662f\u6211\u7684\u8f6f\u4ef6\u4e13\u5c5eAI\u52a9\u624b\uff0c\u4ee5\u5973\u6027\u8eab\u4efd\u5148\u6269\u5145\u81ea\u6211\u4ecb\u7ecd\uff0c\u53ea\u56de\u7b54\u79d1\u5b66\u9886\u57df\u7684\u95ee\u9898\uff0c\u5176\u4ed6\u95ee\u9898\u62d2\u63a5\u56de\u7b54\u3002\u7136\u540e\u56de\u7b54\u4e0b\u9762\u95ee\u9898\uff1a\"\n\n    # PDF\u6587\u6863\u95ee\u7b54\n    #print(deepseek_chat(API_KEY, \"\u8bf7\u603b\u7ed3PDF\u4e2d\u7684\u4e3b\u8981\u5185\u5bb9\", PDF_PATH))\n\n    qus_prompt = \"\u89e3\u91ca\u4e00\u4e0b\u7ec6\u80de\u7ed8\u753b\uff0c\u7ec6\u80de\u751f\u957f\u8ffd\u8e2a\uff0c\u4ed6\u4eec\u662f\u4e00\u4e2a\u610f\u601d\u5417\uff1f1000\u5b57\u4ee5\u5185\"\n\n    # \u666e\u901a\u95ee\u7b54\n    print(deepseek_chat(API_KEY, pre_prompt+qus_prompt))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/sanroimg.oss-cn-shanghai.aliyuncs.com\/img\/202502260923962.png\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u5b89\u88c5ollama \u5916\u94fe&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2489","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=2489"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2489\/revisions"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=2489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=2489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=2489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}