Step 3: Retrieve Chunks
This section will teach you how to retrieve raw chunks using the retrieval API
The "R" in RAG stands for Retrieval. Now that we've loaded some data into Ragie, it's time to see how we can access that data using the Retrieval API.
const apiKey = "<YOUR API KEY>";
(async () => {
const response = await fetch("https://api.ragie.ai/retrievals", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + apiKey,
},
body: JSON.stringify({
query: "davos",
filter: {
scope: "tutorial"
}
}),
});
const data = await response.json();
console.log(data);
})();
When you run the code above, you will see a response that returns 8 chunks by default:
// "..." is a placeholder for additional content
{
"scored_chunks": [
{
"text": "...",
"score": 0.4155921,
"document_id": "21881de1-65f7-4816-b571-3ef69661c375",
"document_metadata": { /** ... metadata fields */ }
},
// ... 7 more chunks
]
}
Once you have retrieved these chunks, you can use them as part of your prompt during generation which we will explain in the next section. Keep in mind that these are the raw chunks that were retrieved by Ragie, the real magic happens when you combine this content with an LLM to generate content. Don't be alarmed if these chunks don't make sense on their own because we still have one more step.
Filtering chunks by metadata
Notice the filter
parameter in the request. In Step 2 of this tutorial, we included metadata as part of the request to create a document. During retrieval, we can use the filter
parameter to filter results by this metadata. This is a simple filter, but filters can be more complex and include operators such as $in
, $eq
, and$nin
. Filters can also combine boolean conditions like $and
and $or
and quite robust.
A bit contrived, but an example of a request with a more complex filter might look something like this:
const response = await fetch("https://api.ragie.ai/retrievals", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + apiKey,
},
body: JSON.stringify({
query: "davos",
filter: {
$or: [
{ scope: { $eq: "tutorial" } },
{ scope: { $eq: "production" } },
],
},
}),
});
For more information on what filters are available, see the Metadata & Filters documentation.
Getting better results with rerank
By default, chunks are retrieved with rerank=true
. Rerank is a technique that is common to RAG applications and a good example of how Ragie helps keep you up to date with the best in class techniques without additional effort. When rerank is used, chunks are filtered through an additional LLM step before they are returned from the API. This helps LLMs reduce hallucinations and generates more accurate content.
In general, Ragie does its best to balance tradeoffs between speed and accuracy with it's defaults. In some cases, Ragie's default may not be what you need for your application. Rerank sacrifices some latency to improve results and most of the time this is not an issue. However, if speed is a major concern for your application, you can always pass rerank=false
to get the raw results without the overhead of filtering results through an LLM step.
Let's recap
In this section, you learned how to:
- Use the Retrieval API to retrieve raw chunks.
- Use filters to filter chunks by metadata.
- Use rerank to get better results and understand how Ragie keeps you up to date on the latest techniques without additional work.
Updated 5 months ago