Skip to main content
Search often returns passages that share words with the query without answering it. This example grades a passage against the query, so you can rerank passages and send only the ones that help. Use it between retrieval and generation.

How it works

The context is JSON with the query and one passage. The keys are yours to choose. One ordinal decision, relevance, grades the passage on four levels, which its rubric describes: The answer gives a probability for each level, under the keys "0" to "3", which are strings. Its expected_level is the average level, from 0 to 3.

Try it

Open this recipe in the playground to run it without an account. The query there asks when the Channel Tunnel opened to passengers. The passage gives the formal opening on 6 May 1994, and says passenger services began on 14 November 1994. To run it from your code, set SQWISH_API_KEY as in the quickstart, then send this request:

Act on the answer

Rerank your passages by score, highest first, and keep as many as your prompt has room for. To keep or drop a passage on its own, compare p_answers with a threshold you test on your own queries.

Adapt it to your data

There are two ways to grade many passages. One request per passage. Send the same query with each passage in turn. Each grade is independent of the others, and this is how it was measured. Several passages in one request. Put the passages under keys in the context, and ask one decision per passage that names its key. You pay for the query once, and one request can ask up to 128 decisions. Decisions in one request can affect each other’s answers, though, so one passage can change another’s grade. Use one request per passage when grades must not influence each other.
Python
Each passage’s grade is under its own key, such as result["decisions"]["p1"], and ranked lists the keys best first.

What to watch

  • On-topic passages graded too high. A passage can share the topic and still not answer the query: that is level 1. In testing, it graded many passages people judged not relevant as level 2.
  • The effect on your answers. Compare what your generator writes with and without the reranking before you trust it.

How well it works

Measured with kettei-core on 1,000 examples from a public dataset, it chose the right answer 78.0% of the time. Always picking the most common answer scores 72.4%. Your own data will differ, so measure it on examples of yours before you rely on it (improve a decision).
  • Dataset: MIRACL English (dev) (Apache-2.0 (the MTEB passage copy is CC BY-SA 4.0)).
  • Sample: 1,000 rows, 1,000 measured decisions.
  • Decisions measured: relevance.
  • Wording: hand-written.
Always picking the most common answer has an accuracy of 0.724, and guessing by how common each answer is has a log loss of 0.5891.These are past results on one dataset, not a promise of accuracy on yours.
The test measured the grade itself, not how much it improves the answers your generator writes.

Next steps