Code examples
Search from your app. Then answer with an LLM.
Search from your code
Set GRAPE_KEY to your API key from the API keys page. Replace $PROJECT with a project ID from the project page. Send the passages in hits to your LLM.
curl -X POST https://grape-inc.in/v1/projects/$PROJECT/search \
-H "Authorization: Bearer $GRAPE_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "your question", "limit": 6}'A prompt that works
These are the steps and the prompts that our benchmark measures. The LLM reads the brief of the project before it reads a passage. Thus it knows the language and the words of the documents.
- Read the brief one time for each project. Each line is a file: its path, its first line and the terms in its index. Keep the lines as
{files}.
curl https://grape-inc.in/grape/$PROJECT/brief \
-H "Authorization: Bearer $GRAPE_KEY"{
"brief": "Octopus.txt: The octopus is a soft-bodied, eight-limbed mollusc | octopus, arms, ink, ...\n
Vaccine.txt: A vaccine is a biological preparation | vaccine, immune, dose, ...\n"
}- Search with the question (Search a project). If the response has
refused: true, the answer is "Not in the documents." You do not need an LLM call. - If
coverageis 0.5 or more, answer with this system prompt. Send the passages as the message:
Answer in 1-2 sentences using only the excerpts, in the language of the question.
The excerpts come from a collection with these files:
{files}
If the excerpts don't contain the answer:
- if these files could still cover it, reply only: SEARCH: <the question as a short plain question,
in the language of the file names above, with the words the documents would use next to the answer>
(for a question with parts found in different places, one SEARCH line per part)
- otherwise reply exactly: Not in the documents.Question: {question}
Excerpts:
[{path}:{line}] {text}
[{path}:{line}] {text}A reply that starts with SEARCH: shows that the passages did not have the answer, but the files can have it. Search again with the question and each SEARCH line in queries. Then answer again. Do a maximum of two more searches.
A different language or different words
A coverage of less than 0.5 usually shows one of these: the question is in a different language, it uses different words, or it is off-topic. Send this short prompt first, with only the question as the message:
A document collection contains these files:
{files}
Reply with one line only:
SEARCH: <question> if these documents could answer the question: the question rewritten as a short
plain question in the language of the file titles, with the words the documents
would use next to the answer (for a question with several parts, one SEARCH
line per part)
NONE if the question cannot be answered from these documentsSearch with the SEARCH lines (put more than one in queries). Then answer with the prompt above. NONE shows that the answer is "Not in the documents." Do not make more calls.
The brief changes only when the project changes. Put it at the start of the system prompt. Then the prompt cache of your LLM provider keeps its cost low.
With MCP
Claude and ChatGPT do the same steps with MCP. The instructions of Grape tell them to call list_projects first. They search in the language and with the words of the documents. They search again with different words before they say that the documents do not have the answer.