P
Prompt Master
Prompt Engineering 教程与提示词实战

从任务定义、示例和工作流到评测与安全边界

Prompt Engineering Tutorial and Prompt PracticePrompt Library

Closed Domain Q&A

Closed-domain question answering prompt example

CHAPTER PROMPT DECISION
01Prompt problem

Closed-domain question answering prompt example

02Reviewable output

A reusable prompt, example set, evaluation record or safety rule with its task boundary preserved.

03Definition of done

Run at least one representative case, inspect the result and record what still requires human review.

TL;DR

  • The core of Closed-domain Q&A: answers must be strictly limited to the given facts/context. No "freestyling."
  • The most common problem: the model fills in information that doesn't exist (hallucination), especially in specialized domains (medical/legal/finance).
  • Production tip: enforce "use exclusively the information above," and pair it with evaluation (cross-check every claim).

Background

The following prompt tests an LLM's capabilities to answer closed-domain questions which involves answering questions within a specific topic or domain.

Note: due to the challenging nature of the task, LLMs are likely to hallucinate when they have no knowledge regarding the question.

How to Apply

This example transfers directly to "rewrite/generate documents from given data" scenarios:

  • Input is facts (bullet list / table / JSON)
  • Output is a structured note (medical note, case summary, policy memo, etc.)
  • Require "use only given information" to avoid speculative content

How to Iterate

  1. Define an explicit output template (sections + fields) to reduce the model's room to improvise
  2. Require citations: each output paragraph tags the corresponding fact line number/field name (if input can be numbered)
  3. Add self-check: before output, list a "claims list," then cross-reference each claim against the input
  4. Add "unknown policy": when information is missing, output "Unknown" or ask clarifying questions

Self-check Rubric

  • Does the output contain specific information not in the input (diagnosis, BMI, complications, treatment recommendations, etc.)?
  • Are facts and speculation mixed together? Can assumptions be clearly labeled?
  • Does it follow "exclusively the information above"?

Practice

Exercise: use your own work materials for a closed-domain rewrite.

  1. Prepare a set of facts (10-20 items, ideally numbered)
  2. Have the model output a structured note
  3. Then use a truthfulness rubric for claim-by-claim review (can pair with /prompt-truthfulness-identify-hallucination)

Prompt

Patient's facts:

-   20 year old female
-   with a history of anerxia nervosa and depression
-   blood pressure 100/50, pulse 50, height 5'5''
-   referred by her nutrionist but is in denial of her illness
-   reports eating fine but is severely underweight

Please rewrite the data above into a medical note, using exclusively the information above.

Code / API

OpenAI (Python)

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {
            "role": "user",
            "content": "Patient's facts:\n- 20 year old female\n- with a history of anerxia nervosa and depression\n- blood pressure 100/50, pulse 50, height 5'5''\n- referred by her nutrionist but is in denial of her illness\n- reports eating fine but is severely underweight\n\nPlease rewrite the data above into a medical note, using exclusively the information above.",
        }
    ],
    temperature=1,
    max_tokens=500,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0,
)

Fireworks (Python)

import fireworks.client

fireworks.client.api_key = "<FIREWORKS_API_KEY>"

completion = fireworks.client.ChatCompletion.create(
    model="accounts/fireworks/models/mixtral-8x7b-instruct",
    messages=[
        {
            "role": "user",
            "content": "Patient's facts:\n- 20 year old female\n- with a history of anerxia nervosa and depression\n- blood pressure 100/50, pulse 50, height 5'5''\n- referred by her nutrionist but is in denial of her illness\n- reports eating fine but is severely underweight\n\nPlease rewrite the data above into a medical note, using exclusively the information above.",
        }
    ],
    stop=["<|im_start|>", "<|im_end|>", "<|endoftext|>"],
    stream=True,
    n=1,
    top_p=1,
    top_k=40,
    presence_penalty=0,
    frequency_penalty=0,
    prompt_truncate_len=1024,
    context_length_exceeded_behavior="truncate",
    temperature=0.9,
    max_tokens=4000,
)

Reference

📚 Related resources