In this example we’ll index customer support tickets and query them to find the most similar ticket to a new one.

Let’s create our first index where we’ll add imaginary support tickets.

from opperai import Opper
from opperai.types import Document

# If not provided, the client will use the OPPER_API_KEY environment variable.
opper = Opper()
index = opper.indexes.create('support-tickets')

Now let’s add some support tickets to our index.

tickets = [
     {
        "title": "Issue with my account",
        "description": "I cannot log in to my account",
        "status": "open",
        "id": 1,
    },
    {
        "title": "Payment issue",
        "description": "I cannot pay for my subscription",
        "status": "open",
        "id": 2,
    },
    {
        "title": "Feature request",
        "description": "I would like to request a new feature",
        "status": "closed",
        "id": 3,
    },
    {
        "title": "Slow response time",
        "description": "The response time for queries is too slow",
        "status": "open",
        "id": 4,
    },
    {
        "title": "Data sync error",
        "description": "There is an error syncing data across devices",
        "status": "open",
        "id": 5,
    },
    {
        "title": "App crash on launch",
        "description": "The app crashes every time I try to launch it",
        "status": "open",
        "id": 6,
    }, 
]

for ticket in tickets:
    index.add(Document(
        content=ticket["title"] + ' ' + ticket["description"],
        metadata={
            "status": ticket["status"],
            "id": ticket["id"],
        }
    ))

Now, let’s imagine that another customer has an issue with their account. We can query our index to find the most similar support ticket

query = 'Issue with my account'
results = index.query(query, 2)

print(results[0].content)
# 'Issue with my account I cannot log in to my account'
print(results[0].metadata)
# { status: 'open', id: '1' }

You can also filter by metadata, for example, to only retrieve open tickets.

open_results = index.query(query, 1, [{'key': 'status', 'operation': '=', 'value': 'open'}])

print(open_results[0].content)
# 'Issue with my account I cannot log in to my account'

That’s it! Indexes are a powerful tool at the core of many AI applications. When combined with functions, they can give knowledge about your data to LLMs and enable a lot of interesting use cases.