📘 Lesson 1.4: Required Tools and Setup (Python, LangChain, Ollama/OpenAI, etc.)

 

📘 Lesson 1.4: Required Tools and Setup (Python, LangChain, Ollama/OpenAI, etc.)

🎯 Lesson Objectives:

By the end of this lesson, you will:

  • Set up your development environment for building AI agents

  • Install Python, LangChain, and connect to a language model (OpenAI or Ollama)

  • Run your first simple LLM-based task using LangChain

  • Understand the difference between using cloud-based LLMs (like OpenAI) vs. local open-source LLMs (like Ollama)


🧰 What You’ll Need

To follow along with the course and build your own AI agents, you’ll need:

Tool/Technology Purpose
Python (3.10+) Programming language used to build agents
Pip Python package manager
LangChain The framework that brings tools + LLMs together
LLM Backend Cloud (OpenAI) or local (Ollama) model
Code Editor (Optional) Visual Studio Code or Jupyter Notebook
Terminal/CLI To run commands and test code

🐍 Step 1: Install Python (If Not Installed)

  • Check your Python version:

python3 --version

✅ Make sure it’s Python 3.10 or higher


🧪 Step 2: Set Up a Virtual Environment (Recommended)

Virtual environments help isolate your project dependencies.

python3 -m venv langchain_env
source langchain_env/bin/activate  # On Windows: langchain_envScriptsactivate

📦 Step 3: Install Required Packages

Install LangChain and an LLM provider:

pip install langchain
pip install openai  # For OpenAI users
pip install ollama  # For local LLMs (like Mistral, LLaMA, etc.)

Optional but recommended:

pip install chromadb  # For document retrieval
pip install python-dotenv  # For managing API keys securely
pip install streamlit  # For building simple UI apps

🔐 Step 4: Connect to a Language Model

✅ Option A: Using OpenAI (Cloud-based)

  1. Get your API key from https://platform.openai.com/account/api-keys

  2. Add it to your environment:

export OPENAI_API_KEY=your_key_here  # or use a .env file

Sample test:

from langchain.llms import OpenAI
llm = OpenAI()
print(llm("Explain LangChain in one sentence."))

✅ Option B: Using Ollama (Local LLMs)

  1. Install Ollama:

curl -fsSL https://ollama.com/install.sh | sh
  1. Start Ollama:

ollama run mistral
  1. Test connection via LangChain:

from langchain_community.llms import Ollama
llm = Ollama(model="mistral")
print(llm("What is an AI agent?"))

🧠 Tip: Ollama lets you run models like Mistral, LLaMA3, or Gemma directly on your device—no internet needed after setup!


🖥 Step 5: Choose a Code Editor

You can code with:

  • VS Code (recommended for real projects)

  • Jupyter Notebook (great for experimentation)

  • Google Colab (online, free)

Install Jupyter (optional):

pip install notebook
jupyter notebook

🚀 First LangChain Test Script

from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

template = "What are three benefits of using AI agents in business?"
prompt = PromptTemplate.from_template(template)

llm = OpenAI()
chain = LLMChain(prompt=prompt, llm=llm)

print(chain.run({}))

If using Ollama, just swap the OpenAI line with:

from langchain_community.llms import Ollama
llm = Ollama(model="mistral")

🧠 Tip: Managing Your API Keys

Create a .env file in your project folder:

OPENAI_API_KEY=your_openai_key

Then load it in Python using:

from dotenv import load_dotenv
load_dotenv()

✅ Key Takeaways

  • You now have a working development environment using Python and LangChain

  • You can run LangChain with OpenAI (cloud) or Ollama (local) backends

  • You’ve written and tested your first LangChain chain

  • This setup will power everything you build in the rest of the course


 

52