참조 : https://python.langchain.com/docs/modules/model_io/prompts/message_prompts
Types of `MessagePromptTemplate` | 🦜️🔗 Langchain
LangChain provides different types of MessagePromptTemplate. The most
python.langchain.com
MessagePromptTemplate
다양한 유형의 MessagePromptTemplate 제공
ChatMessagePromptTemplate
But, chat model이 임의의 역할로 chat meaages 가져오기를 지원하는 경우 사용자가 role 지정 가능
from langchain.prompts import ChatMessagePromptTemplate
prompt = "May the {subject} be with you"
chat_message_prompt = ChatMessagePromptTemplate.from_template(
role="Jedi", template=prompt
)
prompt_final = chat_message_prompt.format(subject="force")
print('Output>', prompt_final)
Output> content='May the force be with you' role='Jedi'
MessagePlaceholder
which gives you full control of what messages to be rendered during formatting
Message prompt tempalte에 어떤 역할을 사용할지 확실하지 않거나 서식 지정 중 메시지 목옥을 삽이하는 경우에 유용
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
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]
)
# print('Output>', chat_prompt)
from langchain_core.messages import AIMessage, HumanMessage
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\
"""
)
prompt_final = chat_prompt.format_prompt(
conversation=[human_message, ai_message], word_count="10"
).to_messages()
print('Output>', prompt_final)
Output> [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.')]
'ML&DL and LLM' 카테고리의 다른 글
LangChain 1.5.1 Types of output parser (0) | 2024.03.29 |
---|---|
LangChain - 1.3.1 LLM QuickStart (0) | 2024.03.28 |
LangChain - 1.2.4 Few-shot prompt template (0) | 2024.03.28 |
LangChain - 1.2.1 PromptTemplate (0) | 2024.03.27 |
LangChain - 1.1 Model I/O Concept (0) | 2024.03.27 |