Stock vs. Stock uses Meta's Llama 3.1 405B open source LLM to compare any pair of S&P 500 stocks based on a top 10 investment factor and gives an investment recommendation. The comparison is based on doing RAG (Resource Augmented Generation) with the 2 companies' recent 2024 financial statements (8-K, 10-K, 10-Q) filed with the SEC, which are embedded using OpenAI Embedding as vectors in TiDB vector database. Also, a summary of the whole analysis and an optional report are generated by Llama. The user can also enter their own custom investment factors to do the stock vs. stock comparison, such as comparing AI strategy.
TiDB was chosen as the vector store because its free tier includes up to five clusters, each up to 5GB vector data, and for this project I needed over 7GB total for vector and metadata storage. The S&P 500 companies' financial statements are loaded to the vector store as described in the project README. The companies are split between 2 clusters, and for a given company the app does a vector search in the cluster configured for it. For example, the user may choose to compare 2 companies which have their data stored in separate clusters. The app caches the 2 connections to the clusters for performance, with reconnect as needed. To minimize the overhead of RAG in the agentic workflow the vector search is done using filters: ticker, date, and SEC filing type.
All prompts used by the app are stored in 15 prompt files. This allows prompt tweaks without touching the code. I engineered the prompts and set the max token size for Llama 3.1 405B. If the Llama version is changed, keep in mind the prompts may need re-tweaking and max token size adjusted.
There has been debate about the trustworthiness of AI generated investment recommendations. By grounding the recommendations in the official and public financial statements of US traded companies, I tried to mitigate the risk of LLM hallucinations. At the very least, Stock vs. Stock will clearly improve users' financial literacy about investing in big companies like the ones in the S&P 500.
Built With
- edgar
- gradio
- lambda-cloud
- langchain
- llama-3.1-40b
- openai-embedding
- python-3.12
- tidb
- unstructured-io
- vector-search
- yfinance


Log in or sign up for Devpost to join the conversation.