티스토리 뷰

1. PromptTemlate비교

클래스 설명
PromptTemplate 가장 기본적인 프롬프트
ChatPromptTemplate 대화형 프롬프트, 메시지 기반의 입력 처리
FewShotPromptTemplate 몇 가지 예시를 포함한 프롬프트, 문맥 제공

 

2. PromptTemplate

from langchain.prompts import PromptTemplate

# 템플릿 정의
template = PromptTemplate(
    input_variables=["name", "task"],
    template="Hello {name}, can you help me with {task}?"
)

# 템플릿 사용
output = template.format(name="Alice", task="coding")
print(output)

# Hello Alice, can you help me with coding?

 

2. ChatPromptTemplate

from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder

# 템플릿 정의
chat_template = ChatPromptTemplate.from_messages([
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is the capital of {country}?"}
])

# 템플릿 사용
output = chat_template.format(country="France")
print(output)

# [
#    {"role": "system", "content": "You are a helpful assistant."},
#    {"role": "user", "content": "What is the capital of France?"}
#]

 

3. FewShotPRomptTemplate

from langchain.prompts import FewShotPromptTemplate, PromptTemplate

# 예제 템플릿 정의
example_template = PromptTemplate(
    input_variables=["word", "definition"],
    template="{word}: {definition}"
)

# FewShot 템플릿 정의
few_shot_template = FewShotPromptTemplate(
    examples=[
        {"word": "Python", "definition": "A programming language."},
        {"word": "AI", "definition": "The simulation of human intelligence by machines."}
    ],
    example_prompt=example_template,
    prefix="Here are some words and their definitions:",
    suffix="Can you define the word '{query}'?",
    input_variables=["query"]
)

# 템플릿 사용
output = few_shot_template.format(query="LangChain")
print(output)

# Here are some words and their definitions:
# Python: A programming language.
# AI: The simulation of human intelligence by machines.
# Can you define the word 'LangChain'?
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함