{"id":28552,"date":"2025-03-13T23:55:49","date_gmt":"2025-03-13T15:55:49","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=28552"},"modified":"2025-03-13T23:55:49","modified_gmt":"2025-03-13T15:55:49","slug":"zai-golang-zhongshiyong","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/en\/zai-golang-zhongshiyong\/","title":{"rendered":"\u5728 Golang \u4e2d\u4f7f\u7528 Ollama API"},"content":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 <a href=\"https:\/\/www.kdjingpai.com\/ollama\/\">Ollama<\/a> API\u3002\u8fd9\u7bc7\u6587\u6863\u65e8\u5728\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624b\u5e76\u5145\u5206\u5229\u7528 Ollama \u7684\u80fd\u529b\u3002Ollama \u672c\u8eab\u662f\u7531 Golang \u8bed\u8a00\u5f00\u53d1\uff0cGolang \u8bed\u8a00\u7248\u672c\u7684\u63a5\u53e3\u4ee3\u7801\u5728\u5b98\u65b9\u4ed3\u5e93\u76ee\u5f55\u4e2d\u00a0https:\/\/github.com\/ollama\/ollama\/tree\/main\/api\uff0c\u5b98\u65b9\u6587\u6863\u4e3ahttps:\/\/pkg.go.dev\/github.com\/ollama\/ollama\/api\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>\u5b98\u65b9\u4ed3\u5e93https:\/\/github.com\/ollama\/ollama\/tree\/main\/api\/examples\u63d0\u4f9b\u4e86\u4e00\u4e9b\u793a\u4f8b\u4ee3 \u7801\uff0c\u4e0b\u6587\u4e2d\u7684\u4ee3\u7801\u53c2\u8003\u4e86\u8fd9\u4e9b\u793a\u4f8b\u5e76\u8fdb\u884c\u4e86\u4fee\u6539\u3002\u6240\u6709\u793a\u4f8b\u5728<code>notebook\/C4\/Golang_API_example<\/code>\u4e2d<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2>\u73af\u5883\u51c6\u5907<\/h2>\n<p>\u5f00\u59cb\u4e4b\u524d\u8bf7\u786e\u4fdd\u60a8\u7684\u5f00\u53d1\u73af\u5883\u6ee1\u8db3\u4e00\u4e0b\u6761\u4ef6\uff1a<\/p>\n<ol>\n<li>Golang \u5f00\u53d1\u73af\u5883\uff0c\u901a\u8fc7<code>go version<\/code>\u67e5\u770b golang \u7248\u672c\uff0c\u672c\u6587\u4f7f\u7528\u7684\u7248\u672c\u4e3a<code>go1.23.6<\/code><br \/>\n<blockquote><p>\u53c2\u8003https:\/\/golang.google.cn\/doc\/install\u8fdb\u884c\u5b89\u88c5<\/p><\/blockquote>\n<\/li>\n<li>\u521b\u5efa\u9879\u76ee\u76ee\u5f55\u5e76\u521d\u59cb\u5316<\/li>\n<\/ol>\n<pre><code>mkdir ollama-demo &amp;&amp; cd ollama-demo\r\ngo mod init ollama-demo\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5b89\u88c5\u4f9d\u8d56<\/li>\n<\/ol>\n<pre><code>go get github.com\/ollama\/ollama\/api\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u65b9\u5f0f<\/h2>\n<p>\u521b\u5efa\u76ee\u5f55<code>chat<\/code>\uff0c\u5728\u76ee\u5f55\u4e2d\u521b\u5efa\u6587\u4ef6<code>main.go<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff08\u793a\u4f8b\u4e2d\u4f7f\u7528\u7684\u662fdeepseek-r1:7b\u6a21\u578b\uff09\uff1a<\/p>\n<pre><code>package main\r\nimport (\r\n\"context\"\r\n\"fmt\"\r\n\"log\"\r\n\"github.com\/ollama\/ollama\/api\"\r\n)\r\nfunc main() {\r\nclient, err := api.ClientFromEnvironment()\r\nif err != nil {\r\nlog.Fatal(err)\r\n}\r\nmessages := []api.Message{\r\napi.Message{\r\nRole:    \"user\",\r\nContent: \"\u4e3a\u4ec0\u4e48\u5929\u7a7a\u662f\u84dd\u8272\u7684\uff1f\",\r\n},\r\n}\r\nctx := context.Background()\r\nreq := &amp;api.ChatRequest{\r\nModel:    \"<a href=\"https:\/\/www.kdjingpai.com\/deepseek-r1nenglixiang\/\">deepseek-r1<\/a>:7b\",\r\nMessages: messages,\r\n}\r\nrespFunc := func(resp api.ChatResponse) error {\r\nfmt.Print(resp.Message.Content)\r\nreturn nil\r\n}\r\nerr = client.Chat(ctx, req, respFunc)\r\nif err != nil {\r\nlog.Fatal(err)\r\n}\r\n}\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u00a0<code>go run chat\/main.go<\/code><\/p>\n<p>&nbsp;<\/p>\n<h2>\u6d41\u5f0f\u8f93\u51fa<\/h2>\n<p>\u521b\u5efa\u76ee\u5f55<code>generate-streaming<\/code>\uff0c\u5728\u76ee\u5f55\u4e2d\u521b\u5efa\u6587\u4ef6<code>main.go<\/code><\/p>\n<pre><code>package main\r\nimport (\r\n\"context\"\r\n\"fmt\"\r\n\"log\"\r\n\"github.com\/ollama\/ollama\/api\"\r\n)\r\nfunc main() {\r\nclient, err := api.ClientFromEnvironment()\r\nif err != nil {\r\nlog.Fatal(err)\r\n}\r\n\/\/ By default, GenerateRequest is streaming.\r\nreq := &amp;api.GenerateRequest{\r\nModel:  \"deepseek-r1:7b\",\r\nPrompt: \"\u4e3a\u4ec0\u4e48\u5929\u7a7a\u662f\u84dd\u8272\u7684\uff1f\",\r\n}\r\nctx := context.Background()\r\nrespFunc := func(resp api.GenerateResponse) error {\r\n\/\/ Only print the response here; GenerateResponse has a number of other\r\n\/\/ interesting fields you want to examine.\r\n\/\/ In streaming mode, responses are partial so we call fmt.Print (and not\r\n\/\/ Println) in order to avoid spurious newlines being introduced. The\r\n\/\/ model will insert its own newlines if it wants.\r\nfmt.Print(resp.Response)\r\nreturn nil\r\n}\r\nerr = client.Generate(ctx, req, respFunc)\r\nif err != nil {\r\nlog.Fatal(err)\r\n}\r\nfmt.Println()\r\n}\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u00a0<code>go run generate-streaming\/main.go<\/code><\/p>\n<p>&nbsp;<\/p>\n<h2>\u7ed3\u6784\u5316\u8f93\u51fa<\/h2>\n<p>\u521b\u5efa\u76ee\u5f55<code>structured_output<\/code>\uff0c\u5728\u76ee\u5f55\u4e2d\u521b\u5efa\u6587\u4ef6<code>main.go<\/code>\uff0c\u5982\u4e0b<\/p>\n<pre><code>package main\r\nimport (\r\n\"context\"\r\n\"encoding\/json\"\r\n\"fmt\"\r\n\"log\"\r\n\"strings\"\r\n\"github.com\/ollama\/ollama\/api\"\r\n)\r\ntype CountryInfo struct {\r\nCapital    string  `json:\"capital\"`\r\nPopulation float64 `json:\"population\"`\r\nArea       float64 `json:\"area\"`\r\n}\r\nfunc main() {\r\nclient, err := api.ClientFromEnvironment()\r\nif err != nil {\r\nlog.Fatal(err)\r\n}\r\nmessages := []api.Message{\r\napi.Message{\r\nRole:    \"user\",\r\nContent: \"\u8bf7\u4ecb\u7ecd\u7f8e\u56fd\u7684\u9996\u90fd\u3001\u4eba\u53e3\u3001\u5360\u5730\u9762\u79ef\u4fe1\u606f\uff0c\u5e76\u4ee5 JSON \u683c\u5f0f\u8fd4\u56de\u3002\",\r\n},\r\n}\r\nctx := context.Background()\r\nreq := &amp;api.ChatRequest{\r\nModel:    \"deepseek-r1:7b\",\r\nMessages: messages,\r\nStream:   new(bool), \/\/ false\r\nFormat:   []byte(`\"json\"`),\r\nOptions: map[string]interface{}{\r\n\"temperature\": 0.0,\r\n},\r\n}\r\nrespFunc := func(resp api.ChatResponse) error {\r\nfmt.Printf(\"%s\\n\", strings.TrimSpace(resp.Message.Content))\r\nvar info CountryInfo\r\nerr := json.Unmarshal([]byte(resp.Message.Content), &amp;info)\r\nif err != nil {\r\nlog.Fatal(err)\r\n}\r\nfmt.Printf(\"\u9996\u90fd: %s\uff0c\u4eba\u53e3: %f\uff0c\u9762\u79ef: %f\", info.Capital, info.Population, info.Area)\r\nreturn nil\r\n}\r\nerr = client.Chat(ctx, req, respFunc)\r\nif err != nil {\r\nlog.Fatal(err)\r\n}\r\n}\r\n<\/code><\/pre>\n<p>\u901a\u8fc7\u6307\u5b9a<code>ChatRequest<\/code>\u8bf7\u6c42\u7684<code>Format:   []byte(<\/code>&#8220;json&#8221;<code>),<\/code>\u00a0\u53c2\u6570\uff0c\u53ef\u4ee5\u8ba9\u6a21\u578b\u8fd4\u56de\u7ed3\u6784\u5316 \u7684\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7<code>json.Unmarshal<\/code>\u89e3\u6790\u8fd4\u56de\u7684\u6570\u636e\u3002<\/p>\n<p>\u8fd0\u884c\u00a0<code>go run structured_output\/main.go<\/code>\u00a0\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre><code>{\"capital\": \"Washington, D.C.\", \"population\": 3.672e6, \"area\": 7.058e6}\r\n\u9996\u90fd: Washington, D.C.\uff0c\u4eba\u53e3: 3672000.000000\uff0c\u9762\u79ef: 7058000.000000<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 Golang \u4e2d\u4f7f\u7528 Ollama API\u3002\u8fd9\u7bc7\u6587\u6863\u65e8\u5728\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u4e0a\u624b\u5e76\u5145\u5206\u5229\u7528 Ollama \u7684\u80fd\u529b\u3002Ollama \u672c\u8eab\u662f\u7531 Golang \u8bed\u8a00\u5f00\u53d1\uff0cGolang \u8bed\u8a00\u7248\u672c\u7684\u63a5\u53e3\u4ee3\u7801\u5728\u5b98\u65b9\u4ed3\u5e93\u76ee\u5f55\u4e2d\u00a0https:\/\/&#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-28552","post","type-post","status-publish","format-standard","hentry","category-shicao"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/28552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/comments?post=28552"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/28552\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/media?parent=28552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/categories?post=28552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/tags?post=28552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}