|
8 | 8 |
|
9 | 9 | from app.activities.extract_metadata import ExtractMetadataRequest, metadata_extraction |
10 | 10 | from app.activities.extract_pdf_content import ExtractPdfContentRequest, text_extraction |
| 11 | +from app.activities.store_workflow_result import StoreWorkflowResultRequest, store_workflow_result |
| 12 | +from app.database.models import WorkflowStatus |
| 13 | +from app.workflows.suggestions import MetadataResult |
11 | 14 |
|
12 | 15 |
|
13 | | -class DocumentMetadata(BaseModel): |
14 | | - """Structured metadata extracted from a PDF document.""" |
| 16 | +class ExtractMetadataWorkflowRequest(BaseModel): |
| 17 | + """Workflow request to extract PDF content and generate metadata suggestions.""" |
15 | 18 |
|
16 | | - title: str | None = Field(default=None, description="The title of the document") |
17 | | - authors: list[str] | None = Field( |
18 | | - default=None, description="List of document authors" |
19 | | - ) |
20 | | - publication_date: str | None = Field( |
21 | | - default=None, |
22 | | - description="Publication date in ISO format (YYYY-MM-DD, YYYY-MM, or YYYY)", |
23 | | - ) |
24 | | - abstract: str | None = Field( |
25 | | - default=None, |
26 | | - description="Abstract or summary of the document, extracted verbatim", |
27 | | - ) |
28 | | - language: str | None = Field( |
29 | | - default=None, description="Language of the document (e.g. 'en', 'fr')" |
30 | | - ) |
31 | | - keywords: list[str] | None = Field( |
32 | | - default=None, description="Key topics or keywords from the document" |
33 | | - ) |
34 | | - |
35 | | - |
36 | | -METADATA_INSTRUCTIONS = """\ |
37 | | -You are an expert at extracting structured metadata from documents. |
38 | | -
|
39 | | -Given the raw text content of a PDF document, extract the following metadata fields: |
40 | | -- title: The main title of the document. |
41 | | -- authors: A list of authors. Look for names near the title, |
42 | | - in headers, or in an authors section. |
43 | | -- publication_date: The publication date in ISO format (YYYY-MM-DD, YYYY-MM, or YYYY). |
44 | | -- abstract: The abstract or summary, extracted verbatim from the document. |
45 | | -- language: The language the document is written in (ISO 639-1 code, e.g. "en"). |
46 | | -- keywords: Key topics or keywords mentioned in the document. |
47 | | -
|
48 | | -IMPORTANT RULES: |
49 | | -1. Only include information explicitly stated in the document. |
50 | | -2. If a field is not present or cannot be determined, leave it as null. |
51 | | -3. For the abstract, include the text verbatim from the document. |
52 | | -4. Do not fabricate or infer information that is not in the text. |
53 | | -""" |
54 | | - |
55 | | -# metadata_agent = Agent( |
56 | | -# "openai:gpt-4o-mini", |
57 | | -# instructions=METADATA_INSTRUCTIONS, |
58 | | -# output_type=DocumentMetadata, |
59 | | -# name="metadata_extractor", |
60 | | -# ) |
61 | | -# |
62 | | -# temporal_metadata_agent = TemporalAgent( |
63 | | -# metadata_agent, |
64 | | -# model_activity_config=workflow.ActivityConfig( |
65 | | -# start_to_close_timeout=timedelta(minutes=5), |
66 | | -# ), |
67 | | -# ) |
68 | | -# |
| 19 | + workflow_id: str = Field(description="Workflow public_id (DB primary identifier)") |
| 20 | + tenant_id: str = Field(description="Tenant id (ownership check)") |
| 21 | + url: str |
| 22 | + extractor: str = "pdfplumber" |
| 23 | + pages: list[int] | None = None |
69 | 24 |
|
70 | 25 |
|
71 | 26 | @workflow.defn |
72 | 27 | class ExtractMetadata(PydanticAIWorkflow): |
73 | 28 | """Workflow that extracts content from a PDF and uses an LLM to extract metadata.""" |
74 | 29 |
|
75 | 30 | @workflow.run |
76 | | - async def run(self, request_data: dict) -> DocumentMetadata: |
77 | | - """Execute the metadata extraction workflow. |
78 | | -
|
79 | | - Args: |
80 | | - request_data: Dictionary containing PDF extraction parameters |
81 | | - (url, extractor, pages). |
82 | | -
|
83 | | - Returns: |
84 | | - DocumentMetadata: Extracted metadata from the PDF document. |
85 | | - """ |
86 | | - # Activity 1: Extract PDF text |
87 | | - content = await workflow.execute_activity( |
88 | | - text_extraction, |
89 | | - ExtractPdfContentRequest(**request_data), |
90 | | - start_to_close_timeout=timedelta(minutes=5), |
91 | | - ) |
92 | | - |
93 | | - # Activity 2: Extract metadata using LLM |
94 | | - metadata = await workflow.execute_activity( |
95 | | - metadata_extraction, |
96 | | - ExtractMetadataRequest(text=content.text), |
97 | | - start_to_close_timeout=timedelta(minutes=5), |
| 31 | + async def run(self, request_data: dict) -> MetadataResult: |
| 32 | + """Execute the extraction + suggestions workflow.""" |
| 33 | + request = ExtractMetadataWorkflowRequest(**request_data) |
| 34 | + try: |
| 35 | + # Activity 1: Extract PDF text |
| 36 | + content = await workflow.execute_activity( |
| 37 | + text_extraction, |
| 38 | + ExtractPdfContentRequest( |
| 39 | + url=request.url, |
| 40 | + extractor=request.extractor, |
| 41 | + pages=request.pages, |
| 42 | + ), |
| 43 | + start_to_close_timeout=timedelta(minutes=5), |
| 44 | + ) |
| 45 | + |
| 46 | + # Activity 2: Generate metadata suggestions using LLM |
| 47 | + result = await workflow.execute_activity( |
| 48 | + metadata_extraction, |
| 49 | + ExtractMetadataRequest(text=content.text), |
| 50 | + start_to_close_timeout=timedelta(minutes=5), |
| 51 | + ) |
| 52 | + except Exception: |
| 53 | + await workflow.execute_activity( |
| 54 | + store_workflow_result, |
| 55 | + StoreWorkflowResultRequest( |
| 56 | + workflow_id=request.workflow_id, |
| 57 | + tenant_id=request.tenant_id, |
| 58 | + status=WorkflowStatus.ERROR, |
| 59 | + result=None, |
| 60 | + ), |
| 61 | + start_to_close_timeout=timedelta(minutes=1), |
| 62 | + ) |
| 63 | + raise |
| 64 | + |
| 65 | + await workflow.execute_activity( |
| 66 | + store_workflow_result, |
| 67 | + StoreWorkflowResultRequest( |
| 68 | + workflow_id=request.workflow_id, |
| 69 | + tenant_id=request.tenant_id, |
| 70 | + status=WorkflowStatus.SUCCESS, |
| 71 | + result=result.model_dump(), |
| 72 | + ), |
| 73 | + start_to_close_timeout=timedelta(minutes=1), |
98 | 74 | ) |
99 | 75 |
|
100 | | - return metadata |
| 76 | + return result |
0 commit comments