Find the matching section

One question, one line

Search should return the section that shares words with the question, and leave the others unread.

The question mentions the ferry, so the ticket line is the one that prints. The apple line and the boat line stay out.

Example

from praisonaiagents.knowledge import KeywordIndex

lines = [
    "Apples grow on trees in the orchard.",
    "The secret colour of the boat is blue.",
    "Tickets for the ferry cost four pounds.",
]
index = KeywordIndex()
index.add_documents(lines)
print(index.query("How much is the ferry?", top_k=1)[0]["text"])

Try it Yourself

Example explained

  • top_k — How many lines to keep. One is enough here.

top_k is two

When the question is vague, retrieve two lines and read both before you generate.

Example

from praisonaiagents.knowledge import KeywordIndex

lines = [
    "Apples grow on trees in the orchard.",
    "The secret colour of the boat is blue.",
    "Tickets for the ferry cost four pounds.",
]
index = KeywordIndex()
index.add_documents(lines)
for hit in index.query("boat and ferry", top_k=2):
    print(hit["text"])

Try it Yourself

Reference: documentation