股票市场是一个复杂且动态的系统,涵盖了大量的市场数据、新闻事件、财务报表以及投资者情绪等多重因素。面对如此庞杂的信息,我们的投资分析常常显得力不从心。今天,我将通过一个由 Groq 提供支持的开源项目——
stockbot
,带大家深入探讨 AI 如何赋能投资分析。通过解析项目的原理,我们还将进一步思考如何打造属于自己的智能投资助手。
StockBot原理分析
先上效果:
从上图中可以看到,在聊天交互界面中,我们可以轻松咨询股市大盘走势、市场热图、公司最新财报、行业股票列表以及公司最近的热门资讯等。所有这些信息都通过不同的专业图表实时呈现,帮助我们快速获取所需的市场数据和分析结果。
看到这里,遇到了我们的第一个问题,这些专业的图表是如何绘制出来的?
这就要提到TradingView(https://www.tradingview.com/widget/)
,一个备受欢迎的金融技术和社交网络平台,拥有完善的金融数据和分析工具。除此之外,它还开发了一系列嵌入式组件,帮助开发者在自己的应用中嵌入专业的股票图表和分析工具。在StockBot
项目中,嵌入的一系列图表组件包含了丰富的功能:
- Heatmap of Daily Market Performance
展示每日股票市场表现的热图,通过不同的颜色和大小反映各个股票的日内表现和市值变化。这种热图能够直观地显示哪些股票表现突出,哪些则处于下跌状态,帮助投资者快速掌握市场动向。 - Breakdown of Financial Data for Stocks
提供某只股票的详细财务数据,包括收入、净利润、资产负债表等关键财务指标。投资者可以通过这些数据,深入了解公司的财务健康状况,为投资决策提供依据。 - Price History of Stock
展示某只股票的历史价格数据,投资者可以根据不同的时间段(如日线、周线、月线等)查看股价的波动趋势,帮助分析股票的长期表现和波动规律。 - Candlestick Stock Charts for Specific Assets
汇总某只股票的最新新闻和事件,来源包括多家媒体和金融信息平台。通过整合各类信息,投资者可以全面了解一只股票的最新动态,包括公司公告、行业变化、市场情绪等。 - Stock Screener to Find New Stocks and ETFs
提供股票筛选器功能,帮助投资者根据多种标准(如市值、PE 比率、增长率等)筛选并发现新的股票和 ETF。对于希望在广阔的市场中发现潜力股的投资者来说,这一功能无疑是一个强大的工具。
我们可以一键复制组件的代码到我们的项目中,而这个组件有一系列参数来控制它的外观及要呈现的金融数据,其中最重要的参数是Symbols
,也就是要查询的股票代码。其它组件类似,在StockBot
项目中,一共引入了10个组件,覆盖了上述所有的能力。
此时,我们遇到第二个问题,用户输入的查询如何转换为对应的图表组件及组件所需要的关键参数?
为了解决这个问题,我们首先要理解LLM的Function Call 能力。Function Call 机制能够将用户输入的查询转换为一系列预置的工具调用(至于选择哪个工具,是由每个工具的功能描述及参数定义决定,LLM会根据查询的语义自动匹配,并从查询中提出工具所需要的参数
),从而实现与外部环境的交互。
在StockBot
项目中,预置了9个工具来渲染不同的TradingView 图表:
- showStockFinancials
- showStockChart
- showStockPrice
- showStockNews
- showStockScreener
- showMarketOverview
- showMarketHeatmap
- showTrendingStocks
- showETFHeatmap
通过命名我们就能得知这些工具的用途。为了让查询响应足够快,StockBot
通过集成Llama3 70B 小模型来理解用户的需求,并通过Function Call 机制将其精准转换为对应的工具调用,我们来看一下提供给模型的System Prompt:
You are a stock market conversation bot. You can provide the user information about stocks include prices and charts in the UI. You do not have access to any information and should only provide information by calling functions.
### Cryptocurrency Tickers
For any cryptocurrency, append "USD" at the end of the ticker when using functions. For instance, "DOGE" should be "DOGEUSD".
### Guidelines:
Never provide empty results to the user. Provide the relevant tool if it matches the user's request. Otherwise, respond as the stock bot.
Example:
User: What is the price of AAPL?
Assistant (you): { "tool\_call": { "id": "pending", "type": "function", "function": { "name": "showStockPrice" }, "parameters": { "symbol": "AAPL" } } }
Example 2:
User: What is the price of AAPL?
Assistant (you): { "tool\_call": { "id": "pending", "type": "function", "function": { "name": "showStockPrice" }, "parameters": { "symbol": "AAPL" } } }
模型在输出时就自动根据输入查询的语义从上述9个绑定的工具集中找到最合适的工具,并解析和注入所需参数,从而完成工具的调用以及对应图表的渲染。
除此之外,我们发现在响应的结果中,除了渲染出的图表外,还有一段总结描述:
这段总结又是如何生成的?原来在第一次和LLM交互产生工具调用后,紧接着又与LLM进行第二次交互,但这次不再是调用工具,而是根据调用的工具名及参数列表对查询的内容进行总结,我们来看这次交互的System Prompt:
You are a stock market conversation bot. You can provide the user information about stocks include prices and charts in the UI. You do not have access to any information and should only provide information by calling functions.
These are the tools you have available:
1. showStockFinancials
This tool shows the financials for a given stock.
2. showStockChart
This tool shows a stock chart for a given stock or currency. Optionally compare 2 or more tickers.
3. showStockPrice
This tool shows the price of a stock or currency.
4. showStockNews
This tool shows the latest news and events for a stock or cryptocurrency.
5. showStockScreener
This tool shows a generic stock screener which can be used to find new stocks based on financial or technical parameters.
6. showMarketOverview
This tool shows an overview of today's stock, futures, bond, and forex market performance including change values, Open, High, Low, and Close values.
7. showMarketHeatmap
This tool shows a heatmap of today's stock market performance across sectors.
8. showTrendingStocks
This tool shows the daily top trending stocks including the top five gaining, losing, and most active stocks based on today's performance.
9. showETFHeatmap
This tool shows a heatmap of today's ETF market performance across sectors and asset classes.
You have just called a tool (` +
toolName +
` for ` +
stockString +
`) to respond to the user. Now generate text to go alongside that tool response, which may be a graphic like a chart or price history.
Example:
User: What is the price of AAPL?
Assistant: { "tool\_call": { "id": "pending", "type": "function", "function": { "name": "showStockPrice" }, "parameters": { "symbol": "AAPL" } } }
Assistant (you): The price of AAPL stock is provided above. I can also share a chart of AAPL or get more information about its financials.
or
Assistant (you): This is the price of AAPL stock. I can also generate a chart or share further financial data.
or
Assistant (you): Would you like to see a chart of AAPL or get more information about its financials?
Example 2 :
User: Compare AAPL and MSFT stock prices
Assistant: { "tool\_call": { "id": "pending", "type": "function", "function": { "name": "showStockChart" }, "parameters": { "symbol": "AAPL" , "comparisonSymbols" : [{"symbol": "MSFT", "position": "SameScale"}] } } }
Assistant (you): The chart illustrates the recent price movements of Microsoft (MSFT) and Apple (AAPL) stocks. Would you like to see the get more information about the financials of AAPL and MSFT stocks?
or
Assistant (you): This is the chart for AAPL and MSFT stocks. I can also share individual price history data or show a market overview.
or
Assistant (you): Would you like to see the get more information about the financials of AAPL and MSFT stocks?
## Guidelines
Talk like one of the above responses, but BE CREATIVE and generate a DIVERSE response.
Your response should be BRIEF, about 2-3 sentences.
Besides the symbol, you cannot customize any of the screeners or graphics. Do not tell the user that you can.
此时,我们就完成了用户输入到数据图表及结论输出的完整流程。我们再来总结一下:
- 理解用户意图
首先,StockBot 需要理解用户到底想要什么信息。比如当用户问:
这个理解过程是通过 Llama3 70b 模型来完成的,它能够准确理解用户的问题背后的真实需求。
- "最近苹果股票怎么样?" → 需要展示股票概览
- "能分析一下特斯拉的走势吗?" → 需要展示技术分析图表
- "亚马逊的财务状况如何?" → 需要展示财务数据
- 智能组件匹配
理解了用户意图后,StockBot 会选择最合适的可视化组件(通过Function Call机制):
就像一个经验丰富的分析师,知道用什么图表最能说明问题。
- 股票概览 → StockPrice 组件
- 技术分析 → StockChart 组件
- 财务分析 → StockFinancials 组件
- 相关新闻 → StockNews 组件
- 市场趋势 → MarketTrending 组件
- 参数提取和转换
确定了要使用的组件后,还需要提取关键参数。比如:
// 用户输入:"分析苹果公司股票"
{
component: "StockChart",
symbol: "AAPL", // 股票代码
interval: "D", // 日线图
theme: "light" // 主题样式
}
到了这一步,再结合对应的TradingView
组件,就可以完成图表的渲染。
- 输出总结内容
将调用的组件及参数信息,整合成另一个Prompt与 Llama3 70b 进行二次交互,输出对这次查询的总结。
正如StockBot
展示的那样,AI与投资的结合不是简单的技术堆砌,而是利用LLM强大的自然语言处理能力,从海量的实时市场数据、财务报告、技术指标、新闻事件等信息中,快速挖掘出用户关注的内容,并能够无缝集成外部的一系列工具将其转换为简单、直观的交互体验。
延伸思考:如何结合AI打造自己的投资助手
在个人投资实践中,首先需要做的是筛选出优质的投资标的。未来10年,人工智能无疑将成为重要的风口领域,相关产业的投资潜力巨大。从AI芯片、AI应用,到具身智能(如机器人)、航天航空等行业,都是值得关注的投资方向。幸运的是,平台如 FUTU 已经为我们整理好了多个相关的板块,免去了我们手动去收集和筛选的麻烦,极大地方便了投资者的决策:
在收集到投资标的后,接下来需要深入了解这些公司的经营方向和状况,帮助我们对这些标的有一个更全面和系统的认识。通过使用 Python 中的 yfinance 库,我们可以轻松获取公司的财务数据、股价表现、以及其他相关的市场信息。同时,利用 AI 技术对这些数据进行总结和提炼,还可以将复杂的信息翻译成中文,帮助我们更快速地理解。
接下来,结合 TradingView 提供的实时数据和图表组件,我们可以展示并筛选这些标的的关键投资数据。通过 AI,我们能够实时查询和跟踪这些标的的各种指标,包括财务状况、股价趋势、技术指标等,甚至可以进行多标的的对比分析。一旦发现市场上出现投资机会,我们可以立即采取行动,抓住最佳时机。
通过上述方法,我们可以结合 AI 开发一套适合自己投资需求的工作流,从而帮助我们做出更为精准和高效的决策。
如果大家有更好的方法或思路,欢迎在留言区与我分享讨论。