Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bench/batch_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ int main() {
auto emb = rag::plugin::make_embedder(nlohmann::json{{"type", "hash"}, {"dim", 384}});
c.set_embedder(std::move(*emb));
for (std::size_t i = 0; i < docs; ++i)
c.add_document("d" + std::to_string(i),
"retrieval document " + std::to_string(i) +
" about vectors graphs indexes ranking and search");
(void)c.add_document("d" + std::to_string(i),
"retrieval document " + std::to_string(i) +
" about vectors graphs indexes ranking and search");
(void)c.build();

std::vector<std::string> qs;
Expand Down
2 changes: 1 addition & 1 deletion bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main(int argc, char** argv) {

auto t0 = Clock::now();
for (std::size_t i = 0; i < n; ++i)
engine.add("doc" + std::to_string(i), synth_doc(rng, 60));
(void)engine.add("doc" + std::to_string(i), synth_doc(rng, 60));
auto t1 = Clock::now();
auto b = engine.build();
auto t2 = Clock::now();
Expand Down
3 changes: 2 additions & 1 deletion bench/code_retrieval_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
// chunks. That isolates chunking, which is the claim.
//
// Usage:
// ragcpp_code_retrieval_bench <corpus-dir> <manifest.json> \
// ragcpp_code_retrieval_bench <corpus-dir> <manifest.json>
// --model=model.onnx --tokenizer=tokenizer.json [--limit=N]
// (one command; shown on two lines)
//
// Requires -DRAGCPP_WITH_ONNX=ON (otherwise there is no embedder and nothing to
// measure).
Expand Down
5 changes: 3 additions & 2 deletions examples/advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <cstdio>
#include <rag/rag.hpp>
#include "must.hpp"

int main() {
rag::Engine engine;
Expand All @@ -28,8 +29,8 @@ int main() {
"Risotto: toast arborio rice, then add warm stock gradually while "
"stirring until creamy; finish with butter and parmesan.",
};
for (int i = 0; i < 7; ++i) engine.add("d" + std::to_string(i), docs[i]);
engine.build();
for (int i = 0; i < 7; ++i) must(engine.add("d" + std::to_string(i), docs[i]), "add");
must(engine.build(), "build");

const char* Q = "how does approximate nearest neighbour vector search work";
std::printf("Query: %s\n\n", Q);
Expand Down
19 changes: 10 additions & 9 deletions examples/full_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cstdio>
#include <rag/rag.hpp>
#include "must.hpp"

int main() {
using namespace rag;
Expand All @@ -16,22 +17,22 @@ int main() {
Engine engine(cfg);
engine.with_embedder(dense::AnyEmbedder{dense::HashEmbedder{256}});

engine.add("ann.md",
must(engine.add("ann.md",
"# Approximate Nearest Neighbours\n\nHNSW builds a layered proximity graph "
"for logarithmic-time vector search. Binary quantization speeds the walk.",
{{"section","index"}});
engine.add("bm25.md",
{{"section","index"}}), "add");
must(engine.add("bm25.md",
"# Lexical Retrieval\n\nBM25 scores documents by term frequency and inverse "
"document frequency, catching exact keyword matches embeddings miss.",
{{"section","lexical"}});
engine.add("fusion.md",
{{"section","lexical"}}), "add");
must(engine.add("fusion.md",
"# Fusion\n\nReciprocal rank fusion combines lexical and dense rankings "
"without score normalization, the most robust hybrid retrieval method.",
{{"section","fusion"}});
engine.add("risotto.md",
{{"section","fusion"}}), "add");
must(engine.add("risotto.md",
"# Risotto\n\nToast arborio rice, then add warm stock one ladle at a time.",
{{"section","cooking"}});
engine.build();
{{"section","cooking"}}), "add");
must(engine.build(), "build");

// A local cross-encoder stand-in: reward passages sharing query terms.
rerank::ScoreFnReranker ce([](std::string_view q, std::string_view p) {
Expand Down
23 changes: 12 additions & 11 deletions examples/graphrag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@

#include <cstdio>
#include <rag/rag.hpp>
#include "must.hpp"

int main() {
rag::Engine engine;

// A small corpus where documents LINK to one another (markdown) and cluster
// by topic. No embedder → similarity edges fall back to lexical Jaccard and
// retrieval degrades gracefully to BM25. Attach an embedder for dense edges.
engine.add("intro.md",
must(engine.add("intro.md",
"Retrieval-augmented generation grounds a language model on external "
"documents. See [dense](dense.md) and [graph](graph.md) for the engine internals.");
engine.add("dense.md",
"documents. See [dense](dense.md) and [graph](graph.md) for the engine internals."), "add");
must(engine.add("dense.md",
"The dense retriever embeds passages and ranks them by cosine similarity "
"over an HNSW index. It pairs with [lexical](lexical.md) BM25 scoring.");
engine.add("lexical.md",
"over an HNSW index. It pairs with [lexical](lexical.md) BM25 scoring."), "add");
must(engine.add("lexical.md",
"BM25 is the Okapi lexical ranker: term frequency saturation plus inverse "
"document frequency. It fuses with dense results via reciprocal rank fusion.");
engine.add("graph.md",
"document frequency. It fuses with dense results via reciprocal rank fusion."), "add");
must(engine.add("graph.md",
"GraphRAG builds a document graph — link edges and similarity edges — then "
"detects communities and summarizes each cluster for corpus-level questions.");
engine.add("cooking.md",
"detects communities and summarizes each cluster for corpus-level questions."), "add");
must(engine.add("cooking.md",
"Risotto: toast arborio rice, then ladle warm stock gradually while stirring "
"until creamy. Finish with butter and parmesan off the heat.");
engine.build();
"until creamy. Finish with butter and parmesan off the heat."), "add");
must(engine.build(), "build");

// ── Document graph ─────────────────────────────────────────────────────────
auto g = engine.graph();
Expand Down
13 changes: 13 additions & 0 deletions examples/must.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
// examples/must.hpp - one helper every example uses: an example must not
// swallow a failure, so it reports it and stops.

#include <cstdio>
#include <cstdlib>

inline void must(const auto& result, const char* what) {
if (!result) {
std::fprintf(stderr, "%s failed: %s\n", what, result.error().message.c_str());
std::exit(1);
}
}
7 changes: 4 additions & 3 deletions examples/plugin_backend/host_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <cstdio>
#include <string>
#include "../must.hpp"

int main(int argc, char** argv) {
// Path to the compiled plugin (defaults to the CMake output location).
Expand Down Expand Up @@ -45,9 +46,9 @@ int main(int argc, char** argv) {
return 1;
}

engine.add("d1", "The Eiffel Tower is in Paris.");
engine.add("d2", "Photosynthesis happens in plant leaves.");
engine.build();
must(engine.add("d1", "The Eiffel Tower is in Paris."), "add");
must(engine.add("d2", "Photosynthesis happens in plant leaves."), "add");
must(engine.build(), "build");

auto hits = engine.search("landmark in Paris", 2);
if (hits) {
Expand Down
3 changes: 1 addition & 2 deletions examples/plugin_backend/local_embedder_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
// of the host and no host knowledge that this type exists.
//
// Build (standalone, MUST match the host toolchain and rag headers/ABI):
// g++-15 -std=c++23 -fPIC -shared -I<rag>/include \
// local_embedder_plugin.cpp -o liblocal_ngram.so
// g++-15 -std=c++23 -fPIC -shared -I<rag>/include local_embedder_plugin.cpp -o liblocal_ngram.so
// Or via this repo's CMake (target: local_ngram_plugin).

#include <rag/plugin/plugin.hpp>
Expand Down
9 changes: 5 additions & 4 deletions examples/plugin_backend/local_host_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <cstdio>
#include <string>
#include "../must.hpp"

namespace {
void print_names(const rag::plugin::Registries& regs) {
Expand Down Expand Up @@ -71,10 +72,10 @@ int main(int argc, char** argv) {
}
std::printf("\nWired embedder from config (onnx || retry(local_ngram)).\n");

engine.add("d1", "The Eiffel Tower is a landmark in Paris.");
engine.add("d2", "Photosynthesis converts light into chemical energy in leaves.");
engine.add("d3", "HNSW is a graph index for approximate nearest neighbours.");
engine.build();
must(engine.add("d1", "The Eiffel Tower is a landmark in Paris."), "add");
must(engine.add("d2", "Photosynthesis converts light into chemical energy in leaves."), "add");
must(engine.add("d3", "HNSW is a graph index for approximate nearest neighbours."), "add");
must(engine.build(), "build");

// Character n-grams give sub-word recall: "landmarks near Paris" matches d1
// even though the exact phrase never appears.
Expand Down
3 changes: 1 addition & 2 deletions examples/plugin_backend/my_embedder_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
// no recompile of the host application.
//
// Build (standalone, matching the host toolchain):
// g++-15 -std=c++23 -fPIC -shared -I<rag>/include \
// my_embedder_plugin.cpp -o libreverse_hash.so
// g++-15 -std=c++23 -fPIC -shared -I<rag>/include my_embedder_plugin.cpp -o libreverse_hash.so
// Or via this repo's CMake (see CMakeLists.txt here).

#include <rag/plugin/plugin.hpp>
Expand Down
7 changes: 4 additions & 3 deletions examples/polyglot/python_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <cstdio>
#include <string>
#include "../must.hpp"

int main(int argc, char** argv) {
std::string server = argc > 1 ? argv[1] : "examples/polyglot/ragcpp_server.py";
Expand All @@ -38,9 +39,9 @@ int main(int argc, char** argv) {
return 1;
}

engine.add("a", "The Eiffel Tower is in Paris.");
engine.add("b", "Plants perform photosynthesis in their leaves.");
engine.build();
must(engine.add("a", "The Eiffel Tower is in Paris."), "add");
must(engine.add("b", "Plants perform photosynthesis in their leaves."), "add");
must(engine.build(), "build");
if (auto hits = engine.search("landmark in the French capital", 2)) {
std::printf("[embedder-via-python] top = %s (%zu hits)\n",
hits->empty() ? "(none)" : (*hits)[0].uri.c_str(), hits->size());
Expand Down
13 changes: 7 additions & 6 deletions examples/quickstart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

#include <cstdio>
#include <rag/rag.hpp>
#include "must.hpp"

int main() {
rag::Engine engine;
engine.with_embedder(rag::dense::AnyEmbedder{rag::dense::HashEmbedder{256}});

engine.add("intro.md",
must(engine.add("intro.md",
"# rag-cpp\n\nrag-cpp is a type-theoretic retrieval engine. It fuses BM25 "
"lexical scoring with dense vector search using reciprocal rank fusion.");
engine.add("hnsw.md",
"lexical scoring with dense vector search using reciprocal rank fusion."), "add");
must(engine.add("hnsw.md",
"# ANN\n\nThe HNSW index gives logarithmic-time nearest-neighbour search "
"over embeddings, with binary quantization and Matryoshka truncation.");
engine.add("unrelated.md",
"# Cooking\n\nTo make a good risotto, toast the rice before adding stock.");
"over embeddings, with binary quantization and Matryoshka truncation."), "add");
must(engine.add("unrelated.md",
"# Cooking\n\nTo make a good risotto, toast the rice before adding stock."), "add");

if (auto b = engine.build(); !b) {
std::printf("build failed: %s\n", b.error().message.c_str());
Expand Down
27 changes: 14 additions & 13 deletions examples/rcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <rag/sparse/splade.hpp>
#include <rag/late/colbert.hpp>
#include <rag/rerank/reranker.hpp>
#include "must.hpp"

namespace {

Expand All @@ -38,19 +39,19 @@ rag::Engine build_demo_engine() {
rag::Engine engine;
engine.with_embedder(rag::dense::AnyEmbedder{rag::dense::HashEmbedder{256}});

engine.add("doc://eiffel", "The Eiffel Tower is a wrought-iron lattice tower on "
"the Champ de Mars in Paris, France, completed in 1889.",
{{"lang", "en"}, {"topic", "landmarks"}});
engine.add("doc://photosynthesis", "Photosynthesis is the process by which plants "
"convert light energy into chemical energy stored in glucose.",
{{"lang", "en"}, {"topic", "biology"}});
engine.add("doc://hnsw", "HNSW is a graph index for approximate nearest-neighbour "
"search, giving logarithmic query time over high-dimensional vectors.",
{{"lang", "en"}, {"topic", "retrieval"}});
engine.add("doc://rrf", "Reciprocal rank fusion merges ranked lists from lexical "
"and dense retrievers into one robust hybrid ranking.",
{{"lang", "en"}, {"topic", "retrieval"}});
engine.build();
must(engine.add("doc://eiffel", "The Eiffel Tower is a wrought-iron lattice tower on "
"the Champ de Mars in Paris, France, completed in 1889.",
{{"lang", "en"}, {"topic", "landmarks"}}), "add");
must(engine.add("doc://photosynthesis", "Photosynthesis is the process by which plants "
"convert light energy into chemical energy stored in glucose.",
{{"lang", "en"}, {"topic", "biology"}}), "add");
must(engine.add("doc://hnsw", "HNSW is a graph index for approximate nearest-neighbour "
"search, giving logarithmic query time over high-dimensional vectors.",
{{"lang", "en"}, {"topic", "retrieval"}}), "add");
must(engine.add("doc://rrf", "Reciprocal rank fusion merges ranked lists from lexical "
"and dense retrievers into one robust hybrid ranking.",
{{"lang", "en"}, {"topic", "retrieval"}}), "add");
must(engine.build(), "build");
return engine;
}

Expand Down
2 changes: 1 addition & 1 deletion include/rag/rcp/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ compile_filter(const Json& raw, const Json& advertised_fields) {
catch (...) { return false; }
};
auto cmp = [&](auto pred) -> bool {
double a, b;
double a;
if (num(mv, a) && val.is_number()) return pred(a, val.get<double>());
return pred(std::string_view(mv), std::string_view(as_str(val)));
};
Expand Down
6 changes: 4 additions & 2 deletions include/rag/text/contextual.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ using Contextualizer =
void contextualize(std::vector<Chunk>& chunks, std::string_view document,
const Contextualizer& ctx = {});

// The deterministic default: document title (first non-empty line) + the
// document sentence with the highest term overlap with the chunk.
// The deterministic default: the document's title + the document sentence with
// the highest term overlap with the chunk. The title is front matter's
// `title:` when present, else the first non-empty line after any front
// matter. Neither is repeated when the chunk already contains it.
[[nodiscard]] std::string
extractive_context(std::string_view document, std::string_view chunk);

Expand Down
Loading
Loading