th2rag 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- th2rag/__init__.py +0 -0
- th2rag/auth/__init__.py +0 -0
- th2rag/auth/exceptions.py +2 -0
- th2rag/auth/router.py +43 -0
- th2rag/auth/schemas.py +15 -0
- th2rag/auth/service.py +115 -0
- th2rag/clients/__init__.py +0 -0
- th2rag/clients/storage/config.py +13 -0
- th2rag/clients/storage/local.py +48 -0
- th2rag/clients/storage/s3.py +93 -0
- th2rag/config.py +267 -0
- th2rag/conversations/__init__.py +0 -0
- th2rag/conversations/dependencies.py +30 -0
- th2rag/conversations/exceptions.py +6 -0
- th2rag/conversations/router.py +47 -0
- th2rag/conversations/schemas.py +43 -0
- th2rag/conversations/service.py +51 -0
- th2rag/database.py +72 -0
- th2rag/dataset/__init__.py +0 -0
- th2rag/dataset/dependencies.py +25 -0
- th2rag/dataset/exceptions.py +2 -0
- th2rag/dataset/router.py +53 -0
- th2rag/dataset/schemas.py +23 -0
- th2rag/dataset/service.py +78 -0
- th2rag/dependencies.py +143 -0
- th2rag/knowledge/__init__.py +0 -0
- th2rag/knowledge/dependencies.py +25 -0
- th2rag/knowledge/exceptions.py +2 -0
- th2rag/knowledge/mage_client.py +505 -0
- th2rag/knowledge/router.py +714 -0
- th2rag/knowledge/schemas.py +108 -0
- th2rag/knowledge/service.py +392 -0
- th2rag/logger.py +21 -0
- th2rag/main.py +51 -0
- th2rag/messages/__init__.py +0 -0
- th2rag/messages/exceptions.py +2 -0
- th2rag/messages/router.py +63 -0
- th2rag/messages/schemas.py +68 -0
- th2rag/messages/service.py +210 -0
- th2rag/models.py +215 -0
- th2rag/pagination.py +68 -0
- th2rag/rag/__init__.py +0 -0
- th2rag/rag/chunkers/__init__.py +0 -0
- th2rag/rag/chunkers/hybrid_chunker.py +23 -0
- th2rag/rag/constants.py +15 -0
- th2rag/rag/converters/__init__.py +0 -0
- th2rag/rag/converters/documents_converter.py +123 -0
- th2rag/rag/converters/json_converter.py +521 -0
- th2rag/rag/converters/legacy_converter.py +124 -0
- th2rag/rag/converters/pdf_converter.py +384 -0
- th2rag/rag/converters/spreadsheet_converter.py +159 -0
- th2rag/rag/converters/txt_converter.py +98 -0
- th2rag/rag/embeddings/__init__.py +0 -0
- th2rag/rag/embeddings/base.py +21 -0
- th2rag/rag/embeddings/exceptions.py +2 -0
- th2rag/rag/embeddings/sentence_transformer.py +97 -0
- th2rag/rag/generation/base.py +22 -0
- th2rag/rag/generation/exceptions.py +2 -0
- th2rag/rag/generation/litellm_generator.py +143 -0
- th2rag/rag/generation/mistral_generator.py +269 -0
- th2rag/rag/generation/test_litellm.py +33 -0
- th2rag/rag/retrieval/__init__.py +0 -0
- th2rag/rag/retrieval/base.py +11 -0
- th2rag/rag/retrieval/exceptions.py +10 -0
- th2rag/rag/retrieval/lancedb/lancedb_retriever.py +25 -0
- th2rag/rag/retrieval/lancedb/lancedb_storage.py +49 -0
- th2rag/rag/retrieval/lancedb/schemas.py +19 -0
- th2rag/rag/services/__init__.py +0 -0
- th2rag/rag/services/rag_service.py +99 -0
- th2rag/rag/services/rag_service_builder.py +42 -0
- th2rag/rag/tasks/process_pdf.py +586 -0
- th2rag/users/dependencies.py +44 -0
- th2rag/users/exceptions.py +6 -0
- th2rag/users/router.py +92 -0
- th2rag/users/schemas.py +61 -0
- th2rag/users/service.py +140 -0
- th2rag/utils/__init__.py +0 -0
- th2rag/utils/helpers.py +40 -0
- th2rag/utils/logger.py +15 -0
- th2rag/utils/security.py +76 -0
- th2rag/utils/welcome_router.py +98 -0
- th2rag/webhooks/__init__.py +0 -0
- th2rag/webhooks/sender.py +126 -0
- th2rag-0.0.2.dist-info/METADATA +1002 -0
- th2rag-0.0.2.dist-info/RECORD +87 -0
- th2rag-0.0.2.dist-info/WHEEL +4 -0
- th2rag-0.0.2.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from fastapi import APIRouter, Depends, status
|
|
2
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
3
|
+
|
|
4
|
+
from th2rag.conversations import dependencies, schemas, service
|
|
5
|
+
from th2rag.database import get_db
|
|
6
|
+
from th2rag.dependencies import get_current_user
|
|
7
|
+
from th2rag.messages.router import router as messages_router
|
|
8
|
+
from th2rag.pagination import PageParams, PageResponse
|
|
9
|
+
from th2rag.users import schemas as user_schemas
|
|
10
|
+
|
|
11
|
+
router = APIRouter(prefix="/conversations", tags=["conversations"])
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@router.get("", response_model=PageResponse[schemas.Conversation])
|
|
15
|
+
async def get_all_conversations(
|
|
16
|
+
page_params: PageParams = Depends(),
|
|
17
|
+
db: AsyncSession = Depends(get_db),
|
|
18
|
+
current_user: user_schemas.User = Depends(get_current_user),
|
|
19
|
+
):
|
|
20
|
+
return await service.get_all_conversations(page_params, current_user, db)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@router.post("", response_model=schemas.Conversation, status_code=status.HTTP_201_CREATED)
|
|
24
|
+
async def create_conversation(
|
|
25
|
+
conversation_in: schemas.ConversationCreate,
|
|
26
|
+
db: AsyncSession = Depends(get_db),
|
|
27
|
+
current_user: user_schemas.User = Depends(get_current_user),
|
|
28
|
+
):
|
|
29
|
+
return await service.create_conversation(conversation_in, current_user.user_id, db)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@router.get("/{conversation_id}", response_model=schemas.Conversation)
|
|
33
|
+
async def get_conversation_by_id(
|
|
34
|
+
conversation: schemas.Conversation = Depends(dependencies.require_conversation_owner_or_admin),
|
|
35
|
+
):
|
|
36
|
+
return conversation
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@router.delete("/{conversation_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
40
|
+
async def delete_conversation(
|
|
41
|
+
conversation: schemas.Conversation = Depends(dependencies.require_conversation_owner_or_admin),
|
|
42
|
+
db: AsyncSession = Depends(get_db),
|
|
43
|
+
):
|
|
44
|
+
await service.delete_conversation(conversation.conversation_id, db)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
router.include_router(messages_router, prefix="/{conversation_id}/messages", tags=["messages"])
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
from typing import TYPE_CHECKING
|
|
3
|
+
|
|
4
|
+
from pydantic import BaseModel, ConfigDict
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
# from th2rag.messages.schemas import Message
|
|
10
|
+
# from th2rag.knowledge.schemas import Knowledge
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ConversationBase(BaseModel):
|
|
14
|
+
"""
|
|
15
|
+
Shared properties for a Conversation.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
knowledge_id: int | None = None
|
|
19
|
+
dataset_id: int | None = None
|
|
20
|
+
title: str
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ConversationCreate(ConversationBase):
|
|
24
|
+
"""
|
|
25
|
+
Schema for creating a new Conversation.
|
|
26
|
+
Inherits from ConversationBase; can add extra fields if needed.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Conversation(ConversationBase):
|
|
33
|
+
"""
|
|
34
|
+
Schema for reading a Conversation (e.g. GET requests).
|
|
35
|
+
Includes DB-generated fields like IDs, timestamps, etc.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
conversation_id: int
|
|
39
|
+
user_id: int
|
|
40
|
+
created_at: datetime
|
|
41
|
+
updated_at: datetime
|
|
42
|
+
|
|
43
|
+
model_config = ConfigDict(from_attributes=True)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from sqlalchemy import select
|
|
2
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
3
|
+
|
|
4
|
+
from th2rag.conversations import exceptions, schemas
|
|
5
|
+
from th2rag.models import Conversation
|
|
6
|
+
from th2rag.pagination import PageParams, PageResponse, paginate
|
|
7
|
+
from th2rag.users import schemas as user_schemas
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def get_all_conversations(
|
|
11
|
+
page_params: PageParams, user: user_schemas.User, db: AsyncSession
|
|
12
|
+
) -> PageResponse[schemas.Conversation]:
|
|
13
|
+
stmt = select(Conversation)
|
|
14
|
+
if user.role != "ADMIN":
|
|
15
|
+
stmt = stmt.where(Conversation.user_id == user.user_id)
|
|
16
|
+
return await paginate(db, stmt, page_params, schemas.Conversation)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def get_conversation_by_id(conversation_id: int, db: AsyncSession) -> schemas.Conversation:
|
|
20
|
+
stmt = select(Conversation).where(Conversation.conversation_id == conversation_id)
|
|
21
|
+
result = await db.execute(stmt)
|
|
22
|
+
conversation = result.scalar_one_or_none()
|
|
23
|
+
|
|
24
|
+
if not conversation:
|
|
25
|
+
raise exceptions.ConversationNotFoundException("Conversation not found")
|
|
26
|
+
|
|
27
|
+
return schemas.Conversation.model_validate(conversation)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
async def create_conversation(
|
|
31
|
+
conversation_in: schemas.ConversationCreate, user_id: int, db: AsyncSession
|
|
32
|
+
) -> schemas.Conversation:
|
|
33
|
+
new_conversation = Conversation(**conversation_in.model_dump(), user_id=user_id)
|
|
34
|
+
|
|
35
|
+
db.add(new_conversation)
|
|
36
|
+
await db.commit()
|
|
37
|
+
await db.refresh(new_conversation)
|
|
38
|
+
|
|
39
|
+
return schemas.Conversation.model_validate(new_conversation)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
async def delete_conversation(conversation_id: int, db: AsyncSession) -> None:
|
|
43
|
+
stmt = select(Conversation).where(Conversation.conversation_id == conversation_id)
|
|
44
|
+
result = await db.execute(stmt)
|
|
45
|
+
conversation = result.scalar_one_or_none()
|
|
46
|
+
|
|
47
|
+
if not conversation:
|
|
48
|
+
raise exceptions.ConversationNotFoundException("Conversation not found")
|
|
49
|
+
|
|
50
|
+
await db.delete(conversation)
|
|
51
|
+
await db.commit()
|
th2rag/database.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
from collections.abc import AsyncIterator
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from sqlalchemy import MetaData
|
|
6
|
+
from sqlalchemy.ext.asyncio import (
|
|
7
|
+
AsyncConnection,
|
|
8
|
+
AsyncSession,
|
|
9
|
+
# async_sessionmaker,
|
|
10
|
+
create_async_engine,
|
|
11
|
+
)
|
|
12
|
+
from sqlalchemy.orm import declarative_base, sessionmaker
|
|
13
|
+
|
|
14
|
+
from th2rag.config import settings
|
|
15
|
+
|
|
16
|
+
metadata = MetaData(schema=settings.db_schema)
|
|
17
|
+
Base = declarative_base(metadata=metadata)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class DatabaseSessionManager:
|
|
21
|
+
def __init__(self, host: str, engine_kwargs: dict[str, Any] = {}):
|
|
22
|
+
self._engine = create_async_engine(
|
|
23
|
+
host, connect_args={"server_settings": {"jit": "off"}}, **engine_kwargs
|
|
24
|
+
)
|
|
25
|
+
# self._sessionmaker = async_sessionmaker(autocommit=False, bind=self._engine)
|
|
26
|
+
self._sessionmaker = sessionmaker(autocommit=False, bind=self._engine, class_=AsyncSession)
|
|
27
|
+
|
|
28
|
+
async def close(self):
|
|
29
|
+
if self._engine is None:
|
|
30
|
+
raise Exception("DatabaseSessionManager is not initialized")
|
|
31
|
+
await self._engine.dispose()
|
|
32
|
+
|
|
33
|
+
self._engine = None
|
|
34
|
+
self._sessionmaker = None
|
|
35
|
+
|
|
36
|
+
@contextlib.asynccontextmanager
|
|
37
|
+
async def connect(self) -> AsyncIterator[AsyncConnection]:
|
|
38
|
+
if self._engine is None:
|
|
39
|
+
raise Exception("DatabaseSessionManager is not initialized")
|
|
40
|
+
|
|
41
|
+
async with self._engine.begin() as connection:
|
|
42
|
+
try:
|
|
43
|
+
yield connection
|
|
44
|
+
except Exception:
|
|
45
|
+
await connection.rollback()
|
|
46
|
+
raise
|
|
47
|
+
|
|
48
|
+
@contextlib.asynccontextmanager
|
|
49
|
+
async def session(self) -> AsyncIterator[AsyncSession]:
|
|
50
|
+
if self._sessionmaker is None:
|
|
51
|
+
raise Exception("DatabaseSessionManager is not initialized")
|
|
52
|
+
|
|
53
|
+
session = self._sessionmaker()
|
|
54
|
+
try:
|
|
55
|
+
yield session
|
|
56
|
+
except Exception:
|
|
57
|
+
await session.rollback()
|
|
58
|
+
raise
|
|
59
|
+
finally:
|
|
60
|
+
await session.close()
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def engine(self):
|
|
64
|
+
return self._engine
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
sessionmanager = DatabaseSessionManager(settings.database_url, {"echo": settings.echo_sql})
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
async def get_db():
|
|
71
|
+
async with sessionmanager.session() as session:
|
|
72
|
+
yield session
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from fastapi import Depends, HTTPException, Path, status
|
|
2
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
3
|
+
|
|
4
|
+
from th2rag.database import get_db
|
|
5
|
+
from th2rag.dataset import exceptions, schemas, service
|
|
6
|
+
from th2rag.dependencies import get_current_user
|
|
7
|
+
from th2rag.users.schemas import User
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def get_dataset_or_404(
|
|
11
|
+
dataset_id: int = Path(...), db: AsyncSession = Depends(get_db)
|
|
12
|
+
) -> schemas.Dataset:
|
|
13
|
+
try:
|
|
14
|
+
return await service.get_dataset_by_id(dataset_id, db)
|
|
15
|
+
except exceptions.DatasetNotFoundException:
|
|
16
|
+
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Dataset not found")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def require_dataset_owner_or_admin(
|
|
20
|
+
dataset: schemas.Dataset = Depends(get_dataset_or_404),
|
|
21
|
+
current_user: User = Depends(get_current_user),
|
|
22
|
+
) -> schemas.Dataset:
|
|
23
|
+
if current_user.role != "ADMIN" and dataset.user_id != current_user.user_id:
|
|
24
|
+
raise HTTPException(status.HTTP_403_FORBIDDEN, detail="Not authorized")
|
|
25
|
+
return dataset
|
th2rag/dataset/router.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from fastapi import APIRouter, Depends, status
|
|
2
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
3
|
+
|
|
4
|
+
from th2rag.database import get_db
|
|
5
|
+
from th2rag.dataset import dependencies, schemas, service
|
|
6
|
+
from th2rag.dependencies import get_current_user
|
|
7
|
+
from th2rag.pagination import PageParams, PageResponse
|
|
8
|
+
from th2rag.users import schemas as user_schemas
|
|
9
|
+
from th2rag.users.schemas import User
|
|
10
|
+
|
|
11
|
+
router = APIRouter(prefix="/datasets", tags=["datasets"])
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@router.get("/", response_model=PageResponse[schemas.Dataset])
|
|
15
|
+
async def get_all_datasets(
|
|
16
|
+
page_params: PageParams = Depends(),
|
|
17
|
+
db: AsyncSession = Depends(get_db),
|
|
18
|
+
current_user: user_schemas.User = Depends(get_current_user),
|
|
19
|
+
):
|
|
20
|
+
return await service.get_all_datasets(page_params, current_user, db)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@router.post("/", response_model=schemas.Dataset, status_code=status.HTTP_201_CREATED)
|
|
24
|
+
async def create_dataset(
|
|
25
|
+
dataset: schemas.DatasetCreate,
|
|
26
|
+
db: AsyncSession = Depends(get_db),
|
|
27
|
+
current_user: User = Depends(get_current_user),
|
|
28
|
+
):
|
|
29
|
+
return await service.create_dataset(dataset, current_user.user_id, db)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@router.put("/{dataset_id}", response_model=schemas.Dataset)
|
|
33
|
+
async def update_dataset(
|
|
34
|
+
dataset_update: schemas.DatasetCreate,
|
|
35
|
+
dataset: schemas.Dataset = Depends(dependencies.require_dataset_owner_or_admin),
|
|
36
|
+
db: AsyncSession = Depends(get_db),
|
|
37
|
+
):
|
|
38
|
+
return await service.update_dataset(dataset.dataset_id, dataset_update, db)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@router.get("/{dataset_id}", response_model=schemas.Dataset)
|
|
42
|
+
async def get_dataset_by_id(
|
|
43
|
+
dataset: schemas.Dataset = Depends(dependencies.require_dataset_owner_or_admin),
|
|
44
|
+
):
|
|
45
|
+
return dataset
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@router.delete("/{dataset_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
49
|
+
async def delete_dataset(
|
|
50
|
+
dataset: schemas.Dataset = Depends(dependencies.require_dataset_owner_or_admin),
|
|
51
|
+
db: AsyncSession = Depends(get_db),
|
|
52
|
+
):
|
|
53
|
+
await service.delete_dataset(dataset.dataset_id, db)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DatasetBase(BaseModel):
|
|
7
|
+
name: str = Field(..., example="sales_data")
|
|
8
|
+
n_columns: int = Field(..., example=5)
|
|
9
|
+
n_rows: int = Field(..., example=10000)
|
|
10
|
+
column_types: dict[str, str] = Field(
|
|
11
|
+
..., example={"price": "float", "date": "datetime", "category": "string"}
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class DatasetCreate(DatasetBase):
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Dataset(DatasetBase):
|
|
20
|
+
dataset_id: int
|
|
21
|
+
created_at: datetime
|
|
22
|
+
user_id: int
|
|
23
|
+
model_config = ConfigDict(from_attributes=True)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from sqlalchemy import select
|
|
2
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
3
|
+
|
|
4
|
+
from th2rag.dataset import exceptions, schemas
|
|
5
|
+
from th2rag.models import Dataset
|
|
6
|
+
from th2rag.pagination import PageParams, PageResponse, paginate
|
|
7
|
+
from th2rag.users.schemas import User
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def get_all_datasets(
|
|
11
|
+
page_params: PageParams, current_user: User, db: AsyncSession
|
|
12
|
+
) -> PageResponse[schemas.Dataset]:
|
|
13
|
+
stmt = select(Dataset)
|
|
14
|
+
if current_user.role != "ADMIN":
|
|
15
|
+
stmt = stmt.where(Dataset.user_id == current_user.user_id)
|
|
16
|
+
return await paginate(db, stmt, page_params, schemas.Dataset)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def create_dataset(
|
|
20
|
+
dataset_in: schemas.DatasetCreate, user_id: int, db: AsyncSession
|
|
21
|
+
) -> schemas.Dataset:
|
|
22
|
+
new_dataset = Dataset(
|
|
23
|
+
name=dataset_in.name,
|
|
24
|
+
n_columns=dataset_in.n_columns,
|
|
25
|
+
n_rows=dataset_in.n_rows,
|
|
26
|
+
column_types=dataset_in.column_types,
|
|
27
|
+
user_id=user_id,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
db.add(new_dataset)
|
|
31
|
+
await db.commit()
|
|
32
|
+
await db.refresh(new_dataset)
|
|
33
|
+
|
|
34
|
+
return schemas.Dataset.model_validate(new_dataset)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def get_dataset_by_id(dataset_id: int, db: AsyncSession) -> schemas.Dataset:
|
|
38
|
+
stmt = select(Dataset).where(Dataset.dataset_id == dataset_id)
|
|
39
|
+
result = await db.execute(stmt)
|
|
40
|
+
dataset = result.scalar_one_or_none()
|
|
41
|
+
|
|
42
|
+
if not dataset:
|
|
43
|
+
raise exceptions.DatasetNotFoundException("Dataset not found")
|
|
44
|
+
|
|
45
|
+
return schemas.Dataset.model_validate(dataset)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
async def update_dataset(
|
|
49
|
+
dataset_id: int, dataset_in: schemas.DatasetCreate, db: AsyncSession
|
|
50
|
+
) -> schemas.Dataset:
|
|
51
|
+
stmt = select(Dataset).where(Dataset.dataset_id == dataset_id)
|
|
52
|
+
result = await db.execute(stmt)
|
|
53
|
+
dataset = result.scalar_one_or_none()
|
|
54
|
+
|
|
55
|
+
if not dataset:
|
|
56
|
+
raise exceptions.DatasetNotFoundException("Dataset not found")
|
|
57
|
+
|
|
58
|
+
dataset.name = dataset_in.name
|
|
59
|
+
dataset.n_columns = dataset_in.n_columns
|
|
60
|
+
dataset.n_rows = dataset_in.n_rows
|
|
61
|
+
dataset.column_types = dataset_in.column_types
|
|
62
|
+
|
|
63
|
+
await db.commit()
|
|
64
|
+
await db.refresh(dataset)
|
|
65
|
+
|
|
66
|
+
return schemas.Dataset.model_validate(dataset)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
async def delete_dataset(dataset_id: int, db: AsyncSession) -> None:
|
|
70
|
+
stmt = select(Dataset).where(Dataset.dataset_id == dataset_id)
|
|
71
|
+
result = await db.execute(stmt)
|
|
72
|
+
dataset = result.scalar_one_or_none()
|
|
73
|
+
|
|
74
|
+
if not dataset:
|
|
75
|
+
raise exceptions.DatasetNotFoundException("Dataset not found")
|
|
76
|
+
|
|
77
|
+
await db.delete(dataset)
|
|
78
|
+
await db.commit()
|
th2rag/dependencies.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
from typing import Annotated
|
|
3
|
+
|
|
4
|
+
from fastapi import Depends, HTTPException, status
|
|
5
|
+
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
6
|
+
from jose import JWTError, jwt
|
|
7
|
+
from sqlalchemy import select
|
|
8
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
9
|
+
|
|
10
|
+
from th2rag.config import settings
|
|
11
|
+
from th2rag.database import get_db
|
|
12
|
+
from th2rag.models import User as UserModel
|
|
13
|
+
from th2rag.models import UserLLMConfig
|
|
14
|
+
from th2rag.rag.services.rag_service import RAGService
|
|
15
|
+
from th2rag.rag.services.rag_service_builder import RAGServiceBuilder
|
|
16
|
+
from th2rag.users import schemas as user_schemas
|
|
17
|
+
|
|
18
|
+
# 🔓 Make authentication optional
|
|
19
|
+
security = HTTPBearer(auto_error=False)
|
|
20
|
+
DBSessionDep = Annotated[AsyncSession, Depends(get_db)]
|
|
21
|
+
|
|
22
|
+
# 🔥 HARDCODED BYPASS - SET TO False FOR PRODUCTION
|
|
23
|
+
BYPASS_AUTH = False
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# ==========================================
|
|
27
|
+
# 1. DEFINE USER AUTH FIRST (So others can use it)
|
|
28
|
+
# ==========================================
|
|
29
|
+
|
|
30
|
+
async def get_current_user(
|
|
31
|
+
credentials: HTTPAuthorizationCredentials | None = Depends(security),
|
|
32
|
+
db: AsyncSession = Depends(get_db)
|
|
33
|
+
) -> user_schemas.User:
|
|
34
|
+
|
|
35
|
+
# 🔓 AUTH BYPASS - NO DATABASE ACCESS
|
|
36
|
+
if BYPASS_AUTH:
|
|
37
|
+
print("AUTH BYPASS ACTIVE - Using fake user")
|
|
38
|
+
|
|
39
|
+
# Return fake user with ALL required fields
|
|
40
|
+
return user_schemas.User(
|
|
41
|
+
user_id=1,
|
|
42
|
+
email="test@example.com",
|
|
43
|
+
role="USER",
|
|
44
|
+
first_name="Test",
|
|
45
|
+
last_name="User",
|
|
46
|
+
created_at=datetime.now(),
|
|
47
|
+
updated_at=datetime.now()
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
if credentials is None:
|
|
51
|
+
raise HTTPException(
|
|
52
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
53
|
+
detail="Not authenticated",
|
|
54
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
token = credentials.credentials
|
|
58
|
+
|
|
59
|
+
try:
|
|
60
|
+
payload = jwt.decode(
|
|
61
|
+
token,
|
|
62
|
+
settings.encrypt_key,
|
|
63
|
+
algorithms=[settings.algorithm]
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
# F14 fix: reject refresh tokens used as access tokens
|
|
67
|
+
token_type = payload.get("type")
|
|
68
|
+
if token_type != "access":
|
|
69
|
+
raise HTTPException(
|
|
70
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
71
|
+
detail="Invalid token type",
|
|
72
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
email: str = payload.get("sub")
|
|
76
|
+
if email is None:
|
|
77
|
+
raise HTTPException(
|
|
78
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
79
|
+
detail="Could not validate credentials",
|
|
80
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
except JWTError:
|
|
84
|
+
raise HTTPException(
|
|
85
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
86
|
+
detail="Could not validate credentials",
|
|
87
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
88
|
+
) from None
|
|
89
|
+
|
|
90
|
+
result = await db.execute(
|
|
91
|
+
select(UserModel).where(UserModel.email == email)
|
|
92
|
+
)
|
|
93
|
+
user = result.scalar_one_or_none()
|
|
94
|
+
|
|
95
|
+
if user is None:
|
|
96
|
+
raise HTTPException(
|
|
97
|
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
98
|
+
detail="User not found",
|
|
99
|
+
headers={"WWW-Authenticate": "Bearer"},
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return user_schemas.User(
|
|
103
|
+
user_id=user.user_id,
|
|
104
|
+
email=user.email,
|
|
105
|
+
role=user.role.value,
|
|
106
|
+
first_name=user.first_name,
|
|
107
|
+
last_name=user.last_name,
|
|
108
|
+
created_at=user.created_at,
|
|
109
|
+
updated_at=user.updated_at
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# ==========================================
|
|
114
|
+
# 2. THEN DEFINE RAG SERVICE (Which uses the user)
|
|
115
|
+
# ==========================================
|
|
116
|
+
|
|
117
|
+
async def get_rag_service(
|
|
118
|
+
current_user: user_schemas.User = Depends(get_current_user),
|
|
119
|
+
db: AsyncSession = Depends(get_db)
|
|
120
|
+
) -> RAGService:
|
|
121
|
+
|
|
122
|
+
# 1. Check if this user has a custom LLM Config in the database
|
|
123
|
+
stmt = select(UserLLMConfig).where(UserLLMConfig.user_id == current_user.user_id)
|
|
124
|
+
result = await db.execute(stmt)
|
|
125
|
+
user_config = result.scalar_one_or_none()
|
|
126
|
+
|
|
127
|
+
# 2. Pass that config (or None) to the Builder
|
|
128
|
+
builder = RAGServiceBuilder()
|
|
129
|
+
return builder.build(user_config=user_config)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
async def get_admin_user(
|
|
133
|
+
current_user: user_schemas.User = Depends(get_current_user),
|
|
134
|
+
) -> user_schemas.User:
|
|
135
|
+
|
|
136
|
+
if BYPASS_AUTH:
|
|
137
|
+
print("⚠️ ADMIN BYPASS ACTIVE")
|
|
138
|
+
current_user.role = "ADMIN"
|
|
139
|
+
return current_user
|
|
140
|
+
|
|
141
|
+
if getattr(current_user, "role", None) != "ADMIN":
|
|
142
|
+
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,detail="Not enough permissions")
|
|
143
|
+
return current_user
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from fastapi import Depends, HTTPException, Path, status
|
|
2
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
3
|
+
|
|
4
|
+
from th2rag.database import get_db
|
|
5
|
+
from th2rag.dependencies import get_current_user
|
|
6
|
+
from th2rag.knowledge import exceptions, schemas, service
|
|
7
|
+
from th2rag.users.schemas import User
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def get_knowledge_or_404(
|
|
11
|
+
knowledge_id: int = Path(...), db: AsyncSession = Depends(get_db)
|
|
12
|
+
) -> schemas.Knowledge:
|
|
13
|
+
try:
|
|
14
|
+
return await service.get_knowledge_by_id(knowledge_id, db)
|
|
15
|
+
except exceptions.KnowledgeNotFoundException:
|
|
16
|
+
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Knowledge not found")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def require_knowledge_owner_or_admin(
|
|
20
|
+
knowledge: schemas.Knowledge = Depends(get_knowledge_or_404),
|
|
21
|
+
current_user: User = Depends(get_current_user),
|
|
22
|
+
) -> schemas.Knowledge:
|
|
23
|
+
if current_user.role != "ADMIN" and knowledge.user_id != current_user.user_id:
|
|
24
|
+
raise HTTPException(status.HTTP_403_FORBIDDEN, detail="Not authorized")
|
|
25
|
+
return knowledge
|