You can use the structured_predict method to define an object, and use that to fill out the filters.
This example assumes just exact match
from llama_index.core.vector_stores import MetadataFilters, MetadataFilter
from pydantic import BaseModel, Field
class Filter(BaseModel):
"""A filter on metadata."""
key: str = Field(description="The key name to filter on")
value: str = Field(description="The value to match on.")
class Filters(BaseModel):
"""A list of metadata filters for a query."""
filters: list[Filter]
sllm = llm.as_structured_llm(Filters)
response = sllm.complete(f"I have an index with metadata like <some examples>. Given a user query, generate some filters (if any) that can be used to help narrow down the search.\n\n{user_query}")
filters = Filters.model_validate_json(str(response))
metadata_filters = []
for filter in filters:
metadata_filters.append(MetadataFilter(key=filter.key, value=filter.value))
nodes = vector_store.get_nodes(filters=MetadataFilters(filters=filters))