All pages
Powered by GitBook
1 of 2

Loading...

Loading...

Filtering Query

This section provides a detailed guide to the filter expression syntax, a lightweight query language for filtering data.

Simple Query

Querying the data works as follows

import os, requests

url = "https://api.activeloop.ai/chat/completions"
token = os.environ["ACTIVELOOP_TOKEN"]
headers = {'Authorization': f'Bearer {token}'}

payload = {
    "model": "activeloop-l0",
    "messages": [
        {
            "role": "user",
            "content": "What is logic?", 
        }   
    ]
}
        
response = requests.post(url, headers=headers, json=payload)

# Print the response
print(response.json())

Retrieving top K elements without Reasoning

When activeloop-l0-retrieval is used there is no reasoning or summary returned. Only top_k results.

Getting answer from Hegel

Now, we can filter files based on metadata, before running the query.

Getting answer from other authors

You can learn more about on various ways you can filter the data via .

Query Syntax

Cheatsheet for filtering the data

Basic Syntax

A filter expression consists of one or more conditions that can be combined using logical operators. Each condition typically follows this pattern:

Supported Operators

payload = {
    "model": "activeloop-l0-retrieval",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "filter",
                    "top_k": 3
                },
                {
                    "type": "text",
                    "text": "What is logic?" # query
                }
            ]
        }   
    ]
}
        
response = requests.post(url, headers=headers, json=payload)

print(response.json()["choices"]) # top k elements
Query Syntax
payload = {
    "model": "activeloop-l0",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "filter",
                    "filter": "metadata.author == 'hegel'"
                }, 
                {   
                    "type": "text",
                    "text": "What is logic?"
                } 
            ]   
        }   
    ],
}       
        
response = requests.post(url, headers=headers, json=payload)
print(response.json())
payload = {
    "model": "activeloop-l0",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "filter",
                    "filter": "metadata.author != 'hegel'"
                },   
                {   
                    "type": "text",
                    "text": "What is logic?"
                }
            ]
        }   
    ],
}       
        
response = requests.post(url, headers=headers, json=payload)
print(response.json())
Operator
Description
Example

=, ==

Equals

name = "John"

!=, <>

Not equals

status != "inactive"

>

Greater than

age > 30

>=

Greater than or equal to

price >= 100

Data Types

The syntax supports the following data types:

  • Strings: Enclosed in double quotes ("value") or single quotes ('value')

  • Numbers: Integers or decimals (42, 3.14)

  • Booleans: true or false

  • Lists: Enclosed in square brackets, with comma-separated values ([1, 2, 3], ["active", "pending"])

Logical Operators

Conditions can be combined using logical operators:

  • AND: condition1 and condition2

  • OR: condition1 or condition2

Parentheses can be used to group conditions and control precedence:

Accessing Nested Properties

For nested data structures, use dot notation to access properties:

Examples

Basic Comparisons

Combining Conditions

Working with Lists

Pattern Matching

Null Checks

Nested Properties

Complex Filters

Reserved Keywords

The following keywords are reserved and should not be used as field names:

  • and

  • or

  • in

  • like

  • is

  • null

  • not

  • true

  • false

Best Practices

  1. Use parentheses to make complex expressions clearer

  2. Be consistent with string quotes (prefer double quotes)

  3. Use whitespace to improve readability

  4. For complex filters, break expressions into multiple lines

column operator value
(age > 30 and status = "active") or role = "admin"
user.profile.name = "John"
metadata.tags[0] = "important"
// Simple equality check
name = "John"

// Numeric comparison
age > 30

// Boolean value
is_active = true
// Using AND
age > 30 and status = "active"

// Using OR
status = "pending" or status = "in_review"

// Combining with parentheses
(age > 30 and status = "active") or role = "admin"
// Check if value is in a list
status in ["active", "pending", "in_review"]

// Empty list
tags in []
// Names that contain "Smith"
name like "%Smith%"

// Email addresses from a specific domain
email like "%@example.com"

// Starts with a specific prefix
product_code like "ABC%"
// Check if a field is null
address is null

// Check if a field is not null
email is not null
// Access nested object properties
user.profile.address = "New York"

// Access array elements
metadata.tags[0] = "important"

// Deep nesting
order.items[0].product.name = "Widget"
// Complex filter combining multiple conditions
(status in ["active", "pending"] and created_date > "2023-01-01") or
(is_priority = true and (assigned_to = "John" or assigned_to is null))

<

Less than

quantity < 5

<=

Less than or equal to

temperature <= 32

in

Value is in a list

status in ["active", "pending"]

like

Pattern matching

name like "%Smith%"

is null

Value is null

address is null

is not null

Value is not null

email is not null