# Leveraging Retrieval-Augmented Generation for Swahili Language Conversation Systems
## 3 Proposed Method
### RAG Mechanism
#### 3.2.3 Retriever and Query Processing  
The retriever component is a critical element of the Retrieval-Augmented Generation (RAG) framework, bridging user queries with contextually relevant responses. Figure 6 illustrates this process, which combines embedding models, vector databases, and retrieval mechanisms to efficiently locate and return the most relevant information. The retrieval process begins when a user submits a query \(Q\). This query is transformed into a dense vector representation \(\mathbf{q}\) using an embedding model, such as Sentence-BERT (SBERT) or multilingual BERT (mBERT). The embedding captures the semantic meaning and contextual nuances of the query, enabling precise comparisons with precomputed vectors stored in the vector database. The query embedding process is expressed mathematically as  
\[\mathbf{q}=f_{\text{embed}}(Q) \tag{2}\]  
where  
* \(\mathbf{q}\) is the dense vector representation of the query. * \(Q\) is the user's query in textual form. * \(f_{\text{embed}}\) is the embedding function from the pre-trained model. The vector database consists of vectors \(\mathbf{c}_{i}\), each representing a chunk of preprocessed text \(T_{i}\). These chunks are created by segmenting larger documents into coherent units and embedding them using the same model. This ensures that both the query and the database entries share a common semantic space. The vector representation for each chunk is  
\[\mathbf{c}_{i}=f_{\text{embed}}(T_{i}),\quad i=1,2,\ldots,n \tag{3}\]  
Figure 6: Retriever and query processing workflow in the RAG framework for Swahili NLP. where  
* \(T_{i}\) is the \(i\)-th chunk of text. * \(\mathbf{c}_{i}\) is the vector representation of the \(i\)-th chunk. * \(n\) is the total number of chunks in the database. The similarity between the query vector \(\mathbf{q}\) and each chunk vector \(\mathbf{c}_{i}\) is calculated using cosine similarity, defined as  
\[\text{Sim}_{\text{cos}}(\mathbf{q},\mathbf{c}_{i})=\frac{\mathbf{q}\cdot \mathbf{c}_{i}}{\|\mathbf{q}\|\|\mathbf{c}_{i}\|} \tag{4}\]  
Here,  
* \(\mathbf{q}\cdot\mathbf{c}_{i}\) is the dot product of the query and chunk vectors. * \(\|\mathbf{q}\|,\|\mathbf{c}_{i}\|\) are the L2 norms (magnitudes) of the vectors. The selection of relevant information is based on the similarity scores calculated between \(\mathbf{q}\) and \(\mathbf{c}_{i}\). Chunks with the highest similarity scores are considered the most contextually relevant. The system retrieves the top-\(k\) chunks, where \(k\) is a parameter defining the number of chunks to use for answer generation. This approach ensures that the retrieved information aligns semantically with the user query while covering diverse contexts for robust responses. To provide additional clarity, Algorithm 1 outlines the detailed steps involved in the retrieval mechanism. This algorithm highlights the key processes, from query embedding to chunk selection, ensuring transparency in how the retriever identifies relevant information. ```
1 Input: Query \(Q\), Vector Database \(\mathcal{C}\), Embedding Function \(f_{\text{embed}}\), Number of Chunks \(k\)
2 Output: Top-\(k\) Relevant Chunks \(\mathcal{C}_{\text{top-k}}\)
3 Generate query embedding: \(\mathbf{q}\gets f_{\text{embed}}(Q)\)
4 foreach\(\mathbf{c}_{i}\in\mathcal{C}\)do
5 Compute similarity score: \(\text{Sim}_{\text{cos}}(\mathbf{q},\mathbf{c}_{i})\)
6 end fore
7 Rank all chunks \(\mathbf{c}_{i}\) by similarity scores in descending order
8 Select top-\(k\) chunks: \(\mathcal{C}_{\text{top-k}}\)
9 return\(\mathcal{C}_{\text{top-k}}\)
```  
**Algorithm 1**Retrieval mechanism for query processing  
The retrieved chunks are passed to a pre-trained large language model (LLM), which synthesizes the information into a coherent response. This ensures that the response is both contextually accurate and comprehensive. To refine retrieval further, a hybrid scoring mechanism combining cosine similarity with BM25, a ranking function for textual relevance, is employed.