DocsStation
LangChain

Deep Dive into LangChain Models

By csestudy247 Updated December 2, 2026 6 min read
6 min read

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:

  1. Language Models: These process text to generate responses.
  2. 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
pip install python-dotenv

Set API Key

OPENAI_API_KEY = "your_api_key"  
# Make .env file 

Code

python
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
import os

# Load environment variables from .env
load_dotenv()

# Create the model
model = ChatOpenAI(
    model="gpt-4o-mini",
    temperature=0.7,
)

# Invoke the model
response = model.invoke("Write a 5-line poem about cricket.")

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
pip install python-dotenv

Set API Key

GOOGLE_API_KEY= "your_api_key"  
# Make .env file 
python
from dotenv import load_dotenv
from langchain_google_genai import ChatGoogleGenerativeAI
import os

# Load environment variables from .env
load_dotenv()

# Create the model
llm = ChatGoogleGenerativeAI(
    model="gemini-2.5-flash",
    temperature=0.7
)

# Invoke the model
response = llm.invoke("Explain LangChain.")

print(response.content)

3. Anthropic Claude (Paid)

Installation

pip install langchain-anthropic
pip install python-dotenv

Set API Key

ANTHROPIC_API_KEY = "your_api_key"  
# Make .env file 
python
from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic

# Load environment variables from .env
load_dotenv()

# Create the Claude model
llm = ChatAnthropic(
    model="claude-sonnet-5",
    temperature=0.7
)

# Invoke the model
response = llm.invoke("Explain LangChain.")

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

ModelParametersApprox. RAMDownload SizeBest ForCommand
TinyLlama1.1B1 GB~640 MBLearning, basic chatbotollama pull tinyllama
SmolLM2135M300-500 MB~90 MBTesting, experimentsollama pull smollm2:135m
SmolLM2360M500-700 MB~220 MBSimple Q&Aollama pull smollm2:360m
SmolLM21.7B1.5-2 GB~1 GBChat, summarizationollama pull smollm2:1.7b
Qwen 2.50.5B700 MB~400 MBLightweight assistantollama pull qwen2.5:0.5b
Qwen 2.51.5B1.5 GB~1 GBChatbot, coding basicsollama pull qwen2.5:1.5b
Gemma2B2-3 GB~1.7 GBGeneral AI tasksollama pull gemma:2b

Installation

pip install langchain-ollama

Download a model:

ollama pull llama3.2 (Example one model run in local system)
python
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
python
from langchain_ollama import ChatOllama

llm = ChatOllama(
    model="llama3.2"
)

response = llm.invoke("Explain LangChain.")

print(response.content)

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.

python
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:

  1. Vectorization: Generating embedding vectors for a set of documents.
  2. Querying: Generating a vector for the user’s question.
  3. Comparison: Using Cosine Similarity to calculate the “angle” between the query vector and document vectors.
  4. 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.

Quiz

Q.1 What is the primary function of the 'Model' component in the LangChain framework?



  • It is a specialized tool used exclusively for fine-tuning open-source models on local hardware.


  • It acts as a common interface to interact with various language and embedding models.


  • It serves as the main database for storing large datasets used in training.


  • It functions as a web server to host the final application for end-users.

Q.2 How do Language Models and Embedding Models differ in their fundamental output?



  • Language Models return text, while Embedding Models return a series of numbers known as vectors.


  • Language Models provide code outputs, while Embedding Models provide image descriptions.


  • Language Models return vectors, while Embedding Models return human-readable summaries.


  • Both models return strings of text, but Embedding Models use a more technical vocabulary.

Q.3 In LangChain, what is the key distinction between a standard 'LLM' and a 'Chat Model'?



  • There is no functional difference; they are different names for the same class.


  • Chat Models are older versions of LLMs that are no longer supported.


  • LLMs are designed for real-time conversation, while Chat Models are for static text generation.


  • LLMs take a single string as input/output, while Chat Models take a sequence of messages.

Q.4 Which setting for the temperature parameter would be most appropriate for a model performing a mathematical calculation?



  • 2.0


  • 1.5


  • 0


  • 0.8

Q.5 Why might a developer use the 'Max Completion Tokens' parameter when calling an API-based model like GPT-4?



  • To allow the model to access more external data from the internet.


  • To control the length of the response and manage API costs.


  • To bypass the need for an API key when connecting to the server.


  • To increase the creative depth of the model's response.

Q.6 Which of the following is a primary reason to choose an Open Source model over a Closed Source model?



  • It requires significantly less hardware and RAM to run than API-based models.


  • It is guaranteed to be more accurate and refined than proprietary models.


  • It eliminates the need for any programming code to interact with the model.


  • It provides full control over data privacy by allowing the model to run locally.

Q.7 In the context of Chat Models, what feature allows a developer to assign a specific personality or background to the AI, such as a 'qualified doctor'?



  • Embedding Search


  • Role Awareness


  • Cosine Similarity


  • Deterministic Temperature

Q.8 When using the embed_documents method in LangChain, what is the expected format of the output?



  • A Boolean value indicating if the documents are similar to each other.


  • A list of lists, where each inner list represents the vector for one document.


  • A dictionary where the keys are the original text and the values are the sentiment scores.


  • A single concatenated string containing the summary of all documents.

Q.9 Which LangChain-supported provider is associated with the 'Claude' model series?



  • OpenAI


  • Anthropic


  • Google


  • Hugging Face

Q.10 What is the purpose of using CosineSimilarity in a document search application?



  • To translate vectors back into human-readable text after processing.


  • To reduce the cost of API calls by compressing the text input.


  • To calculate the mathematical closeness or angle between two vectors to determine their semantic similarity.


  • To train the model on new data without needing a GPU.

Scroll to Top