Chapter 172 — How to Connect AI Models and Prompts with LangChain
1. Core Fundamentals — Models and Prompts in LangChain
When building applications powered by Large Language Models (LLMs) using LangChain, every interaction relies on two foundational components:
- The LLM Model: The underlying AI model (e.g., OpenAI's GPT, Google's Gemini, or Anthropic's Claude) responsible for processing input queries and generating intelligent text outputs.
- The Prompt (Model Input): The text input, question, or instruction passed to the LLM model to guide its response.
┌─────────────────────────────────────────────────────────────┐
│ Basic LangChain Interaction Flow │
├─────────────────────────────────────────────────────────────┤
│ 1. Prompt (User Input) ──► "What is the capital of │
│ Pakistan?" │
│ │ │
│ ▼ │
│ 2. LangChain Model Wrapper ──► OpenAI / GPT Model │
│ │ │
│ ▼ │
│ 3. LLM Response ──► "The capital of Pakistan is │
│ Islamabad." │
└─────────────────────────────────────────────────────────────┘
Connecting these two components allows developers to transition from basic manual prompts to automated, software-driven AI pipelines.
2. Setting Up the Development Environment
To build and execute LangChain scripts reliably in Python, a dedicated Virtual Environment should be created using Visual Studio Code (VS Code) or terminal commands.
┌─────────────────────────────────────────────────────────────┐
│ Development Setup Pipeline in VS Code │
├─────────────────────────────────────────────────────────────┤
│ Step 1: Create Virtual Environment (`python -m venv venv`) │
│ Step 2: Activate Environment (`venv\Scripts\activate.bat`) │
│ Step 3: Define Dependencies in `requirements.txt` │
│ Step 4: Install Packages (`pip install -r requirements.txt`)│
└─────────────────────────────────────────────────────────────┘
A. Creating & Activating a Virtual Environment
Creating a virtual environment isolates project dependencies, preventing version conflicts with system-wide Python libraries.
- Create Virtual Environment:
python -m venv venv (Where venv specifies the environment directory name).
- Activate Virtual Environment (Windows Command Prompt / VS Code):
venv\Scripts\activate.bat (Upon execution, the terminal prompt reflects the active virtual environment).
B. Managing Dependencies via requirements.txt
Project dependencies are managed by listing required packages in a requirements.txt file.
# --- Core LangChain Libraries ---
langchain
langchain-core
# --- Model Provider Integrations ---
langchain-openai
openai
langchain-anthropic
langchain-google-genai
google-generativeai
# --- Open-Source & Hugging Face Integrations ---
langchain-community
transformers
huggingface_hub
# --- Environment & Utility Libraries ---
python-dotenv
numpy
scikit-learn
torch
tensorflow
flask
C. Installing Dependencies
Dependencies specified in requirements.txt are installed using pip:
pip install -r requirements.txt
If pip is not configured directly in the system PATH, access it via Python:
python -m pip install -r requirements.txt
3. Managing API Keys & Environment Variables (.env)
Because commercial LLM APIs (such as OpenAI) require authentication, API keys must be handled securely rather than hardcoded into source files.
┌─────────────────────────────────────────────────────────────┐
│ Secure Key Management via `.env` │
├─────────────────────────────────────────────────────────────┤
│ 1. Generate Key ──► Obtain API Secret from OpenAI Platform│
│ │
│ 2. Store in File ──► Save in `.env` file in root directory │
│ │
│ 3. Load in Code ──► `load_dotenv()` loads key into │
│ environment │
└─────────────────────────────────────────────────────────────┘
A. Creating the .env Configuration File
Create a file named .env in the root directory of the project containing the API secret key:
OPENAI_API_KEY="your_actual_openai_api_key_here"
B. Loading Environment Variables in Python
The python-dotenv package automatically reads key-value pairs from the .env file and sets them as environment variables:
from dotenv import load_dotenv
# Load environment variables from .env file into Python execution context
load_dotenv()
4. Connecting and Invoking an AI Model in LangChain
Once the environment and API authentication are configured, LangChain connects to the OpenAI model to process input prompts.
A. Step-by-Step Python Script
# =====================================================================
# Connecting OpenAI LLM with Prompts using LangChain (Topic 172)
# =====================================================================
from langchain_openai import OpenAI
from dotenv import load_dotenv
# 1. Load API Key from .env file into environment variables
load_dotenv()
# 2. Instantiate OpenAI Model Object
# Using model "gpt-3.5-turbo-instruct" for direct completion tasks
llm = OpenAI(model_name="gpt-3.5-turbo-instruct")
# 3. Define the Prompt (Model Input)
prompt = "What is the capital of Pakistan?"
# 4. Invoke the LLM with the Prompt
result = llm.invoke(prompt)
# 5. Output the Generated Response
print("Model Output:")
print(result)
B. Execution Flow & Network Behavior:
- Instantiation:
OpenAI(model_name="gpt-3.5-turbo-instruct") creates an LLM wrapper object using the loaded OPENAI_API_KEY.
- Invocation: Calling
llm.invoke(prompt) sends an HTTP request over the internet to OpenAI's remote API servers.
- Response Processing: OpenAI evaluates the prompt and returns the output text (e.g., "The capital of Pakistan is Islamabad."), which LangChain captures and outputs.
5. Summary Comparison Matrix
| Component / Tool |
Python Package / Command |
Core Function in LangChain |
| Virtual Environment |
python -m venv venv |
Isolates project libraries from global environment. |
| Dependency Manager |
pip install -r requirements.txt |
Installs batch libraries listed in text file. |
| Environment Loader |
from dotenv import load_dotenv |
Loads API keys from .env into system environment. |
| OpenAI LLM Wrapper |
from langchain_openai import OpenAI |
Provides standardized interface to OpenAI models. |
| Model Invocation |
llm.invoke(prompt) |
Sends prompt over network and receives output. |