جواب «Step 2: Answer with a Source» چالش 2 از جلسهی 44
سؤال
answer(question): تکه را با best_chunk پیدا کن؛ اگر None بود NOT_FOUND را بدون صدا زدن مدل برگردان. وگرنه پرامپت سهخطی بساز:Use only this handout part. If the answer is not in it, say I don't know.HANDOUT PART: + تکهQUESTION: + سؤال
و ask(prompt, system=SYSTEM) + " [part 3]" را برگردان (شماره = اندیس + ۱). مدل ساختگی test جملهی اول تکه را برمیگرداند. خروجی:The robot Sprout waters the beds at 7 in the morning. [part 3]Ripe tomatoes are picked on Fridays. [part 4]I don't know. The handout does not say.
راهنما
خطها را با "\n" به هم بچسبان. "HANDOUT PART: " باید دقیقاً همین باشد، چون مدل ساختگی دنبالش میگردد.
یک جواب درست
HANDOUT = """The Robo Garden club meets every Tuesday after school.
The club has 12 members and one teacher.
The garden has 4 tomato beds and 2 herb beds.
Each tomato bed needs 3 liters of water every day.
The robot Sprout waters the beds at 7 in the morning.
Sprout runs on a small solar panel.
Ripe tomatoes are picked on Fridays.
Herbs are picked whenever the cook asks for them."""
STOP = {"the", "a", "an", "is", "of", "in", "on", "and", "what", "how", "do", "does", "it", "at", "to", "many"}
def words(text):
clean = ""
for ch in text.lower():
if ch in ".,?!":
clean += " "
else:
clean += ch
result = set()
for w in clean.split():
if w not in STOP:
result.add(w)
return result
def split_chunks(text):
chunks = []
for part in text.split("\n\n"):
part = part.strip()
if part != "":
chunks.append(part)
return chunks
def score(question, chunk):
return len(words(question) & words(chunk))
CHUNKS = split_chunks(HANDOUT)
def best_chunk(question):
best = None
best_score = 0
for i in range(len(CHUNKS)):
s = score(question, CHUNKS[i])
if s > best_score:
best = i
best_score = s
return best
from ai import ask
SYSTEM = "You are a study helper for the Robo Garden club. Be short and kind."
NOT_FOUND = "I don't know. The handout does not say."
def answer(question):
i = best_chunk(question)
if i is None:
return NOT_FOUND
prompt = ("Use only this handout part. If the answer is not in it, say I don't know.\n"
+ "HANDOUT PART: " + CHUNKS[i] + "\n"
+ "QUESTION: " + question)
return ask(prompt, system=SYSTEM) + " [part " + str(i + 1) + "]"
# ---- test with a pretend model (do not change) ----
def fake_ask(prompt, system=None, history=None):
if "HANDOUT PART: " not in prompt:
return "???"
part = prompt.split("HANDOUT PART: ")[1]
if "week" in prompt and "TOOL" in (system or ""):
return "TOOL calc 12 * 7"
return part.split(".")[0] + "."
ask = fake_ask
print(answer("Who waters the beds?"))
print(answer("When are tomatoes picked?"))
print(answer("What is the capital of France?"))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن