The rise of open-source AI tools has made it easier than ever to build sophisticated, cost-effective solutions. In this guide, I’ll show you how to create an AI-driven stock analysis system leveraging powerful open-source tools like LangGraph, DeepSeek R1–7B, and Ollama — all hosted on Google Colab to minimize infrastructure costs.
Whether you’re a data scientist, software developer, or just someone interested in AI, this workflow is designed to be modular, scalable, and developer-friendly.
The Case for Open Source AI
The democratization of large language models (LLMs) has opened exciting possibilities for developers:
- Free Compute Resources: Use platforms like Google Colab.
- Cost-effective Models: Avoid hefty cloud API costs by running models locally with Ollama.
- Customizability: Full control over models and pipelines.
- Learning Potential: Experiment hands-on with tools like DeepSeek.
By combining these tools, you can create systems that rival enterprise-grade setups at a fraction of the cost.
Why Run LLMs Locally?
Ollama allows you to run DeepSeek R1–7B locally, even in a Colab environment, with several advantages:
- Cost-Effective: No reliance on costly API subscriptions.
- Data Privacy: Sensitive stock data stays local.
- Flexibility: Customize workflows and models to suit your needs.
- Scalability: Expand functionality without vendor lock-in.
System Overview
Our AI pipeline follows a four-step architecture to deliver insights about any publicly traded stock.
Workflow:
- Technical Analysis: Evaluates stock price patterns and market indicators.
- Market Analysis: Reviews sector performance and comparative metrics.
- News Analysis: Processes sentiment and breaking news events.
- Recommendation: Synthesizes insights into actionable advice.
Each stage operates as an independent “Agent” and is part of a Directed Acyclic Graph (DAG) workflow.
Tools We’ll Use
Here’s a breakdown of the core technologies:
- LangChain: Framework for chaining AI prompts and agents.
- LangGraph: For orchestrating workflows with state-sharing across agents.
- DeepSeek R1–7B: Open-source LLM for analytical reasoning.
- Ollama: Local LLM hosting for cost savings and data privacy.
- Google Colab: Free, flexible compute environment.
These tools combine the power of modern LLMs with practical, developer-friendly workflows.
Implementing the Stock Analysis Pipeline
The heart of this system is the Directed Acyclic Graphs(DAG) workflow powered by LangGraph. Each agent in the workflow focuses on a specific analysis area.
1: Define the Workflow
def create_analysis_graph() -> Graph:
"""Create the analysis workflow graph"""
# Create workflow graph
workflow = StateGraph(State)
# Add nodes workflow.add_node("technical", technical_analysis) workflow.add_node("market", market_analysis) workflow.add_node("news", news_analysis) workflow.add_node("recommendation", generate_recommendation) # Define edges workflow.add_edge("technical", "market") workflow.add_edge("market", "news") workflow.add_edge("news", "recommendation") workflow.add_edge(START, "technical") # Set end node workflow.add_edge("recommendation", END)
2: Build Each Agent
Here’s how to define a sample agent, like the Technical Analysis Agent:
# Technical Analysis Node
def technical_analysis(state: State) -> State:
"""Node for technical analysis"""
symbol = state["symbol"]
llm = state["llm"]
# Fetch technical data stock = yf.Ticker(symbol) hist = stock.history(period='1y') # Calculate indicators sma_20 = hist['Close'].rolling(window=20).mean() sma_50 = hist['Close'].rolling(window=50).mean() rsi = calculate_rsi(hist['Close']) data = { 'current_price': hist['Close'].iloc[-1], 'sma_20': sma_20.iloc[-1], 'sma_50': sma_50.iloc[-1], 'rsi': rsi.iloc[-1], 'volume_trend': hist['Volume'].iloc[-5:].mean() / hist['Volume'].iloc[-20:].mean() } prompt = PromptTemplate.from_template( """Analyze these technical indicators for {symbol}: {data} Provide: 1. Trend analysis 2. Support/Resistance levels 3. Technical rating (Bullish/Neutral/Bearish) 4. Key signals """ ) chain = LLMChain(llm=llm, prompt=prompt) analysis = chain.run(symbol=symbol, data=json.dumps(data, indent=2)) state["results"]["technical"] = { "data": data, "analysis": analysis } return state
Similar steps for Market Analysis, News Analysis, and Recommendation Generation Agents.
Explore the Code
The repo includes:
- Full Colab notebook.
- Modular agent workflows for easy customization.
Real-World Applications
Once deployed, this pipeline provides comprehensive insights:
- Technical Indicators: Analyze moving averages, RSI, MACD, and more.
- Market Trends: Understand sector-wide or macroeconomic impacts.
- News Sentiment: Detect bullish or bearish trends in breaking news.
- Actionable Advice: Generate recommendations with confidence scores.
Final Thoughts
This guide shows how LangGraph, DeepSeek, and Ollama unlock new possibilities in AI-powered financial analysis. With open-source tools and free compute resources, you can build robust systems to tackle real-world challenges.
Important: AI-based recommendations are a supplement, not a substitute, for professional financial advice. Always conduct thorough research before making investment decisions.
Let’s build something incredible together! 🚀, and I look forward to connecting with fellow AI enthusiasts and professionals!