RAG · Search a RAG workspace2 / 9
  1. 01
  2. 03
  3. 04
  4. 05
  5. 06
  6. 07
  7. 08
  8. 09

Search a RAG workspace

Example on GitHub(packages/sdk/examples/rag/rag-hyperdb/ingest.ts)

We've got a workspace from the previous lesson. This lesson queries it.

ragSearch({ modelId, workspace, query, topK }) returns an array of { score, content }. The modelId must match the one used at ingest, because different models put vectors in different spaces and cross-model similarity scores are meaningless.

Result is sorted by score descending. The call passes all four options to ragSearch:

const results = await ragSearch({
  modelId,
  workspace,
  query: "How do I make a peanut butter sandwich?",
  topK: 3,
});

slice(0, 80) clips the preview to one line. 80 chars is arbitrary, pick what fits your terminal. Also, the loop uses toFixed(4) for the score so keep that in mind:

let i = 0;
for (const result of results) {
  console.log(`Score ${result.score.toFixed(4)}: ${result.content.slice(0, 80)}...`);
  i += 1;
}

Each result carries the same content we put in, with a score field added (higher means more similar).

Note: topK is an integer, not a "score threshold". If you want to filter by confidence, sort by score and drop anything below your threshold.

Put it to the test

  1. Call ragSearch({ modelId, workspace, query, topK }) and store the results.
  2. Add a for (const result of results) loop that console.logs result.score and the first 80 chars of result.content.
index.ts

$ Run your code to see results

$