Working through this module?0 of 17 done
Module 04 · Page 5 of 20 · 7 sections

LLM Models and Integrations

What LLMs are, the providers LangChain supports, how the integration layer works and examples for OpenAI, Anthropic and local Ollama models.

4.1

What are LLMs?

  • Large Language Models (LLMs) are AI models trained on massive text data.
  • They can understand, generate and manipulate human language.
  • Examples: OpenAI (GPT), Anthropic (Claude), Google (Gemini), Meta (Llama), etc.
  • LangChain provides easy integration with multiple LLM providers through a unified interface.
4.2

Supported LLM Providers in LangChain

  • OpenAI (GPT-3.5, GPT-4, GPT-4o)
  • Anthropic (Claude)
  • Google (Gemini)
  • HuggingFace (open-source models like Llama)
  • Ollama (local LLMs)
  • Cohere
  • Azure OpenAI
  • and many more via community integrations
4.3

Integration Architecture

Your Application
LangChainLLM interface
OpenAIGPT models
AnthropicClaude models
GoogleGemini models
HuggingFaceOpen-source models
OllamaLocal models
ResponseText, tokens, etc.
4.4

OpenAI Integration Example

python
from langchain_openai import ChatOpenAI

# Create LLM instance
llm = ChatOpenAI(
    model="gpt-3.5-turbo",
    temperature=0.7,
    api_key="your_api_key"
)

# Simple invocation
response = llm.invoke("Explain LangChain in simple words.")
print(response.content)
4.5

Anthropic (Claude) Integration Example

python
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
    model="claude-3-sonnet-20240229",
    temperature=0.7,
    api_key="your_api_key"
)

response = llm.invoke("What is LangChain?")
print(response.content)
4.6

Using a Local LLM with Ollama

python
from langchain_community.llms import Ollama

llm = Ollama(
    model="llama3",
    base_url="http://localhost:11434"
)

response = llm.invoke("Tell me a joke.")
print(response)
4.7

Key Parameters

ParameterDescription
modelSpecify the LLM model name
temperatureControls randomness (0.0 - 1.0)
api_keyAPI key for the provider
base_urlRequired for local models (Ollama)
max_tokensMaximum number of tokens in the response
top_pNucleus sampling parameter
stopList of stop sequences