The AI Stock Data Retriever is a Streamlit application that integrates with the llama3.2:1b
model (via the Ollama library) and Yahoo Finance API. This app processes natural language queries to retrieve stock data, including historical prices and the Price-to-Earnings (PE) ratio.
- Natural Language Query Handling: Users can input queries like "Get 1 year stock prices of TCS," and the AI processes the query to determine the stock symbol and required data.
- Stock Price Retrieval: Fetch historical stock prices for a specified duration using the Yahoo Finance API.
- PE Ratio Retrieval: Get the current Price-to-Earnings ratio of a stock.
- Streamlit Integration: A user-friendly interface for query input and displaying results.
- Python 3.7+
- Required libraries:
ollama
yfinance
pandas
streamlit
- Clone the repository or save the script to your local machine.
- Install the required dependencies:
pip install ollama yfinance pandas streamlit
- Run the Streamlit app:
streamlit run src/trading_agent.py
- Open the provided URL in your browser to interact with the app.
-
get_stock_prices(symbol: str, duration: str) -> pd.DataFrame
:- Fetches historical stock prices for a given symbol and duration.
- Uses Yahoo Finance API to query data.
-
current_pe_ratio(symbol: str) -> int
:- Retrieves the current Price-to-Earnings ratio of a stock using Yahoo Finance.
-
AI Integration:
- The
llama3.2:1b
model processes the user's query and determines the required data. - The model calls the appropriate Python functions (
get_stock_prices
orcurrent_pe_ratio
) using function calling capabilities provided by the Ollama library.
- The
-
Streamlit Interface:
- Accepts user input and displays the results interactively.
- User Query: The app accepts a natural language query about a stock.
- AI Processing: The query is passed to the
llama3.2:1b
model via the Ollama library. - Function Execution:
- Based on the AI’s response, the appropriate function (
get_stock_prices
orcurrent_pe_ratio
) is executed.
- Based on the AI’s response, the appropriate function (
- Result Display:
- If the function returns a DataFrame (e.g., stock prices), it is displayed in a table.
- For single values (e.g., PE ratio), the result is shown as text.
- "Get 1 year stock prices of TCS."
- "What is the current PE ratio of Infosys?"
- "Fetch 1 month stock prices for Reliance."
- Ollama Connection Issues:
- Ensure Ollama is running locally.
- Verify the
llama3.2:1b
model is available and loaded in Ollama.
- Yahoo Finance API Errors:
- Confirm the stock symbol and duration are valid.
- Check your internet connection for API requests.
- Add support for multiple stock exchanges and suffixes.
- Implement error handling for invalid queries or symbols.
- Extend functionality to include other stock metrics, such as dividends or EPS.
Let me know if you need further customization or clarification!