Say when it is not there

No shared words

If the question shares no important word with any line, search has not found an answer. Say so, and do not ask the model.

A word counts when it is longer than three letters. handbook is not in the note, so the result is Not in the note.

Example

from praisonai 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)
question = "Who wrote the handbook?"
words = [word.strip("?.!,").lower() for word in question.split() if len(word) > 3]
hit = index.query(question, top_k=1)[0]
if any(word in hit["text"].lower() for word in words):
    print(hit["text"])
else:
    print("Not in the note.")

Try it Yourself

Example explained

  • words — The important words in the question.
  • any — True only when one of those words is in the line.

The line does not say

A shared word can still find the wrong fact. The boat line is about colour, not where the boat is kept. The agent must say the note does not answer that.

Example

from praisonai import Agent, 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)
question = "Where is the boat kept?"
section = index.query(question, top_k=1)[0]["text"]
print(section)
agent = Agent(instructions="Answer in one sentence. Use only the note. If the note does not say, reply: Not in the note.")
print(agent.start(question + "\nNote: " + section))

Try it Yourself

Example explained

  • Not in the note — The reply when the line does not contain the answer.

Reference: documentation