-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add simple ingestion workflow and document processing activities #36
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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 hidden or 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
Empty file.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from datetime import timedelta | ||
|
|
||
| from temporalio import activity, workflow | ||
| from temporalio.common import RetryPolicy | ||
| from temporalio.workflow import execute_activity | ||
| from .schema import IngestionRequest | ||
|
|
||
| with workflow.unsafe.imports_passed_through(): | ||
| from tc_hivemind_backend.ingest_qdrant import CustomIngestionPipeline | ||
| from llama_index.core import Document | ||
|
|
||
|
|
||
| @workflow.defn | ||
| class IngestionWorkflow: | ||
| """A Temporal workflow for processing document ingestion requests. | ||
|
|
||
| This workflow handles the orchestration of document processing activities, | ||
| including retry logic and timeout configurations. | ||
| """ | ||
|
|
||
| @workflow.run | ||
| async def run(self, ingestion_request: IngestionRequest) -> None: | ||
| """Execute the ingestion workflow. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ingestion_request : IngestionRequest | ||
| The request containing all necessary information for document processing, | ||
| including community ID, platform ID, text content, and metadata. | ||
|
|
||
| Notes | ||
| ----- | ||
| The workflow implements a retry policy with the following configuration: | ||
| - Initial retry interval: 1 second | ||
| - Maximum retry interval: 1 minute | ||
| - Maximum retry attempts: 3 | ||
| - Activity timeout: 5 minutes | ||
| """ | ||
| retry_policy = RetryPolicy( | ||
| initial_interval=timedelta(seconds=1), | ||
| maximum_interval=timedelta(minutes=1), | ||
| maximum_attempts=3, | ||
| ) | ||
|
|
||
| await execute_activity( | ||
| process_document, | ||
| ingestion_request, | ||
| retry_policy=retry_policy, | ||
| start_to_close_timeout=timedelta(minutes=5), | ||
| ) | ||
|
|
||
|
|
||
| @activity.defn | ||
| async def process_document( | ||
| ingestion_request: IngestionRequest, | ||
| ) -> None: | ||
| """Process the document according to the ingestion request specifications. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ingestion_request : IngestionRequest | ||
| The request containing all necessary information for document processing, | ||
| including community ID, platform ID, text content, and metadata. | ||
|
|
||
| Notes | ||
| ----- | ||
| This activity will be implemented by the user to handle the actual document | ||
| processing logic, including any necessary embedding or LLM operations. | ||
| """ | ||
| if ingestion_request.collectionName is None: | ||
| collection_name = ( | ||
| f"{ingestion_request.communityId}_{ingestion_request.platformId}" | ||
| ) | ||
| else: | ||
| collection_name = ingestion_request.collectionName | ||
|
|
||
| # Initialize the ingestion pipeline | ||
| pipeline = CustomIngestionPipeline( | ||
| community_id=ingestion_request.communityId, | ||
| collection_name=collection_name, | ||
| ) | ||
|
|
||
| document = Document( | ||
| doc_id=ingestion_request.docId, | ||
| text=ingestion_request.text, | ||
| metadata=ingestion_request.metadata, | ||
| ) | ||
|
|
||
| pipeline.run_pipeline(docs=[document]) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| from pydantic import BaseModel | ||
| from uuid import uuid4 | ||
|
|
||
|
|
||
| class IngestionRequest(BaseModel): | ||
| """A model representing an ingestion request for document processing. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| communityId : str | ||
| The unique identifier of the community. | ||
| platformId : str | ||
| The unique identifier of the platform. | ||
| text : str | ||
| The text content to be processed. | ||
| metadata : dict | ||
| Additional metadata associated with the document. | ||
| docId : str, optional | ||
| Unique identifier for the document. If not provided, a UUID will be generated. | ||
| Default is a new UUID. | ||
| excludedEmbedMetadataKeys : list[str], optional | ||
| List of metadata keys to exclude from embedding process. | ||
| Default is an empty list. | ||
| excludedLlmMetadataKeys : list[str], optional | ||
| List of metadata keys to exclude from LLM processing. | ||
| Default is an empty list. | ||
| collectionName : str | None, optional | ||
| The name of the collection to use for the document. | ||
| Default is `None` means it would follow the default pattern of `[communityId]_[platformId]` | ||
| """ | ||
|
|
||
| communityId: str | ||
| platformId: str | ||
| text: str | ||
| metadata: dict | ||
| docId: str = str(uuid4()) | ||
| excludedEmbedMetadataKeys: list[str] = [] | ||
| excludedLlmMetadataKeys: list[str] = [] | ||
| collectionName: str | None = None |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.