0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LangChainを学びたい(Step2:ポケモン図鑑を作る)

0
Posted at

はじめに

前回までの続きです。今回からはポケモンを題材にして少しづつ難易度を上げながらLangChainを学んでいきます!

成果物

ポケモンの名称を入れたら、ポケモンの説明が返ってくるようにします
Nov-24-2025 10-02-22.gif

Hands-on

pokemon_zukan_simple.py
# pokemon_zukan_simple.py
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
import os

# .env から OPENROUTER_API_KEY を読み込む
load_dotenv()

# OpenRouter 経由の Gemini を使う
model = ChatOpenAI(
    model="google/gemini-2.5-flash",
    openai_api_key=os.getenv("OPENROUTER_API_KEY"),
    base_url="https://openrouter.ai/api/v1",  # OpenRouter の共通エンドポイント
)

# ポケモン図鑑用のプロンプトテンプレート
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "あなたはポケモン図鑑です。"
            "ユーザーが指定したポケモンについて、図鑑風に日本語でわかりやすく説明してください。"
            "存在しないポケモン名の場合は、その旨を丁寧に伝えてください。"
        ),
        (
            "human",
            "ポケモン名: {pokemon_name}\n"
            "このポケモンのタイプと、簡単な説明を教えてください。"
        ),
    ]
)

# Prompt → Model のチェーン
chain = prompt | model


def main() -> None:
    print("=== ポケモン図鑑(Gemini 版 × LangChain × OpenRouter) ===")
    print("ポケモン名を入力してください(例: ピカチュウ, ヒトカゲ)")
    print("空で Enter を押すと終了します。")

    while True:
        pokemon_name = input("\nあなた > ").strip()
        if not pokemon_name:
            print("終了します。")
            break

        # チェーンを実行して結果を取得
        res = chain.invoke({"pokemon_name": pokemon_name})

        print("\n図鑑 >")
        print(res.content)


if __name__ == "__main__":
    main()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?