始める無料で始める

チャットプロンプトテンプレート

チャットモデルは多くの LLM アプリケーションで重要な役割を果たします。LangChain では、異なるチャットの「役割(ロール)」に対してメッセージを構造化するプロンプトテンプレートを作成する機能が提供されています。

ChatPromptTemplate クラスはすでにインポート済みで、LLM も定義されています。

この演習はコースの一部です

LangChainを使ったLLMアプリケーション開発

コースを見る

演習の手順

  • ChatPromptTemplate.from_messages() を使用して、役割とメッセージのペアをチャットプロンプトテンプレートに変換します。
  • 提供されたメッセージに適切な役割を割り当て、会話のパターンを作成します。
  • LCEL チェーンを作成し、提供された入力を使って呼び出します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

llm = ChatOpenAI(model="gpt-4o-mini", api_key='')

# Create a chat prompt template
prompt_template = ChatPromptTemplate.____(
    [
        ("____", "You are a geography expert that returns the colors present in a country's flag."),
        ("____", "France"),
        ("____", "blue, white, red"),
        ("____", "{country}")
    ]
)

# Chain the prompt template and model, and invoke the chain
llm_chain = ____ | llm

country = "Japan"
response = llm_chain.invoke({"country": country})
print(response.content)
コードを編集して実行