-
Notifications
You must be signed in to change notification settings - Fork 89
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
added boilerplate for user management #123
Changes from 6 commits
866270b
77e8c1b
e785b30
14e1eba
36e156e
3f5e665
e7b0e71
a93bd53
9616545
218d322
b4d1a73
7930586
797e7ba
66749fa
2717272
401167b
07e2bc1
085ca5e
7d3e124
b5a3b69
9a2cde9
6035010
709a10c
73dd6c2
0519986
cb9bfa2
4749995
7846291
acf41ff
3f35a45
3e3134b
0dad12c
a590c6a
896a2ce
e492a18
ed6e8eb
a34acbc
1c89119
8a63aa3
4f72eb5
6d38bcd
887d4e1
fd20fac
8fa572a
9927855
841e7b5
f808cf1
e3c3f35
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 |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
from fastapi.middleware.cors import CORSMiddleware | ||
from pydantic import BaseModel | ||
|
||
from cognee.infrastructure.databases.relational.user_authentication.routers import permission_router | ||
|
||
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. Reorganize import statements to the top of the file. Imports should be at the top of the file to comply with PEP 8 standards. - from contextlib import asynccontextmanager
- from fastapi import Depends, FastAPI
- from cognee.infrastructure.databases.relational.user_authentication.authentication_db import User, create_db_and_tables
- from cognee.infrastructure.databases.relational.user_authentication.schemas import UserCreate, UserRead, UserUpdate
- from cognee.infrastructure.databases.relational.user_authentication.users import auth_backend, current_active_user, fastapi_users
+ from contextlib import asynccontextmanager
+ from fastapi import Depends, FastAPI
+ from cognee.infrastructure.databases.relational.user_authentication.routers import permission_router
+ from cognee.infrastructure.databases.relational.user_authentication.authentication_db import User, create_db_and_tables
+ from cognee.infrastructure.databases.relational.user_authentication.schemas import UserCreate, UserRead, UserUpdate
+ from cognee.infrastructure.databases.relational.user_authentication.users import auth_backend, current_active_user, fastapi_users Also applies to: 31-32, 53-60 |
||
# Set up logging | ||
logging.basicConfig( | ||
level=logging.INFO, # Set the logging level (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL) | ||
|
@@ -26,8 +28,13 @@ | |
traces_sample_rate = 1.0, | ||
profiles_sample_rate = 1.0, | ||
) | ||
|
||
app = FastAPI(debug = os.getenv("ENV") != "prod") | ||
from contextlib import asynccontextmanager | ||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
# Not needed if you setup a migration system like Alembic | ||
await create_db_and_tables() | ||
yield | ||
app = FastAPI(debug = os.getenv("ENV") != "prod", lifespan=lifespan) | ||
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. Remove duplicate The - @asynccontextmanager
- async def lifespan(app: FastAPI):
- # Not needed if you setup a migration system like Alembic
- await create_db_and_tables()
- yield Also applies to: 90-95 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. @Vasilije1990 This one 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.
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. Remove duplicate The - @asynccontextmanager
- async def lifespan(app: FastAPI):
- # Not needed if you setup a migration system like Alembic
- await create_db_and_tables()
- yield Also applies to: 90-95 |
||
|
||
origins = [ | ||
"http://frontend:3000", | ||
|
@@ -43,6 +50,74 @@ | |
allow_headers=["*"], | ||
) | ||
|
||
from contextlib import asynccontextmanager | ||
|
||
from fastapi import Depends, FastAPI | ||
|
||
from cognee.infrastructure.databases.relational.user_authentication.authentication_db import User, create_db_and_tables | ||
from cognee.infrastructure.databases.relational.user_authentication.schemas import UserCreate, UserRead, UserUpdate | ||
from cognee.infrastructure.databases.relational.user_authentication.users import auth_backend, current_active_user, fastapi_users | ||
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. Reorganize import statements to the top of the file. Imports should be at the top of the file to comply with PEP 8 standards. - from contextlib import asynccontextmanager
- from fastapi import Depends, FastAPI
- from cognee.infrastructure.databases.relational.user_authentication.authentication_db import User, create_db_and_tables
- from cognee.infrastructure.databases.relational.user_authentication.schemas import UserCreate, UserRead, UserUpdate
- from cognee.infrastructure.databases.relational.user_authentication.users import auth_backend, current_active_user, fastapi_users
+ from contextlib import asynccontextmanager
+ from fastapi import Depends, FastAPI
+ from cognee.infrastructure.databases.relational.user_authentication.authentication_db import User, create_db_and_tables
+ from cognee.infrastructure.databases.relational.user_authentication.schemas import UserCreate, UserRead, UserUpdate
+ from cognee.infrastructure.databases.relational.user_authentication.users import auth_backend, current_active_user, fastapi_users
ToolsRuff
|
||
|
||
app.include_router( | ||
fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"] | ||
) | ||
app.include_router( | ||
fastapi_users.get_register_router(UserRead, UserCreate), | ||
prefix="/auth", | ||
tags=["auth"], | ||
) | ||
app.include_router( | ||
fastapi_users.get_reset_password_router(), | ||
prefix="/auth", | ||
tags=["auth"], | ||
) | ||
app.include_router( | ||
fastapi_users.get_verify_router(UserRead), | ||
prefix="/auth", | ||
tags=["auth"], | ||
) | ||
app.include_router( | ||
fastapi_users.get_users_router(UserRead, UserUpdate), | ||
prefix="/users", | ||
tags=["users"], | ||
) | ||
|
||
app.include_router(permission_router, prefix="/manage", tags=["management"]) | ||
@app.get("/authenticated-route") | ||
async def authenticated_route(user: User = Depends(current_active_user)): | ||
return {"message": f"Hello {user.email}!"} | ||
|
||
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. Remove duplicate function The function - @app.get("/authenticated-route")
- async def authenticated_route(user: User = Depends(current_active_user)):
- return {"message": f"Hello {user.email}!"} Also applies to: 120-123 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. @Vasilije1990 This one 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.
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. Remove duplicate function The function - @app.get("/authenticated-route")
- async def authenticated_route(user: User = Depends(current_active_user)):
- return {"message": f"Hello {user.email}!"} Also applies to: 120-123 |
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
# Not needed if you setup a migration system like Alembic | ||
await create_db_and_tables() | ||
yield | ||
app.include_router( | ||
fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"] | ||
) | ||
app.include_router( | ||
fastapi_users.get_register_router(UserRead, UserCreate), | ||
prefix="/auth", | ||
tags=["auth"], | ||
) | ||
app.include_router( | ||
fastapi_users.get_reset_password_router(), | ||
prefix="/auth", | ||
tags=["auth"], | ||
) | ||
app.include_router( | ||
fastapi_users.get_verify_router(UserRead), | ||
prefix="/auth", | ||
tags=["auth"], | ||
) | ||
app.include_router( | ||
fastapi_users.get_users_router(UserRead, UserUpdate), | ||
prefix="/users", | ||
tags=["users"], | ||
) | ||
|
||
|
||
|
||
@app.get("/") | ||
async def root(): | ||
""" | ||
|
@@ -267,8 +342,8 @@ def start_api_server(host: str = "0.0.0.0", port: int = 8000): | |
relational_config.create_engine() | ||
|
||
from cognee.modules.data.deletion import prune_system, prune_data | ||
asyncio.run(prune_data()) | ||
asyncio.run(prune_system(metadata = True)) | ||
# asyncio.run(prune_data()) | ||
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 uncomment this after you are done. |
||
# asyncio.run(prune_system(metadata = True)) | ||
|
||
uvicorn.run(app, host = host, port = port) | ||
except Exception as e: | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,14 @@ | ||||||
import asyncio | ||||||
import hashlib | ||||||
import logging | ||||||
import uuid | ||||||
from typing import Union | ||||||
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession | ||||||
|
||||||
from cognee.infrastructure.databases.graph import get_graph_config | ||||||
from cognee.infrastructure.databases.relational.user_authentication.authentication_db import async_session_maker | ||||||
from cognee.infrastructure.databases.relational.user_authentication.users import get_user_permissions, fastapi_users | ||||||
from cognee.modules.cognify.config import get_cognify_config | ||||||
from cognee.infrastructure.databases.relational.config import get_relationaldb_config | ||||||
from cognee.modules.data.processing.document_types.AudioDocument import AudioDocument | ||||||
|
@@ -25,7 +31,21 @@ | |||||
|
||||||
update_status_lock = asyncio.Lock() | ||||||
|
||||||
async def cognify(datasets: Union[str, list[str]] = None, root_node_id: str = None): | ||||||
class PermissionDeniedException(Exception): | ||||||
def __init__(self, message: str): | ||||||
self.message = message | ||||||
super().__init__(self.message) | ||||||
|
||||||
async def cognify(datasets: Union[str, list[str]] = None, root_node_id: str = None, user_id:str="default_user"): | ||||||
session: AsyncSession = async_session_maker() | ||||||
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. Let's put this login in a function in some module and just use it here. 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. Let's discuss this live |
||||||
user = await fastapi_users.get_user_manager.get(user_id) | ||||||
user_permissions = await get_user_permissions(user, session) | ||||||
hash_object = hashlib.sha256(user.encode()) | ||||||
hashed_user_id = hash_object.hexdigest() | ||||||
required_permission = "write" | ||||||
if required_permission not in user_permissions: | ||||||
raise PermissionDeniedException("Not enough permissions") | ||||||
|
||||||
relational_config = get_relationaldb_config() | ||||||
db_engine = relational_config.database_engine | ||||||
create_task_status_table() | ||||||
|
@@ -60,7 +80,7 @@ async def run_cognify_pipeline(dataset_name: str, files: list[dict]): | |||||
root_node_id = "ROOT" | ||||||
|
||||||
tasks = [ | ||||||
Task(process_documents, parent_node_id = root_node_id, task_config = { "batch_size": 10 }), # Classify documents and save them as a nodes in graph db, extract text chunks based on the document type | ||||||
Task(process_documents, parent_node_id = root_node_id, task_config = { "batch_size": 10 }, user_id = hashed_user_id, user_permissions=user_permissions), # Classify documents and save them as a nodes in graph db, extract text chunks based on the document type | ||||||
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. Undefined variables The variables - Task(process_documents, parent_node_id = root_node_id, task_config = { "batch_size": 10 }, user_id = hashed_user_id, user_permissions=user_permissions), # Classify documents and save them as a nodes in graph db, extract text chunks based on the document type
+ Task(process_documents, parent_node_id = root_node_id, task_config = { "batch_size": 10 }), # Classify documents and save them as a nodes in graph db, extract text chunks based on the document type Committable suggestion
Suggested change
ToolsRuff
|
||||||
Task(establish_graph_topology, topology_model = KnowledgeGraph), # Set the graph topology for the document chunk data | ||||||
Task(expand_knowledge_graph, graph_model = KnowledgeGraph), # Generate knowledge graphs from the document chunks and attach it to chunk nodes | ||||||
Task(filter_affected_chunks, collection_name = "chunks"), # Find all affected chunks, so we don't process unchanged chunks | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
from sqlalchemy.orm import declarative_base | ||
|
||
ModelBase = declarative_base() | ||
from sqlalchemy.orm import DeclarativeBase | ||
|
||
|
||
class Base(DeclarativeBase): | ||
pass | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,40 @@ | ||
from enum import Enum | ||
|
||
from cognee.infrastructure.databases.relational.sqlalchemy.SqlAlchemyAdapter import SQLAlchemyAdapter | ||
from cognee.infrastructure.files.storage import LocalStorage | ||
from cognee.infrastructure.databases.relational import DuckDBAdapter | ||
|
||
def create_relational_engine(db_path: str, db_name: str): | ||
|
||
class DBProvider(Enum): | ||
DUCKDB = "duckdb" | ||
POSTGRES = "postgresql+asyncpg" | ||
|
||
def create_relational_engine(db_path: str, db_name: str, db_provider:str, db_host:str, db_port:str, db_user:str, db_password:str): | ||
LocalStorage.ensure_directory_exists(db_path) | ||
|
||
return DuckDBAdapter( | ||
db_name = db_name, | ||
db_path = db_path, | ||
) | ||
provider = DBProvider(db_provider) | ||
|
||
if provider == DBProvider.DUCKDB: | ||
# return DuckDBAdapter( | ||
# db_name = db_name, | ||
# db_path = db_path, | ||
# ) | ||
return SQLAlchemyAdapter( | ||
db_name = db_name, | ||
db_path = db_path, | ||
db_type = db_provider, | ||
db_host=db_host, | ||
db_port=db_port, | ||
db_user=db_user, | ||
db_password=db_password | ||
) | ||
elif provider == DBProvider.POSTGRES: | ||
return SQLAlchemyAdapter( | ||
db_name = db_name, | ||
db_path = db_path, | ||
db_type = db_provider, | ||
db_host= db_host, | ||
db_port= db_port, | ||
db_user= db_user, | ||
db_password= db_password | ||
) | ||
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. Remove commented-out code and add error handling for invalid The function correctly selects the database adapter based on the - # return DuckDBAdapter(
- # db_name = db_name,
- # db_path = db_path,
- # )
+ try:
+ provider = DBProvider(db_provider)
+ except ValueError:
+ raise ValueError(f"Invalid database provider: {db_provider}")
|
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.
Reorganize import statements to the top of the file.
Imports should be at the top of the file to comply with PEP 8 standards.
Also applies to: 31-31, 53-59