Langchain PDF Chatbot 만들기 - 13 - Example Selector
목차
지난 Few-shot Prompt의 문제점
지난 게시글에서 구현한 Few-shot Prompt에는 문제점이 있습니다.
그것은 의도와 다르게 질의와 관련이 없는 다수의 예제가 프롬프트에 포함이 되어 모델의 응답 품질이 떨어지거나 혼란이 발생하는 것 입니다.
또한 예제가 많아질 수록 컨텍스트 길이가 늘어나 모델이 주의해야 할 핵심 정보가 희석되는 현상이 발생하는 것 입니다.
해결 방법 Example Selector
해당 문제는 Example Selector를 사용하여 해결할 수 있습니다.
Example Selector는 사용자의 질의와 각 예제 간의 의미적 유사도를 정량적으로 평가하여
상위 몇 개의 가장 관련성 높은 예제만을 선택함으로써 불필요한 예제가 모델의 응답에 혼입되어 발생할 수 있는 혼란을 최소화합니다.
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
from langchain_core.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate, FewShotChatMessagePromptTemplate
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
example_prompt = ChatPromptTemplate.from_messages([HumanMessagePromptTemplate.from_template("""{human}"""),
AIMessagePromptTemplate.from_template("""**답변 :** {ai}""")])
examples = [{
"human": HumanMessagePromptTemplate.from_template("질문 1"),
"ai": AIMessagePromptTemplate.from_template("""답변 : 답변 1""")
},
{
"human": HumanMessagePromptTemplate.from_template("질문 2"),
"ai": AIMessagePromptTemplate.from_template("""답변 : 답변 2""")
},
{
"human": HumanMessagePromptTemplate.from_template("질문 3"),
"ai": AIMessagePromptTemplate.from_template("""답변 : 답변 3""")
}]
example_selector = await SemanticSimilarityExampleSelector.afrom_examples(examples=examples, embeddings=embedding, k=4, vectorstore_cls=FAISS)
few_shot_prompt = FewShotChatMessagePromptTemplate(example_selector=example_selector, example_prompt=example_prompt, input_variables=["query"])
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("You are an expert AI assistant that provides detailed and accurate answers."),
AIMessagePromptTemplate.from_template("Hello! How can I assist you today?"),
few_shot_prompt.format(question=question),
HumanMessagePromptTemplate.from_template("{question}")])
기존 코드에서 달라진 내용은
1
2
example_selector = await SemanticSimilarityExampleSelector.afrom_examples(examples=examples, embeddings=embedding, k=4, vectorstore_cls=FAISS)
few_shot_prompt = FewShotChatMessagePromptTemplate(example_selector=example_selector, example_prompt=example_prompt, input_variables=["query"])
이 부분입니다.
자세히 설명하자면
examples
는 선태 가능한 전체 예제 목록
embeddings
는 유사도를 측정하는데 사용되는 임베딩 클래스
k
는 선택할 예시의 개수로 기본 값은 4입니다.
vectorstore_cls
는 유사도를 측정하는데 사용되는 VectorStore 클래스