[LangChain] 2. Prompt Template

김제현·2024년 1월 20일
0

LangChain

목록 보기
2/6
post-thumbnail

1. Prompt

  • "Prompt"는 모델에 대한 입력을 의미한다. 이 입력은 여러 구성 요소로 구성되는데 "Prompt Template"은 입력의 구성을 담당해 LangChain이 프롬포트를 쉽게 구성하고 작업할 수 있도록 여러 클래스와 함수를 제공한다.
#API KEY 저장을 위한 os 라이브러리 호출
import os

#기본 LLM 로드를 위한 라이브러리 호출
from langchain.llms import OpenAI

#채팅 LLM 로드를 위한 라이브러리 호출
from langchain.chat_models import ChatOpenAI

#OPENAI API키 저장
os.environ["OPENAI_API_KEY"] = 'YOUR_API_KEY'

2. Prompt Template

  • Prompt Template은 크게 Prompt Template과 Chat Prompt Template으로 나뉜다.

    Prompt Template: 일반적인 프롬포트 템플릿을 생성할 때 활용
    Chat Prompt Template: 채팅 LLM에 프롬포트를 전달하는 데에 활용할 수 있는 특화 프롬포트 템플릿

3. Few Shot Template

  • Few-shot이란, 딥러닝 모델이 결과물을 출력할 때 예시 결과물을 제시함으로써 원하는 결과물로 유도하는 방법론이다.
  • LLM 역시, Few-shot 예제를 제공하면 예제와 유사한 형태의 결과물을 출력한다. 내가 원하는 결과물의 형태가 특수하거나, 구조화된 답변을 원할 경우, 결과물의 예시를 수 개 제시함으로써 결과물의 품질을 향상시킬 수 있다.
from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.prompts.prompt import PromptTemplate

examples = [
  {
    "question": "아이유로 삼행시 만들어줘",
    "answer":
"""
아: 아이유는
이: 이런 강의를 들을 이
유: 유가 없다.
"""
  },

  {
    "question": "김민수로 삼행시 만들어줘",
    "answer":
"""
김: 김치는 맛있다
민: 민달팽이도 좋아하는 김치!
수: 수억을 줘도 김치는 내꺼!
"""
  }
]

example_prompt = PromptTemplate(input_variables=["question", "answer"], template="Question: {question}\n{answer}")

print(example_prompt.format(**examples[0]))

참고

모두의 AI

0개의 댓글