Working through this module?
Module 11 · Page 12 of 20 · 10 sections
LangChain with LLM Providers
Why use several providers, the providers and classes LangChain supports, the shared chat-model interface and examples for OpenAI, Anthropic, Gemini, Ollama and Hugging Face.
What are LLM Providers?
- LLM providers offer powerful language models through APIs or local deployment.
- LangChain supports multiple LLM providers with a unified interface.
- You can easily switch between providers without changing your application logic.
- Popular providers include OpenAI, Anthropic, Google, Cohere, Hugging Face and local models like Llama (via Ollama).
Why Use Multiple LLM Providers?
- Choose the best model for your use case.
- Compare performance, cost and speed.
- Switch providers easily using LangChain.
- Use local models for privacy and cost saving.
- Fall back to another provider if one fails.
- Experiment with different models for better results.
- Supports both cloud-based and local LLMs.
Popular LLM Providers Supported by LangChain
| Provider | Model Examples | How to Use |
|---|---|---|
| OpenAI | GPT-4, GPT-3.5 | ChatOpenAI |
| Anthropic | Claude 3, Claude 2 | ChatAnthropic |
| Gemini Pro, Gemini Flash | ChatGoogleGenerativeAI | |
| Cohere | Command R, Command R+ | ChatCohere |
| Hugging Face | Open-source models | HuggingFacePipeline |
| Ollama (Local) | Llama 3, Mistral, Phi | Ollama |
| Azure OpenAI | GPT-4 (Azure) | AzureChatOpenAI |
| AWS Bedrock | Claude, Llama, etc. | BedrockChat |
Unified Interface in LangChain
BaseChatModelCommon interface
ChatOpenAI
ChatAnthropic
ChatGoogleGenerativeAI
Same Methodsinvoke(), stream(), batch()
python
from langchain_core.language_models import BaseChatModel
Example 1: OpenAI (GPT-4)
python
from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-4", temperature=0.7) response = llm.invoke("Explain microservices in simple words.") print(response.content)
Example 2: Anthropic (Claude)
python
from langchain_anthropic import ChatAnthropic llm = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0.7) response = llm.invoke("What is LangChain?") print(response.content)
Example 3: Google Gemini
python
from langchain_google_genai import ChatGoogleGenerativeAI llm = ChatGoogleGenerativeAI( model="gemini-pro", temperature=0.7 ) response = llm.invoke("List key features of LangChain.") print(response.content)
Example 4: Local LLM with Ollama
python
from langchain_community.llms import Ollama llm = Ollama(model="llama3") response = llm.invoke("Explain RAG in simple words.") print(response)
Example 5: Hugging Face Model
python
from langchain_huggingface import HuggingFacePipeline from transformers import pipeline pipe = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.2", max_new_tokens=200) llm = HuggingFacePipeline(pipeline=pipe) response = llm.invoke("What is Docker?") print(response)
Key Takeaways
- LangChain supports multiple LLM providers.
- Use the unified interface to switch between models.
- Choose the right model based on your use case (cost, speed, accuracy).
- You can use cloud models (OpenAI, Claude, Gemini) or local models (Ollama, Hugging Face).
- Easily integrate new providers with LangChain.
- Helps in building flexible and future-proof AI applications.
- Always handle API keys and model limits properly.