- 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:- Google Generative AI docs: https://developers.generativeai.google/
- LangChain Python docs: https://python.langchain.com/en/latest/
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:- Import the provider wrapper (e.g.,
OpenAIorGoogleGenerativeAI). - Instantiate the wrapper (optionally specifying model and parameters).
- Call the wrapper with a prompt and handle the response.
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.- If you need deterministic output or other behavior, pass parameters like
temperatureor an explicitmodelwhen instantiatingOpenAI(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.- 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):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.
- LangChain docs: https://python.langchain.com/en/latest/
- OpenAI docs: https://platform.openai.com/docs
- Google Generative AI docs: https://developers.generativeai.google/