Objectives
- Configure Groq, Google Gemini, Anthropic, and OpenAI APIs.
- Initialize models using
init_chat_modelwith multiple provider strings. - 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 usinginit_chat_model, LangChain automatically detects theGROQ_API_KEYorGOOGLE_API_KEYfrom 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
- Import
init_chat_modelfromlangchain.chat_models. - Initialize each model using the unified initializer.
- 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
- Initialize both OpenAI and Google models using
init_chat_model. - Call
invoke()with the greeting query and print the responses side-by-side.
Solution
Solution
Practice & Exercises
To practice, open the interactive notebook:Practice & Exercises
Practice configuring multiple API models and running unified message formats.💻 VS Code | 🚀 Colab | 📥 Download