In the world of Generative AI development, the Model component in LangChain serves as the crucial foundation for interacting with various artificial intelligence capabilities. Whether you are building a simple text generator or a complex autonomous agent, understanding how LangChain handles different models is essential.
The Model Component: A Unified Interface
At its core, the Model component acts as a common interface that allows developers to connect with diverse AI models from different companies easily. This standardization is powerful because different models often behave differently; LangChain abstracts these complexities, allowing you to switch between providers like OpenAI, Anthropic, or Google with minimal code changes.
LangChain primarily supports two categories of models:
- Language Models: These process text to generate responses.
- Embedding Models: These convert text into numerical vectors for semantic understanding.
Language Models: LLMs vs. Chat Models
While both fall under “Language Models,” there is a significant distinction between traditional LLMs and modern Chat Models.
- LLMs (Legacy): These are general-purpose models that follow a “string-in, string-out” pattern. They are trained on vast amounts of general text like Wikipedia and books. However, they lack conversational memory and role awareness, and support for them is gradually being phased out in newer LangChain versions.
- Chat Models (Recommended): These are specialized for conversational tasks and work with a “sequence of messages” rather than just plain strings. They are fine-tuned on conversational data and support Role Awareness (System, Human, and AI roles) and Conversation History. Today, Chat Models are the industry standard for building chatbots and virtual assistants.
Closed-Source vs. Open-Source Models
Developers often have to choose between proprietary models (Closed-Source) and freely available models (Open-Source).
- Closed-Source (OpenAI, Claude, Gemini): These are hosted on the provider’s servers and accessed via paid APIs. They are highly refined and easy to use but offer zero control over the underlying infrastructure and require sending your data to external servers.
How to Call Models in LangChain #
1. OpenAI (Paid) #
Installation #
pip install langchain-openai
Set API Key #
import os
os.environ["OPENAI_API_KEY"] = "your_api_key"
# Make .env file
Code #
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-4.1-mini",
temperature=0.7
)
response = llm.invoke("What is LangChain?")
print(response.content)
2. Google Gemini (Free Tier + Paid) #
Google provides a free API quota, making Gemini a popular choice for beginners.
Installation #
pip install langchain-google-genai
Set API Key #
import os
os.environ["GOOGLE_API_KEY"] = "your_api_key"
# Make .env file
Code #
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
model="gemini-2.5-flash",
temperature=0.7
)
response = llm.invoke("Explain LangChain.")
print(response.content)
3. Anthropic Claude (Paid) #
Installation #
pip install langchain-anthropic
Set API Key #
import os
os.environ["ANTHROPIC_API_KEY"] = "your_api_key"
# Make .env file
Code #
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model="claude-3-5-sonnet-latest"
)
response = llm.invoke("Explain RAG.")
print(response.content)
Open-Source (Llama, Mistral, TinyLlama): These can be downloaded and run on your own machine. This provides full control and data privacy, making them ideal for sensitive documents. However, running them locally requires significant hardware resources (GPUs) and a more complex setup.
4. Ollama (Free Local Model) #
Ollama allows you to run open-source LLMs locally without sending data to external servers.
First need to you ollama Download in your system, then choose a lightweight model to run on your system.
Lightweight Ollama Models #
| Model | Parameters | Approx. RAM | Download Size | Best For | Command |
|---|---|---|---|---|---|
| TinyLlama | 1.1B | 1 GB | ~640 MB | Learning, basic chatbot | ollama pull tinyllama |
| SmolLM2 | 135M | 300-500 MB | ~90 MB | Testing, experiments | ollama pull smollm2:135m |
| SmolLM2 | 360M | 500-700 MB | ~220 MB | Simple Q&A | ollama pull smollm2:360m |
| SmolLM2 | 1.7B | 1.5-2 GB | ~1 GB | Chat, summarization | ollama pull smollm2:1.7b |
| Qwen 2.5 | 0.5B | 700 MB | ~400 MB | Lightweight assistant | ollama pull qwen2.5:0.5b |
| Qwen 2.5 | 1.5B | 1.5 GB | ~1 GB | Chatbot, coding basics | ollama pull qwen2.5:1.5b |
| Gemma | 2B | 2-3 GB | ~1.7 GB | General AI tasks | ollama pull gemma:2b |
Installation #
pip install langchain-ollama
Download a model:
ollama pull llama3.2 (Example one model run in local system)
Code #
from langchain_ollama import ChatOllama
llm = ChatOllama(
model="llama3.2"
)
response = llm.invoke("Explain LangChain.")
print(response.content)
5. Hugging Face (Free) #
Installation #
pip install langchain-huggingface
Code #
from langchain_huggingface import HuggingFacePipeline
from transformers import pipeline
pipe = pipeline(
"text-generation",
model="TinyLlama/TinyLlama-1.1B-Chat-v1.0"
)
llm = HuggingFacePipeline(pipeline=pipe)
response = llm.invoke("What is LangChain?")
print(response)
Embedding Models and Semantic Search
Embedding models are unique because they do not return text; instead, they return a series of numbers called vectors. These vectors represent the semantic meaning or context of the input text.
These models are the engine behind Semantic Search and RAG (Retrieval-Augmented Generation) applications. By converting your private documents into vectors, you can perform a similarity search to find the most relevant information for a user’s query.
from langchain_huggingface import HuggingFaceEmbeddings
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
embedding = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
documents = [
"Delhi is the capital of India",
"Kolkata is the capital of West Bengal",
"Paris is the capital of France"
]
query = 'capital of India'
doc_embeddings = embedding.embed_documents(documents)
query_embedding = embedding.embed_query(query)
scores = cosine_similarity([query_embedding], doc_embeddings)[0]
index, score = sorted(list(enumerate(scores)),key=lambda x:x[1])[-1]
print(query)
print(documents[index])
print("similarity score is:", score)
Practical Application: Document Similarity
One of the most practical uses of these components is building a Document Similarity Application. The process involves:
- Vectorization: Generating embedding vectors for a set of documents.
- Querying: Generating a vector for the user’s question.
- Comparison: Using Cosine Similarity to calculate the “angle” between the query vector and document vectors.
- Retrieval: Fetching the document with the highest similarity score.
This process allows an AI to “know” which part of a massive database contains the answer to a specific question without reading every single file every time.