from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
AIMessagePromptTemplate,
)
human_prompt1 = "What is the best way to learn programming?"
human_message_template1 = HumanMessagePromptTemplate.from_template(human_prompt1)
ai_prompt ="""\
1. Choose a programming language: Decide on a programming language that you want to learn.
2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.
3. Practice, practice, practice: The best way to learn programming is through hands-on experience\
"""
ai_message_template =AIMessagePromptTemplate.from_template(ai_prompt)
human_prompt2 = "Summarize our conversation so far in {word_count} words."
human_message_template2 = HumanMessagePromptTemplate.from_template(human_prompt2)
# chatプロンプトテンプレートの準備
chat_prompt = ChatPromptTemplate.from_messages([human_message_template1, ai_message_template, human_message_template2])
chat_prompt.format_prompt(word_count="10").to_messages()
[HumanMessage(content='What is the best way to learn programming?'),
AIMessage(content='1. Choose a programming language: Decide on a programming language that you want to learn.\n\n2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\n\n3. Practice, practice, practice: The best way to learn programming is through hands-on experience'),
HumanMessage(content='Summarize our conversation so far in 10 words.')]
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
from langchain_core.messages import AIMessage, HumanMessage
human_prompt = "Summarize our conversation so far in {word_count} words."
human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)
chat_prompt = ChatPromptTemplate.from_messages(
[MessagesPlaceholder(variable_name="conversation"), human_message_template]
)
human_message = HumanMessage(content="What is the best way to learn programming?")
ai_message = AIMessage(
content="""\
1. Choose a programming language: Decide on a programming language that you want to learn.
2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.
3. Practice, practice, practice: The best way to learn programming is through hands-on experience\
"""
)
chat_prompt.format_prompt(
conversation=[human_message, ai_message], word_count="10"
).to_messages()
from langchain_core.messages import AIMessage, HumanMessageという行では、AIMessageとHumanMessageというクラスをインポートしています。これらのクラスは、それぞれAIと人間のメッセージを表現するためのものです。
そして、human_message = HumanMessage(content="What is the best way to learn programming?")では、HumanMessageクラスのインスタンスを作成し、その内容を"What is the best way to learn programming?"としています。同様に、ai_message = AIMessage(...)では、AIMessageクラスのインスタンスを作成し、その内容を指定しています。
from langchain.prompts import ChatMessagePromptTemplate
prompt = "May the {subject} be with you"
chat_message_prompt = ChatMessagePromptTemplate.from_template(
role="Jedi", template=prompt
)
chat_message_prompt.format(subject="force")
このコードでは、"May the {subject} be with you"というメッセージを送信したロールを"Jedi"と指定しています。コードを実行すると、以下のようなプロンプトテンプレートが得られます。
ChatMessage(content='May the force be with you', role='Jedi')