Skip to main content
Welcome to the first demo of this course. In this lesson you’ll learn how to interact with large language models (LLMs) using LangChain wrappers. The objectives are:
  • Show the minimal code required to call an LLM.
  • Demonstrate how to switch providers (OpenAI and Google Generative AI / Gemini) while keeping the same prompt and code flow.
  • Highlight the environment variables and authentication patterns required for each provider.
Before you begin, install the core LangChain package and any provider-specific integrations you plan to use. Make sure provider API keys or credentials are set as environment variables as shown below.

1) Authentication and environment variables

Store credentials in environment variables rather than hard-coding them. On a UNIX-like shell:
Which Google method you need depends on the LangChain integration and the client library. Consult the Google Generative AI docs and LangChain docs for the correct authentication flow: Environment variables quick reference: If you must set keys inside a Python session for testing (not recommended for production), use os.environ:

2) Imports and minimal LangChain pattern

LangChain exposes provider-specific LLM wrappers. The typical pattern is:
  1. Import the provider wrapper (e.g., OpenAI or GoogleGenerativeAI).
  2. Instantiate the wrapper (optionally specifying model and parameters).
  3. Call the wrapper with a prompt and handle the response.
Below are two minimal, copy-paste-ready examples demonstrating this pattern.

3) Example: OpenAI LLM (text-generation)

This example shows the simplest flow to instantiate an OpenAI wrapper, call it with a prompt, and print the response.
Sample output (one possible response):
Notes:
  • If you need deterministic output or other behavior, pass parameters like temperature or an explicit model when instantiating OpenAI(temperature=0.0, model="gpt-4o-mini").
  • Some LangChain releases also expose chat-specific wrappers (e.g., ChatOpenAI) which are better suited for chat-native models.

4) Example: Google Generative AI (Gemini)

Switching to Google Generative AI (Gemini) is straightforward: import the Google wrapper, instantiate with the desired Gemini model, and keep the same prompt and call flow.
Sample output (one possible response):
Tips:
  • Choose the Gemini model appropriate for your use case (e.g., gemini-ultra, gemini-pro, etc.). Model names and availability can vary by account and LangChain version.
  • If authentication fails, verify the environment variable method (API key vs service account) required by your client and LangChain integration.

5) Switching providers on the fly

LangChain’s wrapper abstraction lets you swap LLM providers with minimal code change. Keep prompts and high-level logic the same; only change the import and instantiation. Example (switching to Google from OpenAI):
This prints the result from whichever LLM wrapper is currently assigned to llm. The same pattern applies when swapping embeddings, vector stores, or other pipeline components — typically only the instantiation/import changes.
When swapping providers, ensure you have installed the provider-specific client package and set the corresponding environment variables for authentication.

6) Quick comparison: OpenAI vs Google Generative AI (Gemini)


7) Best practices and final notes

  • Use environment variables for credentials; never commit keys to source control.
  • Pass model-specific parameters (temperature, max tokens) at instantiation when deterministic or constrained behavior is required (e.g., OpenAI(temperature=0.2)).
  • Prefer chat-specific wrappers for chat-native models when available.
  • This lesson focuses on invoking LLMs with simple prompts. Subsequent lessons will cover chat-style APIs, embeddings, vector stores, and chaining LangChain components to build more advanced pipelines.
Recommended reading and references:

Watch Video