Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add support for metadata filtering on search API #1245

Merged
merged 3 commits into from
Feb 6, 2024

Conversation

deshraj
Copy link
Collaborator

@deshraj deshraj commented Feb 6, 2024

Description

Requested by one of our users on the slack group.

This PR adds support for following:

  • Metadata filtering on the search API
  • Add support for two ways to do metadata filtering: 1) Passing simple key value filters, 2) Passing raw query for the vector database
  • Update docs for the same
  • Refactor code

Type of change

  • New feature (non-breaking change which adds functionality)
  • Documentation update

How Has This Been Tested?

  • Test Script (please provide)
import os

from embedchain import App

os.environ["PINECONE_API_KEY"] = "xxx"

config = {
    "vectordb": {
        "provider": "pinecone",
        "config": {
            "metric": "dotproduct",
            "vector_dimension": 1536,
            "index_name": "ec-test",
            "serverless_config": {"cloud": "aws", "region": "us-west-2"},
        },
    }
}

app = App.from_config(config=config)

app.add("https://www.forbes.com/profile/bill-gates", metadata={"type": "forbes", "person": "gates", "year": 2022})
app.add("https://en.wikipedia.org/wiki/Bill_Gates", metadata={"type": "wiki", "person": "gates", "year": 2024})
app.add("https://www.forbes.com/profile/elon-musk", metadata={"type": "forbes", "person": "elon", "year": 2024})
app.add("https://en.wikipedia.org/wiki/Elon_Musk", metadata={"type": "wiki", "person": "elon", "year": 2022})

print("Filter with person: gates")
results = app.search("What is the net worth of Bill Gates?", where={"person": "gates"})
print("Num of search results: ", len(results))


print("Filter with person: elon and type: forbes")
results = app.search("What is the net worth of Bill Gates?", where={"person": "elon", "type": "forbes"})
print("Num of search results: ", len(results))


print("Filter with person: elon and year > 2023")
results = app.search(
    "What is the net worth of Elon Musk?", raw_filter={"$and": [{"person": "elon"}, {"year": {"$gt": 2023}}]}
)
print("Num of search results: ", len(results))

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Feb 6, 2024
@deshraj deshraj merged commit 4afef04 into main Feb 6, 2024
3 checks passed
@deshraj deshraj deleted the user/dyadav/improve-search branch February 7, 2024 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant