{"id":28557,"date":"2025-03-14T00:02:41","date_gmt":"2025-03-13T16:02:41","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=28557"},"modified":"2025-03-14T00:03:06","modified_gmt":"2025-03-13T16:03:06","slug":"ollama-zai-langchain-2","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/de\/ollama-zai-langchain-2\/","title":{"rendered":"Ollama \u5728 LangChain \u4e2d\u7684\u4f7f\u7528 &#8211; JavaScript \u96c6\u6210"},"content":{"rendered":"<h2>\u7b80\u4ecb<\/h2>\n<p>\u672c\u6587\u6863\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 JavaScript \u73af\u5883\u4e2d\u4f7f\u7528 <a href=\"https:\/\/www.kdjingpai.com\/de\/ollama\/\">Ollama<\/a> \u4e0e LangChain \u96c6\u6210\uff0c\u4ee5\u521b\u5efa\u5f3a\u5927\u7684 AI \u5e94\u7528\u3002Ollama \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5927\u8bed\u8a00\u6a21\u578b\u90e8\u7f72\u5de5\u5177\uff0c\u800c LangChain \u5219\u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u57fa\u4e8e\u8bed\u8a00\u6a21\u578b\u7684\u5e94\u7528\u7684\u6846\u67b6\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e24\u8005\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u672c\u5730\u73af\u5883\u4e2d\u5feb\u901f\u90e8\u7f72\u548c\u4f7f\u7528\u5148\u8fdb\u7684 AI \u6a21\u578b\u3002<\/p>\n<blockquote><p>\u6ce8: \u672c\u6587\u6863\u5305\u542b\u6838\u5fc3\u4ee3\u7801\u7247\u6bb5\u548c\u8be6\u7ec6\u89e3\u91ca\u3002\u5b8c\u6574\u4ee3\u7801\u53ef\u5728 notebook\/c5\/ollama_langchain_javascript \u4e2d\u627e\u5230\u3002<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<h2>1. \u73af\u5883\u8bbe\u7f6e<\/h2>\n<h3>\u914d\u7f6eNode.js\u73af\u5883<\/h3>\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u5df2\u5b89\u88c5 Node.js\u3002\u4f60\u53ef\u4ee5\u4ece\u00a0Node.js\u5b98\u7f51\u00a0\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002<\/p>\n<h3>\u521b\u5efa\u9879\u76ee\u5e76\u5b89\u88c5\u4f9d\u8d56<\/h3>\n<ol>\n<li>\u5207\u6362\u5230\u8fd0\u884c\u76ee\u5f55\u8fd0\u884c\uff1a<\/li>\n<\/ol>\n<pre><code>cd notebook\/C5\/ollama_langchain_javascript\r\nnpm init -y\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5b89\u88c5\u5fc5\u8981\u7684\u4f9d\u8d56\uff1a<\/li>\n<\/ol>\n<pre><code>npm install @langchain\/ollama @langchain\/core @langchain\/community zod\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5728\u00a0<code>package.json<\/code>\u00a0\u6587\u4ef6\u4e2d\u6dfb\u52a0\u00a0<code>\"type\": \"module\"<\/code>\u00a0\u4ee5\u542f\u7528ES\u6a21\u5757\u652f\u6301\uff1a<\/li>\n<\/ol>\n<pre><code>{\r\n\"type\": \"module\",\r\n\/\/ ... \u5176\u4ed6\u914d\u7f6e\r\n}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>2. \u4e0b\u8f7d\u6240\u9700\u6a21\u578b\u5e76\u521d\u59cb\u5316 Ollama<\/h2>\n<h3>\u4e0b\u8f7d llama3.1 \u6a21\u578b<\/h3>\n<ol>\n<li>\u8fdb\u5165\u5b98\u7f51\u00a0https:\/\/ollama.com\/download\u00a0\u4e0b\u8f7d\u5e76\u5b89\u88c5 Ollama \u5230\u53ef\u7528\u7684\u53d7\u652f\u6301\u5e73\u53f0\u3002<\/li>\n<li>\u67e5\u770b\u00a0https:\/\/ollama.ai\/library\u00a0\u4e86\u89e3\u6240\u6709\u53ef\u7528\u7684\u6a21\u578b\u3002<\/li>\n<li>\u901a\u8fc7\u00a0<code>ollama pull &lt;name-of-model&gt;<\/code>\u00a0\u547d\u4ee4\u83b7\u53d6\u53ef\u7528 LLM \u6a21\u578b\uff08\u4f8b\u5982\uff1a<code>ollama pull llama3.1<\/code>\uff09\u3002<\/li>\n<\/ol>\n<p>\u547d\u4ee4\u884c\u6267\u884c\u5b8c\u6bd5\u540e\u5982\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28558\" title=\"Ollama \u5728 LangChain \u4e2d\u7684\u4f7f\u7528 - JavaScript \u96c6\u6210-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/6f3c088dabe9da1-1.png\" alt=\"Ollama \u5728 LangChain \u4e2d\u7684\u4f7f\u7528 - JavaScript \u96c6\u6210-1\" width=\"1186\" height=\"265\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/6f3c088dabe9da1-1.png 1186w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/6f3c088dabe9da1-1-768x172.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/6f3c088dabe9da1-1-18x4.png 18w\" sizes=\"auto, (max-width: 1186px) 100vw, 1186px\" \/><\/p>\n<p>\u6a21\u578b\u5b58\u50a8\u4f4d\u7f6e\uff1a<\/p>\n<ul>\n<li>Mac:\u00a0<code>~\/.ollama\/models\/<\/code><\/li>\n<li>Linux\uff08\u6216 WSL\uff09:\u00a0<code>\/usr\/share\/ollama\/.ollama\/models<\/code><\/li>\n<li>Windows:\u00a0<code>C:\\Users\\Administrator\\.ollama\\models<\/code><\/li>\n<\/ul>\n<p>\u4e0b\u8f7d\u5b8c\u6bd5\u540e\uff0c\u9700\u8981\u786e\u4fdd Ollama \u670d\u52a1\u5df2\u7ecf\u542f\u52a8\uff1a<\/p>\n<pre><code>ollama ps\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28559\" title=\"Ollama \u5728 LangChain \u4e2d\u7684\u4f7f\u7528 - JavaScript \u96c6\u6210-2\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/6386d788a97ae93.png\" alt=\"Ollama \u5728 LangChain \u4e2d\u7684\u4f7f\u7528 - JavaScript \u96c6\u6210-2\" width=\"922\" height=\"76\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/6386d788a97ae93.png 922w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/6386d788a97ae93-768x63.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/6386d788a97ae93-18x1.png 18w\" sizes=\"auto, (max-width: 922px) 100vw, 922px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>3. \u57fa\u672c\u4f7f\u7528\u793a\u4f8b<\/h2>\n<h3>\u4f7f\u7528 ChatOllama \u8fdb\u884c\u7b80\u5355\u5bf9\u8bdd<\/h3>\n<p>\u53ef\u8fd0\u884c\u00a0<code>base_chat.js<\/code>\u00a0\u6587\u4ef6\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>import { Ollama } from \"@langchain\/community\/llms\/ollama\";\r\nconst ollama = new Ollama({\r\nbaseUrl: \"http:\/\/localhost:11434\",  \/\/ \u786e\u4fddOllama\u670d\u52a1\u5df2\u7ecf\u542f\u52a8\r\nmodel: \"llama3.1\", \/\/ \u66ff\u6362\u4e3a\u5b9e\u9645\u4f7f\u7528\u7684\u6a21\u578b\r\n});\r\nconst stream = await ollama.stream(\r\n`\u4f60\u6bd4GPT4\u5389\u5bb3\u5417?`\r\n);\r\nconst chunks = [];\r\nfor await (const chunk of stream) {\r\nchunks.push(chunk);\r\n}\r\nconsole.log(chunks.join(\"\"));\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee3\u7801\uff1a<\/p>\n<pre><code>node base_chat.js\r\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u505a\u4e86\u4ee5\u4e0b\u51e0\u4ef6\u4e8b\uff1a<\/p>\n<ol>\n<li>\u5bfc\u5165 Ollama \u7c7b\u5e76\u521d\u59cb\u5316\u5b83\uff0c\u6307\u5b9a\u6a21\u578b\u548c\u57fa\u7840URL\u3002<\/li>\n<li>\u4f7f\u7528\u00a0<code>stream<\/code>\u00a0\u65b9\u6cd5\u53d1\u9001\u4e00\u4e2a\u95ee\u9898\u7ed9\u6a21\u578b\uff0c\u8fd9\u5141\u8bb8\u6211\u4eec\u9010\u5757\u63a5\u6536\u54cd\u5e94\u3002<\/li>\n<li>\u4f7f\u7528 for-await \u5faa\u73af\u6536\u96c6\u6240\u6709\u54cd\u5e94\u5757\u3002<\/li>\n<li>\u5c06\u6240\u6709\u5757\u7ec4\u5408\u5e76\u6253\u5370\u51fa\u5b8c\u6574\u54cd\u5e94\u3002<\/li>\n<\/ol>\n<h3>\u591a\u6a21\u6001\u6a21\u578b\u4f7f\u7528<\/h3>\n<p>\u8fd0\u884c<code>base_multimodal.js<\/code>\u6587\u4ef6\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>import { Ollama } from \"@langchain\/community\/llms\/ollama\";\r\nimport * as fs from \"node:fs\/promises\";\r\nconst imageData = await fs.readFile(\"..\/..\/..\/docs\/images\/C5-1-4.png\"); \/\/ \u53ef\u4ee5\u66ff\u6362\u4e3a\u4f60\u60f3\u8be2\u95ee\u7684\u56fe\u7247\r\nconst model = new Ollama({\r\nmodel: \"llava\",\r\nbaseUrl: \"http:\/\/127.0.0.1:11434\",\r\n}).bind({\r\nimages: [imageData.toString(\"base64\")],\r\n});\r\nconst res = await model.invoke(\"\u56fe\u7247\u91cc\u662f\u4ec0\u4e48\u52a8\u7269\u5440\uff1f\");\r\nconsole.log({ res });\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee3\u7801\uff1a<\/p>\n<pre><code>node base_multimodal.js\r\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u6a21\u6001\u6a21\u578b\uff08\u5982 llava\uff09\u5904\u7406\u56fe\u50cf\u548c\u6587\u672c\u8f93\u5165\uff1a<\/p>\n<ol>\n<li>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3abase64\u7f16\u7801\u3002<\/li>\n<li>\u521d\u59cb\u5316 Ollama \u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u00a0<code>bind<\/code>\u00a0\u65b9\u6cd5\u5c06\u56fe\u50cf\u6570\u636e\u7ed1\u5b9a\u5230\u6a21\u578b\u3002<\/li>\n<li>\u4f7f\u7528\u00a0<code>invoke<\/code>\u00a0\u65b9\u6cd5\u53d1\u9001\u4e00\u4e2a\u5173\u4e8e\u56fe\u50cf\u7684\u95ee\u9898\u3002<\/li>\n<li>\u6253\u5370\u6a21\u578b\u7684\u54cd\u5e94\u3002<\/li>\n<\/ol>\n<h3>\u5de5\u5177\u8c03\u7528<\/h3>\n<p>\u8fd0\u884c\u00a0<code>base_tool.js<\/code>\u00a0\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>import { tool } from \"@langchain\/core\/tools\";\r\nimport { <a href=\"https:\/\/www.kdjingpai.com\/de\/chatollamajiyun\/\">ChatOllama<\/a> } from \"@langchain\/ollama\";\r\nimport { z } from \"zod\";\r\n\/\/ \u5b9a\u4e49\u7b80\u5355\u8ba1\u7b97\u5668\u5de5\u5177\r\nconst simpleCalculatorTool = tool((args) =&gt; {\r\nconst { operation, x, y } = args;\r\nswitch (operation) {\r\ncase \"add\":\r\nreturn x + y;\r\ncase \"subtract\":\r\nreturn x - y;\r\ncase \"multiply\":\r\nreturn x * y;\r\ncase \"divide\":\r\nif (y !== 0) {\r\nreturn x \/ y;\r\n} else {\r\nthrow new Error(\"Cannot divide by zero\");\r\n}\r\ndefault:\r\nthrow new Error(\"Invalid operation\");\r\n}\r\n}, {\r\nname: \"simple_calculator\",\r\ndescription: \"Perform simple arithmetic operations\",\r\nschema: z.object({\r\noperation: z.enum([\"add\", \"subtract\", \"multiply\", \"divide\"]),\r\nx: z.number(),\r\ny: z.number(),\r\n}),\r\n});\r\n\/\/ \u5b9a\u4e49\u6a21\u578b\r\nconst llm = new ChatOllama({\r\nmodel: \"llama3.1\",\r\ntemperature: 0,\r\n});\r\n\/\/ \u5c06\u5de5\u5177\u7ed1\u5b9a\u5230\u6a21\u578b\r\nconst llmWithTools = llm.bindTools([simpleCalculatorTool]);\r\n\/\/ \u4f7f\u7528\u6a21\u578b\u8fdb\u884c\u5de5\u5177\u8c03\u7528\r\nconst result = await llmWithTools.invoke(\r\n\"\u4f60\u77e5\u9053\u4e00\u5343\u4e07\u4e58\u4e8c\u662f\u591a\u5c11\u5417\uff1f\u8bf7\u4f7f\u7528 'simple_calculator' \u5de5\u5177\u6765\u8ba1\u7b97\u3002\"\r\n);\r\nconsole.log(result);\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee3\u7801\uff1a<\/p>\n<pre><code>node base_tool.js\r\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u5de5\u5177\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528\u00a0<code>tool<\/code>\u00a0\u51fd\u6570\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u5de5\u5177\uff0c\u5305\u62ec\u64cd\u4f5c\u903b\u8f91\u548c\u53c2\u6570schema\u3002<\/li>\n<li>\u521d\u59cb\u5316 ChatOllama \u6a21\u578b\u3002<\/li>\n<li>\u4f7f\u7528\u00a0<code>bindTools<\/code>\u00a0\u65b9\u6cd5\u5c06\u5de5\u5177\u7ed1\u5b9a\u5230\u6a21\u578b\u3002<\/li>\n<li>\u4f7f\u7528\u00a0<code>invoke<\/code>\u00a0\u65b9\u6cd5\u53d1\u9001\u4e00\u4e2a\u9700\u8981\u8ba1\u7b97\u7684\u95ee\u9898\uff0c\u6a21\u578b\u4f1a\u81ea\u52a8\u8c03\u7528\u76f8\u5e94\u7684\u5de5\u5177\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2>4. \u8fdb\u9636\u7528\u6cd5<\/h2>\n<h3>\u81ea\u5b9a\u4e49\u63d0\u793a\u6a21\u677f<\/h3>\n<p>\u81ea\u5b9a\u4e49\u63d0\u793a\u6a21\u677f\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u5185\u5bb9\u751f\u6210\u7684\u6548\u7387\uff0c\u8fd8\u786e\u4fdd\u4e86\u8f93\u51fa\u7684\u4e00\u81f4\u6027\u548c\u9488\u5bf9\u6027\u3002\u901a\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u6a21\u677f\uff0c\u6211\u4eec\u53ef\u4ee5\u5145\u5206\u5229\u7528AI\u6a21\u578b\u7684\u80fd\u529b\uff0c\u540c\u65f6\u4fdd\u6301\u5bf9\u8f93\u51fa\u5185\u5bb9\u7684\u63a7\u5236\u548c\u6307\u5bfc\uff1a<\/p>\n<pre><code>import { ChatOllama } from \"@langchain\/ollama\";\r\nimport { ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate } from \"@langchain\/core\/prompts\";\r\n\/\/ \u521d\u59cb\u5316ChatOllama\u6a21\u578b\r\nconst model = new ChatOllama({\r\nmodel: \"llama3.1\",\r\ntemperature: 0.7,\r\n});\r\nconst systemMessageContent = `\r\n\u4f60\u662f\u4e00\u4f4d\u7ecf\u9a8c\u4e30\u5bcc\u7684\u7535\u5546\u6587\u6848\u64b0\u5199\u4e13\u5bb6\u3002\u4f60\u7684\u4efb\u52a1\u662f\u6839\u636e\u7ed9\u5b9a\u7684\u4ea7\u54c1\u4fe1\u606f\u521b\u4f5c\u5438\u5f15\u4eba\u7684\u5546\u54c1\u63cf\u8ff0\u3002\r\n\u8bf7\u786e\u4fdd\u4f60\u7684\u63cf\u8ff0\u7b80\u6d01\u3001\u6709\u529b\uff0c\u5e76\u4e14\u7a81\u51fa\u4ea7\u54c1\u7684\u6838\u5fc3\u4f18\u52bf\u3002\r\n`;\r\nconst humanMessageTemplate = `\r\n\u8bf7\u4e3a\u4ee5\u4e0b\u4ea7\u54c1\u521b\u4f5c\u4e00\u6bb5\u5438\u5f15\u4eba\u7684\u5546\u54c1\u63cf\u8ff0\uff1a\r\n\u4ea7\u54c1\u7c7b\u578b: {product_type}\r\n\u6838\u5fc3\u7279\u6027: {key_feature}\r\n\u76ee\u6807\u53d7\u4f17: {target_audience}\r\n\u4ef7\u683c\u533a\u95f4: {price_range}\r\n\u54c1\u724c\u5b9a\u4f4d: {brand_positioning}\r\n\u8bf7\u63d0\u4f9b\u4ee5\u4e0b\u4e09\u79cd\u4e0d\u540c\u98ce\u683c\u7684\u63cf\u8ff0\uff0c\u6bcf\u79cd\u5927\u7ea650\u5b57\uff1a\r\n1. \u7406\u6027\u5206\u6790\u578b\r\n2. \u60c5\u611f\u8bc9\u6c42\u578b\r\n3. \u6545\u4e8b\u5316\u8425\u9500\u578b\r\n`;\r\nconst prompt = ChatPromptTemplate.fromMessages([\r\nSystemMessagePromptTemplate.fromTemplate(systemMessageContent),\r\nHumanMessagePromptTemplate.fromTemplate(humanMessageTemplate),\r\n]);\r\nconst chain = prompt.pipe(model);\r\nasync function generateProductDescriptions(productInfo) {\r\nconst response = await chain.invoke(productInfo);\r\nreturn response.content;\r\n}\r\n\/\/ \u793a\u4f8b\u4f7f\u7528\r\nconst productInfo = {\r\nproduct_type: \"\u667a\u80fd\u624b\u8868\",\r\nkey_feature: \"\u5fc3\u7387\u76d1\u6d4b\u548c\u7761\u7720\u5206\u6790\",\r\ntarget_audience: \"\u6ce8\u91cd\u5065\u5eb7\u7684\u5e74\u8f7b\u4e13\u4e1a\u4eba\u58eb\",\r\nprice_range: \"\u4e2d\u9ad8\u7aef\",\r\nbrand_positioning: \"\u79d1\u6280\u4e0e\u5065\u5eb7\u7684\u5b8c\u7f8e\u7ed3\u5408\"\r\n};\r\ngenerateProductDescriptions(productInfo)\r\n.then((result) =&gt; console.log(result))\r\n.catch((error) =&gt; console.error(\"Error:\", error));\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee3\u7801\uff1a<\/p>\n<pre><code>node advanced_prompt.js\r\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u63d0\u793a\u6a21\u677f\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u7cfb\u7edf\u6d88\u606f\u548c\u4eba\u7c7b\u6d88\u606f\u6a21\u677f\u3002<\/li>\n<li>\u4f7f\u7528\u00a0<code>ChatPromptTemplate.fromMessages<\/code>\u00a0\u521b\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u63d0\u793a\u6a21\u677f\u3002<\/li>\n<li>\u4f7f\u7528\u00a0<code>pipe<\/code>\u00a0\u65b9\u6cd5\u5c06\u63d0\u793a\u6a21\u677f\u4e0e\u6a21\u578b\u8fde\u63a5\u8d77\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u5904\u7406\u94fe\u3002<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u751f\u6210\u4ea7\u54c1\u63cf\u8ff0\uff0c\u8be5\u51fd\u6570\u4f7f\u7528\u5904\u7406\u94fe\u6765\u5904\u7406\u8f93\u5165\u7684\u4ea7\u54c1\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<p>\u81ea\u5b9a\u4e49\u63d0\u793a\u6a21\u677f\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u7528\u9014\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u751f\u6210\u7279\u5b9a\u683c\u5f0f\u6216\u98ce\u683c\u7684\u5185\u5bb9\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<ol>\n<li>\u7535\u5b50\u5546\u52a1\u4ea7\u54c1\u63cf\u8ff0\u751f\u6210\uff1a\u6b63\u5982\u672c\u4f8b\u6240\u793a\uff0c\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u751f\u6210\u4e0d\u540c\u98ce\u683c\u7684\u4ea7\u54c1\u63cf\u8ff0\uff0c\u63d0\u9ad8\u4ea7\u54c1\u9875\u9762\u7684\u5438\u5f15\u529b\u548c\u8f6c\u5316\u7387\u3002<\/li>\n<li>\u5ba2\u6237\u670d\u52a1\u56de\u590d\u6a21\u677f\uff1a\u53ef\u4ee5\u521b\u5efa\u5404\u79cd\u60c5\u666f\u7684\u56de\u590d\u6a21\u677f\uff0c\u5982\u5904\u7406\u6295\u8bc9\u3001\u63d0\u4f9b\u4ea7\u54c1\u4fe1\u606f\u7b49\uff0c\u786e\u4fdd\u5ba2\u670d\u56de\u590d\u7684\u4e00\u81f4\u6027\u548c\u4e13\u4e1a\u6027\u3002<\/li>\n<li>\u65b0\u95fb\u62a5\u9053\u751f\u6210\uff1a\u53ef\u4ee5\u8bbe\u8ba1\u6a21\u677f\u6765\u751f\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u65b0\u95fb\u62a5\u9053\uff0c\u5982\u7a81\u53d1\u65b0\u95fb\u3001\u6df1\u5ea6\u5206\u6790\u7b49\uff0c\u5e2e\u52a9\u8bb0\u8005\u5feb\u901f\u8d77\u8349\u521d\u7a3f\u3002<\/li>\n<li>\u4e2a\u6027\u5316\u8425\u9500\u90ae\u4ef6\uff1a\u6839\u636e\u5ba2\u6237\u6570\u636e\u548c\u8425\u9500\u76ee\u6807\uff0c\u751f\u6210\u4e2a\u6027\u5316\u7684\u8425\u9500\u90ae\u4ef6\u5185\u5bb9\uff0c\u63d0\u9ad8\u90ae\u4ef6\u8425\u9500\u7684\u6548\u679c\u3002<\/li>\n<\/ol>\n<h3>\u9ad8\u7ea7JSON\u8f93\u51fa\u4e0e\u77e5\u8bc6\u56fe\u8c31\u751f\u6210<\/h3>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Ollama \u548c LangChain \u751f\u6210\u7ed3\u6784\u5316\u7684 JSON \u8f93\u51fa\uff0c\u7279\u522b\u662f\u7528\u4e8e\u521b\u5efa\u77e5\u8bc6\u56fe\u8c31\u3002 \u8fd9\u79cd\u65b9\u6cd5\u4e0e Microsoft \u7684\u5f00\u6e90\u9879\u76ee GraphRAG \u6709\u7740\u5bc6\u5207\u7684\u8054\u7cfb\uff0c\u5c24\u5176\u662f\u5728\u81ea\u52a8\u5316\u77e5\u8bc6\u63d0\u53d6\u548c\u4e09\u5143\u7ec4\u751f\u6210\u65b9\u9762\u3002<\/p>\n<pre><code>import { ChatOllama } from \"@langchain\/ollama\";\r\nimport { PromptTemplate } from \"@langchain\/core\/prompts\";\r\nimport { HumanMessage, SystemMessage } from \"@langchain\/core\/messages\";\r\nconst systemTemplate = `\r\n\u4f60\u662f\u4e00\u4f4d\u533b\u7597\u9886\u57df\u7684\u4e13\u5bb6\uff0c\u64c5\u957f\u521b\u5efa\u77e5\u8bc6\u56fe\u8c31\u3002\u8bf7\u5c06\u6240\u6709\u54cd\u5e94\u683c\u5f0f\u5316\u4e3a\u5177\u6709\u4ee5\u4e0b\u7ed3\u6784\u7684JSON\u5bf9\u8c61\uff1a\r\n{\r\n\"\u8282\u70b9\": [\r\n{\"id\": \"string\", \"\u6807\u7b7e\": \"string\", \"\u7c7b\u578b\": \"string\"}\r\n],\r\n\"\u5173\u7cfb\": [\r\n{\"\u6e90\": \"string\", \"\u76ee\u6807\": \"string\", \"\u5173\u7cfb\": \"string\"}\r\n]\r\n}\r\n\u786e\u4fdd\u6240\u6709\u8282\u70b9id\u90fd\u662f\u552f\u4e00\u7684\uff0c\u5e76\u4e14\u5173\u7cfb\u5f15\u7528\u7684\u662f\u5df2\u5b58\u5728\u7684\u8282\u70b9id\u3002\r\n`;\r\nconst humanTemplate = `\r\n\u8bf7\u4e3a\u533b\u7597\u4e3b\u9898\"{topic}\"\u521b\u5efa\u4e00\u4e2a\u77e5\u8bc6\u56fe\u8c31\u3002\u5305\u62ec\u4ee5\u4e0b\u76f8\u5173\u6982\u5ff5: {concepts}\u3002\r\n\u63d0\u4f9b\u81f3\u5c115\u4e2a\u8282\u70b9\u548c5\u4e2a\u5173\u7cfb\u3002\u8bf7\u786e\u4fdd\u4f7f\u7528\u4e2d\u6587\u56de\u7b54\u3002\r\n`;\r\nconst systemMessage = new SystemMessage(systemTemplate);\r\nconst humanPrompt = PromptTemplate.fromTemplate(humanTemplate);\r\nconst llmJsonMode = new ChatOllama({\r\nbaseUrl: \"http:\/\/localhost:11434\", \/\/ \u9ed8\u8ba4\u503c\r\nmodel: \"llama3.1\",\r\nformat: \"json\",\r\n});\r\nasync function generateMedicalKnowledgeGraph(topic, concepts) {\r\ntry {\r\nconst humanMessageContent = await humanPrompt.format({\r\ntopic: topic,\r\nconcepts: concepts.join(\"\u3001\"),\r\n});\r\nconst humanMessage = new HumanMessage(humanMessageContent);\r\nconst messages = [systemMessage, humanMessage];\r\nconst result = await llmJsonMode.call(messages);\r\nconsole.log(JSON.stringify(result, null, 2));\r\nreturn result;\r\n} catch (error) {\r\nconsole.error(\"\u751f\u6210\u77e5\u8bc6\u56fe\u8c31\u65f6\u51fa\u9519:\", error);\r\n}\r\n}\r\n\/\/ \u4f7f\u7528\u793a\u4f8b\r\nconst topic = \"\u7cd6\u5c3f\u75c5\";\r\nconst concepts = [\"\u80f0\u5c9b\u7d20\", \"\u8840\u7cd6\", \"\u5e76\u53d1\u75c7\", \"\u996e\u98df\u7ba1\u7406\", \"\u8fd0\u52a8\u7597\u6cd5\"];\r\ngenerateMedicalKnowledgeGraph(topic, concepts);\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee3\u7801\uff1a<\/p>\n<pre><code>node advanced_json.js\r\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Ollama \u751f\u6210\u7ed3\u6784\u5316\u7684 JSON \u8f93\u51fa\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u7cfb\u7edf\u6a21\u677f\uff0c\u6307\u5b9a\u671f\u671b\u7684 JSON \u7ed3\u6784\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u4eba\u7c7b\u63d0\u793a\u6a21\u677f\uff0c\u7528\u4e8e\u751f\u6210\u77e5\u8bc6\u56fe\u8c31\u7684\u8bf7\u6c42\u3002<\/li>\n<li>\u521d\u59cb\u5316 ChatOllama \u6a21\u578b\uff0c\u8bbe\u7f6e\u00a0<code>format: \"json\"<\/code>\u00a0\u4ee5\u83b7\u53d6JSON\u8f93\u51fa\u3002<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u751f\u6210\u533b\u7597\u77e5\u8bc6\u56fe\u8c31\uff0c\u8be5\u51fd\u6570\u7ec4\u5408\u7cfb\u7edf\u6d88\u606f\u548c\u4eba\u7c7b\u6d88\u606f\uff0c\u5e76\u8c03\u7528\u6a21\u578b\u3002<\/li>\n<\/ol>\n<p>\u6211\u4eec\u53ef\u4ee5\u7aa5\u89c1\u5230\u901a\u8fc7 json \u683c\u5f0f\u7684\u7279\u5b9a\u8f93\u51fa\uff0c\u53ef\u4ee5\u5c06\u5176\u8fd0\u7528\u5230\u8bb8\u591a\u65b9\u9762\uff1a<\/p>\n<ol>\n<li><strong>\u81ea\u52a8\u5316\u4e09\u5143\u7ec4\u751f\u6210<\/strong>\u00a0\uff1a \u5728\u4f20\u7edf\u65b9\u6cd5\u4e2d\uff0c\u521b\u5efa\u77e5\u8bc6\u56fe\u8c31\u901a\u5e38\u9700\u8981\u5927\u91cf\u7684\u4eba\u5de5\u6807\u6ce8\u5de5\u4f5c\u3002\u4e13\u5bb6\u9700\u8981\u4ed4\u7ec6\u9605\u8bfb\u6587\u6863\uff0c\u8bc6\u522b\u91cd\u8981\u6982\u5ff5\u548c\u5b83\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u7136\u540e\u624b\u52a8\u521b\u5efa\u4e09\u5143\u7ec4\uff08\u4e3b\u4f53-\u5173\u7cfb-\u5ba2\u4f53\uff09\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u4e0d\u4ec5\u8017\u65f6\uff0c\u800c\u4e14\u5bb9\u6613\u51fa\u9519\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6587\u6863\u65f6\u3002 \u4f7f\u7528\u6211\u4eec\u7684\u65b9\u6cd5\uff0c\u5927\u8bed\u8a00\u6a21\u578b\u53ef\u4ee5\u81ea\u52a8\u4ece\u7ed9\u5b9a\u7684\u4e3b\u9898\u548c\u6982\u5ff5\u751f\u6210\u76f8\u5173\u7684\u8282\u70b9\u548c\u5173\u7cfb\u3002\u8fd9\u6781\u5927\u5730\u52a0\u901f\u4e86\u77e5\u8bc6\u56fe\u8c31\u7684\u6784\u5efa\u8fc7\u7a0b\u3002<\/li>\n<li><strong>\u6570\u636e\u589e\u5f3a<\/strong>\u00a0\uff1a \u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u76f4\u63a5\u751f\u6210\u77e5\u8bc6\u56fe\u8c31\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u589e\u5f3a\u3002\u4f8b\u5982\uff1a\n<ul>\n<li>\u6269\u5c55\u73b0\u6709\u7684\u8bad\u7ec3\u6570\u636e\u96c6\uff1a\u901a\u8fc7\u8ba9\u6a21\u578b\u57fa\u4e8e\u5df2\u6709\u7684\u4e09\u5143\u7ec4\u751f\u6210\u65b0\u7684\u76f8\u5173\u4e09\u5143\u7ec4\u3002<\/li>\n<li>\u521b\u5efa\u591a\u6837\u5316\u7684\u793a\u4f8b\uff1a\u4e3a\u4e0d\u540c\u7684\u533b\u7597\u6761\u4ef6\u751f\u6210\u77e5\u8bc6\u56fe\u8c31\uff0c\u589e\u52a0\u6570\u636e\u7684\u591a\u6837\u6027\u3002<\/li>\n<li>\u8de8\u8bed\u8a00\u6570\u636e\u751f\u6210\uff1a\u901a\u8fc7\u8c03\u6574\u63d0\u793a\uff0c\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u8bed\u8a00\u7684\u77e5\u8bc6\u56fe\u8c31\uff0c\u652f\u6301\u591a\u8bed\u8a00\u5e94\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u63d0\u9ad8\u6570\u636e\u8d28\u91cf<\/strong>\u00a0\uff1a \u5927\u8bed\u8a00\u6a21\u578b\u53ef\u4ee5\u5229\u7528\u5176\u5e7f\u6cdb\u7684\u77e5\u8bc6\u57fa\u7840\uff0c\u751f\u6210\u9ad8\u8d28\u91cf\u3001\u8fde\u8d2f\u7684\u77e5\u8bc6\u56fe\u8c31\u3002\u901a\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u63d0\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u6570\u636e\u7b26\u5408\u7279\u5b9a\u7684\u8d28\u91cf\u6807\u51c6\u548c\u9886\u57df\u89c4\u8303\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027<\/strong>\u00a0\uff1a \u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u8f7b\u677e\u9002\u5e94\u4e0d\u540c\u7684\u9886\u57df\u548c\u9700\u6c42\uff1a\n<ul>\n<li>\u901a\u8fc7\u4fee\u6539\u7cfb\u7edf\u63d0\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u8f93\u51fa\u7684JSON\u7ed3\u6784\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u77e5\u8bc6\u56fe\u8c31\u683c\u5f0f\u3002<\/li>\n<li>\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6b64\u65b9\u6cd5\u6269\u5c55\u5230\u5176\u4ed6\u9886\u57df\uff0c\u5982\u79d1\u6280\u3001\u91d1\u878d\u3001\u6559\u80b2\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Ollama \u548c LangChain \u5728 JavaScript \u73af\u5883\u4e2d\u6784\u5efa\u5404\u79cd AI \u5e94\u7528\uff0c\u4ece\u7b80\u5355\u7684\u5bf9\u8bdd\u7cfb\u7edf\u5230\u590d\u6742\u7684\u77e5\u8bc6\u56fe\u8c31\u751f\u6210\u3002\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\u4e3a\u5f00\u53d1\u5f3a\u5927\u7684AI\u5e94\u7528\u63d0\u4f9b\u4e86\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<p>Ollama \u548c LangChain \u7684\u7ed3\u5408\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\u548c\u53ef\u80fd\u6027\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u548c\u7ec4\u4ef6\uff0c\u6784\u5efa\u51fa\u9002\u5408\u4f60\u7684\u5e94\u7528\u573a\u666f\u7684 AI \u7cfb\u7edf\u3002<\/p>\n<p>\u968f\u7740\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u6211\u4eec\u671f\u5f85\u770b\u5230\u66f4\u591a\u521b\u65b0\u7684\u5e94\u7528\u51fa\u73b0\u3002\u5e0c\u671b\u8fd9\u4e2a\u6307\u5357\u80fd\u591f\u5e2e\u52a9\u4f60\u5f00\u59cb\u4f60\u7684 AI \u5f00\u53d1\u4e4b\u65c5\uff0c\u5e76\u6fc0\u53d1\u4f60\u7684\u521b\u9020\u529b\uff0c\u53bb\u63a2\u7d22 AI \u6280\u672f\u7684\u65e0\u9650\u53ef\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u672c\u6587\u6863\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 JavaScript \u73af\u5883\u4e2d\u4f7f\u7528 Ollama \u4e0e LangChain \u96c6\u6210\uff0c\u4ee5\u521b\u5efa\u5f3a\u5927\u7684 AI \u5e94\u7528\u3002Ollama \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5927\u8bed\u8a00\u6a21\u578b\u90e8\u7f72\u5de5\u5177\uff0c\u800c LangChain \u5219\u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u57fa\u4e8e\u8bed\u8a00\u6a21\u578b\u7684\u5e94\u7528\u7684\u6846\u67b6&#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-28557","post","type-post","status-publish","format-standard","hentry","category-shicao"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/28557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/comments?post=28557"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/28557\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media?parent=28557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/categories?post=28557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/tags?post=28557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}