0Pricing
Prompt Engineering & LLM Optimization for Developers · 课时

自洽性与生成式知识

实现相关技术,让 LLM 生成多条推理路径并选择最一致的答案,或生成知识来辅助推理。

自洽性与生成式知识 是 CoddyKit 上的免费 Prompt Engineering & LLM Optimization for Developers 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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:

  1. "List key characteristics of a black hole."
  2. "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!

常见问题解答

「自洽性与生成式知识」课时是免费的吗?

是的 — 「自洽性与生成式知识」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Prompt Engineering & LLM Optimization for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Prompt Engineering & LLM Optimization for Developers 课程共包含 4 节课。

「自洽性与生成式知识」这节课中我会学到什么?

实现相关技术,让 LLM 生成多条推理路径并选择最一致的答案,或生成知识来辅助推理。 你通过在浏览器中直接运行的动手代码来练习 Prompt Engineering & LLM Optimization for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Prompt Engineering & LLM Optimization for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Prompt Engineering & LLM Optimization for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「自洽性与生成式知识」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Prompt Engineering & LLM Optimization for Developers 课中编写并运行代码吗?

能。每节 Prompt Engineering & LLM Optimization for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 思维链提示
  2. 自洽性与生成式知识
  3. 思维树与图结构提示
  4. ReAct:使用工具进行推理与行动
← 返回 Prompt Engineering & LLM Optimization for Developers