Docs / Guides

Code examples

Search from your app, then answer with any LLM.

Search from code

Set GRAPE_KEY to your API key from the API keys page, and replace $PROJECT with a project ID from its page. Hand the passages in hits to your LLM.

1curl -X POST https://grape-industries.vercel.app/grape/$PROJECT/search \2  -H "Authorization: Bearer $GRAPE_KEY" \3  -d '{"query": "your question", "limit": 6}'

A prompt that works

These are the steps and the exact prompts our benchmark measures. The LLM sees the project's brief before it reads any passage, so it knows the documents' language and words.

  1. Once per project, read the brief. Each line is a file: its path, its opening line and the terms in its index. Keep the lines as {files}.
.cURL
1curl https://grape-industries.vercel.app/grape/$PROJECT/brief \2  -H "Authorization: Bearer $GRAPE_KEY"
.JSON
1{2  "brief": "Octopus.txt: The octopus is a soft-bodied, eight-limbed mollusc | octopus, arms, ink, ...\n3            Vaccine.txt: A vaccine is a biological preparation | vaccine, immune, dose, ...\n"4}
  1. Search with the question as asked (Search a project). If the response says refused: true, the answer is "Not in the documents." and no LLM call is needed.
  2. If coverage is 0.5 or more, answer with this system prompt and the passages as the message:
.PROMPT
1Answer in 1-2 sentences using only the excerpts, in the language of the question.2The excerpts come from a collection with these files:3{files}4If the excerpts don't contain the answer:5- if these files could still cover it, reply only: SEARCH: <the question as a short plain question,6  in the language of the file names above, with the words the documents would use next to the answer>7  (for a question with parts found in different places, one SEARCH line per part)8- otherwise reply exactly: Not in the documents.
.MESSAGE
1Question: {question}23Excerpts:4[{path}:{line}] {text}5[{path}:{line}] {text}

A reply that starts with SEARCH: means the passages missed, but the files could still cover it. Search again with the question and each SEARCH line in queries, and answer once more. Allow two extra searches at most.

Another language or other words

A coverage under 0.5 usually means the question is in another language than the documents, uses other words, or is off-topic. Send this short prompt first, with only the question as the message:

.PROMPT
1A document collection contains these files:2{files}3Reply with one line only:4SEARCH: <question>   if these documents could answer the question: the question rewritten as a short5                     plain question in the language of the file titles, with the words the documents6                     would use next to the answer (for a question with several parts, one SEARCH7                     line per part)8NONE                 if the question cannot be answered from these documents

Search the SEARCH lines (several go in queries) and answer with the prompt above. NONE means the answer is "Not in the documents.", with no more calls.

With MCP

Claude and ChatGPT follow the same steps over MCP. Grape's instructions tell them to call list_projects first, search in the documents' language with the documents' words, and search again with other words before saying the answer is not there.