Β
π 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
-
Download Python: https://www.python.org/downloads
π§ͺ 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)
-
Get your API key from https://platform.openai.com/account/api-keys
-
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)
-
Install Ollama:
curl -fsSL https://ollama.com/install.sh | sh
-
Start Ollama:
ollama run mistral
-
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
Β
57
