from langchain_core.prompts import PromptTemplate
template = "{country}의 수도는 어디인가요?"
prompt = PromptTemplate.from_template(template)
result = prompt.format(country="대한민국")
from langchain_core.prompts import ChatPromptTemplate
chat_prompt = ChatPromptTemplate.from_messages([
("system", "이 시스템은 천문학 질문에 답변할 수 있습니다."),
("user", "{user_input}")
])
# 예제 데이터
examples = [
{
"country": "France",
"answer": """
Here is what I know:
Capital: Paris
...
"""
},
# 추가 예제들...
]
# 기본 대화 형식 정의
example_prompt = ChatPromptTemplate.from_messages([
("human", "What do you know about {country}?"),
("ai", " {answer}")
])
# Few-shot 템플릿 생성
example_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)
# 최종 프롬프트 조합
final_prompt = ChatPromptTemplate.from_messages([
("system", "You are a travel guide"), # 시스템 역할 정의
example_prompt, # Few-shot 예제들
("human", "what do you know about {country}?") # 실제 질문
])
Few-shot 프롬프팅에서 입력에 따라 적절한 예시를 동적으로 선택해주는 Langchain의 기능
ExampleSelector 종류
Fixed Example Selector : 사전에 정의된 고정된 예시들을 항상 사용
Length-Based Example Selector : 지정된 최대 길이를 넘지 않도록 예시의 수를 조절
Semantic Similarity Example Selector : 입력과 예시들 간의 의미적 유사도를 계산하여 선택
Maximum Marginal Relevance (MMR) Example Selector : 입력과의 유사성과 예시들 간의 다양성을 모두 고려
Random Example Selector : 무작위로 일정 수의 예시를 선택
사용 이점