{"id":28551,"date":"2025-03-13T23:54:26","date_gmt":"2025-03-13T15:54:26","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=28551"},"modified":"2025-03-13T23:54:26","modified_gmt":"2025-03-13T15:54:26","slug":"zai-c-zhongshiyong-ol","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/ja\/zai-c-zhongshiyong-ol\/","title":{"rendered":"\u5728 C++ \u4e2d\u4f7f\u7528 Ollama API"},"content":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 C++ \u4e2d\u4f7f\u7528 <a href=\"https:\/\/www.kdjingpai.com\/ollama\/\">Ollama<\/a> API \u3002\u8fd9\u7bc7\u6587\u6863\u65e8\u5728\u5e2e\u52a9 C++ \u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624b\u5e76\u5145\u5206\u5229\u7528 Ollama \u7684\u80fd\u529b\u3002\u901a\u8fc7\u5b66\u4e60\u672c\u6587\u6863\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u96c6\u6210 Ollama \u5230\u4f60\u7684\u9879\u76ee\u4e2d\u3002<\/p>\n<blockquote><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cOllama \u7684\u793e\u533a\u548c\u6587\u6863\u53ef\u80fd\u66f4\u4fa7\u91cd\u4e8e Python \u548c\u5176\u4ed6\u9ad8\u7ea7\u8bed\u8a00\u7684\u96c6\u6210\u3002\u5728 C++ \u9879\u76ee\u4e2d\u4f7f\u7528\u5927\u6a21\u578b\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528 <a href=\"https:\/\/www.kdjingpai.com\/llamacpp\/\">Llama.cpp<\/a>\u00a0<a href=\"https:\/\/www.datacamp.com\/tutorial\/llama-cpp-tutorial\">(llama.cpp \u6559\u7a0b)<\/a> \u3002Llama.cpp \u662f\u4e00\u4e2a\u7531 Georgi Gerganov \u5f00\u53d1\u7684\u5f00\u6e90\u9879\u76ee\uff0c\u5b83\u63d0\u4f9b\u4e86 Meta \u7684 LLaMa \u67b6\u6784\u7684\u9ad8\u6548 C\/C++ \u5b9e\u73b0\uff0c\u4e13\u6ce8\u4e8e\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u7684\u63a8\u7406\u8fc7\u7a0b\u3002<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>\u4e00\u3001 \u73af\u5883\u51c6\u5907<\/h2>\n<p>\u5173\u4e8e C++ \u9879\u76ee\u7684\u5f00\u53d1\u73af\u5883\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\uff0c\u53ea\u4ecb\u7ecd\u5982\u4f55\u96c6\u6210 Ollama\u3002\u8bf7\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u5e76\u8fd0\u884c\u4e86 Ollama\uff0c\u5e76\u4e0b\u8f7d\u4e86\u6240\u9700\u8981\u7684\u6a21\u578b\uff08\u53c2\u8003\u7b2c\u4e8c\u7ae0\u5185\u5bb9\uff09\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u4e8c\u3001\u8c03\u7528 Ollama API<\/h2>\n<p>\u76f4\u63a5\u8c03\u7528 Ollama API \u6bd4\u8f83\u7e41\u7410\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u4e24\u79cd\u601d\u8def\u3002<\/p>\n<ul>\n<li>\u4f7f\u7528\u5c01\u88c5\u597d\u7684 C++ \u7c7b\u5e93\u3002<\/li>\n<li>\u901a\u8fc7 Python \u8c03\u7528 Ollama \u7684\u6a21\u578b\u3002<\/li>\n<\/ul>\n<h3>1. \u4f7f\u7528\u5c01\u88c5\u597d\u7684 C++ \u7c7b\u5e93<\/h3>\n<p>\u5728 Github \u53ef\u4ee5\u627e\u5230\u5c01\u88c5\u597d\u7684 C++ \u7c7b\u5e93\uff0c\u53ea\u9700\u8981\u5f15\u5165\u5934\u6587\u4ef6\u5373\u53ef\u4f7f\u7528\uff0c\u907f\u514d\u91cd\u590d\u9020\u8f6e\u5b50\u3002\u8fd9\u91cc\u4ee5\u00a0<a href=\"https:\/\/github.com\/jmont-dev\/ollama-hpp\">ollama-hpp<\/a>\u00a0\u4e3a\u4f8b\uff1a<\/p>\n<pre><code>#include \"ollama.hpp\"\r\nstd::cout &lt;&lt; ollama::generate(\"llama3.1:latest\", \"how to use ollama in a cpp project?\") &lt;&lt; std::endl;\r\n<\/code><\/pre>\n<p>\u76ee\u524d\uff0cGithub \u4e0a\u5c01\u88c5\u597d\u7684 C++ \u7c7b\u5e93\u53ea\u6709\u8fd9\u4e00\u4e2a\uff0c\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u4e5f\u5b58\u5728\u4e00\u4e9b bug \u9700\u8981\u624b\u52a8\u4fee\u590d\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<h3>2. \u901a\u8fc7 Python \u8c03\u7528 Ollama \u7684\u6a21\u578b<\/h3>\n<p>\u5728 C++ \u9879\u76ee\u4e2d\u96c6\u6210 Ollama \u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7 Python \u8c03\u7528 Ollama \u7684\u6a21\u578b\uff0c\u518d\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9 C++ \u7a0b\u5e8f\uff1a<\/p>\n<ol>\n<li>\u7f16\u5199 Python \u4ee3\u7801\u8c03\u7528 Ollama API\uff1a\u4f7f\u7528 Python \u7f16\u5199\u4ee3\u7801\uff0c\u901a\u8fc7 Ollama \u63d0\u4f9b\u7684 API \u63a5\u53e3\u8c03\u7528\u6240\u9700\u7684\u6a21\u578b\u751f\u6210\u6587\u672c\u6216\u8fdb\u884c\u5bf9\u8bdd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Python \u8c03\u7528\u793a\u4f8b\uff1a\n<pre><code>import requests\r\ndef call_ollama_api(prompt, model_name=\"llama3.1:latest\"):\r\nurl = \"http:\/\/localhost:11434\/api\/generate\"\r\nheaders = {\"Content-Type\": \"application\/json\"}\r\npayload = {\r\n\"model\": model_name,\r\n\"prompt\": prompt,\r\n\"stream\": False\r\n}\r\nresponse = requests.post(url, json=payload, headers=headers)\r\nreturn response.json()\r\n# \u793a\u4f8b\u8c03\u7528\r\nresponse = call_ollama_api(\"\u9ed1\u795e\u8bdd\u597d\u73a9\u5417\uff1f\")\r\nprint(response)\r\n<\/code><\/pre>\n<\/li>\n<li>\u4ece C++ \u8c03\u7528 Python \u811a\u672c\uff1a\u5728 C++ \u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528\u6765\u6267\u884c Python \u811a\u672c\uff0c\u5e76\u5c06\u6240\u9700\u7684\u6587\u672c\u6216\u95ee\u9898\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 Python \u811a\u672c\u3002\u4f8b\u5982\uff0c\u5728 C++ \u4e2d\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8c03\u7528 Python \u811a\u672c\uff1a\n<pre><code>#include &lt;cstdlib&gt;\r\nint main() {\r\nsystem(\"python call_ollama.py '\u9ed1\u795e\u8bdd\u597d\u73a9\u5417\uff1f'\");\r\nreturn 0;\r\n}\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003\u6587\u6863<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/jmont-dev\/ollama-hpp\">ollama-hpp<\/a><\/li>\n<li><a href=\"https:\/\/www.datacamp.com\/tutorial\/llama-cpp-tutorial\">llama.cpp \u6559\u7a0b<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 C++ \u4e2d\u4f7f\u7528 Ollama API \u3002\u8fd9\u7bc7\u6587\u6863\u65e8\u5728\u5e2e\u52a9 C++ \u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624b\u5e76\u5145\u5206\u5229\u7528 Ollama \u7684\u80fd\u529b\u3002\u901a\u8fc7\u5b66\u4e60\u672c\u6587\u6863\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u96c6\u6210 Ollama \u5230\u4f60\u7684\u9879\u76ee\u4e2d\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cOllama \u7684\u793e\u533a\u548c\u6587\u6863\u53ef\u80fd\u66f4&#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-28551","post","type-post","status-publish","format-standard","hentry","category-shicao"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/posts\/28551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/comments?post=28551"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/posts\/28551\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/media?parent=28551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/categories?post=28551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/ja\/wp-json\/wp\/v2\/tags?post=28551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}