جواب «Step 2: Search, Ask, Cite» چالش 2 از جلسهی 72
سؤال
تابع rag_answer(question) را بنویس: با best_chunk بهترین تکه را پیدا کن؛ اگر None بود، بدون صدا زدن مدل Not in the notes. برگردان؛ وگرنه پرامپت را بساز، fake_model را صدا بزن و جواب را با [source: chunk <i>] برگردان. شمارندهی calls تعداد صدا زدن مدل است. خروجی:Condensation is when the vapour cools high in the sky and forms clouds. [source: chunk 1]Precipitation is when the drops in a cloud get heavy and fall as rain or snow. [source: chunk 2]Not in the notes.model calls: 2
راهنما
index, score = best_chunk(question, CHUNKS)؛ if index is None: return "Not in the notes."؛ reply = fake_model(build_prompt(question, CHUNKS[index]))؛ return f"{reply} [source: chunk {index}]".
یک جواب درست
STOP = {"the", "a", "an", "is", "are", "and", "or", "of", "in", "on", "to", "into", "it", "this",
"that", "what", "how", "why", "does", "do", "as", "when", "they", "called", "its"}
CHUNKS = [
"Evaporation is when the sun heats water in seas and lakes and the warm water turns into vapour and rises.",
"Condensation is when the vapour cools high in the sky and forms clouds.",
"Precipitation is when the drops in a cloud get heavy and fall as rain or snow.",
"Transpiration is when plants lose water through their leaves.",
]
RULE = 'Answer ONLY from the context below. If the answer is not there, say "I don\'t know".'
def tokens(text):
words = []
for w in text.lower().split():
w = "".join(ch for ch in w if ch.isalpha())
if w and w not in STOP:
words.append(w)
return words
def jaccard(a, b):
a, b = set(a), set(b)
if not a or not b:
return 0
return len(a & b) / len(a | b)
def best_chunk(question, chunks):
q = tokens(question)
best, best_score = None, 0
for i, chunk in enumerate(chunks):
s = jaccard(q, tokens(chunk))
if s > best_score:
best, best_score = i, s
return best, best_score
def build_prompt(question, context):
return "\n".join([RULE, "<context>", context, "</context>", "Question: " + question])
calls = 0
def fake_model(prompt):
# A pretend model: it repeats the context, or says it does not know.
global calls
calls += 1
if "<context>" not in prompt:
return "I am guessing: it is a famous fact."
context = prompt.split("<context>")[1].split("</context>")[0].strip()
return context if context else "I don't know."
def rag_answer(question):
index, score = best_chunk(question, CHUNKS)
if index is None:
return "Not in the notes."
reply = fake_model(build_prompt(question, CHUNKS[index]))
return f"{reply} [source: chunk {index}]"
for q in ["What is condensation?", "Why does rain fall?", "Who invented the bicycle?"]:
print(rag_answer(q))
print("model calls:", calls)
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن