![[interface] image of a laptop with ai legal tech software interface in an office](https://cdn.prod.website-files.com/69f81c02e87a58f0517f69cb/69fad0b19436459e2f26f433_Query%20Expansion%20for%20Retrieving%20Relevant%20Documents%20from%20Vector%20Database%20blogpost.png)
When performing document retrieval, the quality of the results usually depends on the query string. If the query is informative, containing relevant keywords and sufficient context, precision and recall will be high. On the other hand, if the query is ambiguous, contains only one or two keywords, and lacks context, the retrieval results may be poor.
With the help of large language models (LLMs), a short query can be expanded by appending context or even examples that help retrieve more relevant documents. Query expansion can:
2. Approach
With the help of LLMs, we can generate query-relevant context or examples using prompts such as:
“The user has the query: {{query}} and wants to retrieverelevant documents from a vector database. Please generate a short, relevantcontext to augment the query so that more precise documents can be retrieved.”
OR
“Given the query: {{query}}, pleasegenerate a short text that may be found in a document containing the answer to the query.”
3. Remarks
Although query expansion can enrich the query content, it may also over-expand the query and lead to the retrieval of irrelevant documents (i.e., reduced precision).Therefore, the approach should be used carefully, especially when the query already contains sufficient context or information. If too many documents are retrieved, it is better to use reranking models, such as cross-encoders or ColBERT, to reorder the documents so that more relevant ones appear at the top of the results.
Teams like Jardine reduced contract review time by 75% with ACE.
