Skip to content

Commit

Permalink
Merge pull request #366 from weaviate/autocut
Browse files Browse the repository at this point in the history
Add support for autocut
  • Loading branch information
dirkkul authored Jul 4, 2023
2 parents 31723ee + e46b638 commit a6a0069
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions integration/test_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{"dataType": ["string"], "description": "description", "name": "description"},
{"dataType": ["int"], "description": "size", "name": "size"},
],
"vectorizer": "text2vec-contextionary",
}
]
}
Expand Down Expand Up @@ -226,6 +227,19 @@ def test_hybrid_properties(client, properties: Optional[List[str]], num_results:
assert len(result["data"]["Get"]["Ship"]) == 0


@pytest.mark.parametrize("autocut,num_results", [(1, 1), (2, 6), (-1, len(SHIPS))])
def test_autocut(client, autocut, num_results):
result = (
client.query.get("Ship", ["name"])
.with_hybrid(query="sponges", properties=["name", "description"], alpha=0.5)
.with_autocut(autocut)
.with_limit(len(SHIPS))
.do()
)
assert len(result["data"]["Get"]["Ship"]) == num_results
assert result["data"]["Get"]["Ship"][0]["name"] == "The Crusty Crab"


def test_group_by(client, people_schema):
"""Test hybrid search with alpha=0.5 to have a combination of BM25 and vector search."""
client.schema.delete_all()
Expand Down
12 changes: 12 additions & 0 deletions weaviate/gql/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,18 @@ def __init__(self, class_name: str, properties: Optional[PROPERTIES], connection
self._group_by: Optional[GroupBy] = None
self._alias: Optional[str] = None
self._tenant: Optional[str] = None
self._autocut: Optional[int] = None
self._consistency_level: Optional[ConsistencyLevel] = None

def with_autocut(self, autocut: int):
"""Cuts off irrelevant results based on "jumps" in scores."""
if not isinstance(autocut, int):
raise TypeError("autocut must be of type int")

self._autocut = autocut
self._contains_filter = True
return self

def with_tenant(self, tenant: str):
"""Sets a tenant for the query."""
if not isinstance(tenant, str):
Expand Down Expand Up @@ -1190,6 +1200,8 @@ def build(self, wrap_get: bool = True) -> str:
query += self._consistency_level
if self._tenant is not None:
query += f'tenant: "{self._tenant}"'
if self._autocut is not None:
query += f"autocut: {self._autocut}"

query += ")"

Expand Down

0 comments on commit a6a0069

Please sign in to comment.