Skip to main content

Command Palette

Search for a command to run...

Part 3: Searching Your Knowledge – Querying the Vector Database

RAG Tutorial Using Open Source LLMs, Vector Database, and Curl Part 3

Updated
3 min read
D

Developer

Blog Post 3: Searching Your Knowledge - Querying the Vector Database

In the previous post, we successfully converted our data into embeddings and uploaded them to our Qdrant vector database. Now, our knowledge base is primed and ready. The next crucial step is to enable users to search this knowledge by asking questions. This part focuses on taking user input, converting it into an embedding, and using that to query Qdrant for the most relevant information.

The Querying Flow

  1. Receiving User Input: Your application will have a way for users to submit their questions or search terms. This could be a search bar, a chatbot interface, or an API endpoint. Let's say the user asks: "What are the benefits of using a vector database?"

  2. Preprocessing User Query (Optional but Recommended): Similar to data preprocessing, you might want to clean or standardize the user's query. This could involve:

    • Removing leading/trailing whitespace.

    • Potentially, the same "standardization" (grammar, meaning refinement) step discussed for source data could be applied here to improve semantic matching.

  3. Embedding the User Query: To find semantically similar content in your Qdrant collection, the user's query must also be converted into an embedding. Crucially, you must use the exact same embedding model that you used to embed your source data. This ensures that the query vector and the document vectors are in the same "semantic space."

    Using a conceptual curl example (similar to embedding data):

       curl --request POST \
       --url https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/embeddings \
       --header 'Authorization: Bearer {api_token}' \
       --header 'Content-Type: application/json' \
       --data '{ "input": "What are the benefits of using a vector database?", "model": "@cf/baai/bge-base-en-v1.5" }'
    

    Querying Qdrant with the Query Embedding: Now that you have the query embedding, you can use it to search your Qdrant collection. Qdrant will compare this query vector to all the vectors in your collection and return the ones that are "closest" (most similar) based on the distance metric you defined (e.g., Cosine similarity).

    Here's a conceptual curl example for searching (querying) your Qdrant collection:

       curl --request POST \
       --url https://localhost:6333/collections/{collection_name}/points/query \
       --header 'Content-Type: application/json' \
       --header 'api-key: {api_key}' \
       --data '{
         "vector": [
                     -0.032501220703125,
                     -0.025634765625,
                     .....,
                     0.0248565673828125,
                     -0.00542449951171875
                 ],
         "limit": 10,
         "score_threshold": 0.7
       }'
    
  4. Parsing the Response from Qdrant: The response from Qdrant will contain a list of the most similar points (documents/chunks) found. Each result typically includes:

    • The id of the point.

    • A score indicating the similarity (higher is usually better for Cosine similarity).

    • The payload you stored, which should contain the actual text content or an identifier to fetch it.

You'll parse this response to extract the text_content (or other relevant fields) from the payload of the top N results. This retrieved text is the "context" that will be used in the next step.

Outcome: You now have a set of relevant text snippets (context) retrieved directly from your knowledge base, based purely on the semantic meaning of the user's query. This context is the foundation for generating an informed answer. In our final post, we'll pass this context along with the original query to an LLM to synthesize a high-quality answer and explore further enhancements.

RAG Tutorial Using Open Source LLMs, Vector Database, and Curl

Part 3 of 5

This series will guide you through building a RAG system from the ground up, using open-source models. Our goal is to avoid vendor lock-in and reliance on third-party SDKs or wrappers, giving you full control and understanding.

Up next

Part 2: Populating Your Vector Database – Embedding and Uploading Data

RAG Tutorial Using Open Source LLMs, Vector Database, and Curl Part 2