import re

documents = [
    ("RAG", "Retrieval augmented generation retrieves relevant documents before answering."),
    ("Agents", "AI agents observe, plan, use tools, and evaluate outcomes."),
    ("Memory", "Persistent memory stores useful information across sessions.")
]

def retrieve(question):
    words = set(re.findall(r"\w+", question.lower()))
    stop = {"what", "is", "the", "a", "an", "for", "and", "to"}
    words -= stop
    ranked = []
    for title, content in documents:
        terms = set(re.findall(r"\w+", (title + " " + content).lower()))
        ranked.append((len(words & terms), title, content))
    ranked.sort(reverse=True)
    return ranked[0] if ranked and ranked[0][0] else None

print("=== RAG Agent ===")
question = input("Question: ")
result = retrieve(question)
if result:
    print("Retrieved document:", result[1])
    print("Answer:", result[2])
else:
    print("No relevant document found.")
