시작하기무료로 시작하기

채팅 프롬프트 템플릿

많은 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)
코드 편집 및 실행