{"id":48564,"date":"2024-05-10T10:09:46","date_gmt":"2024-05-10T02:09:46","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=48564"},"modified":"2024-05-10T17:18:14","modified_gmt":"2024-05-10T09:18:14","slug":"ollama-zheng-heopenai","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2024\/05\/10\/ollama-zheng-heopenai\/","title":{"rendered":"Ollama\u6574\u5408OpenAI"},"content":{"rendered":"<h1><a id=\"ollama%E6%95%B4%E5%90%88openai\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Ollama\u6574\u5408OpenAI<\/h1>\n<p>Ollama\u63d0\u4f9b\u4e86\u90e8\u5206OpenAI API\u7684\u517c\u5bb9\uff0c\u4ee5\u5e2e\u52a9\u5c06\u73b0\u6709\u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u5230Ollama\u3002\u4eca\u5929\u5927\u5bb6\u5c31\u6765\u4e00\u8d77\u770b\u770b\u8be5\u600e\u4e48\u5904\u7406\u5427\u3002<\/p>\n<h2><a id=\"%E4%BD%BF%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528<\/h2>\n<h3><a id=\"openai-python%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>OpenAI Python\u5e93<\/h3>\n<pre><code class=\"language-python\">from openai import OpenAI\n\nclient = OpenAI(\n    base_url='http:\/\/localhost:11434\/v1\/',\n\n    # required but ignored\n    api_key='ollama',\n)\n\nchat_completion = client.chat.completions.create(\n    messages=[\n        {\n            'role': 'user',\n            'content': 'Say this is a test',\n        }\n    ],\n    model='llama3',\n)\n<\/code><\/pre>\n<h3><a id=\"openai-javascript%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>OpenAI JavaScript\u5e93<\/h3>\n<pre><code class=\"language-js\">import OpenAI from 'openai'\n\nconst openai = new OpenAI({\n  baseURL: 'http:\/\/localhost:11434\/v1\/',\n\n  \/\/ required but ignored\n  apiKey: 'ollama',\n})\n\nconst chatCompletion = await openai.chat.completions.create({\n  messages: [{ role: 'user', content: 'Say this is a test' }],\n  model: 'llama3',\n})\n<\/code><\/pre>\n<h3><a id=\"curl\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>curl<\/h3>\n<pre><code class=\"language-sh\">curl http:\/\/localhost:11434\/v1\/chat\/completions \\\n    -H \"Content-Type: application\/json\" \\\n    -d '{\n        \"model\": \"llama3\",\n        \"messages\": [\n            {\n                \"role\": \"system\",\n                \"content\": \"You are a helpful assistant.\"\n            },\n            {\n                \"role\": \"user\",\n                \"content\": \"Hello!\"\n            }\n        ]\n    }'\n<\/code><\/pre>\n<h2><a id=\"models\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Models<\/h2>\n<p>\u5728\u4f7f\u7528\u6a21\u578b\u4e4b\u524d\uff0c\u5148\u8fdb\u884c\u672c\u5730\u62c9\u53d6\u6a21\u578b<\/p>\n<pre><code class=\"language-sh\">ollama pull llama3\n<\/code><\/pre>\n<h3><a id=\"%E9%BB%98%E8%AE%A4%E6%A8%A1%E5%9E%8B%E5%90%8D%E7%A7%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u9ed8\u8ba4\u6a21\u578b\u540d\u79f0<\/h3>\n<p>\u5bf9\u4e8e\u4f9d\u8d56\u4e8e\u9ed8\u8ba4OpenAI\u6a21\u578b\u540d\u79f0\uff08\u5982gpt-3.5-turbo\uff09\u7684\u5de5\u5177\uff0c\u4f7f\u7528ollama cp\u547d\u4ee4\u5c06\u73b0\u6709\u6a21\u578b\u540d\u79f0\u590d\u5236\u5230\u4e34\u65f6\u540d\u79f0\uff1a<\/p>\n<pre><code class=\"language-sh\">ollama cp llama3 gpt-3.5-turbo\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u53ef\u4ee5\u5728\u6a21\u578b\u5b57\u6bb5\u4e2d\u6307\u5b9a\u8fd9\u4e2a\u65b0\u6a21\u578b\u540d<\/p>\n<pre><code class=\"language-sh\">curl http:\/\/localhost:11434\/v1\/chat\/completions \\\n    -H \"Content-Type: application\/json\" \\\n    -d '{\n        \"model\": \"gpt-3.5-turbo\",\n        \"messages\": [\n            {\n                \"role\": \"user\",\n                \"content\": \"Hello!\"\n            }\n        ]\n    }'\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ollama\u6574\u5408OpenAI Ollama\u63d0\u4f9b\u4e86\u90e8\u5206OpenAI API\u7684\u517c\u5bb9\uff0c\u4ee5\u5e2e\u52a9\u5c06\u73b0\u6709\u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u5230Ol [&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-48564","post","type-post","status-publish","format-standard","hentry","category-javabase"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/48564","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=48564"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/48564\/revisions"}],"predecessor-version":[{"id":48565,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/48564\/revisions\/48565"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=48564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=48564"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=48564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}