Working through this module?0 of 17 done
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.

11.1

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).
11.2

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.
11.3

Popular LLM Providers Supported by LangChain

ProviderModel ExamplesHow to Use
OpenAIGPT-4, GPT-3.5ChatOpenAI
AnthropicClaude 3, Claude 2ChatAnthropic
GoogleGemini Pro, Gemini FlashChatGoogleGenerativeAI
CohereCommand R, Command R+ChatCohere
Hugging FaceOpen-source modelsHuggingFacePipeline
Ollama (Local)Llama 3, Mistral, PhiOllama
Azure OpenAIGPT-4 (Azure)AzureChatOpenAI
AWS BedrockClaude, Llama, etc.BedrockChat
11.4

Unified Interface in LangChain

BaseChatModelCommon interface
ChatOpenAI
ChatAnthropic
ChatGoogleGenerativeAI
Same Methodsinvoke(), stream(), batch()
python
from langchain_core.language_models import BaseChatModel
11.5

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)
11.6

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)
11.7

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)
11.8

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)
11.9

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)
11.10

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.