> For the complete documentation index, see [llms.txt](https://docs.activeloop.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.activeloop.ai/user-guide/filtering-query.md).

# Filtering Query

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

### Simple Query&#x20;

Querying the data works as follows

```python
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.

```python
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
```

### Getting answer from Hegel

Now, we can filter files based on metadata, before running the query.&#x20;

```python
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())
```

### Getting answer from other authors

```python
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())
```

You can learn more about on various ways you can filter the data via [Query Syntax](/user-guide/filtering-query/query-syntax.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.activeloop.ai/user-guide/filtering-query.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
