-
Notifications
You must be signed in to change notification settings - Fork 2
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
Flatten documents to be top-level #13
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
@@ -1,20 +1,18 @@ | ||
from typing import Annotated, List | ||
|
||
import asyncpg | ||
from fastapi import APIRouter, BackgroundTasks, Body, HTTPException, Path, status | ||
from fastapi import APIRouter, BackgroundTasks, Body, HTTPException, Path, status, Query | ||
from loguru import logger | ||
|
||
from app.collections.router import PathCollectionId | ||
from app.common.db import PgConnectionDep, PgPoolDep | ||
from app.documents.models import Document | ||
from app.ingest.extract import extract | ||
from app.ingest.extract.source import ExtractSource | ||
from app.ingest.store import Store, StoreDep | ||
|
||
# TODO: Move this to `/documents`. Will require figuring out | ||
# how to specify the collection for create, list, etc. | ||
router = APIRouter(prefix="/collections/{collection_id}/documents") | ||
from .models import CreateRequest | ||
|
||
router = APIRouter(prefix="/documents") | ||
|
||
# We can't use the session from the request because it ends as soon | ||
# as the request completes. So we need to pass the engine and start | ||
|
@@ -53,11 +51,10 @@ async def ingest_document(id: int, store: Store, pg_pool: asyncpg.Pool): | |
|
||
@router.put("/") | ||
async def add_document( | ||
collection_id: PathCollectionId, | ||
store: StoreDep, | ||
pg_pool: PgPoolDep, | ||
background: BackgroundTasks, | ||
url: Annotated[str, Body(..., description="The URL of the document to add.")], | ||
req: CreateRequest, | ||
) -> Document: | ||
"""Add a document.""" | ||
|
||
|
@@ -69,8 +66,8 @@ async def add_document( | |
VALUES ($1, $2, 'pending') | ||
RETURNING id, collection_id, url, ingest_state, ingest_error | ||
""", | ||
collection_id, | ||
url, | ||
req.collection_id, | ||
req.url, | ||
) | ||
|
||
document = Document.model_validate(dict(row)) | ||
|
@@ -83,31 +80,39 @@ async def add_document( | |
|
||
@router.get("/") | ||
async def list_documents( | ||
collection_id: PathCollectionId, conn: PgConnectionDep | ||
conn: PgConnectionDep, | ||
collection_id: Annotated[int | None, Query(description="Limit to documents associated with this collection")] = None, | ||
) -> List[Document]: | ||
"""List documents.""" | ||
# TODO: Test | ||
results = await conn.fetch( | ||
if collection_id == 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. Potential concern here around auth -- maybe require it for now? |
||
results = await conn.fetch( | ||
""" | ||
SELECT id, collection_id, url, ingest_state, ingest_error | ||
FROM document | ||
""" | ||
SELECT id, collection_id, url, ingest_state, ingest_error | ||
FROM document WHERE collection_id = $1 | ||
""", | ||
collection_id, | ||
) | ||
) | ||
else: | ||
results = await conn.fetch( | ||
""" | ||
SELECT id, collection_id, url, ingest_state, ingest_error | ||
FROM document WHERE collection_id = $1 | ||
""", | ||
collection_id, | ||
) | ||
return [Document.model_validate(dict(result)) for result in results] | ||
|
||
|
||
@router.get("/{id}") | ||
async def get_document( | ||
conn: PgConnectionDep, collection_id: PathCollectionId, id: PathDocumentId | ||
conn: PgConnectionDep, id: PathDocumentId | ||
) -> Document: | ||
# TODO: Test / return not found? | ||
result = await conn.fetchrow( | ||
""" | ||
SELECT id, collection_id, url, ingest_state, ingest_error | ||
FROM document WHERE id = $1 AND collection_id = $2 | ||
FROM document WHERE id = $1 | ||
""", | ||
id, | ||
collection_id, | ||
) | ||
return Document.model_validate(dict(result)) |
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.
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.
DocumentCreate
?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.
We have a couple different patterns - I may try to normalize in future PRs