프롬프트 템플릿

LangChain으로 LLM 애플리케이션 개발하기

Jonathan Bennion

AI Engineer & LangChain Contributor

프롬프트 템플릿

  • LLM 프롬프트를 정의하는 레시피
  • 지침, 예시, 추가 컨텍스트를 포함할 수 있음

입력 변수 플레이스홀더가 있는 프롬프트 템플릿.

LangChain으로 LLM 애플리케이션 개발하기

프롬프트 템플릿

from langchain_core.prompts import PromptTemplate


template = "Expain this concept simply and concisely: {concept}"
prompt_template = PromptTemplate.from_template( template=template )
prompt = prompt_template.invoke({"concept": "Prompting LLMs"}) print(prompt)
text='Expain this concept simply and concisely: Prompting LLMs'
LangChain으로 LLM 애플리케이션 개발하기
llm = HuggingFacePipeline.from_model_id(
    model_id="meta-llama/Llama-3.3-70B-Instruct",
    task="text-generation"
)

llm_chain = prompt_template | llm
concept = "Prompting LLMs" print(llm_chain.invoke({"concept": concept}))
Prompting LLMs (Large Language Models) refers to the process of giving a model a
specific input or question to generate a response.
  • LangChain 표현 언어(LCEL): | (파이프) 연산자
  • 체인: 다양한 컴포넌트 호출을 연결
LangChain으로 LLM 애플리케이션 개발하기

채팅 모델

  • 채팅 역할: system, human, ai
from langchain_core.prompts import ChatPromptTemplate


template = ChatPromptTemplate.from_messages( [ ("system", "You are a calculator that responds with math."), ("human", "Answer this math question: What is two plus two?"), ("ai", "2+2=4"), ("human", "Answer this math question: {math}") ] )
LangChain으로 LLM 애플리케이션 개발하기

ChatPromptTemplate 통합

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


llm_chain = template | llm
math='What is five times five?'
response = llm_chain.invoke({"math": math}) print(response.content)
5x5=25
LangChain으로 LLM 애플리케이션 개발하기

연습해 봅시다!

LangChain으로 LLM 애플리케이션 개발하기

Preparing Video For Download...