๐ 1. ํ๋กฌํํธ ์์ง๋์ด๋ง ๊ฐ์
ํ๋กฌํํธ ์์ง๋์ด๋ง์ด๋?
๐ AI๊ฐ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋๋ก ๋ต๋ณํ๋๋ก ์ง๋ฌธ์ ํจ๊ณผ์ ์ผ๋ก ์ค๊ณํ๋ ๊ธฐ์
3๊ฐ์ง ํ๋กฌํํ ๊ธฐ๋ฒ:
1๏ธโฃ Zero-shot ํ๋กฌํํ
โ ์์ ์์ด AI์๊ฒ ๋ฐ๋ก ์ง๋ฌธ
2๏ธโฃ One-shot ํ๋กฌํํ
โ ์์๋ฅผ ํ๋๋ง ์ฃผ๊ณ AI์๊ฒ ํ์ต
3๏ธโฃ Few-shot ํ๋กฌํํ
โ ์ฌ๋ฌ ๊ฐ์ ์์๋ฅผ ์ ๊ณตํ์ฌ AI์ ์ ํ๋๋ฅผ ๋์
๐ต 2. Zero-shot ํ๋กฌํํ
๐ Zero-shot (์ ๋ก์ท) ํ๋กฌํํ
์ด๋?
AI๊ฐ ์์ ์์ด ์ฆ์ ๋ต์ ์์ฑํ๋ ๋ฐฉ์
๊ฐ๋จํ ์ง๋ฌธ์ด๋ ์ง๊ด์ ์ธ ์์
์ ์ ํฉ
โ
Zero-shot ์ฝ๋ ์์
๐ ์ค๋ช
PromptTemplate โ AI์๊ฒ ์
๋ ฅํ ์ง๋ฌธ ํ
ํ๋ฆฟ์ ๋ง๋ ๋ค.
LangChain์ LLM(ChatOpenAI) โ GPT ๋ชจ๋ธ์ ํธ์ถํ๋ค.
์ฒด์ธ(chain) โ ํ๋กฌํํธ โ AI ๋ชจ๋ธ โ ์ถ๋ ฅ ํ์๋ฅผ ์ฐ๊ฒฐํด์ ์คํ.
Zero-shot ์คํ โ "์ธ๊ณต์ง๋ฅ ๋ฐ๋์ฒด"๋ผ๋ ์ฃผ์ ์ ๋ํ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด๋ฅผ ์ฐพ๋๋ก ์์ฒญ.
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model='gpt-4o-mini', temperature=0.3, top_p=0.9)
# Zero-shot ํ๋กฌํํธ ํ
ํ๋ฆฟ ์์ฑ
zero_shot_prompt = PromptTemplate(
input_variables=["topic"],
template="๋ค์ ์์ฅ์์ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด๋ฅผ ์ค๋ช
ํด์ฃผ์ธ์: {topic}"
)
# ์ฒด์ธ ์์ฑ
chain = zero_shot_prompt | llm | StrOutputParser()
# ์คํ
topic = "์ธ๊ณต์ง๋ฅ ๋ฐ๋์ฒด"
zero_shot_result = chain.invoke(input={"topic": topic})
print(zero_shot_result)
๐ก 3. One-shot ํ๋กฌํํ
๐ One-shot (์์ท) ํ๋กฌํํ ์ด๋?
์์ ํ๋๋ง ์ ๊ณตํ์ฌ AI๊ฐ ํจํด์ ์ดํดํ๋๋ก ๋๋ ๋ฐฉ์
Zero-shot๋ณด๋ค ์กฐ๊ธ ๋ ์ ๊ตํ ๋ต๋ณ ๊ฐ๋ฅ
๐ ์ค๋ช
AI๊ฐ ์์ ๋ฅผ ํ๋ ๋ณด๊ณ ํ์ตํ๋๋ก ํจ.
"์ค๋งํธํฐ ์์ฅ"์์ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด ์์ ๋ฅผ ๋จผ์ ๋ณด์ฌ์ค.
์ดํ "๋ฐ๋์ฒด ์์ฅ"์์ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด๋ฅผ ๋ฌป๋ ์ง๋ฌธ์ ๋์ง.
AI๊ฐ ์์ ๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ ์ ํํ ๋ต์ ์์ฑํจ.
โ One-shot ์ฝ๋ ์์
# One-shot ํ๋กฌํํธ ํ
ํ๋ฆฟ ์์ฑ
one_shot_prompt = PromptTemplate(
input_variables=["example", "question"],
template="""
[์์ ]
์ง๋ฌธ: {example}
๋ต๋ณ: ์ผ์ฑ์ ์, SKํ์ด๋์ค
[์ง๋ฌธ]
์ง๋ฌธ: {question}
๋ต๋ณ:
"""
)
# ์ฒด์ธ ์์ฑ
chain = one_shot_prompt | llm | StrOutputParser()
# ์คํ
example = "์ค๋งํธํฐ ์์ฅ์์ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด"
question = "๋ฐ๋์ฒด ์์ฅ์์ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด"
one_shot_result = chain.invoke(input={"example": example, "question": question})
print(one_shot_result)
๐ข 4. Few-shot ํ๋กฌํํ
๐ Few-shot (ํจ์ท) ํ๋กฌํํ ์ด๋?
AI์๊ฒ 2๊ฐ ์ด์์ ์์๋ฅผ ์ ๊ณตํ์ฌ ์ํ๋ ๋ต๋ณ ํ์์ ํ์ตํ๋๋ก ํ๋ ๋ฐฉ์
๊ฐ์ฅ ์ ํ๋ ๋์ ๋ต๋ณ์ ์ป์ ์ ์์
ํ์ง๋ง ํ๋กฌํํธ ๊ธธ์ด๊ฐ ๊ธธ์ด์ง๊ณ ๋น์ฉ์ด ์ฆ๊ฐํ ์ ์์
๐ ์ค๋ช
AI์๊ฒ 2๊ฐ์ ์์๋ฅผ ์ ๊ณตํด์ ๋ต๋ณ ํจํด์ ํ์ต์ํด.
์ค๋งํธํฐ/๋ฐ๋์ฒด ์์ฅ์ ๋ํ ์์๋ฅผ ๋ณด๊ณ , ์๋์ฐจ ์์ฅ์ ๋ํ ๋ต์ ์ ์ถํ๋๋ก ํจ.
AI๊ฐ ์์๋ฅผ ๋ฐํ์ผ๋ก ๋์ฑ ์ ๊ตํ ๋ต์ ์ ๊ณตํจ.
โ Few-shot ์ฝ๋ ์์
from langchain_core.prompts import FewShotPromptTemplate
# Few-shot ์์ ๋ฐ์ดํฐ
examples = [
{"question": "์ค๋งํธํฐ ์์ฅ์์ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด", "answer": "์ ํ, ์ค์ค๋ฏธ, ํ์จ์ด"},
{"question": "๋ฐ๋์ฒด ์์ฅ์์ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด", "answer": "TSMC, ์ธํ
, SKํ์ด๋์ค"}
]
# Few-shot ํ๋กฌํํธ ํ
ํ๋ฆฟ ์์ฑ
few_shot_prompt = FewShotPromptTemplate(
examples=examples,
input_variables=["question"],
example_prompt=PromptTemplate(
input_variables=["question", "answer"],
template="์ง๋ฌธ: {question}\n๋ต๋ณ: {answer}\n"
),
prefix="๋ค์ ์ง๋ฌธ์ ๋ํ ๋ต๋ณ์ ์ ๊ณตํฉ๋๋ค:\n\n",
suffix="์ง๋ฌธ: {question}\n๋ต๋ณ: ",
)
# ์ฒด์ธ ์์ฑ
chain = few_shot_prompt | llm | StrOutputParser()
# ์คํ
question = "์๋์ฐจ ์์ฅ์์ ์ผ์ฑ์ ์์ ๊ฒฝ์์
์ฒด"
few_shot_result = chain.invoke(input={"question": question})
print(few_shot_result)
๐ 5. Zero-shot vs One-shot vs Few-shot ๋น๊ต
๐ฏ 6. ์ ๋ฆฌ
โ
Zero-shot โ ์์ ์์ด ๋ฐ๋ก ์ง๋ฌธ! (๋น ๋ฅด์ง๋ง ์ ํ๋ ๋ฎ์)
โ
One-shot โ ์์ ํ๋ ์ ๊ณต! (์ ํ๋ ๊ฐ์ , ํ์ง๋ง ๊ณผ์์กด ๊ฐ๋ฅ)
โ
Few-shot โ ์ฌ๋ฌ ์์ ์ ๊ณต! (๊ฐ์ฅ ์ ํํ์ง๋ง, ํ๋กฌํํธ ๊ธธ์ด์ง)
โ๏ธ Zero-shot์ ๊ฐ๋จํ ์ง๋ฌธ์ ์ ํฉ
โ๏ธ Few-shot์ ๋ณต์กํ ์ง๋ฌธ์ ์ ํฉ
โ๏ธ One-shot์ ์ค๊ฐ ๋์ด๋์ ์ง๋ฌธ์ ์ ํฉ