Skip to main content
In this section, you will learn how to switch between different LLM providers using LangChain’s unified core abstractions.

Objectives

  1. Configure Groq, Google Gemini, Anthropic, and OpenAI APIs.
  2. Initialize models using init_chat_model with multiple provider strings.
  3. Reuse the same list of messages across different models without rewriting logic.

Initializing and Calling Alternative Models

Here is how to configure and invoke different model providers using LangChain’s core abstractions.

1. Initializing with Groq

2. Initializing with Google Gemini

[!NOTE] When using init_chat_model, LangChain automatically detects the GROQ_API_KEY or GOOGLE_API_KEY from your environment variables.

Swap Providers

Goal

Query the same math problem to OpenAI, Anthropic, and Google models.

Sample Input

Sample Output

Outputs matching standard math solving from all three API providers.

Plan

  1. Import init_chat_model from langchain.chat_models.
  2. Initialize each model using the unified initializer.
  3. Call invoke() on each model using the same messages list and print results.

Code Implementation

Exercise: Multi-Provider Greeting Agent 🤝

Goal

Initialize OpenAI and Google models and verify how they respond to a simple creative greeting message.

Sample Input

Sample Output

Greetings from both OpenAI and Google models.

Plan

  1. Initialize both OpenAI and Google models using init_chat_model.
  2. Call invoke() with the greeting query and print the responses side-by-side.

Practice & Exercises

To practice, open the interactive notebook:

Practice & Exercises

Practice configuring multiple API models and running unified message formats.💻 VS Code | 🚀 Colab | 📥 Download