File size: 2,549 Bytes
46e547f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import re

from rag_assignment.utils.load_utils import DocEntry


# ===== System Prompt ==================================================
SYSTEM_PROMPT = (
    "Use the evidence to answer the question. Output only the exact answer entity, "
    "date, number, or shortest answer phrase. Give one answer without explanation."
)
# ===== System Prompt ==================================================


# ===== Base Template ==================================================
PURE_TEMPLATE = """\
Question: {question}
Answer: """

RAG_TEMPLATE = """\
{retrieved_passages}\n
Context is provided above. Read the context carefully and answer the question based on it.\n
Question: {question}
Answer: """
# ===== Base Template ==================================================


def format_text(i: int, entry: DocEntry) -> str:
    return f"[{i}] Title: {entry.title} | {entry.text}"


def build_messages(question: str, passages: list[DocEntry], use_context: bool = True,
                   system_prompt: str | None = None) -> list[dict[str, str]]:
    if not use_context:
        user_message = PURE_TEMPLATE.format(question=question)
    else:
        psgs_text_list = [format_text(i, psg) for i, psg in enumerate(passages, start=1)]
        retrieved_passages = f"\n".join(psgs_text_list)
        user_message = RAG_TEMPLATE.format(retrieved_passages=retrieved_passages, question=question)
    return [
        {"role": "system", "content": SYSTEM_PROMPT if system_prompt is None else system_prompt},
        {"role": "user", "content": user_message}
    ]


_THINK_BLOCK = re.compile(r"<think>.*?</think>", flags=re.DOTALL)
_ANSWER_PREFIX = re.compile(r"^\s*(the\s+)?(final answer|answer|a)\s*[::]\s*", flags=re.IGNORECASE)


def postprocess_answer(text: str) -> str:
    text = _THINK_BLOCK.sub("", text)
    lines = [line.strip() for line in text.strip().splitlines() if line.strip()]
    if not lines:
        return ""
    answer = _ANSWER_PREFIX.sub("", lines[0])
    answer = answer.strip().strip("*").strip().strip("\"'“”‘’").strip()
    return answer.rstrip(".").strip()



if __name__ == "__main__":
    from rag_assignment.paths import DATA_DIR
    from rag_assignment.utils.load_utils import load_corpus
    passages = load_corpus(DATA_DIR / "small_corpus.jsonl")[:5]
    question = "Let's build prompt!"
    
    pure_prompt = build_messages(question, passages, False)
    rag_prompt = build_messages(question, passages)

    print("===== Pure =====")
    print(pure_prompt)
    print("===== RAG =====")
    print(rag_prompt)