{"id":1355,"date":"2024-08-16T07:51:54","date_gmt":"2024-08-15T23:51:54","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1355"},"modified":"2024-08-15T08:53:11","modified_gmt":"2024-08-15T00:53:11","slug":"%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0-ai-%e9%a1%be%e6%8a%95%ef%bc%9a%e5%9c%a8-ollama-%e4%b8%ad%e4%bd%bf%e7%94%a8-python-%e8%b0%83%e7%94%a8%e9%87%91%e8%9e%8d-api-%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/www.laoyulaoyu.com\/index.php\/2024\/08\/16\/%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0-ai-%e9%a1%be%e6%8a%95%ef%bc%9a%e5%9c%a8-ollama-%e4%b8%ad%e4%bd%bf%e7%94%a8-python-%e8%b0%83%e7%94%a8%e9%87%91%e8%9e%8d-api-%e6%9c%8d%e5%8a%a1\/","title":{"rendered":"\u624b\u628a\u624b\u6559\u4f60 AI \u987e\u6295\uff1a\u5728 Ollama \u4e2d\u4f7f\u7528 Python \u8c03\u7528\u91d1\u878d API \u670d\u52a1"},"content":{"rendered":"\n<p>\u4f5c\u8005\uff1a<a href=\"https:\/\/www.laoyulaoyu.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u8001\u4f59\u635e\u9c7c<\/a><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002<\/mark><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-3.png\" alt=\"\" class=\"wp-image-1460\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-verse\"><strong>\u5199\u5728\u524d\u9762\u7684\u8bdd\uff1a<\/strong>\u5c3d\u7ba1Ollama\u672c\u8eab\u4e0d\u662f\u4e00\u4e2a\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u5f00\u53d1\u5de5\u5177\uff0c\u800c\u662f\u4e00\u4e2aLLM\u7684\u5e73\u53f0\u3002\u4f46\u5b83\u6700\u8fd1\u5ba3\u5e03\u4e86\u5176\u7cfb\u7edf\u4e2d\u76f4\u63a5\u4f7f\u7528\u5de5\u5177\u8c03\u7528\u7684\u529f\u80fd\uff0c\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86\u6700\u65b0\u7248\u672c\u7684\u4e3b\u8981\u66f4\u65b0\uff0c\u7279\u522b\u662f\u5173\u4e8e\u529f\u80fd\u8c03\u7528\u7684\u4f7f\u7528\uff0c\u5e76\u6f14\u793a\u4e86<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5982\u4f55\u5728Ollama \u4e2d\u4f7f\u7528 Python \u8c03\u7528\u5916\u90e8 API \u670d\u52a1\u3002<\/mark><\/pre>\n<\/blockquote>\n\n\n\n<p id=\"7ca0\"><a href=\"https:\/\/levelup.gitconnected.com\/introduction-to-ollama-part-1-1156f9563b8d\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001 \u90e8\u7f72 Ollama \u5728\u672c\u5730\u8fd0\u884c <\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"66e0\"><br><strong>1.1 \u8bbe\u7f6e\u5f00\u53d1\u73af\u5883<\/strong><\/h3>\n\n\n\n<p id=\"00a7\">\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u6700\u597d\u7684\u505a\u6cd5\u662f\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u5f00\u53d1\u73af\u5883\uff0c\u4ee5\u4fdd\u6301\u6211\u4eec\u7684\u9879\u76ee\u5404\u81ea\u72ec\u7acb\u3002\u6211\u4f7f\u7528 conda \u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u4f46\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528\u81ea\u5df1\u559c\u6b22\u7684\u5de5\u5177\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS C:\\Users\\thoma&gt; conda create -n ollama_tools python=3.11 -y\nPS C:\\Users\\thoma&gt; conda activate ollama_tools\n(ollama_tools) PS C:\\Users\\thoma&gt; pip install jupyter yfinance ollama<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u5b89\u88c5 Ollama<\/strong><\/h3>\n\n\n\n<p>\u8bbf\u95ee <a href=\"https:\/\/ollama.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">ollama.com<\/a>\uff0c\u4e3a\u4f60\u7684\u7cfb\u7edf\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684 Ollama\uff0c\u7136\u540e\u5b89\u88c5\u3002\u68c0\u67e5\u60a8\u7684\u7248\u672c\u3002\u60a8\u81f3\u5c11\u9700\u8981 \u4e0b\u8f7d 0.3.0 \u7248\u624d\u80fd\u4f7f\u7528\u5de5\u5177\u8c03\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(ollama_tools) PS C:\\Users\\thoma&gt; ollama --version\nollama version is 0.3.0<\/code><\/pre>\n\n\n\n<p id=\"4a02\">\u9009\u62e9\u60a8\u7684 LLM \u6a21\u578b\u5e76\u901a\u8fc7 Ollama \u4e0b\u8f7d\uff0c\u6709\u5173\u5141\u8bb8\u4f7f\u7528\u5de5\u5177\u7684 Ollama\u517c\u5bb9\u578b\u53f7\u5217\u8868\uff0c\u8bf7\u67e5\u770b\u5176\u7f51\u7ad9\u7684\u8fd9\u4e00\u90e8\u5206\u3002<a href=\"https:\/\/ollama.com\/search?c=tools\"><\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/ollama.com\/search?c=tools\"><strong>Get up and running with large language models.<\/strong><\/a><\/h4>\n\n\n\n<p id=\"9970\"><br>\u4e00\u65e6\u51b3\u5b9a\u8981\u4f7f\u7528\u54ea\u79cd\u578b\u53f7\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 Ollama \u5c06\u5176\u63d0\u53d6\uff08\u4e0b\u8f7d\uff09\u5230\u672c\u5730\u7cfb\u7edf\u3002\u4f8b\u5982\uff0c\u6211\u9009\u62e9\u4f7f\u7528 Llama 3.1 8B \u578b\u53f7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(ollama_tools) PS C:\\Users\\thoma&gt; ollama pull llama3.1<\/code><\/pre>\n\n\n\n<p>\u5927\u6982\u4e00\u5230\u4e24\u5206\u949f\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u67e5\u770b\u672c\u5730\u7cfb\u7edf\u4e2d\u7684\u578b\u53f7<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(ollama_tools) PS C:\\Users\\thoma&gt; ollama list<\/code><\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u8981\u8fd0\u884c\u4e00\u4e2a\u6a21\u578b\uff08\u5728\u4e0e\u4e4b\u4ea4\u4e92\u4e4b\u524d\u9700\u8981\u8fd9\u6837\u505a\uff09\uff0c\u8bf7\u4f7f\u7528\u8fd0\u884c\u547d\u4ee4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(ollama_tools) PS C:\\Users\\thoma&gt; ollam run llama3.1:latest --keepalive 120m<\/code><\/pre>\n\n\n\n<p>\u8fdb\u5165 &gt;&gt;&gt; \u63d0\u793a\u7b26\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u9000\u51fa\uff0c\u6a21\u578b\u5c06\u7ee7\u7eed\u5728\u540e\u53f0\u8fd0\u884c 120 \u5206\u949f\uff0c\u7136\u540e\u81ea\u52a8\u5378\u8f7d\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u4f7f\u7528\u5de5\u5177<\/strong>\uff1f<\/h2>\n\n\n\n<p id=\"c58a\">\u6211\u4eec\u8981\u6c42\u4f7f\u7528\u5de5\u5177\u7684\u539f\u56e0\u662f\uff0cLLMs\u7684\u77e5\u8bc6\u4e00\u822c\u90fd\u6709\u4e00\u4e2a\u622a\u6b62\u65e5\u671f\u3002\u8fd9\u610f\u5473\u7740\u4ed6\u4eec\u4e0d\u4e86\u89e3\u65f6\u4e8b\u3001\u5f62\u52bf\u7b49&#8230;&#8230;\u9664\u975e\u4ed6\u4eec\u80fd\u76f4\u63a5\u8bbf\u95ee\u4e92\u8054\u7f51\uff0c\u800c\u5927\u591a\u6570LLMs\u90fd\u4e0d\u80fd\u3002<\/p>\n\n\n\n<p id=\"e8d6\">\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u95eeLLMs\u5317\u4eac\u76ee\u524d\u7684\u6c14\u6e29\u662f\u591a\u5c11\u5b83\u662f\u65e0\u6cd5\u544a\u8bc9\u4f60\u7684\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u67d0\u53ea\u80a1\u7968\u7684\u5f53\u524d\u4ef7\u683c\u4e5f\u662f\u5982\u6b64\u3002\u8981\u56de\u7b54\u6211\u4eec\u7684\u95ee\u9898\uff0c\u9996\u5148\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u8fd4\u56de\u80a1\u7968\u7684\u5f53\u524d\u4ef7\u683c\u3002\u67e5\u627e\u80a1\u7968\u5f53\u524d\u4ef7\u683c\u7684\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; what is the current price of MSFT (Microsoft)\n\nAs I'm a large language model, I don't have direct access to real-time market data. However, I can suggest some\nways for you to find out the current stock price of Microsoft (MSFT):\n\n1. **Financial websites**: Visit reputable financial websites like:\n        * Yahoo Finance (finance.yahoo.com)\n        * Google Finance (finance.google.com)\n        * Bloomberg (bloomberg.com)\n        * CNBC (cnbc.com)\n2. **Stock market apps**: Use a mobile app like:\n        * Robinhood\n        * Fidelity Investments\n        * eTrade\n        * TradingView\n3. **Direct from the source**: Visit Microsoft's investor relations page (ir.microsoft.com) for real-time stock\ndata.\n\nAs of my knowledge cutoff, the current price of MSFT was around $245-$250 per share. However, please note that\nthis information may be outdated, and I recommend checking a reliable financial website or app for the most recent\nand accurate price.\n\nIf you want to get an estimate, here are some historical price ranges:\n\n* 52-week high: around $280\n* 52-week low: around $210\n\nKeep in mind that stock prices can fluctuate rapidly due to various market factors. Always consult multiple\nsources or a financial advisor for more precise and up-to-date information.<\/code><\/pre>\n\n\n\n<p>\u9996\u5148\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u8fd4\u56de\u80a1\u7968\u7684\u5f53\u524d\u4ef7\u683c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u96c5\u864e\u8d22\u7ecf\u7684API\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import yfinance as yf\n\ndef get_current_stock_price(ticker_symbol):\n\n  # Get the stock data\n  stock = yf.Ticker(ticker_symbol)\n\n  # Get the current price\n  current_price = stock.history(period='1d')&#91;'Close'].iloc&#91;0]\n\n  return current_price<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\uff0c\u5411 Ollama \u6307\u5b9a\u5de5\u5177\uff08\u529f\u80fd\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import ollama\n\n# Want the current price of Microsoft - ticker symbol MSFT\n\nresponse = ollama.chat(\n    model='llama3.1',\n    messages=&#91;{'role': 'user','content': \n        'What is the current price of MSFT'}],\n\n  # provide a tool to get the current price of a stock\n    tools=&#91;{\n      'type': 'function',\n      'function': {\n        'name': 'get_current_stock_price',\n        'description': 'Get the current price for a stock',\n        'parameters': {\n          'type': 'object',\n          'properties': {\n            'ticker_symbol': {\n              'type': 'string',\n              'description': 'The ticker symbol of the stock',\n            },\n          },\n          'required': &#91;'ticker_symbol'],\n        },\n      },\n    },\n  ],\n)\n\nprint(response&#91;'message']&#91;'tool_calls'])<\/code><\/pre>\n\n\n\n<p>\u52a8\u6001\u8c03\u7528\u5de5\u5177\uff08Call the tool dynamically\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create a dictionary mapping function names to functions\nfunction_map = {\n    'get_current_stock_price': get_current_stock_price,\n    # Add more functions here as needed\n}\n\ndef call_function_safely(response, function_map):\n    # Extract the function name and arguments from the response\n    tool_call = response&#91;'message']&#91;'tool_calls']&#91;0]\n    function_name = tool_call&#91;'function']&#91;'name']\n    arguments = tool_call&#91;'function']&#91;'arguments']\n\n    # Look up the function in the function map\n    function_to_call = function_map.get(function_name)\n\n    if function_to_call:\n        try:\n            # Call the function with the arguments\n            result = function_to_call(**arguments)\n            print(f\"The current price of {arguments&#91;'ticker_symbol']} is : {result}\")\n        except TypeError as e:\n            print(f\"Argument error: {e}\")\n    else:\n        print(f\"{function_name} is not a recognized function\")\n\ncall_function_safely(response, function_map)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The current price of MSFT is : 427.4599914550781<\/code><\/pre>\n\n\n\n<p>\u5230\u8fd9\u91cc\u5df2\u7ecf\u5b8c\u6210\u4e86\u6574\u4e2a\u5728Ollama \u4e2d\u4f7f\u7528 Python \u8c03\u7528\u96c5\u864e\u8d22\u7ecf\u7684 API \u670d\u52a1\uff0c\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u7528\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/08\/image-4.png\" alt=\"\" class=\"wp-image-1462\"\/><\/figure>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\u672c\u6587\u5185\u5bb9\u4ec5\u4ec5\u662f\u6280\u672f\u63a2\u8ba8\u548c\u5b66\u4e60\uff0c\u5e76\u4e0d\u6784\u6210\u4efb\u4f55\u6295\u8d44\u5efa\u8bae\u3002<br>\u8f6c\u53d1\u8bf7\u6ce8\u660e\u539f\u4f5c\u8005<\/mark><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u8001\u4f59\u635e\u9c7c \u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u8bf7\u6807\u660e\u51fa\u5904\u53ca\u539f\u4f5c\u8005\u3002&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.laoyulaoyu.com\/index.php\/2024\/08\/16\/%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0-ai-%e9%a1%be%e6%8a%95%ef%bc%9a%e5%9c%a8-ollama-%e4%b8%ad%e4%bd%bf%e7%94%a8-python-%e8%b0%83%e7%94%a8%e9%87%91%e8%9e%8d-api-%e6%9c%8d%e5%8a%a1\/\">Continue reading<span class=\"screen-reader-text\">\u624b\u628a\u624b\u6559\u4f60 AI \u987e\u6295\uff1a\u5728 Ollama \u4e2d\u4f7f\u7528 Python \u8c03\u7528\u91d1\u878d API \u670d\u52a1<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[5],"class_list":["post-1355","post","type-post","status-publish","format-standard","hentry","category-aiinvest","tag-ai","entry"],"_links":{"self":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1355"}],"version-history":[{"count":1,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1355\/revisions"}],"predecessor-version":[{"id":1356,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/posts\/1355\/revisions\/1356"}],"wp:attachment":[{"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laoyulaoyu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}