Augment Search with AI for Smarter Answers
Augment any search API with GPT query generation and HyDE-style embedding re-ranking for accurate Q&A.
Maintainer of this project? Claim this page to edit the listing.
1.0.0Add to Favorites
Why it matters
Enhance existing search systems by integrating AI to improve the relevance and quality of search results, leading to more accurate and context-aware answers.
Outcomes
What it gets done
Generate diverse search queries from user questions.
Execute searches in parallel across multiple sources.
Re-rank search results using semantic similarity to a hypothetical ideal answer.
Synthesize top results into a concise, referenced answer.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/oai-questionansweringusingasearchapi | bash Overview
Question answering using a search API and re-ranking
An OpenAI Cookbook technique for answering questions via an existing search API, using GPT to generate queries and HyDE-style embedding re-ranking to surface the most relevant results. Use it when you already have a search API or index and want better answer quality without maintaining a separate vector database.
What it does
This notebook demonstrates a hybrid technique for answering questions using an existing search API - here, the News API - without building a vector database. It sits between two common approaches: mimicking human browsing (GPT iteratively triggers and refines searches, which can be slow) and pure embeddings retrieval (fast but requires embedding and maintaining an entire knowledge base in advance). The hybrid approach layers query generation and re-ranking on top of any existing search endpoint, such as a Slack search API or an internal ElasticSearch instance.
When to use - and when NOT to
Use this pattern when you already have a search API or index (public or internal) and want better answers from it without standing up and maintaining a separate vector database. It trades some of the latency benefits of pure embeddings retrieval for the flexibility of working directly on top of whatever search system you already have.
Inputs and outputs
The pipeline runs in three steps. Search: GPT generates a diverse list of candidate search queries from the user's question, and these run in parallel against the search API - which often returns many results that aren't actually relevant. Re-rank: rather than comparing results directly to the question, the system first generates a hypothetical ideal answer (following the HyDE approach) and measures each result's semantic similarity to that hypothetical answer via embeddings, since results resembling a good answer rank better than results merely resembling the question's wording; since OpenAI embeddings are returned normalized, a plain dot product substitutes for a full cosine similarity calculation. Answer: the top-ranked results (the top 5 in the example) are passed to the model to generate a final answer with references and links.
Integrations
Requires OPENAI_API_KEY and, for the News API example specifically, a NEWS_API_KEY. The re-ranking step is the generalizable part - the same query-generation-plus-HyDE-re-ranking pattern can sit on top of any search backend, not just the News API used for this walkthrough.
Who it's for
Developers who already have a search system in place and want to improve its answer quality and relevance using GPT-driven query expansion and embedding-based re-ranking, without committing to a full retrieval-embeddings architecture. This hybrid approach is explicitly positioned as a middle ground: lower latency than the fully iterative human-browsing style of retrieval, and no need to pre-embed or continuously maintain a vector database the way pure embeddings retrieval requires.
Source README
Question answering using a search API and re-ranking
Searching for relevant information can sometimes feel like looking for a needle in a haystack, but don’t despair, GPTs can actually do a lot of this work for us. In this guide we explore a way to augment existing search systems with various AI techniques, helping us sift through the noise.
Two ways of retrieving information for GPT are:
- Mimicking Human Browsing: GPT triggers a search, evaluates the results, and modifies the search query if necessary. It can also follow up on specific search results to form a chain of thought, much like a human user would do.
- Retrieval with Embeddings: Calculate embeddings for your content and a user query, and then retrieve the content most related as measured by cosine similarity. This technique is used heavily by search engines like Google.
These approaches are both promising, but each has their shortcomings: the first one can be slow due to its iterative nature and the second one requires embedding your entire knowledge base in advance, continuously embedding new content and maintaining a vector database.
By combining these approaches, and drawing inspiration from re-ranking methods, we identify an approach that sits in the middle. This approach can be implemented on top of any existing search system, like the Slack search API, or an internal ElasticSearch instance with private data. Here’s how it works:
Step 1: Search
- User asks a question.
- GPT generates a list of potential queries.
- Search queries are executed in parallel.
Step 2: Re-rank
- Embeddings for each result are used to calculate semantic similarity to a generated hypothetical ideal answer to the user question.
- Results are ranked and filtered based on this similarity metric.
Step 3: Answer
- Given the top search results, the model generates an answer to the user’s question, including references and links.
This hybrid approach offers relatively low latency and can be integrated into any existing search endpoint, without requiring the upkeep of a vector database. Let's dive into it! We will use the News API as an example domain to search over.
Setup
In addition to your OPENAI_API_KEY, you'll have to include a NEWS_API_KEY in your environment. You can get an API key here.
1. Search
It all starts with a user question.
Now, in order to be as exhaustive as possible, we use the model to generate a list of diverse queries based on this question.
The queries look good, so let's run the searches.
As we can see, oftentimes, the search queries will return a large number of results, many of which are not relevant to the original question asked by the user. In order to improve the quality of the final answer, we use embeddings to re-rank and filter the results.
2. Re-rank
Drawing inspiration from HyDE (Gao et al.), we first generate a hypothetical ideal answer to rerank our compare our results against. This helps prioritize results that look like good answers, rather than those similar to our question. Here’s the prompt we use to generate our hypothetical answer.
Now, let's generate embeddings for the search results and the hypothetical answer. We then calculate the cosine distance between these embeddings, giving us a semantic similarity metric. Note that we can simply calculate the dot product in lieu of doing a full cosine similarity calculation since the OpenAI embeddings are returned normalized in our API.
Finally, we use these similarity scores to sort and filter the results.
Awesome! These results look a lot more relevant to our original query. Now, let's use the top 5 results to generate a final answer.
3. Answer
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.