Online · Lisbon · Portugal

Online Robotics Classes for Kids in Lisbon

Online robotics classes for kids in Lisbon cater to the city’s growing tech expat community with clear English instruction and the optional 100-Mission Hero Journey for structured progression.

Online robotics Lisbon

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:

  1. 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.
  2. 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:

  1. Instantiation: OpenAI(model_name="gpt-3.5-turbo-instruct") creates an LLM wrapper object using the loaded OPENAI_API_KEY.
  2. Invocation: Calling llm.invoke(prompt) sends an HTTP request over the internet to OpenAI's remote API servers.
  3. 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.

Lesson Q&A

Virtual Environment Setup: In Visual Studio Code, a dedicated Python virtual environment is created using the command python -m venv venv. Before executing code, the virtual environment is activated (e.g., running venv\Scripts\activate.bat) to isolate project packages.

Dependency Management via requirements.txt: Project dependencies and optional version constraints are defined inside a requirements.txt file.

Key Installed Packages:

  1. LangChain Core: langchain and langchain-core.
  2. Model Provider Integrations: Provider packages such as langchain-openai, openai, langchain-google-genai, google-generativeai, and Hugging Face integration libraries.
  3. Environment & Utility Libraries: python-dotenv for environment variable management, alongside foundational machine learning libraries like numpy, scikit-learn, torch, and tensorflow.

Installation: All libraries are installed into the active environment by executing pip install -r requirements.txt (or python -m pip install -r requirements.txt).

API Key Generation: Commercial model providers (such as OpenAI) require an authentication API key to process request calls.

Storage in .env File: To avoid hardcoding sensitive credentials inside source code, the API key is stored in a local .env file in the project directory (e.g., OPENAI_API_KEY="your_api_key_here").

Loading Credentials with python-dotenv:

  1. The load_dotenv function is imported from the dotenv library.
  2. Calling load_dotenv() at the beginning of the script automatically loads the keys from the .env file into system memory.
  3. LangChain automatically reads OPENAI_API_KEY from the environment to authenticate model calls without exposing keys in code.

Step 1: Module Imports: Import the model wrapper from the provider integration module (e.g., from langchain_openai import OpenAI) and load_dotenv from dotenv.

Step 2: Environment Initialization & Model Instantiation: Execute load_dotenv() to load API credentials, then initialize the model object while passing the target model name (e.g., gpt-3.5-turbo-instruct).

Step 3: Prompt Invocation: Pass a prompt string to the model using the .invoke() method (e.g., result = llm.invoke("What is the capital of Pakistan?")).

Step 4: API Execution & Output Display: The invocation sends a request over the internet to the provider's API, receives the output, and prints the result (e.g., "The capital of Pakistan is Islamabad").

What children can learn

WhatsApp +92 322 421 7059

AI and Robotics Club | The 100 Mission AI & Robotics Hero Journey

Recommended self-paced course for parents & educators

mBlock Coding for Kids on Udemy.

Disclosure: This page contains affiliate links. We may earn from qualifying purchases at no extra cost to you. Links are intended for parents and educators, not as marketing directed at young children.

Tech Expat Hub Coders: Hands-On Robotics Engineering in Lisbon

Lisbon’s tech and expat communities often seek practical English STEM online. Classes cover the robotics control loop without inventing a campus. The 100-Mission pathway offers structured levels. Enquire WhatsApp +92 322 4217059 or support@aiandroboticsclub.com. No fabricated local labs or career guarantees.

Frequently asked questions

Are classes in Lisbon suitable for international school students?

Yes, sessions are conducted in clear English, catering to Lisbon's growing tech expat community.

What framework guides student learning progression?

Students follow our 100-Mission Hero Journey, advancing step-by-step from Explorers to Tech Champions.

How can parents in Lisbon contact customer support?

Email us at support@aiandroboticsclub.com or message our WhatsApp helpline.

Explore related learning

PortoPython · Porto MadridRobotics · Madrid BrusselsRobotics · Brussels