Update get_cognify_router.py
This commit is contained in:
parent
3f72652500
commit
57ae93b3cd
1 changed files with 19 additions and 3 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from typing import List, Optional
|
from typing import List, Optional, Any
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, create_model
|
||||||
from cognee.modules.users.models import User
|
from cognee.modules.users.models import User
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from cognee.modules.users.methods import get_authenticated_user
|
from cognee.modules.users.methods import get_authenticated_user
|
||||||
|
|
@ -9,7 +9,14 @@ from fastapi import Depends
|
||||||
|
|
||||||
class CognifyPayloadDTO(BaseModel):
|
class CognifyPayloadDTO(BaseModel):
|
||||||
datasets: List[str]
|
datasets: List[str]
|
||||||
graph_model: Optional[BaseModel] = None
|
graph_model: Optional[Any] = None
|
||||||
|
|
||||||
|
|
||||||
|
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_cognify_router() -> APIRouter:
|
def get_cognify_router() -> APIRouter:
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
@ -18,6 +25,15 @@ def get_cognify_router() -> APIRouter:
|
||||||
async def cognify(payload: CognifyPayloadDTO, user: User = Depends(get_authenticated_user)):
|
async def cognify(payload: CognifyPayloadDTO, user: User = Depends(get_authenticated_user)):
|
||||||
""" This endpoint is responsible for the cognitive processing of the content."""
|
""" This endpoint is responsible for the cognitive processing of the content."""
|
||||||
from cognee.api.v1.cognify.cognify_v2 import cognify as cognee_cognify
|
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)
|
||||||
|
else:
|
||||||
|
graph_model_instance = None
|
||||||
try:
|
try:
|
||||||
await cognee_cognify(payload.datasets, user, payload.graph_model)
|
await cognee_cognify(payload.datasets, user, payload.graph_model)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue