{"id":1508,"date":"2024-10-11T07:45:00","date_gmt":"2024-10-10T23:45:00","guid":{"rendered":"https:\/\/blog.laoyulaoyu.top\/?p=1508"},"modified":"2024-09-14T10:17:43","modified_gmt":"2024-09-14T02:17:43","slug":"%e5%80%9f%e5%8a%a9-opena-%e6%89%93%e9%80%a0%e8%82%a1%e7%a5%a8%e5%88%86%e6%9e%90%e5%88%a9%e5%99%a8%ef%bc%8c%e6%8e%a2%e7%b4%a2%e7%a7%91%e6%8a%80%e8%82%a1%e6%8a%95%e8%b5%84%e4%b9%8b%e9%81%93","status":"publish","type":"post","link":"https:\/\/blog.laoyulaoyu.top\/index.php\/2024\/10\/11\/%e5%80%9f%e5%8a%a9-opena-%e6%89%93%e9%80%a0%e8%82%a1%e7%a5%a8%e5%88%86%e6%9e%90%e5%88%a9%e5%99%a8%ef%bc%8c%e6%8e%a2%e7%b4%a2%e7%a7%91%e6%8a%80%e8%82%a1%e6%8a%95%e8%b5%84%e4%b9%8b%e9%81%93\/","title":{"rendered":"\u501f\u52a9 OpenAI \u6253\u9020\u80a1\u7968\u5206\u6790\u5229\u5668\uff0c\u63a2\u7d22\u79d1\u6280\u80a1\u6295\u8d44\u4e4b\u9053"},"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\/09\/image-160.png\" alt=\"\" class=\"wp-image-2072\"\/><\/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>\u672c\u6587\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5206\u6b65\u6307\u5357\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6307\u5bfc\u5927\u5bb6\u5982\u4f55\u5229\u7528 OpenAI GPT-4 \u6a21\u578b\u548c \u96c5\u864e\u8d22\u7ecf yfinance \u5e93\u6bd4\u8f83\u4e0d\u540c\u80a1\u7968\u7684\u6295\u8d44\u4ef7\u503c<\/mark>\uff0c\u5e76\u901a\u8fc7\u5f02\u6b65\u8bf7\u6c42\u52a0\u5feb\u5904\u7406\u901f\u5ea6\uff0c\u6700\u7ec8\u6784\u5efa\u4e00\u4e2a\u57fa\u4e8e Gradio \u7684 Web UI \u5e94\u7528\u7a0b\u5e8f\u3002<\/pre>\n<\/blockquote>\n\n\n\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528 GPT-4 \u6bd4\u8f83\u6f5c\u5728\u7684\u80a1\u7968\u8d2d\u4e70\u3002\u5b83\u57fa\u4e8e Matt Shumer \u4f7f\u7528<a href=\"https:\/\/github.com\/mshumer\/gpt-investor\">Claude <\/a>\u8fdb\u884c\u91d1\u878d\u5206\u6790\u7684\u4ee3\u7801\u3002\u672c\u6559\u7a0b\u4ecb\u7ecd\u4e86\u5bf9\u8be5\u7b56\u7565\u7684\u4fee\u6539\uff0c\u4ee5\u4fbf\u4e0e OpenAI API \u914d\u5408\u4f7f\u7528\u3002\u6211\u4eec\u8fd8\u5b9e\u73b0\u4e86\u5f02\u6b65\u8bf7\u6c42\uff0c\u8fd9\u4e00\u6539\u52a8\u5927\u5927\u52a0\u5feb\u4e86\u5904\u7406\u901f\u5ea6\u3002\u6211\u4eec\u4f7f\u7528\u7684\u4e3b\u8981 python \u5e93\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>yfinance\uff1a&nbsp;<a href=\"https:\/\/finance.yahoo.com\/\">Yahoo finance<\/a> \u662f\u4e00\u4e2a python \u5e93\uff0c\u63d0\u4f9b\u7ebf\u7a0b\u548c Pythonic \u65b9\u5f0f\u4ece\u96c5\u864e\u8d22\u7ecf\u4e0b\u8f7d\u5e02\u573a\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9996\u5148\uff0c\u4e3a\u4e86\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a requirements.txt \u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yfinance\nrequests\nbeautifulsoup4\nopenai\npython-dotenv\ngradio<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 conda \u73af\u5883\u5e76\u5b89\u88c5\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>conda create --name gptinvestor python=3.10\nconda activate gptinvestor\npip install -r requirements.txt<\/code><\/pre>\n\n\n\n<p>\u7a0b\u5e8f\u5305\u62ec\u51e0\u4e2a\u8fde\u7eed\u7684\u51fd\u6570\u8c03\u7528\uff0c\u9996\u5148\u901a\u8fc7 yfinance \u5e93\u83b7\u53d6\u80a1\u7968\u4ee3\u7801\u6570\u636e\uff08\u4f8b\u5982\u5fae\u8f6f\u7684 MSFT\uff09\u3002\u6570\u636e\u68c0\u7d22\u5b8c\u6210\u540e\uff0c\u7a0b\u5e8f\u4f1a\u5c06\u8fd9\u4e9b\u4fe1\u606f\u8f6c\u53d1\u7ed9 OpenAI API \u8fdb\u884c\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u8be5\u7a0b\u5e8f\u7684\u5de5\u4f5c\u6d41\u7a0b\u6982\u89c8\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.laoyulaoyu.com\/wp-content\/uploads\/2024\/09\/image-161.png\" alt=\"\" class=\"wp-image-2073\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><br><strong>1. \u57fa\u7840\u4ee3\u7801<\/strong><\/h2>\n\n\n\n<p id=\"0d04\">\u6211\u4eec\u9996\u5148\u5bfc\u5165\u6240\u9700\u7684 python \u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from dataclasses import dataclass, field\nfrom typing import List, Dict, Optional\nimport pandas as pd\nimport asyncio\nimport requests\nfrom bs4 import BeautifulSoup\nfrom datetime import datetime, timedelta\n\nfrom openai import OpenAI, AsyncOpenAI\nfrom dotenv import load_dotenv\nimport yfinance as yf<\/pre>\n\n\n\n<p id=\"75a1\">\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e24\u4e2a OpenAI \u5ba2\u6237\u7aef\uff0c\u4e00\u4e2a\u7528\u4e8e\u5f02\u6b65\u8c03\u7528\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u6700\u540e\u7684\u540c\u6b65\u8c03\u7528\uff0c\u5728\u540c\u6b65\u8c03\u7528\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b GPT \u4e3a\u6211\u4eec\u5bf9\u80a1\u7968\u8fdb\u884c\u6392\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>load_dotenv(\"env_variables.env\")\nsyncclient = OpenAI()\nasyncclient = AsyncOpenAI()<\/code><\/pre>\n\n\n\n<p id=\"77e0\"><strong>\u6ce8\u610f\uff1a<\/strong>\u60a8\u5fc5\u987b\u5728\u8fd0\u884c\u4ee3\u7801\u7684\u540c\u4e00\u76ee\u5f55\u4e0b\u521b\u5efa\u540d\u4e3a env_variables.env \u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u653e\u5165 OpenAI API \u5bc6\u94a5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OPENAI_API_KEY=abcdefghijk12345<\/code><\/pre>\n\n\n\n<p id=\"c0e3\"><code>load_dotenv(\u201cenv_variables.env\u201d)<\/code>&nbsp;\u4f1a\u5c06\u4f60\u7684 OpenAI API \u5bc6\u94a5\u4f5c\u4e3a\u73af\u5883\u53d8\u91cf\u5373\u65f6\u52a0\u8f7d\u3002<\/p>\n\n\n\n<p id=\"33bb\">\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u80a1\u7968\u4ee3\u7801\u5b9a\u4e49\u4e00\u4e2a\u6570\u636e\u7c7b\uff0c\u5e76\u5c06\u8be5\u7c7b\u547d\u540d\u4e3a TickerClass\u3002\u8be5\u6570\u636e\u7c7b\u7528\u4e8e\u5b58\u50a8\u6bcf\u4e2a\u80a1\u7968\u4ee3\u7801\uff084 \u4e2a\u5b57\u6bcd\u7684\u80a1\u7968\u8868\u793a\u6cd5\uff09\u7684\u4e0d\u540c\u4fe1\u606f\u3002<\/p>\n\n\n\n<p id=\"2e27\">\u6211\u4eec\u5f15\u5165\u4e86\u4e00\u4e2a\u540d\u4e3a <code>TickerClass<\/code>\u7684\u6570\u636e\u7c7b\uff0c\u7528\u4e8e\u4fdd\u5b58\u6bcf\u4e2a\u80a1\u7968\u4ee3\u7801\u7684\u4e0d\u540c\u8be6\u7ec6\u4fe1\u606f\uff0c\u8fd9\u4e9b\u4ee3\u7801\u7531\u56db\u4e2a\u5b57\u6bcd\u7ec4\u6210\uff08\u5982 MSFT \u8868\u793a Microsoft\uff09\u3002\u8be5\u7c7b\u4f5c\u4e3a\u4e00\u4e2a\u7ed3\u6784\u5316\u5bb9\u5668\uff0c\u7528\u4e8e\u5b58\u50a8\u6bcf\u53ea\u80a1\u7968\u7684\u76f8\u5173\u4fe1\u606f\uff0c\u5305\u62ec\u5386\u53f2\u6570\u636e\u3001\u8d44\u4ea7\u8d1f\u503a\u8868\u3001\u5206\u6790\u5e08\u8bc4\u7ea7\u548c\u4ef7\u683c\u7b49\u3002\u6b64\u5916\uff0c\u5b83\u8fd8\u5305\u62ec\u60c5\u611f\u5206\u6790\u3001\u884c\u4e1a\u5206\u6790\u548c\u6700\u7ec8\u5206\u6790\u5b57\u6bb5\uff0c\u8fd9\u4e9b\u5b57\u6bb5\u662f\u6839\u636e OpenAI API \u7684\u7ed3\u679c\u586b\u5145\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@dataclass\nclass TickerClass:\n    name: str\n    hist_data: Optional&#91;pd.DataFrame] = field(default=None)\n    balance_sheet: Optional&#91;pd.DataFrame] = field(default=None)\n    financials: Optional&#91;pd.DataFrame] = field(default=None)\n    news: Optional&#91;Dict] = field(default=None)\n    analyst_ratings: str = field(default=None)\n    price: float = field(default=None)\n    ######\n    sentiment_analysis: str = field(default=None)\n    industry_analysis: str = field(default=None)\n    final_analysis: str = field(default=None)<\/code><\/pre>\n\n\n\n<p id=\"034a\">\u901a\u8fc7\u91c7\u7528\u8fd9\u79cd\u7c7b\u522b\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u6bcf\u53ea\u80a1\u7968\u7684\u6240\u6709\u5173\u952e\u7ec6\u8282\u548c\u5206\u6790\u89c1\u89e3\u90fd\u88ab\u6574\u9f50\u5730\u7ec4\u7ec7\u8d77\u6765\uff0c\u5e76\u4e14\u6613\u4e8e\u8bbf\u95ee\uff0c\u4ece\u800c\u7b80\u5316\u4e86\u6211\u4eec\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u5904\u7406\u548c\u68c0\u7d22\u6570\u636e\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p id=\"27a1\">\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4f7f\u7528 yfinance \u5e93\u83b7\u53d6\u6bcf\u53ea\u80a1\u7968\u539f\u59cb\u4fe1\u606f\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def get_article_text(url: str) -&gt; str:\n    try:\n        response = requests.get(url)\n        soup = BeautifulSoup(response.content, 'html.parser')\n        article_text = ' '.join(&#91;p.get_text() for p in soup.find_all('p')])\n        return article_text\n    except:\n        return \"Error retrieving article text.\"\n\ndef get_stock_data(ticker: str, years: int=10) -&gt; tuple:\n    end_date = datetime.now().date()\n    start_date = end_date - timedelta(days=years*365)\n\n    stock = yf.Ticker(ticker)\n\n    # Retrieve historical price data\n    hist_data = stock.history(start=start_date, end=end_date)\n\n    # Retrieve balance sheet\n    balance_sheet = stock.balance_sheet\n\n    # Retrieve financial statements\n    financials = stock.financials\n\n    # Retrieve news articles\n    news = stock.news\n\n    return hist_data, balance_sheet, financials, news\n\ndef get_analyst_ratings(ticker: str) -&gt; str:\n    stock = yf.Ticker(ticker)\n    recommendations = stock.get_recommendations()\n    if recommendations is None or recommendations.empty:\n        return \"No analyst ratings available.\"\n\n    latest_rating = recommendations.iloc&#91;0]\n\n\n    rating_summary = f\"Latest analyst rating for {ticker}:\\n {str(latest_rating.to_dict())}\"\n\n    return rating_summary\n\ndef get_current_price(ticker :str) -&gt; float:\n    stock = yf.Ticker(ticker)\n    data = stock.history(period='1d')\n    return data.iloc&#91;0]&#91;'Close']<\/code><\/pre>\n\n\n\n<p id=\"cb9c\">\u5728<code>get_stock_data<\/code> \u4e2d\uff0c\u6211\u4eec\u68c0\u7d22\u6bcf\u53ea\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e\u3001\u8d44\u4ea7\u8d1f\u503a\u8868\u3001\u8d22\u52a1\u4fe1\u606f\u548c\u6700\u65b0\u6d88\u606f\u3002\u5728 <code>get_analyst_ratings<\/code> \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u63a5\u6536\u6bcf\u53ea\u80a1\u7968\u7684\u6700\u65b0\u5206\u6790\u5e08\u8bc4\u7ea7\u3002\u5206\u6790\u5e08\u8bc4\u7ea7\u6839\u636e\u91d1\u878d\u5206\u6790\u5e08\u7684\u5efa\u8bae\u5bf9\u80a1\u7968\u8fdb\u884c\u5206\u7c7b\uff0c\u4f8b\u5982\u5efa\u8bae &#8220;\u5f3a\u529b\u4e70\u5165&#8221;\u3001&#8221;\u4e70\u5165&#8221;\u3001&#8221;\u6301\u6709 &#8220;\u7b49\u7684\u6570\u91cf\u3002\u6700\u540e\uff0c<code>get_current_price<\/code>\u4f1a\u83b7\u53d6\u80a1\u7968\u7684\u5f53\u524d\u4ef7\u683c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"55ab\"><strong>2. OpenAI \u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n\n\n\n<p id=\"cdf2\">\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5b9a\u4e49\u7528\u4e8e\u5411 OpenAI gpt \u6a21\u578b\u53d1\u9001\u6bcf\u53ea\u80a1\u7968\u4fe1\u606f\u7684\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u7528\u4e8e\u5206\u6790\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async def get_sentiment_analysis(ticker: TickerClass) -&gt; None:\n    print(f\"analyzing sentiment for {ticker.name}\")\n\n    news_text = \"\"\n    for article in ticker.news:\n        article_text = get_article_text(article&#91;'link'])\n        timestamp = datetime.fromtimestamp(article&#91;'providerPublishTime']).strftime(\"%Y-%m-%d\")\n        news_text += f\"\\n\\n---\\n\\nDate: {timestamp}\\nTitle: {article&#91;'title']}\\nText: {article_text}\"\n\n    messages = &#91;\n        {\n            \"role\": \"system\",\n            \"content\": f\"You are a sentiment analysis assistant. Analyze the sentiment of the given news articles for {ticker.name} and provide a summary of the overall sentiment and any notable changes over time. Be measured and discerning. You are a skeptical investor.\"\n        },\n        {\n            \"role\": \"user\", \n            \"content\": f\"News articles for {ticker.name}:\\n{news_text}\\n\\n----\\n\\nProvide a summary of the overall sentiment and any notable changes over time.\"},\n    ]\n\n    \n    response = await asyncclient.chat.completions.create(\n        temperature = 0.1,\n        model=\"gpt-4-1106-preview\",\n        messages=messages\n    )\n    \n    ticker.sentiment_analysis = response.choices&#91;0].message.content\n\n\n\n\nasync def get_industry_analysis(ticker: TickerClass) -&gt; None:\n    print(f\"Industry analysis for {ticker.name}\")\n    stock = yf.Ticker(ticker.name)\n    industry = stock.info&#91;'industry']\n    sector = stock.info&#91;'sector']\n\n\n    messages = &#91;\n        {\n            \"role\": \"system\",\n            \"content\": f\"You are an industry analysis assistant. Provide an analysis of the {industry} industry and {sector} sector, including trends, growth prospects, regulatory changes, and competitive landscape. Be measured and discerning. Truly think about the positives and negatives of the stock. Be sure of your analysis. You are a skeptical investor.\"\n        },\n        {\n            \"role\": \"user\", \n            \"content\": f\"Provide an analysis of the {industry} industry and {sector} sector.\"}\n    ]\n\n    \n    response = await asyncclient.chat.completions.create(\n        temperature = 0.1,\n        model=\"gpt-4-1106-preview\",\n        messages=messages\n    )\n    \n    ticker.industry_analysis = response.choices&#91;0].message.content\n\nasync def get_final_analysis(ticker: TickerClass) -&gt; None:\n    print(f\"Final analysis for {ticker.name}\")\n    messages = &#91;\n        {\n            \"role\": \"system\",\n            \"content\": f\"You are a financial analyst providing a final investment recommendation for {ticker.name} based on the given data and analyses. Be measured and discerning. Truly think about the positives and negatives of the stock. Be sure of your analysis. You are a skeptical investor.\"\n        },\n        {\n            \"role\": \"user\", \n            \"content\": f\"Ticker: {ticker.name}\\n\\nSentiment Analysis:\\n{ticker.sentiment_analysis}\\n\\nLatest Analyst Ratings:\\n{ticker.analyst_ratings}\\n\\nIndustry Analysis:\\n{ticker.industry_analysis}\\n\\nBased on the provided data and analyses, please provide a comprehensive investment analysis and recommendation for {ticker.name}. Consider the company's financial strength, growth prospects, competitive position, and potential risks. Provide a clear and concise recommendation on whether to buy, hold, or sell the stock, along with supporting rationale.\"}\n    ]\n\n    \n    response = await asyncclient.chat.completions.create(\n        temperature = 0.1,\n        model=\"gpt-4-1106-preview\",\n        messages=messages\n    )\n    \n    ticker.final_analysis = response.choices&#91;0].message.content\n\n\n\n\ndef rank_companies(ticker_info_list: List&#91;TickerClass], industry: str) -&gt; str:\n    print(f\"Ranking ...\")\n    analysis_text = \"\\n\\n\".join(f\"Ticker: {ticker.name}\\nCurrent Price: {ticker.price}\\nAnslysis:\\n{ticker.final_analysis}\" for ticker in ticker_info_list)\n    analysis_text = analysis_text\n\n\n    messages = &#91;\n        {\n            \"role\": \"system\",\n            \"content\": f\"You are a financial analyst providing a ranking of companies in the {industry} industry based on their investment potential. Be discerning and sharp. Truly think about whether a stock is valuable or not. You are a skeptical investor.\"\n        },\n        {\n            \"role\": \"user\", \n            \"content\": f\"Industry: {industry}\\n\\nCompany Analyses:\\n{analysis_text}\\n\\nBased on the provided analyses, please rank the companies from most attractive to least attractive for investment. Provide a brief rationale for your ranking. In each rationale, include the current price (if available) and a price target.\"},\n    ]\n\n    \n    response = syncclient.chat.completions.create(\n        temperature = 0.1,\n        model=\"gpt-4-1106-preview\",\n        messages=messages\n    )\n    \n    return response.choices&#91;0].message.content<\/code><\/pre>\n\n\n\n<p id=\"a0e8\">\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86 4 \u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u3002\u7b2c\u4e00\u4e2a\u51fd\u6570 get_sentiment_analysis\uff08\u60c5\u611f\u5206\u6790\uff09\u63a5\u6536\u6211\u4eec\u4e4b\u524d\u5b9a\u4e49\u7684 TickerClass \u5bf9\u8c61\uff0c\u63d0\u53d6\u6211\u4eec\u4e4b\u524d\u4f7f\u7528 yfinance \u4e0b\u8f7d\u7684\u65b0\u95fb\u6587\u7ae0\uff0c\u5e76\u8981\u6c42 GPT \u5bf9\u65b0\u95fb\u6587\u672c\u8fdb\u884c\u60c5\u611f\u5206\u6790\u3002\u60a8\u53ef\u4ee5\u66f4\u6539\u7cfb\u7edf\u63d0\u793a\uff0c\u5373\u6d88\u606f\u5217\u8868\u4e2d\u4e0e\u7cfb\u7edf\u89d2\u8272\u76f8\u5bf9\u5e94\u7684\u503c\u3002\u6211\u4f7f\u7528\u4e86 Matt Shumer \u5728\u4ed6\u7684\u7b14\u8bb0\u672c\u4e2d\u5b9a\u4e49\u7684\u7cfb\u7edf\u63d0\u793a\u548c\u7528\u6237\u63d0\u793a\u7684\u9ed8\u8ba4\u503c\u3002\u60a8\u8fd8\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u54ea\u79cd GPT \u6a21\u578b\u6765\u5904\u7406\u8bf7\u6c42\u3002\u6211\u5728\u8fd9\u91cc\u4f7f\u7528\u7684\u662f gpt-4-1106-preview\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u66f4\u6539\u6a21\u578b\u5e76\u9009\u62e9\u4f60\u7684\u6a21\u578b\u3002\u901a\u8fc7 OpenAI API \u83b7\u53d6\u7684\u53ef\u7528 GPT \u6a21\u578b\u5217\u8868\u53ef\u5728&nbsp;<a href=\"https:\/\/platform.openai.com\/docs\/models\/overview\">here<\/a> \u627e\u5230\u3002<\/p>\n\n\n\n<p id=\"e14c\">\u4e0b\u4e00\u4e2a\u51fd\u6570 get_industry_analysis \u8981\u6c42 GPT \u5bf9\u80a1\u7968\u6240\u5c5e\u884c\u4e1a\u7684\u589e\u957f\u524d\u666f\u3001\u76d1\u7ba1\u53d8\u5316\u548c\u7ade\u4e89\u683c\u5c40\u8fdb\u884c\u603b\u4f53\u5206\u6790\u3002\u4e0b\u4e00\u4e2a\u51fd\u6570 get_final_analysis \u8981\u6c42 GPT \u4f7f\u7528\u4e4b\u524d\u8fdb\u884c\u7684\u6240\u6709\u5206\u6790\u548c\u4fe1\u606f\u5bf9\u80a1\u7968\u8fdb\u884c\u6700\u7ec8\u5206\u6790\u3002\u6700\u540e\uff0crank_companies \u51fd\u6570\u63a5\u6536\u4e00\u4e2a TickerClass \u5bf9\u8c61\u5217\u8868\uff08\u6bcf\u53ea\u80a1\u7968\u4e00\u4e2a\uff09\uff0c\u5e76\u6839\u636e\u4e4b\u524d\u6240\u505a\u7684\u5206\u6790\u5bf9\u80a1\u7968\u8fdb\u884c\u6392\u540d\u3002<\/p>\n\n\n\n<p id=\"b71d\"><strong>\u6ce8\uff1a<\/strong>\u672c\u8282\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u51fd\u6570\u8c03\u7528\uff08\u4e0d\u5305\u62ec rank_companies\uff09\u90fd\u662f\u5f02\u6b65\u7f16\u5199\u7684\uff0c\u4ee5\u52a0\u5feb\u5904\u7406\u901f\u5ea6\u3002\u5229\u7528\u5f02\u6b65\u51fd\u6570\u8c03\u7528\u53ef\u663e\u8457\u63d0\u9ad8\u6d41\u7a0b\u6548\u7387\u3002\u5f53\u9700\u8981\u6bd4\u8f83\u591a\u53ea\u80a1\u7968\u65f6\uff0c\u8fd9\u79cd\u52a0\u901f\u6548\u679c\u5c24\u4e3a\u660e\u663e\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u8981\u6bd4\u8f83 AAPL\u3001MSFT\u3001AMZN \u548c NVDA\u3002\u4e3a\u4e86\u8bf4\u660e\u540c\u6b65\u548c\u5f02\u6b65\u65b9\u6cd5\u4e4b\u95f4\u7684\u5bf9\u6bd4\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u60c5\u51b5\u3002<\/p>\n\n\n\n<p id=\"3928\"><strong>\u540c\u6b65\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e AAPL\uff0c\u8c03\u7528 get_sentiment_analysis\uff0c\u7b49\u5f85\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u6536\u5230\u60c5\u611f\u5206\u6790\u7ed3\u679c\u540e\uff0c\u8c03\u7528 AAPL \u7684 get_industry_analysis\uff0c\u518d\u6b21\u7b49\u5f85\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u83b7\u5f97\u884c\u4e1a\u5206\u6790\u540e\uff0c\u8c03\u7528 get_final_analysis for AAPL \u3002<\/li>\n\n\n\n<li>\u5bf9\u6bcf\u53ea\u80a1\u7968\uff08MSFT\u3001AMZN\u3001NVDA\uff09\u4f9d\u6b21\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\u3002<\/li>\n\n\n\n<li>\u6700\u540e\uff0c\u8c03\u7528 rank_companies \u5bf9\u80a1\u7968\u8fdb\u884c\u6bd4\u8f83\u6392\u540d\u3002<\/li>\n<\/ul>\n\n\n\n<p id=\"9dca\"><strong>\u5f02\u6b65\u65b9\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u540c\u65f6\u5bf9\u6240\u6709\u80a1\u7968\uff08AAPL,MSFT,AMZN,NVDA\uff09\u542f\u52a8 get_sentiment_analysis\uff1a\u901a\u8fc7\u4e00\u6b21\u6027\u53d1\u9001\u6240\u6709\u8bf7\u6c42\u5e76\u7b49\u5f85\u5176\u96c6\u4f53\u7ed3\u679c\uff0c\u6211\u4eec\u5927\u5927\u7f29\u77ed\u4e86\u7b49\u5f85\u65f6\u95f4\u3002\u8fd9\u79cd\u6548\u7387\u6e90\u4e8e OpenAI API \u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\u7684\u80fd\u529b\uff0c\u4e0e\u6309\u987a\u5e8f\u63d0\u4ea4\u76f8\u6bd4\uff0c\u51cf\u5c11\u4e86\u6574\u4f53\u5904\u7406\u65f6\u95f4\u3002<\/li>\n\n\n\n<li>\u540c\u6837\uff0c\u540c\u65f6\u4e3a\u6bcf\u4e2a\u5e93\u5b58\u8bf7\u6c42 get_industry_analysis\uff0c\u53ef\u5927\u5927\u51cf\u5c11\u7b49\u5f85\u65f6\u95f4\u3002<\/li>\n\n\n\n<li>\u7136\u540e\uff0c\u5bf9\u6240\u6709\u80a1\u7968\u5e76\u884c\u8c03\u7528 get_final_analysis\uff0c\u7b49\u5f85\u6c47\u603b\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u5b8c\u6210\u6240\u6709\u5206\u6790\u540e\uff0crank_companies \u4f1a\u7efc\u5408\u6240\u6709\u53d7\u8bc4\u4f30\u80a1\u7968\u7684\u6700\u7ec8\u6392\u540d\u3002<\/li>\n<\/ul>\n\n\n\n<p id=\"af1c\">\u5982\u9700\u5168\u9762\u4e86\u89e3 Python \u4e2d\u7684 Asyncio\uff0c\u53ef\u9605\u8bfb<a href=\"https:\/\/realpython.com\/async-io-python\/\">\u672c\u6587<\/a>\u3002<\/p>\n\n\n\n<p id=\"c5c1\">\u6700\u540e\uff0c\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u5b8c\u6210\u4e0a\u8ff0\u6240\u6709\u6b65\u9aa4\u5e76\u5f97\u5230\u7ed3\u679c\uff08\u60a8\u9700\u8981\u5c06\u80a1\u7968\u6240\u5c5e\u7684\u884c\u4e1a\u4f5c\u4e3a\u884c\u4e1a\u53c2\u6570\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>async def get_openai_verdict_2(tickers, industry):\n    ticker_info_list = &#91;]\n    for ticker in tickers:\n        temp_ticker_info = TickerClass(name = ticker)\n        temp_ticker_info.hist_data, temp_ticker_info.balance_sheet, temp_ticker_info.financials, temp_ticker_info.news = get_stock_data(ticker, years=1)\n\n        temp_ticker_info.analyst_ratings = get_analyst_ratings(ticker)\n        temp_ticker_info.price = get_current_price(ticker)\n        ticker_info_list.append(temp_ticker_info)\n\n    tasks1 = &#91;get_sentiment_analysis(ticker_object) for ticker_object in ticker_info_list]\n    await asyncio.gather(*tasks1)\n\n    tasks2 = &#91;get_industry_analysis(ticker_object) for ticker_object in ticker_info_list]\n    await asyncio.gather(*tasks2)\n\n    tasks3 = &#91;get_final_analysis(ticker_object) for ticker_object in ticker_info_list]\n    await asyncio.gather(*tasks3)\n\n    final_ranking = rank_companies(ticker_info_list, industry)\n    return final_ranking\n\ntickers = &#91;'AAPL', 'MSFT', 'AMZN', 'NVDA']\nfinal_rankings = await get_openai_verdict_2(tickers=tickers, industry=\"tech\")\nprint(final_ranking)<\/code><\/pre>\n\n\n\n<p id=\"5bab\">\u4ee5\u4e0b\u662f\u6211\u6700\u7ec8\u6536\u5230\u7684 AAPL\u3001MSFT\u3001AMZN \u548c NVDA \u7684\u622a\u56fe\u3002\u56e0\u4e3a\u592a\u957f\u4e86\uff0c\u6211\u628a\u5b83\u88c1\u526a\u4e86\u4e00\u4e0b\uff0c\u4f46\u4f60\u5e94\u8be5\u5df2\u7ecf\u660e\u767d\u4e86\uff1a):<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"1547\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.readmedium.com\/v2\/resize:fit:800\/1*k5YTyehNP_8hBlmH6_2cZg.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d3ca\"><strong>3. \u7f51\u7edc\u7528\u6237\u754c\u9762\uff08Gradio\uff09<\/strong><\/h2>\n\n\n\n<p id=\"f59d\"><br>\u4e3a\u4e86\u7ed9\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u754c\u9762\uff0c\u6211\u4eec\u4f7f\u7528\u4e86&nbsp;<a href=\"https:\/\/www.gradio.app\/guides\" target=\"_blank\" rel=\"noreferrer noopener\">Gradio<\/a>\u3002Gradio \u662f\u4e00\u4e2a\u5f88\u68d2\u7684\u5e93\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u5728\u51e0\u5206\u949f\u5185\u4e3a\u4f60\u7684\u6570\u636e\u79d1\u5b66\u9879\u76ee\u8f7b\u677e\u521b\u5efa\u7528\u6237\u754c\u9762\u3002\u4f60\u53ef\u4ee5\u5728\u6211\u7684\u4ed3\u5e93\u4e2d\u83b7\u53d6 Gradio UI \u4ee3\u7801\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/alexyu2013\/gpt-investor\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub \u2014alexyu2013\/gpt-investor<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u89c2\u70b9\u56de\u987e<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5229\u7528 GPT-4 \u548c yfinance \u8fdb\u884c\u80a1\u7968\u6295\u8d44\u5206\u6790\u53ef\u4ee5\u63d0\u4f9b\u6df1\u5165\u7684\u5e02\u573a\u6d1e\u5bdf<\/strong>\u3002GPT-4 \u7684\u5f3a\u5927\u5206\u6790\u80fd\u529b\u53ef\u4ee5\u5e2e\u52a9\u6295\u8d44\u8005\u4ece\u5927\u91cf\u6570\u636e\u4e2d\u63d0\u53d6\u6709\u7528\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u60c5\u611f\u5206\u6790\u3001\u884c\u4e1a\u8d8b\u52bf\u548c\u7ade\u4e89\u683c\u5c40\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u6b65\u8bf7\u6c42\u53ef\u663e\u8457\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387<\/strong>\u3002\u901a\u8fc7\u540c\u65f6\u5904\u7406\u591a\u4e2a\u80a1\u7968\u7684\u5206\u6790\u8bf7\u6c42\uff0c\u53ef\u4ee5\u51cf\u5c11\u603b\u4f53\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u80a1\u7968\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u6784\u5efa\u7528\u6237\u53cb\u597d\u7684 Web UI \u5bf9\u4e8e\u666e\u53ca\u6295\u8d44\u5de5\u5177\u81f3\u5173\u91cd\u8981<\/strong>\u3002Gradio \u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u5feb\u901f\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u7528\u6237\u754c\u9762\uff0c\u4f7f\u5f97\u975e\u6280\u672f\u7528\u6237\u4e5f\u80fd\u591f\u8f7b\u677e\u4f7f\u7528\u8fd9\u4e9b\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\u3002<\/li>\n\n\n\n<li><strong>\u5206\u6790\u5e08\u8bc4\u7ea7\u548c\u80a1\u7968\u4ef7\u683c\u662f\u6295\u8d44\u51b3\u7b56\u7684\u91cd\u8981\u53c2\u8003<\/strong>\u3002\u6587\u7ae0\u4e2d\u63d0\u5230\u7684\u5206\u6790\u5e08\u8bc4\u7ea7\u548c\u5b9e\u65f6\u80a1\u7968\u4ef7\u683c\u4fe1\u606f\u5bf9\u4e8e\u5f62\u6210\u6295\u8d44\u51b3\u7b56\u63d0\u4f9b\u4e86\u5173\u952e\u7684\u6307\u5bfc\u3002<\/li>\n\n\n\n<li><strong>OpenAI API \u7684\u53ef\u8bbf\u95ee\u6027\u548c\u7075\u6d3b\u6027\u4e3a\u91d1\u878d\u5206\u6790\u63d0\u4f9b\u4e86\u65b0\u7684\u53ef\u80fd\u6027<\/strong>\u3002\u901a\u8fc7 OpenAI API\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684 GPT \u6a21\u578b\u6765\u5904\u7406\u6570\u636e\uff0c\u5e76\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u5b9a\u5236\u5206\u6790\u6d41\u7a0b\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\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<\/mark><\/strong><strong><mark style=\"background-color:#ffffff\" class=\"has-inline-color has-cyan-bluish-gray-color\">\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<\/mark><\/strong><\/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\">\u8f6c\u53d1\u8bf7\u6ce8\u660e\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/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:\/\/blog.laoyulaoyu.top\/index.php\/2024\/10\/11\/%e5%80%9f%e5%8a%a9-opena-%e6%89%93%e9%80%a0%e8%82%a1%e7%a5%a8%e5%88%86%e6%9e%90%e5%88%a9%e5%99%a8%ef%bc%8c%e6%8e%a2%e7%b4%a2%e7%a7%91%e6%8a%80%e8%82%a1%e6%8a%95%e8%b5%84%e4%b9%8b%e9%81%93\/\">Continue reading<span class=\"screen-reader-text\">\u501f\u52a9 OpenAI \u6253\u9020\u80a1\u7968\u5206\u6790\u5229\u5668\uff0c\u63a2\u7d22\u79d1\u6280\u80a1\u6295\u8d44\u4e4b\u9053<\/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,6],"class_list":["post-1508","post","type-post","status-publish","format-standard","hentry","category-aiinvest","tag-ai","tag-6","entry"],"_links":{"self":[{"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/posts\/1508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/comments?post=1508"}],"version-history":[{"count":2,"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/posts\/1508\/revisions"}],"predecessor-version":[{"id":1510,"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/posts\/1508\/revisions\/1510"}],"wp:attachment":[{"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/media?parent=1508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/categories?post=1508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.laoyulaoyu.top\/index.php\/wp-json\/wp\/v2\/tags?post=1508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}