Working through this module?
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.
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.
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
Integration Architecture
Your Application
LangChainLLM interface
OpenAIGPT models
AnthropicClaude models
GoogleGemini models
HuggingFaceOpen-source models
OllamaLocal models
ResponseText, tokens, etc.
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)
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)
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)
Key Parameters
| Parameter | Description |
|---|---|
| model | Specify the LLM model name |
| temperature | Controls randomness (0.0 - 1.0) |
| api_key | API key for the provider |
| base_url | Required for local models (Ollama) |
| max_tokens | Maximum number of tokens in the response |
| top_p | Nucleus sampling parameter |
| stop | List of stop sequences |