Get AI summaries of any video or article — Sign up free
LangChain Models | Indepth Tutorial with Code Demo | Video 3 | CampusX thumbnail

LangChain Models | Indepth Tutorial with Code Demo | Video 3 | CampusX

CampusX·
6 min read

Based on CampusX's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

LangChain’s Models component standardizes access to different AI providers through a common interface, reducing code changes when switching models.

Briefing

LangChain’s “Models” component is built to give one common interface for working with different AI model providers—so code can switch between language and embedding models without rewriting everything. The core idea is that LangChain standardizes how applications talk to models that otherwise behave differently across companies. In practice, LangChain supports two model types: language models (for text in → text out) and embedding models (for text in → vectors out). Language models power chatbots and other text-generation features, while embedding models convert text into numeric vectors that enable semantic search—an essential ingredient for RAG-style systems that answer questions using relevant documents.

The tutorial then drills into the practical differences between LLMs and chat models, because LangChain treats them as distinct categories even though both return text. LLMs are general-purpose models suited for tasks like summarization, translation, question answering, and code generation; they typically take a single prompt string and return a single text string. Chat models are specialized for conversation: they accept sequences of messages (multi-turn context) and are better aligned with building assistants, customer-support bots, and coding helpers. A key operational takeaway is that modern LangChain workflows increasingly favor chat models, while LLM support is described as less recommended for newer projects.

After establishing the conceptual split, the walkthrough moves into code demos that show how to call multiple providers through LangChain with a consistent pattern. For language models, it demonstrates OpenAI’s GPT 3.5 Turbo Instruct via LangChain’s OpenAI integration, then repeats the same flow using chat models with GPT 4. It also shows how outputs differ: chat model responses include structured metadata (tokens, completion details), so developers may need to extract the actual answer from fields like “content.” The tutorial further compares provider options by switching to Anthropic’s Claude 3.5 and Google’s Gemini 1.5 Pro, again using LangChain’s chat model interface.

Two generation controls get special attention: temperature and max completion tokens. Temperature is framed as a creativity/determinism dial—lower values yield more predictable outputs (useful for coding or factual tasks), while higher values increase randomness (useful for brainstorming, stories, and poems). Max completion tokens limits how much text the model can produce, which matters because API pricing is token-based.

The tutorial then tackles open-source models, explaining why they’re attractive: they can be downloaded and run locally (reducing dependency on paid APIs and improving control over data), but they require stronger hardware and can be less refined due to different training/fine-tuning approaches. Using Hugging Face, it demonstrates both API-based access (via Hugging Face Inference) and local execution. A small model, TinyLlama (1.1B parameters), is used to keep the demo feasible, and the local run highlights real-world constraints like slow downloads and heavy memory/compute demands.

Finally, the focus shifts to embedding models. It shows how to generate embeddings with OpenAI (including vector dimensionality choices) and how to generate embeddings locally with Hugging Face using Sentence Transformers—specifically all-MiniLM-L6-v2. The closing demo builds a simple document similarity system: it embeds a set of documents and a user query, computes cosine similarity between vectors, sorts by similarity score while preserving document indices, and returns the most relevant document. The workflow is positioned as the foundation for RAG: embeddings enable retrieval, and retrieval is what grounds answers in the most relevant text.

Cornell Notes

LangChain’s Models component standardizes how applications connect to two categories of AI models: language models and embedding models. Language models take text prompts and return text, with chat models optimized for multi-turn conversations via message sequences. Embedding models convert text into vectors, enabling semantic search through cosine similarity—an essential building block for RAG. The tutorial demonstrates consistent LangChain code patterns across providers (OpenAI, Anthropic, Google) for language/chat models, then extends the same idea to embeddings (OpenAI and Hugging Face Sentence Transformers). It ends with a mini document-similarity app that retrieves the most relevant document by comparing query and document embeddings.

Why does LangChain’s Models component matter when working with multiple AI providers?

Different providers expose different APIs and response formats, so switching models can break code. LangChain’s Models component provides a common interface so the application can connect to various language and embedding models with minimal code changes. The tutorial emphasizes that this “common interface” is the practical reason developers can swap providers without rewriting the whole pipeline.

What’s the functional difference between LLMs and chat models in LangChain?

LLMs are general-purpose models that typically accept a single prompt string and return a single text string (useful for summarization, translation, question answering, and code generation). Chat models are designed for conversation: they accept a sequence of messages as input and return a sequence of chat messages as output, making them better suited for assistants, customer-support bots, and multi-turn interactions. The tutorial also notes that chat model outputs often include extra metadata, so developers may need to extract the answer from fields like content.

How do temperature and max completion tokens change model behavior?

Temperature controls randomness: low values (around 0–0.3) produce more deterministic, predictable outputs (good for coding or factual tasks), while higher values (around 1.0–1.5+) increase creativity and variability (good for brainstorming, poems, and stories). Max completion tokens limits output length; since pricing is token-based, restricting tokens helps control cost and prevents overly long responses. The tutorial demonstrates both by running the same prompt with different temperature values and by capping max tokens to see shorter outputs.

What trade-offs come with open-source models versus paid API models?

Open-source models can be downloaded and run locally, which reduces ongoing API costs and gives more control over data and customization (including fine-tuning and local deployment). But they require stronger hardware (often a capable GPU), can be slow to set up, and may produce less refined responses because fine-tuning and human feedback processes may differ. The tutorial also notes that multimodal capabilities may be limited compared with some closed models at the time of writing.

How does the document similarity app work end-to-end?

It embeds a list of documents into vectors, embeds the user query into a vector, then computes cosine similarity between the query vector and each document vector. The similarity scores are sorted to find the highest match. To avoid losing which document corresponds to which score, the tutorial preserves indices (e.g., by pairing scores with document indices before sorting). The top-scoring document is returned as the answer context.

Why are embeddings essential for semantic search and RAG-style systems?

Embeddings transform text into vectors that capture contextual meaning. Once text is in vector form, semantic search becomes a similarity problem: compare vectors using cosine similarity to find the most relevant documents. The tutorial links this directly to RAG, where retrieval uses embeddings to select relevant text, and the language model uses that retrieved context to answer questions.

Review Questions

  1. In LangChain, what input/output behavior distinguishes a language model from an embedding model, and how does that affect the kind of app you build?
  2. When would you prefer a chat model over an LLM, and what does multi-turn message input change in practice?
  3. In the document similarity demo, why is cosine similarity used, and how does sorting by score still preserve the correct document mapping?

Key Points

  1. 1

    LangChain’s Models component standardizes access to different AI providers through a common interface, reducing code changes when switching models.

  2. 2

    LangChain supports two core model types: language models (text in → text out) and embedding models (text in → vectors).

  3. 3

    Chat models are optimized for multi-turn conversation using message sequences, while LLMs are general-purpose single-prompt text generators.

  4. 4

    Temperature controls output randomness (deterministic at low values, creative at higher values), and max completion tokens caps response length and cost.

  5. 5

    Provider calls follow a consistent LangChain pattern (create model instance → invoke with prompt/messages → extract content/answer).

  6. 6

    Open-source models can be run locally (more control, potentially lower API cost) but require heavier hardware and can be slower to set up.

  7. 7

    Embedding-based retrieval works by generating vectors for documents and queries, then using cosine similarity to select the most relevant document.

Highlights

LangChain’s “Models” component is essentially a unifying interface: it lets applications talk to different language and embedding providers without rebuilding the whole integration layer.
Chat models return richer structured outputs (often with metadata), so the answer typically lives in a content field rather than the raw response object.
Temperature and max completion tokens are the two practical levers for balancing creativity, determinism, and token-based cost.
Open-source models shift cost and control from paid APIs to local compute—download and run locally, but expect GPU/RAM constraints.
The document similarity demo is a direct blueprint for retrieval in RAG: embed → cosine similarity → sort by score → return top match.

Topics

  • LangChain Models
  • LLM vs Chat Models
  • OpenAI Anthropic Gemini
  • Hugging Face Inference
  • Embeddings Cosine Similarity

Mentioned