Putting documents in the prompt
Answer from your documents, and say so when they do not help.
Once you have the best documents, put them in the prompt as context, and tell the model to answer only from that context. This keeps answers grounded in your real information instead of guesses. If the search finds nothing, it's often better not to call the model at all and just say you couldn't find it.
# To run on your own computer with an API key (it cannot run in this browser).
# First: pip install anthropic, and set the ANTHROPIC_API_KEY environment variable.
import anthropic
client = anthropic.Anthropic()
def answer(question):
found = search(question, docs)
if not found:
return "Sorry, I couldn't find that in our help pages."
context = "\n".join("- " + d for d in found)
prompt = (
"Answer the question using only the context. If the context doesn't say, say you don't know.\n"
f"Context:\n{context}\nQuestion: {question}"
)
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=300,
messages=[{"role": "user", "content": prompt}])
return reply.content[0].text'Use only the context' plus permission to say 'I don't know' greatly reduces made-up answers.
The pretend model here looks for 'Context:' and 'Question:' in your prompt and answers with the best matching sentence from the context.
Showing which document an answer came from (a source) helps people check it.