-
Notifications
You must be signed in to change notification settings - Fork 99
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
feat: Add graph_model parameter to cognify api call #377
base: dev
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from fastapi import APIRouter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from typing import List, Optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from pydantic import BaseModel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from typing import List, Optional, Any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from pydantic import BaseModel, create_model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from cognee.modules.users.models import User | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from fastapi.responses import JSONResponse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from cognee.modules.users.methods import get_authenticated_user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -9,7 +9,14 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class CognifyPayloadDTO(BaseModel): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
datasets: List[str] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
graph_model: Optional[BaseModel] = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
graph_model: Optional[Any] = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why changing to |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def json_to_pydantic_model(name: str, json_schema: dict) -> BaseModel: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Create a Pydantic model on the fly from JSON. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return create_model(name, **{k: (type(v), ...) for k, v in json_schema.items()}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+17
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add schema validation and improve type mapping The current implementation has several potential issues:
Consider this improved implementation: def json_to_pydantic_model(name: str, json_schema: dict) -> BaseModel:
"""
Create a Pydantic model on the fly from JSON.
"""
- return create_model(name, **{k: (type(v), ...) for k, v in json_schema.items()})
+ def get_field_type(value: Any) -> tuple:
+ if isinstance(value, dict):
+ return (dict, ...)
+ if isinstance(value, list):
+ return (List[Any], ...)
+ return (type(value), None) # Make fields optional by default
+
+ if not isinstance(json_schema, dict):
+ raise ValueError("JSON schema must be a dictionary")
+
+ return create_model(
+ name,
+ **{k: get_field_type(v) for k, v in json_schema.items()}
+ ) 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth merging? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @borisarzentar, yes, the suggested improvements provide additional schema validation and handle nested data structures more robustly. By making fields optional by default, they also reduce the chances of unexpected errors. Unless you foresee any compatibility issues, it seems worth merging. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def get_cognify_router() -> APIRouter: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
router = APIRouter() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -18,6 +25,15 @@ def get_cognify_router() -> APIRouter: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
async def cognify(payload: CognifyPayloadDTO, user: User = Depends(get_authenticated_user)): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" This endpoint is responsible for the cognitive processing of the content.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from cognee.api.v1.cognify.cognify_v2 import cognify as cognee_cognify | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Dynamic conversion of `graph_model` to Pydantic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if payload.graph_model: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
graph_model_schema = payload.graph_model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GraphModelDynamic = json_to_pydantic_model("GraphModelDynamic", graph_model_schema) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
graph_model_instance = GraphModelDynamic(**graph_model_schema) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print(graph_model_instance) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the print statement |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
graph_model_instance = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix control flow and remove debug code Several issues need attention:
Apply this fix: try:
# Dynamic conversion of `graph_model` to Pydantic
if payload.graph_model:
graph_model_schema = payload.graph_model
GraphModelDynamic = json_to_pydantic_model("GraphModelDynamic", graph_model_schema)
graph_model_instance = GraphModelDynamic(**graph_model_schema)
- print(graph_model_instance)
- else:
- graph_model_instance = None
- try:
- await cognee_cognify(payload.datasets, user, payload.graph_model)
- except Exception as error:
+ else:
+ graph_model_instance = None
+
+ await cognee_cognify(payload.datasets, user, graph_model_instance)
+ except ValueError as error:
+ return JSONResponse(
+ status_code=400,
+ content={"error": f"Invalid graph model schema: {str(error)}"}
+ )
+ except Exception as error:
return JSONResponse(
status_code=409,
content={"error": str(error)}
) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await cognee_cognify(payload.datasets, user, payload.graph_model) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as error: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using a more specific type than Any
Using
Optional[Any]
reduces type safety and could lead to runtime errors. Consider using a Union type with specific expected types or creating a base interface that all graph models must implement.📝 Committable suggestion