[KongchangAI]
· 2 min read· 1,367 words

LangChain Prompt Templates in Practice: Building Your Own Customizable AI JARVIS

LangChain Prompt Templates in Practice: Building Your Own Customizable AI JARVIS

Build a JARVIS-like AI assistant using LangChain Prompt templates, covering LLM vs Chat Model and prompt engineering.

This article explores how to use LangChain's Prompt templates and prompt engineering techniques to build a customizable AI assistant. It covers LangChain's unified LLM interface design philosophy, practical use of PromptTemplate and ChatPromptTemplate, and the key differences between LLM (text completion) and Chat Model (structured message roles) — providing the foundation for creating your own JARVIS-style intelligent agent.

Introduction: Beyond ChatGPT — Building a Customizable AI Assistant

In the learning path of AI Agent application development, mastering the LangChain framework has become an essential step. LangChain is an open-source framework released by Harrison Chase in October 2022, born at a pivotal moment when demand for LLM application development exploded. Its core value lies in standardizing the complex LLM application development process, providing core modules such as Chain (chained calls), Agent (intelligent agents), and Memory (memory management) — helping developers quickly build complex AI applications ranging from RAG (Retrieval-Augmented Generation) to multi-step reasoning.

The goal of this chapter is ambitious: "Build your own version of JARVIS." JARVIS, of course, refers to the intelligent assistant in Iron Man — one that understands intent, can be given a personality, and responds to commands at any time.

The key to achieving this goal is using Prompt templates to precisely control the inputs and outputs of large language models. In other words, we're no longer satisfied with simply calling a generic tool like ChatGPT. Instead, we want to learn how to shape a customized LLM with a specific personality and behavioral logic — one that's truly our own.

You can go ahead and build your own customizable large language model

This chapter covers three main areas: LangChain's LLM interface design philosophy, practical usage of Prompt templates (prompt engineering), and the differences between the two core model types — LLM and Chat Model. Let's break them down one by one.

Part 1: Understanding LangChain's LLM Interface Design

From earlier study, we already have a basic understanding of LangChain's LLM interface. This chapter digs deeper into its design philosophy — understanding the framework's architectural thinking helps us make better technical decisions in real-world engineering.

We can fully understand LangChain's design philosophy

A major reason LangChain is so popular among developers is that it provides a unified abstraction layer over LLM calls. Developers don't need to worry about the underlying API differences between model providers — they simply program against a unified interface. This design principle is known in software engineering as "interface-oriented programming." Whether the underlying model is OpenAI, Anthropic, Tongyi Qianwen, or ERNIE Bot, the business logic only interacts with the unified BaseLanguageModel interface. This abstraction reduces integration overhead and makes model swapping and upgrades extremely flexible — truly achieving the engineering goal of "switch models without changing code."

By studying the LLM interface design, we can see LangChain's overall architectural philosophy: decomposing "input processing → model invocation → output parsing" into independent modules. This modular design is the foundation for building complex Agent applications.

Part 2: Prompt Templates — The Core Battleground of Prompt Engineering

If the large model is the engine, the Prompt is the steering wheel. This chapter demonstrates through extensive hands-on coding how to use Prompt templates systematically — what we commonly call Prompt Engineering.

Techniques for working with Prompts

Why Are Prompt Templates So Critical?

Prompt Engineering is the systematic practice of designing input text to guide large language models toward desired outputs. Its importance stems from how LLMs work — models are essentially performing probabilistic predictions over the next most likely token in a sequence. As a result, how the input is phrased, the context provided, and even punctuation choices directly affect the model's reasoning path and output quality. Academic research has shown that the same question phrased in different ways can result in accuracy differences of over 20%.

Within the LangChain ecosystem, Prompt templates hold an extremely central position: they directly determine input quality and structure, which in turn affects the accuracy and usability of model outputs.

A well-designed Prompt template transforms a user's raw request into structured, context-rich input that guides the model toward more expected responses. This is precisely the key to "building JARVIS" — using Prompts to give the assistant a specific personality, tone, and behavioral boundaries.

LangChain's Built-In Prompt Engineering Techniques

LangChain includes a rich set of Prompt construction tools. The core ones are:

  • Variable placeholders (PromptTemplate): Uses {variable} syntax to dynamically inject variables, enabling parameterized template reuse
  • Few-shot examples (FewShotPromptTemplate): Embeds input-output examples directly in the Prompt, letting the model learn task intent through analogy rather than rule memorization — a technique rooted in the In-Context Learning capability introduced in the GPT-3 paper
  • Message templates (ChatPromptTemplate): Manages templates for System, Human, and AI message roles in the structured Chat Model format
  • Pipeline composition: Uses LCEL (LangChain Expression Language) to chain multiple Prompt templates together, building complex prompt processing pipelines

These tools let developers manage and reuse prompts in an engineering-disciplined way, completely replacing the inefficient practice of hardcoding large blocks of text into source code. Mastering them means you can systematically optimize model performance rather than relying on trial-and-error guesswork.

Part 3: Two Types of Models — The Essential Difference Between LLM and Chat Model

The third major focus of this chapter is clarifying the differences between two core model types in LangChain and putting them to practical use. This is a distinction that's easy to overlook but critically important — rooted in differences in underlying training paradigms and API design philosophy.

1. LLM: Pure Text Language Model

The first type is the LLM (Large Language Model). In the LangChain context, this specifically refers to pure text-based language models, whose design philosophy inherits the "text completion" paradigm from early GPT series — given a prefix of text, the model predicts and generates the continuation. The characteristics are straightforward:

  • Accepts only a single text string as input
  • Returns a single text string as output

In short, it's a simple "text in, text out" pipeline — well-suited for single-turn tasks without complex contextual structure, such as text summarization, keyword extraction, and standalone code generation tasks. In LangChain, the OpenAI class (not ChatOpenAI) corresponds to this interface type.

2. Chat Model: Conversational Large Language Model

This is called the Chat Model

The second type is the Chat Model — a conversational large language model that differs fundamentally from LLM. This distinction arises from the introduction of RLHF (Reinforcement Learning from Human Feedback) — modern dialogue models like GPT-3.5-turbo, Claude, and Gemini have all been fine-tuned this way and are specifically optimized for conversational scenarios. Their API design introduces a structured message role system:

  • System message: Sets the AI's identity, personality, and behavioral constraints — the key entry point for defining the "JARVIS" persona
  • Human message: Represents user input
  • AI message: Represents the model's previous replies, used to build multi-turn conversation context and memory

At the interface level:

  • Input is a list of messages (List of Messages)
  • Output is also a structured message object (AIMessage), not a raw string

Chat Model's greatest advantage is that its inputs and outputs have clearly defined data structures that distinguish between System messages, Human messages, and AI messages — enabling multi-turn conversation context management and role assignment. In LangChain, this corresponds to classes like ChatOpenAI and ChatAnthropic.

For an intelligent assistant like "JARVIS" that requires continuous interaction, memory of context, and maintenance of a specific persona, Chat Model is clearly the better technical choice — because System messages allow us to inject a persona description at the start of each conversation, keeping the model consistently in the role we've defined.

Summary: From Understanding to Practice

The learning path of this chapter is clear and practical: start by understanding LangChain's architectural philosophy through LLM interface design, then master the core techniques for shaping model behavior through Prompt templates, and finally clarify the differences between LLM and Chat Model before putting them into practice.

All three parts are tightly interconnected, forming the technical foundation for building a customizable AI assistant. The central logic runs throughout: LangChain's unified abstraction lets us focus on business logic rather than API adaptation; Prompt templates let us precisely control model behavior in an engineering-disciplined way; and Chat Model's message structure provides a solid data foundation for continuous interaction and role-playing.

With these in hand, you have the first building block for creating your own "JARVIS." The hands-on coding that follows will transform these concepts into a real, runnable intelligent agent application.

Key Takeaways

#LangChain#Prompt Engineering#Chat Model#LLM#AI Assistant#PromptTemplate#JARVIS#ChatOpenAI#Few-Shot Learning#RLHF
Share:

Related articles