AI System
RAG Architecture
ClayDiary answers descriptive questions from retrieved context: the most relevant events are found and handed to the generator. Questions that SQL can answer, such as counts, go to the SQL agent instead. If the SQL agent produces no result, the question falls back to retrieval. See the Pipeline Overview.
One database for everything
- Structured data, text and vectors live in one PostgreSQL database, in the same rows. There is no separate vector store to keep in sync. See Database.
- Row-level security applies to retrieval like to everything else: every search runs in the user's own database session.
What is embedded
- The unit that is searched is one event in a piece's history. Pieces themselves are not embedded.
- Each event gets one text, built from the piece (title, clay body, tags) and the event (stage, location, glazes, notes).
- The text is embedded when the event is added, in the same request, by
text-embedding-3-smallthrough OpenRouter. The vector is stored with the text in the event's row. If the embedding fails, the event is not saved. - The vector index, the keyword index and the reranker all use this same text, so each judges the same words.
Indexes
- Vector index (DiskANN): it covers the vectors and finds events whose meaning is close to the question, even when the words differ.
- Keyword index (BM25): it covers the text and finds exact terms, such as a glaze name. It ranks by relevance, so a missing word does not rule an event out.
- Both are created once, by a database migration. Every insert then updates them automatically.
- At API startup the keyword index is rebuilt. After a database restart it lost its statistics and returned nothing, and the rebuild fixes that.
How retrieval works
- Two searches: every question runs a vector search and a BM25 search, each returning up to 50 candidates. The question is embedded with the same model as the events.
- Fusion: the two lists are merged by rank. An event found by both searches ranks higher. Only positions are used, not scores.
- Reranking: Cohere's reranker reads the question and each event together, while the two searches compare them separately. It reorders the best 50 candidates, and 15 are kept.
What the generator receives
- The 15 events are grouped under their pieces. Each piece shows its current state, then its events in time order, with location, glazes, rating and notes.
- Pieces are ordered by their best-ranked event. The source chips in the interface use the same order.
- The pieces handed to the generator are the consulted ones. Which of them the answer actually used is decided by the generator; see Answer Generation.
- If nothing matches, the generator gets the bare question.
When something fails
- If the embedding fails, the BM25 search answers alone.
- If the BM25 search returns nothing, the vector search answers alone.
- If reranking fails, the fused order stands.
A retrieval problem lowers the quality of an answer but does not stop it.
Seeing it
The retrieval step is recorded in the turn's trace. It shows, for every matched event, which search found it and its score, plus whether reranking ran and how many results each search returned. A keyword count of zero on every trace means the BM25 index is not working. See Monitoring.