자기 일관성과 생성 지식
LLM이 여러 추론 경로를 생성한 뒤 가장 일관된 답변을 선택하거나 추론에 도움이 되는 지식을 생성하는 기법을 구현합니다.
자기 일관성과 생성 지식은(는) CoddyKit의 무료 Prompt Engineering & LLM Optimization for Developers 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Prompt Engineering & LLM Optimization for Developers 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Prompt Engineering & LLM Optimization for Developers 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Intro: Consistency & Knowledge
Welcome to Lesson 2! In complex problem-solving, Large Language Models (LLMs) can sometimes struggle, leading to incorrect or inconsistent answers.
This lesson introduces two powerful techniques to boost their reliability: Self-Consistency and Generated Knowledge. These methods help LLMs 'think' more deeply and systematically.
Why Advanced Reasoning?
LLMs are great at generating text, but they can sometimes make logical errors or 'hallucinate' (produce factually incorrect information), especially with multi-step reasoning.
Advanced prompting strategies like Self-Consistency and Generated Knowledge aim to:
- Improve accuracy for complex tasks.
- Reduce the likelihood of factual errors.
- Make LLM responses more robust and reliable.
Understanding Self-Consistency
Self-Consistency is a technique where you prompt an LLM to generate multiple distinct reasoning paths or answers for the same question.
Instead of relying on a single output, you then aggregate these different outputs and select the most consistent (e.g., the most frequent) answer. It's like asking several experts and taking the majority opinion.
Self-Consistency in Action
Imagine asking an LLM: "If a train leaves station A at 8 AM traveling at 60 mph, and another leaves station B (300 miles away) at 9 AM traveling at 70 mph, when do they meet?"
A single prompt might give a wrong answer. With self-consistency, you'd ask this multiple times, perhaps with slightly varied phrasing, then compare the results to find the most common meeting time.
Code: Simple Self-Consistency
This Python example simulates calling an LLM multiple times for a math problem. It then picks the most frequent answer, enhancing reliability.
import collections
import random
def call_llm(prompt):
# Simulate LLM responses for a math problem
# In a real app, this would be an actual LLM API call
if "What is (15 * 3) - 7?" in prompt:
return random.choice(["38", "The answer is 38.", "40 (Oops!)"])
return "Simulated response."
def main():
print("--- Self-Consistency Example ---")
question_prompt = "What is (15 * 3) - 7? Give your final numeric answer only."
answers = []
num_attempts = 5
for i in range(num_attempts):
raw_output = call_llm(question_prompt)
# Simple extraction of numeric part
numeric_answer = ''.join(filter(str.isdigit, raw_output))
if numeric_answer:
answers.append(int(numeric_answer))
print(f"Attempt {i+1}: {numeric_answer}")
else:
print(f"Attempt {i+1}: Could not parse '{raw_output}'")
# Find the most common answer (voting)
if answers:
most_common = collections.Counter(answers).most_common(1)
print(f"\nMost consistent answer: {most_common[0][0]}")
else:
print("\nNo valid answers generated.")
if __name__ == "__main__":
main()Introducing Generated Knowledge
Generated Knowledge is a technique where the LLM first generates relevant facts, context, or intermediate thoughts, and then uses this self-generated information to answer the main query.
This is akin to doing research before writing an essay. The LLM essentially 'pre-computes' or 'recalls' relevant knowledge to build a stronger foundation for its final answer.
Generated Knowledge in Practice
Consider a question like: "Describe the main differences between a black hole and a wormhole."
Instead of directly answering, you could first prompt the LLM to:
- "List key characteristics of a black hole."
- "List key characteristics of a wormhole."
Then, use these generated lists as context for the original question, leading to a more informed and accurate comparison.
Code: Pre-computation with LLM
This Python example demonstrates a two-step process: first, asking the LLM to generate knowledge, and then using that knowledge in a subsequent prompt to answer a complex question.
def call_llm(prompt):
# Simulate LLM responses for knowledge generation
if "What are the key components of prompt injection?" in prompt:
return "Malicious user input, LLM vulnerability to new instructions, attempt to bypass security or extract data."
elif "Using the information about Malicious user input, LLM vulnerability to new instructions, attempt to bypass security or extract data., explain the concept of 'prompt injection' in cybersecurity." in prompt:
return "Prompt injection is an attack where crafted malicious user input manipulates an LLM to override its original instructions, potentially leading to unauthorized actions, data exposure, or harmful content generation. It exploits the LLM's tendency to follow new directions even if they contradict its safety guidelines."
return "Simulated response."
def main():
print("--- Generated Knowledge Example ---")
# Step 1: Generate knowledge
knowledge_prompt = "What are the key components of prompt injection?"
print(f"LLM generating knowledge...")
generated_knowledge = call_llm(knowledge_prompt)
print(f"Generated Knowledge:\n{generated_knowledge}\n")
# Step 2: Use generated knowledge to answer the main question
main_question = "explain the concept of 'prompt injection' in cybersecurity."
final_prompt = f"Using the information about {generated_knowledge}, {main_question}"
print(f"LLM answering main question using generated knowledge...")
final_answer = call_llm(final_prompt)
print(f"Final Answer:\n{final_answer}")
if __name__ == "__main__":
main()Synergies: Combining Techniques
While powerful individually, Self-Consistency and Generated Knowledge can also be combined for even greater robustness.
For instance, you could first use Generated Knowledge to create a robust set of facts, and then apply Self-Consistency to the final answer generation phase, ensuring both factual grounding and reliable output.
Check Your Understanding
Test your knowledge about Self-Consistency and Generated Knowledge.
Summary: Powering LLMs
In this lesson, you learned about two advanced prompting strategies:
- Self-Consistency: Generating multiple answers and selecting the most common one to improve reliability.
- Generated Knowledge: Having the LLM create relevant context first, then using that context to answer the main question.
These techniques empower LLMs to tackle complex problems with greater accuracy and less risk of errors. Continue experimenting with them to unlock more robust LLM applications!
자주 묻는 질문
“자기 일관성과 생성 지식” 강의는 무료인가요?
네 — “자기 일관성과 생성 지식” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Prompt Engineering & LLM Optimization for Developers 강의 전체를 잠금 해제할 수 있습니다. Prompt Engineering & LLM Optimization for Developers 강의에는 총 4개의 강의가 포함되어 있습니다.
“자기 일관성과 생성 지식”에서 뭘 배우나요?
LLM이 여러 추론 경로를 생성한 뒤 가장 일관된 답변을 선택하거나 추론에 도움이 되는 지식을 생성하는 기법을 구현합니다. 브라우저에서 직접 실행하는 실습 코드로 Prompt Engineering & LLM Optimization for Developers을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Prompt Engineering & LLM Optimization for Developers을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Prompt Engineering & LLM Optimization for Developers은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“자기 일관성과 생성 지식” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Prompt Engineering & LLM Optimization for Developers 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Prompt Engineering & LLM Optimization for Developers 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 사고 과정 프롬프트
- 자기 일관성과 생성 지식
- 사고 트리 및 그래프 프롬프트
- ReAct: 도구를 활용한 추론과 실행