LangChainのPrompts主要クラスを理解する
2025-12-04
azblob://2025/12/04/eyecatch/2025-12-04-langchain-prompts-main-classes-000.png

本記事では、LangChainの概要から、Prompts機能の全体像、よく使われる主要クラスの解説まで、初心者に役立つ内容を一つのブログとしてまとめてお届けします。


1. LangChainとは?

LangChainは、大規模言語モデル(LLM)を活用したアプリケーション開発を容易にするためのフレームワークです。
単純なプロンプト呼び出しだけでなく、複数ステップの対話型処理外部データアクセスメモリ保持など、LLMを実際のアプリケーションに組み込むための構造を提供します。

特に以下のような用途に強いのが特徴です:

  • LLMを中心としたアプリ開発を効率化
  • 各種LLM(OpenAI, Anthropic, Azure, Llama など)の抽象化
  • ユーザー入力や外部データを組み合わせた対話フロー構築
  • プロンプト工程を高度に管理・最適化

2. LangChainの機能構成要素

LangChainは複数のレイヤーで構成されており、次の6つが重要な主要コンポーネントとして定義されています。

2-1. Prompts

LLMに対して入力テンプレートを設計・組み立てるための仕組みです。

2-2. Models

LLMやEmbeddingモデルを包括的にラップしたインターフェイス。

2-3. Retrieval

外部データベースやドキュメントから情報を取得し、LLMに与える仕組み。

2-4. Chains

プロンプト → モデル呼び出し → 出力処理 といった
一連の流れをパイプライン化する役割。

2-5. Agents

LLMに「ツール使用」や「意思決定」を行わせ、
状況に応じて処理フローを柔軟に選択させる高機能コンポーネント。

2-6. Memory

ユーザーとの過去のやりとりを保持し、
コンテキストをもった自然な対話を可能にする仕組み。


3. LangChainのPrompts機能とは?

Promptsは、LLMに渡す入力設計を明示的に管理するための機能です。

  • プロンプトの再利用
  • 変数の埋め込み(テンプレート化)
  • 出力形式の指定(構造化出力)
  • マルチメッセージ構造(チャット形式)
  • プロンプト生成の自動化(Few-shot、例示)

など、単純な文字列ではなく「設計された入力」として扱えることが最大の利点です。


4. Promptsにおける主要クラスの解説

ここからは、LangChainでよく使用されるPrompts関連の重要クラスをピックアップして解説します。


4-1. PromptTemplate

LLMに渡すテキストプロンプトをテンプレート化するための最も基本的なクラスです。

4-1-1. 特徴

  • {variable} 形式の変数埋め込みが可能
  • 再利用性の高いプロンプトを作成できる
  • 単一メッセージ(テキスト)を扱う

4-1-2. 使用例

Pythonfrom langchain_core.prompts import PromptTemplate

prompt = PromptTemplate(
    input_variables=["product"],
    template="Write a tagline for the product: {product}"
)

prompt.format(product="AI camera")

4-2. ChatPromptTemplate

ChatGPTなどのチャットモデル向けプロンプトテンプレート

4-2-1. 特徴

  • system / human / assistant など複数ロールのメッセージを構成
  • チャット履歴が扱いやすい
  • エージェントや対話システムで最も使用頻度が高い

4-2-2. 使用例

Pythonfrom langchain_core.prompts import ChatPromptTemplate

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("human", "Explain {topic} in simple terms.")
])

chat_prompt.format_messages(topic="machine learning")

4-3. MessagesPlaceholder

エージェント・チャット系のチェーンでよく使われるメッセージ挿入用コンポーネント

4-3-1. 特徴

  • メモリ機能(会話履歴)と統合する際によく使われる
  • ChatPromptTemplate内でメッセージの集合を挿入可能

4-3-2. 使用例

Pythonfrom langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder(variable_name="history"),
    ("human", "{question}")
])

4-4. FewShotPromptTemplate

例示(Few-shot learning)を効率的に管理するテンプレート

4-4-1. 特徴

  • 複数の例示をテンプレートとして管理
  • Zero-shotより高精度な応答を引き出せるケースが多い
  • QA、文章分類、要約などで活躍

4-4-2. 使用例

Pythonfrom langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

example = {"input": "Happy", "output": "Joyful"}
example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Input: {input}\nOutput: {output}"
)

prompt = FewShotPromptTemplate(
    examples=[example],
    example_prompt=example_prompt,
    prefix="Here are examples:",
    suffix="Input: {word}\nOutput:",
    input_variables=["word"]
)

4-5. StructuredOutputParser / ResponseSchema

構造化された形式での出力を誘導するための仕組み

4-5-1. 特徴

  • JSON形式や指定スキーマの出力に誘導できる
  • 外部システムとの統合でよく使われる
  • モデル出力の一貫性を確保できる

4-5-2. 使用例

Pythonfrom langchain_core.output_parsers import StructuredOutputParser, ResponseSchema

schemas = [
    ResponseSchema(name="title", description="Book title"),
    ResponseSchema(name="author", description="Book author")
]

parser = StructuredOutputParser.from_response_schemas(schemas)
format_instructions = parser.get_format_instructions()

5. まとめ

本記事では以下を整理しました:

  • LangChainはLLMアプリ開発のための強力なフレームワーク
  • コンポーネントは Prompts / Models / Retrieval / Chains / Agents / Memory
  • Prompts機能は「LLMに渡す入力設計」を高度に扱うための基盤
  • 特に重要なプロンプト関連クラス
    • PromptTemplate
    • ChatPromptTemplate
    • MessagesPlaceholder
    • FewShotPromptTemplate
    • StructuredOutputParser ほか

まずは PromptTemplate / ChatPromptTemplate あたりから触り、
慣れてきたら FewShotPromptTemplate や load_prompt で本格的な運用に進むのがおすすめです。