LabHub

Blog

ch02 LangChain Notes

한국어English日本語

Wiki: https://wikidocs.net/book/14314

Github: https://github.com/teddylee777/langchain-kr

Few-shot Prompt

rom langchain_core.prompts.few_shot import FewShotPromptTemplate
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser


examples = [
    {
        "question": "스티브 잡스와 아인슈타인 중 누가 더 오래 살았나요?",
        "answer": """이 질문에 추가 질문이 필요한가요:.
추가 질문: 스티브 잡스는 몇 살에 사망했나요?
중간 답변: 스티브 잡스는 56세에 사망했습니다.
추가 질문: 아인슈타인은 몇 살에 사망했나요?
중간 답변: 아인슈타인은 76세에 사망했습니다.
최종 답변은: 아인슈타인
""",
    },
    {
        "question": "네이버의 창립자는 언제 태어났나요?",
        "answer": """이 질문에 추가 질문이 필요한가요:.
추가 질문: 네이버의 창립자는 누구인가요?
중간 답변: 네이버는 이해진에 의해 창립되었습니다.
추가 질문: 이해진은 언제 태어났나요?
중간 답변: 이해진은 1967622일에 태어났습니다.
최종 답변은: 1967622""",
    }
]


example_prompt = PromptTemplate.from_template(
    "Question:\n{question}\nAnswer:\n{answer}"
)

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

prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    suffix="Question:\n{question}\nAnswer:",
    input_variables=["question"],
)

question = "Google이 창립된 연도에 Bill Gates의 나이는 몇 살인가요?"
final_prompt = prompt.format(question=question)
print(final_prompt)

Example Selector

SemanticSimilarityExampleSelector

Using a Semantic Example Selector, you can select a few examples from multiple examples based on cosine similarity and use them as few-shot examples.

from langchain_core.example_selectors import (
    MaxMarginalRelevanceExampleSelector,
    SemanticSimilarityExampleSelector,
)
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma

# Create a Vector DB (store name, embedding class)
chroma = Chroma("example_selector", OpenAIEmbeddings())

example_selector = SemanticSimilarityExampleSelector.from_examples(
    # Here is a list of selectable examples.
    examples,
    # Here is the embedding class used to generate embeddings for measuring semantic similarity.
    OpenAIEmbeddings(),
    # Here is the VectorStore class used to store embeddings and perform similarity searches.
    Chroma,
    # This is the number of examples to generate.
    k=1,
)

# Select the examples most similar to the input.
selected_examples = example_selector.select_examples({"question": question})

question = "Google이 창립된 연도에 Bill Gates의 나이는 몇 살인가요?"
print(f"Most similar example to the input:\n{question}\n")
for example in selected_examples:
    print(f'question:\n{example["question"]}')
    print(f'answer:\n{example["answer"]}')

MaxMarginalRelevanceExampleSelector

Used to select results that consider both diversity and relevance.

Quiz

Q1: What is the main topic covered in "ch02 LangChain Notes"? ch02 LangChain Notes

Q2: What is Example Selector? SemanticSimilarityExampleSelector Using a Semantic Example Selector, you can select a few examples from multiple examples based on cosine similarity and use them as few-shot examples.

Comments

No comments yet.

Sign in to leave a comment