Notebooks
M
Milvus
Generating Milvus Query Filter Expressions

Generating Milvus Query Filter Expressions

image-searchvector-databasesemantic-searchtutorialsmilvusembeddingsunstructured-dataquestion-answeringLLMmilvus-bootcampdeep-learningimage-recognitionimage-classificationaudio-searchPythonquickstartragNLP

Generating Milvus Query Filter Expressions with Large Language Models

In this tutorial, we will demonstrate how to use Large Language Models (LLMs) to automatically generate Milvus filter expressions from natural language queries. This approach makes vector database querying more accessible by allowing users to express complex filtering conditions in plain English, which are then converted to proper Milvus syntax.

Milvus supports sophisticated filtering capabilities including:

  • Basic Operators: Comparison operators like ==, !=, >, <, >=, <=
  • Boolean Operators: Logical operators like and, or, not for complex conditions
  • String Operations: Pattern matching with like and other string functions
  • Array Operations: Working with array fields using array_contains, array_length, etc.
  • JSON Operations: Querying JSON fields with specialized operators

By integrating LLMs with Milvus documentation, we can create an intelligent system that understands natural language queries and generates syntactically correct filter expressions. This tutorial will walk through the process of setting up this system, highlighting its effectiveness in various filtering scenarios.

Dependencies and Environment

[ ]

Set up environment variables

Configure your OpenAI API credentials to enable embedding generation and LLM-based filter expression creation. Replace 'your_openai_api_key' with your actual OpenAI API key.

[ ]

Create a Sample Collection

Now let's create a sample Milvus collection with user data. This collection will contain both scalar fields (for filtering) and vector embeddings (for semantic search). We'll use OpenAI's text embedding model to generate vector representations of user information.

[ ]

Print 3 sample data

The code above creates a Milvus collection with the following structure:

  • pk: Primary key field (VARCHAR)
  • name: User name (VARCHAR)
  • age: User age (INT64)
  • city: User city (VARCHAR)
  • hobby: User hobby (VARCHAR)
  • embedding: Vector embedding (FLOAT_VECTOR, 1536 dimensions)

We have inserted 11 sample users with their personal information and generate embeddings for semantic search capabilities. Each user's information is converted into a descriptive text that captures their name, location, age, and interests before being embedded. Let's verify that our collection was created successfully and contains the expected data by querying a few sample records.

[ ]

Collecting Milvus Filter Expression Documentation

To help the large language model better understand Milvus's filter expression syntax, we need to provide it with relevant official documentation. We'll use the docling library to scrape several key pages from the official Milvus website.

These pages contain detailed information about:

  • Boolean operators: and, or, not for complex logical conditions
  • Basic operators: Comparison operators like ==, !=, >, <, >=, <=
  • Filtering templates: Advanced filtering patterns and syntax
  • String matching: Pattern matching with like and other string operations

This documentation will serve as the knowledge base for our LLM to generate accurate filter expressions.

[ ]

The documentation scraping provides comprehensive coverage of Milvus filter syntax. This knowledge base will enable our LLM to understand the nuances of filter expression construction, including proper operator usage, field referencing, and complex condition combinations.

LLM-Powered Filter Generation

Now that we have the documentation context, let's set up the LLM system to generate filter expressions. We'll create a structured prompt that combines the scraped documentation with user queries to produce syntactically correct Milvus filter expressions.

Our filter generation system uses a carefully crafted prompt that:

  1. Provides context: Includes the complete Milvus documentation as reference material
  2. Sets constraints: Ensures the LLM only uses documented syntax and features
  3. Enforces accuracy: Requires syntactically correct expressions
  4. Maintains focus: Returns only the filter expression without explanations

Let's test this with a natural language query and see how well the LLM performs.

[ ]

The LLM successfully generated a filter expression that combines multiple conditions:

  • Age comparison using >
  • Multiple city matching using in operator
  • Proper field referencing and syntax

This demonstrates the power of providing comprehensive documentation context to guide LLM filter generation.

Test the Generated Filter

Now let's test our generated filter expression by using it in an actual Milvus search operation. We'll combine semantic search with precise filtering to find users that match both the query intent and the specific criteria.

[ ]

Results Analysis

The search results demonstrate successful integration of LLM-generated filters with Milvus vector search. The filter correctly identified users who:

  • Are older than 30 years
  • Live in London, Tokyo, or Toronto
  • Match the semantic context of the query

This approach combines the precision of structured filtering with the flexibility of natural language input, making vector databases more accessible to users who may not be familiar with specific query syntax.