-
Notifications
You must be signed in to change notification settings - Fork 101
Add custom exceptions for better error-handling #46
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c03346c
Add custom exceptions
willtai ccb7451
Add exceptions for Weaviate retriever and index methods, added types …
willtai a035b00
Add errors to documentation
willtai 2adf48d
Update exception names
willtai 6503a23
Update documentation
willtai 1335052
Fixed docstrings
willtai a328869
Addressed comments
willtai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ htmlcov/ | |
docs/build/ | ||
.vscode/ | ||
.python-version | ||
.DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright (c) "Neo4j" | ||
# Neo4j Sweden AB [https://neo4j.com] | ||
# # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# # | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# # | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
class Neo4jGenAiError(Exception): | ||
"""Global exception used for the neo4j-genai package.""" | ||
|
||
pass | ||
|
||
|
||
class RetrieverInitializationError(Neo4jGenAiError): | ||
"""Exception raised when initialization of a retriever fails.""" | ||
|
||
def __init__(self, errors: str): | ||
super().__init__(f"Initialization failed: {errors}") | ||
self.errors = errors | ||
|
||
|
||
class SearchValidationError(Neo4jGenAiError): | ||
"""Exception raised for validation errors during search.""" | ||
|
||
def __init__(self, errors): | ||
super().__init__(f"Search validation failed: {errors}") | ||
self.errors = errors | ||
|
||
|
||
class FilterValidationError(Neo4jGenAiError): | ||
"""Exception raised when input validation for metadata filtering fails.""" | ||
|
||
pass | ||
|
||
|
||
class EmbeddingRequiredError(Neo4jGenAiError): | ||
"""Exception raised when an embedding method is required but not provided.""" | ||
|
||
pass | ||
|
||
|
||
class InvalidRetrieverResultError(Neo4jGenAiError): | ||
"""Exception raised when the Retriever fails to return a result.""" | ||
|
||
pass | ||
|
||
|
||
class Neo4jIndexError(Neo4jGenAiError): | ||
"""Exception raised when handling Neo4j index fails.""" | ||
|
||
pass | ||
|
||
|
||
class Neo4jVersionError(Neo4jGenAiError): | ||
"""Exception raised when Neo4j version does not meet minimum requirements.""" | ||
|
||
def __init__(self): | ||
super().__init__("This package only supports Neo4j version 5.18.1 or greater") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright (c) "Neo4j" | ||
# Neo4j Sweden AB [https://neo4j.com] | ||
# # | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# # | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# # | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Optional | ||
|
||
from pydantic import ( | ||
field_validator, | ||
BaseModel, | ||
PositiveInt, | ||
model_validator, | ||
ConfigDict, | ||
) | ||
from weaviate.client import WeaviateClient | ||
from weaviate.collections.classes.filters import _Filters | ||
|
||
from neo4j_genai.retrievers.utils import validate_search_query_input | ||
from neo4j_genai.types import Neo4jDriverModel, EmbedderModel | ||
|
||
|
||
class WeaviateModel(BaseModel): | ||
client: WeaviateClient | ||
model_config = ConfigDict(arbitrary_types_allowed=True) | ||
|
||
@field_validator("client") | ||
def check_client(cls, value): | ||
if not isinstance(value, WeaviateClient): | ||
raise TypeError( | ||
"Provided client needs to be of type weaviate.client.WeaviateClient" | ||
) | ||
return value | ||
|
||
|
||
class WeaviateNeo4jRetrieverModel(BaseModel): | ||
driver_model: Neo4jDriverModel | ||
client_model: WeaviateModel | ||
collection: str | ||
id_property_external: str | ||
id_property_neo4j: str | ||
embedder_model: Optional[EmbedderModel] | ||
return_properties: Optional[list[str]] = None | ||
retrieval_query: Optional[str] = None | ||
|
||
|
||
class WeaviateNeo4jSearchModel(BaseModel): | ||
top_k: PositiveInt = 5 | ||
query_vector: Optional[list[float]] = None | ||
query_text: Optional[str] = None | ||
weaviate_filters: Optional[_Filters] = None | ||
model_config = ConfigDict(arbitrary_types_allowed=True) | ||
|
||
@field_validator("weaviate_filters") | ||
def check_weaviate_filters(cls, value): | ||
if value and not isinstance(value, _Filters): | ||
raise TypeError( | ||
"Provided filters need to be of type weaviate.collections.classes.filters._Filters" | ||
) | ||
return value | ||
|
||
@model_validator(mode="before") | ||
def check_query(cls, values): | ||
stellasia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Validates that one of either query_vector or query_text is provided exclusively. | ||
""" | ||
query_vector, query_text = values.get("query_vector"), values.get("query_text") | ||
validate_search_query_input(query_text, query_vector) | ||
return values |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.