LLMs have enourmous reasoning capabilities at the cost that the original information, even though contained within the model, is embedded in the deep abstract layers. Retrieval-Augmented Generation (RAG) aims to overcome this shortage by providing context to the LLM in the form of a prompt:
prompt=f"""
Answer using this context:
{context}
Question:
{Original user question}
"""
Finding the correct context / code snippet FlatIndex or InverseFileIndex to find the best top_k matching snippets:
Apart from the Indexing method, we can use techniques like Multi-Query searching, where we find, using an LLM, four equivalent but differently worded queries
We use the labeled dataset CodeSearchNet, which contains code functions and their annotation which is the docstring.
Example function:
def is_palindrome(s):
"""
Checks whether a string is a palindrome.
Args:
s: The string to check.
Returns:
True if the string reads the same backwards, otherwise False.
"""
return s == s[::-1]
And its docstring:
Checks whether a string is a palindrome.
First, we search the snippets using the docstring directly (this is cheating, but a reference/benchmark): identical_query.
Then, we search using an equivalent but differently worded query (real scenario simulation): single_query.
We create four similar queries using Llama and Qwen.
Lastly, we implement the multi-query, simulating how we will combine different queries: multi_query.
Knowing the Ground-truth index of the snippets and their docstrings, we can compute the Recall for each of the six retrieving methods. We use 540 augmented samples. IVF is meant to speed up the search of large databases while compromising accuracy/recall. We obtain:
Identical Query Single-query Multi-query
Flat Recall@10 = 0.961 0.943 0.972
IVF Recall@10 = 0.950 0.930 0.967
We expected identical query to give the best results. However, some functions may be badly annotated, reducing recall. Single-query shows the lowest recall / capacity of finding the correct code. Multi-query combines different querys of different wording, overcoming the badly phrased docstrings and finding the relevant context code snippets with highest recall.
Lastly, we see that the IVF method indeed compromises recall at the advantage of performing the fastest search.
This benchmarking method will allow us to compare future Retrievers and techniques to maximize context quality, for example Hybrid retrieval.
Next necessary step in this project will be to chunk the downloaded Github repo (which we want to build a RAG for) respecting functions, classes, etc. rather than blocks of 100 lines.
Lastly, we wonder if the context needs multiple code snippets, how we could benchmark this scenario, since CodeSearchNet only contains single functions that dont necessarily work together.