Skip to main content
You have GitHub’s Terms of Service and a plain-language question about it. You need the lines that answer the question and a way to detect when the document has no answer. You end up with find(), which returns the exists probability and one relevance score per line. A query scans a document and reveals an answer attached to the matching
line The search has three parts:
  1. Tag each line with an ID so Jev can point to it.
  2. Use a Choice question to rank those line IDs by how well they answer the query.
  3. In the same request, use a Noul question to check whether the document contains an answer at all.

Setup

Run in Colab

Runs as published without an API key.

Python file

This page as one .py file, to run locally.

Open in playground

Edit one request and run it live. Needs a TypeSafe account.
To run it on your own machine instead:
With uv, uv run semantic_find.py installs the dependencies for you. The code replays saved answers, so it runs as published without an API key. To change a request, create a TypeSafe API key and set TYPESAFE_API_KEY; in Colab, add it in the Secrets panel. The first block imports typesafe_sdk, reads TYPESAFE_API_KEY and creates the client:

Step 1: tag every line with an ID

The test document is GitHub’s Terms of Service, split into 218 clauses, so every search result points to one quotable line.
Now prefix each line with a short ID and join the lines back into one document. The model uses these IDs to point to its answer.
DOCUMENT now looks like this:

Step 2: ask where the answer is

A Choice question returns a probability for every option. Use the line IDs as the options.
The option descriptions are None because the document already contains the text for each ID. The query goes in instructions; the state stays unchanged between searches.
A Choice question accepts up to 255 options, so this recipe searches documents of up to 255 lines in one request. Past that, search in two passes: one Choice question picks a window of lines, and a second ranks the lines inside it.

Step 3: check whether an answer exists

Choice probabilities always add up to 1, so some line ranks first even when the document doesn’t answer the question.
Unlike the Choice probabilities, the Noul probability doesn’t depend on the other options, so it can fall near zero when the document has no answer.

Step 4: send both questions in one request

Both questions go in one system_one call, so the document is sent once.
The relevance list keeps one score per line, in document order.

Step 5: read the result

The exists probability becomes one of three verdicts, the middle one for partial answers. The ranking prints as a bar chart.
Tune these thresholds on your own documents before using them in production. Ask two questions that have direct answers, one that has a partial answer, and one that has no answer.

What the scores mean

The first two queries return direct answers and the source lines needed to verify them.
  • Arbitration: The Terms never mention arbitration. The top line, at 0.80, is the governing-law clause, which sends disputes to the courts in San Francisco. exists is 0.65, so the result is partially addressed.
  • Class action: The Terms never mention class actions. The ranking still puts the governing-law clause first, at 0.23, because a ranking always has a top line. exists is 0.05, so the result is not in this document.
The ranking tells you where to look; the exists score tells you whether the result answers the question. To search your own document, load its lines into LINES; every other line of the script works off LINES.

Open it in the playground

The link holds the tagged contract and both questions for “who owns the code I upload?”.
Open in the TypeSafe playground ↑