<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Juan Pereira</title>
    <description>The latest articles on DEV Community by Juan Pereira (@juan_pereira).</description>
    <link>https://dev.to/juan_pereira</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4100101%2F8bcdd484-1ea0-4566-a56c-7d4e3a0dc7e0.png</url>
      <title>DEV Community: Juan Pereira</title>
      <link>https://dev.to/juan_pereira</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/juan_pereira"/>
    <language>en</language>
    <item>
      <title>Building a Hybrid Agentic RAG System with FAISS, BM25 and Qwen</title>
      <dc:creator>Juan Pereira</dc:creator>
      <pubDate>Sun, 30 Aug 2026 19:47:27 +0000</pubDate>
      <link>https://dev.to/juan_pereira/building-a-hybrid-agentic-rag-system-with-faiss-bm25-and-qwen-3ec0</link>
      <guid>https://dev.to/juan_pereira/building-a-hybrid-agentic-rag-system-with-faiss-bm25-and-qwen-3ec0</guid>
      <description>&lt;p&gt;Large Language Models (LLMs) are powerful at understanding and generating text, but they do not automatically have access to information stored in private or domain-specific documents. Retrieval-Augmented Generation (RAG) addresses this problem by retrieving relevant information from an external knowledge base and providing it to the language model as context.&lt;/p&gt;

&lt;p&gt;In this project, I built a small hybrid Agentic RAG system that combines semantic search, keyword-based search, and language generation. The system uses Sentence Transformers for embeddings, FAISS for vector search, BM25 for keyword retrieval, and Qwen as the language model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Workflow&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;User Query → Hybrid Retrieval → Relevant Context → Qwen LLM → Final Answer&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Architecture&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl3ecoswtp4xz5w59o15c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl3ecoswtp4xz5w59o15c.jpg" alt="Architecture" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document Processing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The system starts with a small knowledge base containing documents related to Artificial Intelligence, Large Language Models, RAG, embeddings, vector search, hybrid search, and RAG evaluation.&lt;/p&gt;

&lt;p&gt;Before retrieval, the documents are divided into smaller chunks using recursive character-based splitting. The main configuration uses a chunk size of 300 characters with 50 characters of overlap. This allows the system to retrieve focused sections of a document while retaining some surrounding context.&lt;/p&gt;

&lt;p&gt;The original eight documents were divided into 12 chunks using the main configuration.&lt;/p&gt;

&lt;p&gt;I also experimented with different chunk sizes. Smaller chunks produced more retrieval units, while larger chunks reduced the total number of chunks. This showed how chunk size can affect the balance between focused retrieval and contextual information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semantic Search with Embeddings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After chunking, each piece of text is converted into a numerical representation called an embedding.&lt;/p&gt;

&lt;p&gt;The project uses the all-MiniLM-L6-v2 Sentence Transformer model, which produces 384-dimensional embeddings.&lt;/p&gt;

&lt;p&gt;The main advantage of embeddings is that they allow the system to search based on meaning rather than only exact words.&lt;/p&gt;

&lt;p&gt;For example, a user might ask a question using different wording from the knowledge base, but the system can still identify documents that discuss the same concept.&lt;/p&gt;

&lt;p&gt;FAISS is then used to compare the query embedding with the stored document embeddings and retrieve the most semantically similar chunks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keyword Search with BM25&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Semantic search is useful, but it is not always the best choice.&lt;/p&gt;

&lt;p&gt;Some queries contain exact technical terms, identifiers, names, or specific phrases where keyword matching is important. To handle these cases, the project also uses BM25, a traditional information-retrieval algorithm.&lt;/p&gt;

&lt;p&gt;This gives the system two complementary retrieval methods:&lt;/p&gt;

&lt;p&gt;FAISS focuses on semantic similarity.&lt;br&gt;
BM25 focuses on keyword and term matching.&lt;br&gt;
Hybrid Retrieval&lt;/p&gt;

&lt;p&gt;Instead of relying on only one retrieval method, the project combines both.&lt;/p&gt;

&lt;p&gt;The FAISS and BM25 scores are normalized and then combined using weighted scoring. The default configuration gives more importance to semantic retrieval while still maintaining a contribution from keyword retrieval.&lt;/p&gt;

&lt;p&gt;This approach is useful because semantic search and keyword search have different strengths. Combining them can provide more useful candidates for the final answer.&lt;/p&gt;

&lt;p&gt;The system also experiments with different vector and keyword weights to understand how changing the balance affects retrieval.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retrieval-Augmented Generation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once the most relevant chunks have been identified, they are combined into a context that is provided to the language model.&lt;/p&gt;

&lt;p&gt;The important distinction is that the retriever does not generate the final answer.&lt;/p&gt;

&lt;p&gt;Its job is to find relevant information.&lt;/p&gt;

&lt;p&gt;The LLM then receives:&lt;/p&gt;

&lt;p&gt;User Question + Retrieved Context&lt;/p&gt;

&lt;p&gt;and generates the final natural-language response.&lt;/p&gt;

&lt;p&gt;This helps ground the response in the information available in the knowledge base rather than relying entirely on the model's internal knowledge.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Agentic Layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The original implementation also introduces an agentic component using smolagents.&lt;/p&gt;

&lt;p&gt;The retrieval system is exposed as a tool that the agent can use. In this architecture, the roles are separated:&lt;/p&gt;

&lt;p&gt;The agent acts as the decision-making layer.&lt;br&gt;
The retriever finds relevant information.&lt;br&gt;
The LLM generates the final response.&lt;/p&gt;

&lt;p&gt;This makes the system more flexible than a simple question-and-answer pipeline because the agent can interact with available tools.&lt;/p&gt;

&lt;p&gt;During testing, the original agent integration encountered a parsing issue with the Qwen model because the generated output did not match the format expected by the CodeAgent. The standalone implementation therefore uses a more explicit retrieval-to-generation flow to make the final user interaction more predictable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Experiments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The project explores several important RAG parameters.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Chunk Size - Different chunk sizes were tested, including 100, 200, 300, 500, and 800 characters. The experiment demonstrated that increasing chunk size reduces the number of chunks, while smaller chunks create more granular retrieval units.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Top-K Retrieval - Different values of top-k were tested to determine how many relevant chunks should be retrieved for a query. A smaller value provides less context, while a larger value provides more information but may also introduce irrelevant content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hybrid Weights - Different combinations of semantic and keyword weights were also tested, ranging from vector-only retrieval to keyword-only retrieval.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These experiments demonstrate that retrieval performance depends heavily on how the retrieval system is configured.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Evaluation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The project also considers several dimensions for evaluating a RAG system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Retrieval relevance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Answer correctness&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Groundedness&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Completeness&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Overall response quality&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other useful metrics for a larger implementation would include retrieval accuracy, Recall@K, MRR, latency, and cost.&lt;/p&gt;

&lt;p&gt;The current project establishes the evaluation framework but does not provide enough completed benchmark results to make a formal performance claim.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Potential Applications&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This architecture can be adapted to several real-world applications.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Company Knowledge Assistant - Internal company policies, HR documents, onboarding material, and technical documentation could be indexed so employees can ask questions about company procedures.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Technical Documentation Assistant - Software documentation, API references, and troubleshooting guides could be used to create an assistant capable of answering developer questions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Educational Assistant - Lecture notes, textbooks, and course materials could be used as the knowledge base for a study assistant.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Customer Support - Product manuals, FAQs, and troubleshooting documents could be indexed to provide support responses based on official product information.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What I Learned&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the main lessons from this project is that building a RAG system is much more than simply connecting a document to an LLM.&lt;/p&gt;

&lt;p&gt;The quality of the final answer depends on several stages:&lt;/p&gt;

&lt;p&gt;Document Quality → Chunking → Embeddings → Retrieval → Context → LLM Generation&lt;/p&gt;

&lt;p&gt;If the retrieval system provides poor or irrelevant information, even a capable language model may produce a poor answer.&lt;/p&gt;

&lt;p&gt;This project helped me understand how different retrieval techniques complement each other and how the retrieval layer plays an important role in building reliable AI applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This project demonstrates a complete hybrid Agentic RAG pipeline combining semantic embeddings, FAISS vector search, BM25 keyword retrieval, hybrid ranking, and Qwen-based language generation.&lt;/p&gt;

&lt;p&gt;The key idea is simple:&lt;/p&gt;

&lt;p&gt;Retrieval finds the relevant information, while the LLM uses that information to generate the answer.&lt;/p&gt;

&lt;p&gt;By combining semantic and keyword search, the system can handle both questions based on meaning and queries where exact terminology matters.&lt;/p&gt;

&lt;p&gt;The next step would be to replace the small manually created knowledge base with real-world documents such as PDFs or company documentation, followed by more systematic evaluation using larger datasets and quantitative retrieval and generation metrics.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>machinelearning</category>
      <category>rag</category>
    </item>
  </channel>
</rss>
