728x90

Messages Placeholder

  • Messages Placeholder를 통해서 이미 존재하는 메시지를 전달할 수 있다.
  • 특정 key를 통해서 맵핑 시킨다.
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant."),
        MessagesPlaceholder("history"),
        ("human", "{question}")
    ]
)
prompt.invoke(
   {
       "history": [("human", "what's 5 + 2"), ("ai", "5 + 2 is 7")],
       "question": "now multiply that by 4"
   }
)

위 경우 invoke 시에 "history"라는 key에 메시지의 리스트가 value로 들어가있는 걸 확인할 수 있다.

prompt Template을 찍어보면 그 값은 아래와 같다

ChatPromptValue(messages=[
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="what's 5 + 2"),
    AIMessage(content="5 + 2 is 7"),
    HumanMessage(content="now multiply that by 4"),
])

Reference

https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.chat.MessagesPlaceholder.html

+ Recent posts